Skip to content

Commit

Permalink
Refresh last online dates more frequently for users who were online i…
Browse files Browse the repository at this point in the history
…n the last 5 minutes
  • Loading branch information
hpeebles committed Jul 26, 2024
1 parent 8f98494 commit ff6e2c3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ic_cdk::query;
use user_canister::get_cached_btc_address::{Response::*, *};

#[query(guard = "caller_is_owner")]
async fn get_cached_btc_address(_args: Args) -> Response {
fn get_cached_btc_address(_args: Args) -> Response {
match read_state(|state| state.data.btc_address.clone()) {
Some(btc_address) => Success(btc_address),
None => NotFound,
Expand Down
23 changes: 19 additions & 4 deletions frontend/openchat-client/src/openchat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,7 @@ export class OpenChat extends OpenChatAgentWorker {
const durationUntilLogoutMs = durationUntilSessionExpireMS - ONE_MINUTE_MILLIS;
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;

function timeout() {
console.debug(
"SESSION: session has timed out after ",
Expand All @@ -789,6 +790,7 @@ export class OpenChat extends OpenChatAgentWorker {
);
self.logout().then(resolve);
}

if (durationUntilLogoutMs <= 5 * ONE_MINUTE_MILLIS) {
timeout();
} else {
Expand Down Expand Up @@ -2330,6 +2332,7 @@ export class OpenChat extends OpenChatAgentWorker {

await this.getMissingUsers(allUserIds);
}

isTyping = isTyping;
trackEvent = trackEvent;
twitterLinkRegex = twitterLinkRegex;
Expand Down Expand Up @@ -2393,6 +2396,7 @@ export class OpenChat extends OpenChatAgentWorker {
]);
}
}

blockCommunityUser(id: CommunityIdentifier, userId: string): Promise<boolean> {
this.blockCommunityUserLocally(id, userId);
return this.sendRequest({ kind: "blockCommunityUser", id, userId })
Expand Down Expand Up @@ -2703,6 +2707,7 @@ export class OpenChat extends OpenChatAgentWorker {
messageContentFromFile(file: File): Promise<AttachmentContent> {
return messageContentFromFile(file, this._liveState.isDiamond);
}

formatFileSize = formatFileSize;

haveCommunityPermissionsChanged(p1: CommunityPermissions, p2: CommunityPermissions): boolean {
Expand Down Expand Up @@ -3143,13 +3148,15 @@ export class OpenChat extends OpenChatAgentWorker {

return loadedUpTo < serverChat.latestEventIndex ? [loadedUpTo + 1, true] : undefined;
}

private confirmedUpToEventIndex(chatId: ChatIdentifier): number | undefined {
const ranges = confirmedEventIndexesLoaded(chatId).subranges();
if (ranges.length > 0) {
return ranges[0].high;
}
return undefined;
}

private confirmedThreadUpToEventIndex(): number | undefined {
const ranges = get(confirmedThreadEventIndexesLoadedStore).subranges();
if (ranges.length > 0) {
Expand Down Expand Up @@ -3234,6 +3241,7 @@ export class OpenChat extends OpenChatAgentWorker {
unconfirmed.delete(context, messageId);
messagesRead.removeUnconfirmedMessage(context, messageId);
}

toggleProposalFilterMessageExpansion = toggleProposalFilterMessageExpansion;
groupWhile = groupWhile;
sameUser = sameUser;
Expand Down Expand Up @@ -4938,12 +4946,18 @@ export class OpenChat extends OpenChatAgentWorker {

if (userId === this._liveState.user.userId) return now;

let lastOnline = lastOnlineDates.get(userId, now);
if (lastOnline === undefined) {
const lastOnline = lastOnlineDates.get(userId, now);
// Refresh the last online date if we don't currently have one for the user, or if the user has been online in
// the last 5 minutes and it has been at least 30 seconds since we refreshed their last online date
if (
lastOnline === undefined ||
(lastOnline.lastOnline > now - 5 * ONE_MINUTE_MILLIS &&
lastOnline.updated < now - 30 * 1000)
) {
const response = await this.getLastOnlineDatesBatched([userId]);
lastOnline = response[userId];
return response[userId];
}
return lastOnline;
return lastOnline?.lastOnline;
}

getPublicProfile(userId?: string): Promise<PublicProfile> {
Expand Down Expand Up @@ -7139,6 +7153,7 @@ export class OpenChat extends OpenChatAgentWorker {

// **** End of Communities stuff
diamondDurationToMs = diamondDurationToMs;

swapRestricted(): Promise<boolean> {
return this.getUserLocation().then((location) => featureRestricted(location, "swap"));
}
Expand Down
9 changes: 6 additions & 3 deletions frontend/openchat-client/src/stores/lastOnlineDates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ function createLastOnlineDatesStore() {

return {
subscribe: store.subscribe,
get: (userId: string, now: number): number | undefined => {
get: (userId: string, now: number): LastOnline | undefined => {
const value = storeValue.get(userId);
return value !== undefined && !expired(value, now) ? value.lastOnline : undefined;
return value !== undefined && !expired(value, now) ? value : undefined;
},
set: (values: Iterable<[string, number]>, now: number): void => {
store.update((map) => {
Expand All @@ -31,7 +31,10 @@ function createLastOnlineDatesStore() {
}
}
for (const [userId, lastOnline] of values) {
newMap.set(userId, { lastOnline, updated: now });
const current = newMap.get(userId);
if (current === undefined || current.lastOnline < lastOnline) {
newMap.set(userId, { lastOnline, updated: now });
}
}
return newMap;
});
Expand Down

0 comments on commit ff6e2c3

Please sign in to comment.