Skip to content

Commit

Permalink
Refresh last online dates sooner for users who were recently online (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Jul 26, 2024
1 parent 8f98494 commit 095abcb
Show file tree
Hide file tree
Showing 3 changed files with 27 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
24 changes: 20 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,19 @@ export class OpenChat extends OpenChatAgentWorker {

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

let lastOnline = lastOnlineDates.get(userId, now);
if (lastOnline === undefined) {
const lastOnlineCached = lastOnlineDates.get(userId, now);

const cacheValid =
lastOnlineCached !== undefined &&
(lastOnlineCached.lastOnline < now - 5 * ONE_MINUTE_MILLIS ||
lastOnlineCached.updated > now - 30 * 1000);

if (cacheValid) {
return lastOnlineCached.lastOnline;
} else {
const response = await this.getLastOnlineDatesBatched([userId]);
lastOnline = response[userId];
return response[userId];
}
return lastOnline;
}

getPublicProfile(userId?: string): Promise<PublicProfile> {
Expand Down Expand Up @@ -7139,6 +7154,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 095abcb

Please sign in to comment.