Fix empty online/offline bug

This commit is contained in:
Natsumi
2021-08-13 11:38:16 +12:00
parent 3f71857c68
commit c1bb664309
+39 -64
View File
@@ -3972,8 +3972,6 @@ speechSynthesis.getVoices();
$app.data.debugWebRequests = false; $app.data.debugWebRequests = false;
$app.data.debugWebSocket = false; $app.data.debugWebSocket = false;
$app.data.APILastOnline = new Map();
$app.data.sharedFeed = { $app.data.sharedFeed = {
gameLog: { gameLog: {
wrist: [], wrist: [],
@@ -5979,9 +5977,6 @@ speechSynthesis.getVoices();
}); });
API.$on('FRIEND:STATE', function (args) { API.$on('FRIEND:STATE', function (args) {
if (args.json.state === 'online') {
$app.APILastOnline.set(args.params.userId, Date.now());
}
$app.updateFriend(args.params.userId, args.json.state); $app.updateFriend(args.params.userId, args.json.state);
}); });
@@ -6094,7 +6089,7 @@ speechSynthesis.getVoices();
$app.data.updateFriendInProgress = new Set(); $app.data.updateFriendInProgress = new Set();
$app.methods.updateFriend = async function (id, newState, origin) { $app.methods.updateFriend = async 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;
@@ -6105,7 +6100,7 @@ speechSynthesis.getVoices();
this.updateFriendInProgress.add(id); this.updateFriendInProgress.add(id);
var ref = API.cachedUsers.get(id); var ref = API.cachedUsers.get(id);
var isVIP = API.cachedFavoritesByObjectId.has(id); var isVIP = API.cachedFavoritesByObjectId.has(id);
if (typeof newState === 'undefined' || ctx.state === newState) { if (typeof stateInput === 'undefined' || ctx.state === stateInput) {
// this is should be: undefined -> user // this is should be: undefined -> user
if (ctx.ref !== ref) { if (ctx.ref !== ref) {
ctx.ref = ref; ctx.ref = ref;
@@ -6176,27 +6171,10 @@ speechSynthesis.getVoices();
) { ) {
API.getUser({ API.getUser({
userId: id userId: id
}).catch(() => {
this.updateFriendInProgress.delete(id);
}); });
} }
} else { } else {
// prevent status flapping var newState = stateInput;
if (
ctx.state === 'online' &&
(newState === 'active' || newState === 'offline')
) {
this.updateFriendInProgress.delete(id);
await new Promise((resolve) => {
setTimeout(resolve, 50000);
});
if (this.APILastOnline.has(id)) {
var date = this.APILastOnline.get(id);
if (date > Date.now() - 60000) {
return;
}
}
}
var location = ''; var location = '';
var $location_at = ''; var $location_at = '';
if ( if (
@@ -6205,18 +6183,19 @@ speechSynthesis.getVoices();
) { ) {
var {location, $location_at} = ref; var {location, $location_at} = ref;
} }
var args = await API.getUser({ try {
userId: id var args = await API.getUser({
}).catch(() => { userId: id
this.updateFriendInProgress.delete(id); });
}); if (
if ( typeof args !== 'undefined' &&
typeof args !== 'undefined' && typeof args.ref !== 'undefined'
typeof args.ref !== 'undefined' ) {
) { newState = args.ref.state;
// eslint-disable-next-line no-param-reassign ctx.ref = args.ref;
newState = args.ref.state; }
ctx.ref = args.ref; } catch (err) {
console.error(err);
} }
if (ctx.state !== newState) { if (ctx.state !== newState) {
if ( if (
@@ -6227,26 +6206,30 @@ speechSynthesis.getVoices();
) { ) {
ctx.ref.$online_for = ''; ctx.ref.$online_for = '';
ctx.ref.$offline_for = Date.now(); ctx.ref.$offline_for = Date.now();
if (ctx.state === 'online') { var ts = Date.now();
var ts = Date.now(); var time = ts - $location_at;
var time = ts - $location_at; var worldName = await this.getWorldName(location);
var worldName = await this.getWorldName(location); var feed = {
var feed = { created_at: new Date().toJSON(),
created_at: new Date().toJSON(), type: 'Offline',
type: 'Offline', userId: ctx.ref.id,
userId: ctx.ref.id, displayName: ctx.ref.displayName,
displayName: ctx.ref.displayName, location,
location, worldName,
worldName, time
time };
}; this.addFeed(feed);
this.addFeed(feed); database.addOnlineOfflineToDatabase(feed);
database.addOnlineOfflineToDatabase(feed);
}
} else if (newState === 'online') { } else if (newState === 'online') {
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 (
typeof ctx.ref.location !== 'undefined' &&
ctx.ref.location !== 'offline'
) {
var {location} = ctx.ref;
}
var worldName = await this.getWorldName(ctx.ref.location); var worldName = await this.getWorldName(ctx.ref.location);
var feed = { var feed = {
created_at: new Date().toJSON(), created_at: new Date().toJSON(),
@@ -6295,17 +6278,9 @@ speechSynthesis.getVoices();
this.friendsGroup3_.push(ctx); this.friendsGroup3_.push(ctx);
this.friendsGroupD_.unshift(ctx); this.friendsGroupD_.unshift(ctx);
} }
// changing property triggers Vue ctx.state = newState;
// so, we need compare and set ctx.name = ctx.ref.displayName;
if (ctx.state !== newState) { ctx.isVIP = isVIP;
ctx.state = newState;
}
if (ctx.name !== ctx.ref.displayName) {
ctx.name = ctx.ref.displayName;
}
if (ctx.isVIP !== isVIP) {
ctx.isVIP = isVIP;
}
} }
this.updateFriendInProgress.delete(id); this.updateFriendInProgress.delete(id);
}; };