Skip to content

Commit

Permalink
Concatenate members and basic_members (#6590)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Oct 15, 2024
1 parent ae647cd commit 669e311
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 22 deletions.
18 changes: 16 additions & 2 deletions frontend/openchat-agent/src/services/common/chatMappersV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2483,9 +2483,23 @@ export function groupDetailsResponse(
}
if ("Success" in value) {
const members =
"participants" in value.Success ? value.Success.participants : value.Success.members;
("participants" in value.Success ? value.Success.participants : value.Success.members).map(member);

const basicMembers = "basic_members" in value.Success ? value.Success.basic_members : [];
const membersSet = new Set<string>();
members.forEach((m) => membersSet.add(m.userId));
for (const id of basicMembers) {
const userId = principalBytesToString(id);
if (membersSet.add(userId)) {
members.push({
role: "member",
userId,
displayName: undefined,
});
}
}
return {
members: members.map(member),
members,
blockedUsers: new Set(value.Success.blocked_users.map(principalBytesToString)),
invitedUsers: new Set(value.Success.invited_users.map(principalBytesToString)),
pinnedMessages: new Set(value.Success.pinned_messages),
Expand Down
19 changes: 5 additions & 14 deletions frontend/openchat-agent/src/services/community/mappersV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,19 +450,6 @@ export function apiMemberRole(domain: MemberRole): TGroupRole {
return "Participant";
}
}
//
// export function communityRole(candid: ApiCommunityRole): MemberRole {
// if ("Member" in candid) {
// return "member";
// }
// if ("Admin" in candid) {
// return "admin";
// }
// if ("Owner" in candid) {
// return "owner";
// }
// throw new UnsupportedValueError("Unknown community role", candid);
// }

export function apiCommunityRole(newRole: MemberRole): TCommunityRole {
switch (newRole) {
Expand Down Expand Up @@ -504,7 +491,11 @@ export function communityDetailsResponse(
role: memberRole(m.role),
userId: principalBytesToString(m.user_id),
displayName: m.display_name,
})),
})).concat(value.Success.basic_members.map((id) => ({
role: "member",
userId: principalBytesToString(id),
displayName: undefined
}))),
blockedUsers: new Set(value.Success.blocked_users.map(principalBytesToString)),
invitedUsers: new Set(value.Success.invited_users.map(principalBytesToString)),
rules: value.Success.chat_rules,
Expand Down
2 changes: 2 additions & 0 deletions frontend/openchat-agent/src/typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4912,6 +4912,7 @@ export const CommunitySelectedInitialSuccessResult = Type.Object({
last_updated: Type.BigInt(),
latest_event_index: EventIndex,
members: Type.Array(CommunityMember),
basic_members: Type.Array(UserId),
blocked_users: Type.Array(UserId),
invited_users: Type.Array(UserId),
chat_rules: VersionedRules,
Expand Down Expand Up @@ -6367,6 +6368,7 @@ export const CommunitySelectedChannelInitialSuccessResult = Type.Object({
last_updated: Type.BigInt(),
latest_event_index: EventIndex,
members: Type.Array(GroupMember),
basic_members: Type.Array(UserId),
blocked_users: Type.Array(UserId),
invited_users: Type.Array(UserId),
pinned_messages: Type.Array(MessageIndex),
Expand Down
26 changes: 24 additions & 2 deletions frontend/openchat-agent/src/utils/caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import type { CryptocurrencyContent } from "openchat-shared";
import type { PrizeContent } from "openchat-shared";
import type { P2PSwapContent } from "openchat-shared";

const CACHE_VERSION = 115;
const CACHE_VERSION = 116;
const EARLIEST_SUPPORTED_MIGRATION = 115;
const MAX_INDEX = 9999999999;

Expand Down Expand Up @@ -150,6 +150,22 @@ type MigrationFunction<T> = (
// await tx.objectStore("chats").clear();
// }

async function clearGroupDetailsStore(
_db: IDBPDatabase<ChatSchema>,
_principal: Principal,
tx: IDBPTransaction<ChatSchema, StoreNames<ChatSchema>[], "versionchange">,
) {
await tx.objectStore("group_details").clear();
}

async function clearCommunityDetailsStore(
_db: IDBPDatabase<ChatSchema>,
_principal: Principal,
tx: IDBPTransaction<ChatSchema, StoreNames<ChatSchema>[], "versionchange">,
) {
await tx.objectStore("community_details").clear();
}

async function clearEverything(
db: IDBPDatabase<ChatSchema>,
_principal: Principal,
Expand All @@ -159,7 +175,13 @@ async function clearEverything(
}

const migrations: Record<number, MigrationFunction<ChatSchema>> = {
114: clearEverything,
115: clearEverything,
116: async (db, principal, transaction) => {
await Promise.all([
clearGroupDetailsStore(db, principal, transaction),
clearCommunityDetailsStore(db, principal, transaction)
])
},
};

async function migrate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import type { MessageIndex } from "../../shared/MessageIndex";
import type { UserId } from "../../shared/UserId";
import type { VersionedRules } from "../../shared/VersionedRules";

export type CommunitySelectedChannelInitialSuccessResult = { timestamp: bigint, last_updated: bigint, latest_event_index: EventIndex, members: Array<GroupMember>, blocked_users: Array<UserId>, invited_users: Array<UserId>, pinned_messages: Array<MessageIndex>, chat_rules: VersionedRules, };
export type CommunitySelectedChannelInitialSuccessResult = { timestamp: bigint, last_updated: bigint, latest_event_index: EventIndex, members: Array<GroupMember>, basic_members: Array<UserId>, blocked_users: Array<UserId>, invited_users: Array<UserId>, pinned_messages: Array<MessageIndex>, chat_rules: VersionedRules, };
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import type { UserGroupDetails } from "../../shared/UserGroupDetails";
import type { UserId } from "../../shared/UserId";
import type { VersionedRules } from "../../shared/VersionedRules";

export type CommunitySelectedInitialSuccessResult = { timestamp: bigint, last_updated: bigint, latest_event_index: EventIndex, members: Array<CommunityMember>, blocked_users: Array<UserId>, invited_users: Array<UserId>, chat_rules: VersionedRules, user_groups: Array<UserGroupDetails>, referrals: Array<UserId>, };
export type CommunitySelectedInitialSuccessResult = { timestamp: bigint, last_updated: bigint, latest_event_index: EventIndex, members: Array<CommunityMember>, basic_members: Array<UserId>, blocked_users: Array<UserId>, invited_users: Array<UserId>, chat_rules: VersionedRules, user_groups: Array<UserGroupDetails>, referrals: Array<UserId>, };
4 changes: 2 additions & 2 deletions tsBindings/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ export type CommunityAddMembersToChannelArgs = { channel_id: bigint, user_ids: A
export type CommunityAddMembersToChannelPartialSuccessResult = { users_added: Array<UserId>, users_already_in_channel: Array<UserId>, users_limit_reached: Array<UserId>, users_failed_with_error: Array<CommunityAddMembersToChannelUserFailedError>, };
export type CommunityAddMembersToChannelFailedResult = { users_already_in_channel: Array<UserId>, users_limit_reached: Array<UserId>, users_failed_with_error: Array<CommunityAddMembersToChannelUserFailedError>, };
export type CommunityChangeChannelRoleArgs = { channel_id: bigint, user_id: UserId, new_role: GroupRole, };
export type CommunitySelectedInitialSuccessResult = { timestamp: bigint, last_updated: bigint, latest_event_index: EventIndex, members: Array<CommunityMember>, blocked_users: Array<UserId>, invited_users: Array<UserId>, chat_rules: VersionedRules, user_groups: Array<UserGroupDetails>, referrals: Array<UserId>, };
export type CommunitySelectedInitialSuccessResult = { timestamp: bigint, last_updated: bigint, latest_event_index: EventIndex, members: Array<CommunityMember>, basic_members: Array<UserId>, blocked_users: Array<UserId>, invited_users: Array<UserId>, chat_rules: VersionedRules, user_groups: Array<UserGroupDetails>, referrals: Array<UserId>, };
export type CommunityBlockUserArgs = { user_id: UserId, };
export type CommunityCreateChannelResponse = { "Success": CommunityCreateChannelSuccessResult } | { "NameTooShort": FieldTooShortResult } | { "NameTooLong": FieldTooLongResult } | "NameReserved" | { "DescriptionTooLong": FieldTooLongResult } | { "RulesTooShort": FieldTooShortResult } | { "RulesTooLong": FieldTooLongResult } | { "AvatarTooBig": FieldTooLongResult } | "AccessGateInvalid" | { "MaxChannelsCreated": number } | "NameTaken" | "UserSuspended" | "NotAuthorized" | "CommunityFrozen" | "ExternalUrlInvalid" | { "InternalError": string } | "UserLapsed";
export type CommunitySelectedUpdatesSuccessResult = { timestamp: bigint, last_updated: bigint, members_added_or_updated: Array<CommunityMember>, members_removed: Array<UserId>, blocked_users_added: Array<UserId>, blocked_users_removed: Array<UserId>, invited_users?: Array<UserId> | undefined, chat_rules?: VersionedRules | undefined, user_groups: Array<UserGroupDetails>, user_groups_deleted: Array<number>, referrals_added: Array<UserId>, referrals_removed: Array<UserId>, };
Expand Down Expand Up @@ -678,7 +678,7 @@ export type LocalUserIndexChatEventsEventsArgs = { context: LocalUserIndexChatEv
export type LocalUserIndexInviteUsersToChannelResponse = "Success" | { "PartialSuccess": LocalUserIndexInviteUsersToChannelPartialSuccessResult } | { "Failed": LocalUserIndexInviteUsersToChannelFailedResult } | "CommunityFrozen" | "UserNotInCommunity" | "ChannelNotFound" | "UserNotInChannel" | "UserSuspended" | "NotAuthorized" | { "TooManyInvites": number } | { "InternalError": string };
export type LocalUserIndexReportMessageArgs = { chat_id: MultiUserChat, thread_root_message_index?: MessageIndex | undefined, event_index: EventIndex, reason_code: number, notes?: string | undefined, };
export type CommunitySelectedChannelUpdatesResponse = { "Success": SelectedGroupUpdates } | { "SuccessNoUpdates": bigint } | "PrivateCommunity" | "ChannelNotFound" | "PrivateChannel";
export type CommunitySelectedChannelInitialSuccessResult = { timestamp: bigint, last_updated: bigint, latest_event_index: EventIndex, members: Array<GroupMember>, blocked_users: Array<UserId>, invited_users: Array<UserId>, pinned_messages: Array<MessageIndex>, chat_rules: VersionedRules, };
export type CommunitySelectedChannelInitialSuccessResult = { timestamp: bigint, last_updated: bigint, latest_event_index: EventIndex, members: Array<GroupMember>, basic_members: Array<UserId>, blocked_users: Array<UserId>, invited_users: Array<UserId>, pinned_messages: Array<MessageIndex>, chat_rules: VersionedRules, };
export type CommunityCommunityMembersResponse = { "Success": CommunityCommunityMembersSuccessResult } | "PrivateCommunity";
export type CommunityAddMembersToChannelResponse = "Success" | { "PartialSuccess": CommunityAddMembersToChannelPartialSuccessResult } | { "Failed": CommunityAddMembersToChannelFailedResult } | "CommunityFrozen" | "CommunityPublic" | "UserSuspended" | "UserLapsed" | "UserNotInCommunity" | "UserNotInChannel" | "ChannelNotFound" | { "UserLimitReached": number } | "NotAuthorized" | { "InternalError": string };
export type CommunitySelectedInitialResponse = { "Success": CommunitySelectedInitialSuccessResult } | "PrivateCommunity";
Expand Down

0 comments on commit 669e311

Please sign in to comment.