Fixes to update friend state

This function is a massive pile of garbage that I don't have time to rewrite
This commit is contained in:
Natsumi
2022-08-14 20:18:32 +12:00
parent ed5a1daf92
commit 270aaeab96
+87 -50
View File
@@ -446,7 +446,7 @@ speechSynthesis.getVoices();
type: 'error' type: 'error'
}); });
$app.avatarDialog.visible = false; $app.avatarDialog.visible = false;
throw new Error(`404: Can't find avatarǃ ${endpoint}`); throw new Error(`404: ${data.error.message} ${endpoint}`);
} }
if ( if (
init.method === 'GET' && init.method === 'GET' &&
@@ -455,7 +455,7 @@ speechSynthesis.getVoices();
this.failedGetRequests.set(endpoint, Date.now()); this.failedGetRequests.set(endpoint, Date.now());
} }
if (status === 404 && endpoint.substring(0, 6) === 'users/') { if (status === 404 && endpoint.substring(0, 6) === 'users/') {
throw new Error(`404: Can't find user! ${endpoint}`); throw new Error(`404: ${data.error.message} ${endpoint}`);
} }
if ( if (
status === 404 && status === 404 &&
@@ -4296,6 +4296,7 @@ speechSynthesis.getVoices();
$app.data.debugUserDiff = false; $app.data.debugUserDiff = false;
$app.data.debugPhotonLogging = false; $app.data.debugPhotonLogging = false;
$app.data.debugGameLog = false; $app.data.debugGameLog = false;
$app.data.debugFriendState = false;
$app.data.APILastOnline = new Map(); $app.data.APILastOnline = new Map();
@@ -6601,7 +6602,7 @@ speechSynthesis.getVoices();
$app.data.updateFriendInProgress = new Map(); $app.data.updateFriendInProgress = new Map();
$app.methods.updateFriend = async function (id, stateInput, origin) { $app.methods.updateFriend = function (id, stateInput, origin) {
var ctx = this.friends.get(id); var ctx = this.friends.get(id);
if (typeof ctx === 'undefined') { if (typeof ctx === 'undefined') {
return; return;
@@ -6611,9 +6612,17 @@ speechSynthesis.getVoices();
stateInput && stateInput &&
ctx.state !== stateInput && ctx.state !== stateInput &&
lastOnlineDate && lastOnlineDate &&
lastOnlineDate > Date.now() - 100 lastOnlineDate > Date.now() - 1000
) { ) {
// crappy double online fix // crappy double online fix
if (this.debugFriendState) {
console.log(
ctx.name,
new Date().toJSON(),
'userAlreadyOnline',
stateInput
);
}
return; return;
} }
if (stateInput === 'online') { if (stateInput === 'online') {
@@ -6692,59 +6701,95 @@ speechSynthesis.getVoices();
) { ) {
API.getUser({ API.getUser({
userId: id userId: id
}).catch(() => {
this.updateFriendInProgress.delete(id);
}); });
} }
} else { } else if (
var newState = stateInput;
var location = '';
var $location_at = '';
if (
typeof ref !== 'undefined' &&
typeof ref.location !== 'undefined'
) {
var {location, $location_at} = ref;
}
// prevent status flapping
if (
ctx.state === 'online' && ctx.state === 'online' &&
(stateInput === 'active' || stateInput === 'offline') (stateInput === 'active' || stateInput === 'offline')
) { ) {
// check if already waiting // delayed second check to prevent status flapping
var date = this.updateFriendInProgress.get(id); var date = this.updateFriendInProgress.get(id);
if (date && date > Date.now() - 110000) { if (date && date > Date.now() - 120000) {
// check if already waiting
if (this.debugFriendState) {
console.log(
ctx.name,
new Date().toJSON(),
'pendingOfflineCheck',
stateInput
);
}
return; return;
} }
this.updateFriendInProgress.set(id, Date.now()); this.updateFriendInProgress.set(id, Date.now());
// wait 2minutes then check if user came back online // wait 2minutes then check if user came back online
await new Promise((resolve) => { workerTimers.setTimeout(() => {
setTimeout(resolve, 110000);
});
var date1 = this.APILastOnline.get(id);
if (date1 && date1 > Date.now() - 120000) {
this.updateFriendInProgress.delete(id); this.updateFriendInProgress.delete(id);
var {location, $location_at} = ref;
this.updateFriendDelayedCheck(
id,
ctx,
stateInput,
isVIP,
location,
$location_at
);
}, 110000);
} else {
var {location, $location_at} = ref;
this.updateFriendDelayedCheck(
id,
ctx,
stateInput,
isVIP,
location,
$location_at
);
}
};
$app.methods.updateFriendDelayedCheck = async function (
id,
ctx,
stateInput,
isVIP,
location,
$location_at
) {
var date = this.APILastOnline.get(id);
if (
ctx.state === 'online' &&
(stateInput === 'active' || stateInput === 'offline') &&
date &&
date > Date.now() - 120000
) {
if (this.debugFriendState) {
console.log(
ctx.name,
new Date().toJSON(),
'falsePositiveOffline',
stateInput
);
}
return; return;
} }
} var newState = stateInput;
try {
var args = await API.getUser({ var args = await API.getUser({
userId: id userId: id
}); });
if (
typeof args !== 'undefined' &&
typeof args.ref !== 'undefined'
) {
newState = args.ref.state; newState = args.ref.state;
ctx.ref = args.ref; if (this.debugFriendState) {
} console.log(
} catch (err) { ctx.name,
console.error(err); new Date().toJSON(),
'updateFriendState',
newState,
stateInput
);
} }
var newRef = args.ref;
if (ctx.state !== newState) { if (ctx.state !== newState) {
if ( if (
typeof ctx.ref.$offline_for !== 'undefined' &&
ctx.ref.$offline_for === '' &&
(newState === 'offline' || newState === 'active') && (newState === 'offline' || newState === 'active') &&
ctx.state === 'online' ctx.state === 'online'
) { ) {
@@ -6756,8 +6801,8 @@ speechSynthesis.getVoices();
var feed = { var feed = {
created_at: new Date().toJSON(), created_at: new Date().toJSON(),
type: 'Offline', type: 'Offline',
userId: ctx.ref.id, userId: newRef.id,
displayName: ctx.ref.displayName, displayName: newRef.displayName,
location, location,
worldName, worldName,
time time
@@ -6768,19 +6813,13 @@ speechSynthesis.getVoices();
ctx.ref.$location_at = Date.now(); ctx.ref.$location_at = Date.now();
ctx.ref.$online_for = Date.now(); ctx.ref.$online_for = Date.now();
ctx.ref.$offline_for = ''; ctx.ref.$offline_for = '';
if ( var worldName = await this.getWorldName(newRef.location);
typeof ctx.ref.location !== 'undefined' &&
ctx.ref.location !== 'offline'
) {
var {location} = ctx.ref;
}
var worldName = await this.getWorldName(ctx.ref.location);
var feed = { var feed = {
created_at: new Date().toJSON(), created_at: new Date().toJSON(),
type: 'Online', type: 'Online',
userId: ctx.ref.id, userId: ctx.ref.id,
displayName: ctx.ref.displayName, displayName: ctx.ref.displayName,
location, location: newRef.location,
worldName, worldName,
time: '' time: ''
}; };
@@ -6826,10 +6865,8 @@ speechSynthesis.getVoices();
this.updateOnlineFriendCoutner(); this.updateOnlineFriendCoutner();
} }
ctx.state = newState; ctx.state = newState;
ctx.name = ctx.ref.displayName; ctx.name = newRef.displayName;
ctx.isVIP = isVIP; ctx.isVIP = isVIP;
}
this.updateFriendInProgress.delete(id);
}; };
$app.methods.getWorldName = async function (location) { $app.methods.getWorldName = async function (location) {