Skip to content

Commit

Permalink
Merge branch 'master' into bash
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Dec 6, 2023
2 parents e66bf1d + a42474f commit 9da5dbc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ export const idlFactory = ({ IDL }) => {
'updated_since' : TimestampMillis,
})
),
'users_suspended_since' : IDL.Opt(TimestampMillis),
});
const UsersV2Response = IDL.Variant({
'Success' : IDL.Record({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,7 @@ export interface UsersV2Args {
'user_groups' : Array<
{ 'users' : Array<UserId>, 'updated_since' : TimestampMillis }
>,
'users_suspended_since' : [] | [TimestampMillis],
}
export type UsersV2Response = {
'Success' : { 'timestamp' : TimestampMillis, 'users' : Array<UserSummary> }
Expand Down
23 changes: 15 additions & 8 deletions frontend/openchat-agent/src/services/userIndex/userIndex.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ import { apiOptional, apiToken } from "../common/chatMappers";
import type { AgentConfig } from "../../config";
import {
getCachedUsers,
getSuspendedUsersSyncedUpTo,
setCachedUsers,
setDisplayNameInCache,
setSuspendedUsersSyncedUpTo,
setUserDiamondStatusInCache,
setUsernameInCache,
} from "../../utils/userCache";
Expand Down Expand Up @@ -121,12 +123,13 @@ export class UserIndexClient extends CandidService {
const allUsers = users.userGroups.flatMap((g) => g.users);

const fromCache = await getCachedUsers(allUsers);
const suspendedUsersSyncedTo = await getSuspendedUsersSyncedUpTo();

// We throw away all of the updatedSince values passed in and instead use the values from the cache, this
// ensures the cache is always correct and doesn't miss any updates
const args = this.buildGetUsersArgs(allUsers, fromCache, allowStale);

const response = await this.getUsersFromBackend(users);
const response = await this.getUsersFromBackend(users, suspendedUsersSyncedTo);

const requestedFromServer = new Set<string>([...args.userGroups.flatMap((g) => g.users)]);

Expand All @@ -142,23 +145,27 @@ export class UserIndexClient extends CandidService {
console.error("Failed to save users to the cache", err),
);

if (mergedResponse.serverTimestamp !== undefined) {
setSuspendedUsersSyncedUpTo(mergedResponse.serverTimestamp).catch((err) =>
console.error("Failed to set 'suspended users synced up to' in the cache", err),
);
}

return mergedResponse;
}

private getUsersFromBackend(users: UsersArgs): Promise<UsersResponse> {
private getUsersFromBackend(
users: UsersArgs,
suspendedUsersSyncedUpTo: bigint | undefined,
): Promise<UsersResponse> {
const userGroups = users.userGroups.filter((g) => g.users.length > 0);

if (userGroups.length === 0) {
return Promise.resolve({
serverTimestamp: undefined,
users: [],
});
}
const args = {
user_groups: userGroups.map(({ users, updatedSince }) => ({
users: users.map((u) => Principal.fromText(u)),
updated_since: updatedSince,
})),
users_suspended_since: apiOptional(identity, suspendedUsersSyncedUpTo),
};
return this.handleQueryResponse(
() => this.userIndexService.users_v2(args),
Expand Down
21 changes: 20 additions & 1 deletion frontend/openchat-agent/src/utils/userCache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { openDB, type DBSchema, type IDBPDatabase } from "idb";
import type { DiamondMembershipStatus, UserSummary } from "openchat-shared";

const CACHE_VERSION = 4;
const CACHE_VERSION = 5;

let db: UserDatabase | undefined;

Expand All @@ -12,6 +12,11 @@ export interface UserSchema extends DBSchema {
key: string;
value: UserSummary;
};

suspendedUsersSyncedUpTo: {
key: "value";
value: bigint;
};
}

export function lazyOpenUserCache(): UserDatabase {
Expand All @@ -27,7 +32,11 @@ function openUserCache(): UserDatabase {
if (db.objectStoreNames.contains("users")) {
db.deleteObjectStore("users");
}
if (db.objectStoreNames.contains("suspendedUsersSyncedUpTo")) {
db.deleteObjectStore("suspendedUsersSyncedUpTo");
}
db.createObjectStore("users");
db.createObjectStore("suspendedUsersSyncedUpTo");
},
});
}
Expand Down Expand Up @@ -109,3 +118,13 @@ export async function setUserDiamondStatusInCache(
}
await tx.done;
}

export async function getSuspendedUsersSyncedUpTo(): Promise<bigint | undefined> {
const resolvedDb = await lazyOpenUserCache();
return await resolvedDb.get("suspendedUsersSyncedUpTo", "value");
}

export async function setSuspendedUsersSyncedUpTo(value: bigint): Promise<void> {
const resolvedDb = await lazyOpenUserCache();
await resolvedDb.put("suspendedUsersSyncedUpTo", value, "value");
}

0 comments on commit 9da5dbc

Please sign in to comment.