Skip to content

Commit d356bbf

Browse files
committed
feat: move member and message updated events to llc
1 parent f41cd88 commit d356bbf

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

src/channel.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,6 +1804,7 @@ export class Channel {
18041804
} else {
18051805
channelState.removePinnedMessage(event.message);
18061806
}
1807+
this.getClient().offlineDb?.handleMessageUpdatedEvent({ event });
18071808
}
18081809
break;
18091810
case 'channel.truncated':
@@ -1853,6 +1854,9 @@ export class Channel {
18531854
...channelState.members,
18541855
[memberCopy.user.id]: memberCopy,
18551856
};
1857+
this.getClient().offlineDb?.handleMemberEvent({
1858+
event: { ...event, member: memberCopy },
1859+
});
18561860
}
18571861

18581862
const currentUserId = this.getClient().userID;
@@ -1875,6 +1879,10 @@ export class Channel {
18751879

18761880
channelState.members = newMembers;
18771881

1882+
this.getClient().offlineDb?.handleMemberEvent({
1883+
event,
1884+
});
1885+
18781886
// TODO?: unset membership
18791887
}
18801888
break;

src/offline_support_api.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
AppSettingsAPIResponse,
44
ChannelAPIResponse,
55
ChannelFilters,
6+
ChannelMemberResponse,
67
ChannelResponse,
78
ChannelSort,
89
Event,
@@ -75,12 +76,23 @@ export type UpsertMessagesType = {
7576
flush?: boolean;
7677
};
7778

79+
export type UpsertMembersType = {
80+
cid: string;
81+
members: ChannelMemberResponse[];
82+
flush?: boolean;
83+
};
84+
7885
export type UpdateReactionType = {
7986
message: MessageResponse | LocalMessage;
8087
reaction: ReactionResponse;
8188
flush?: boolean;
8289
};
8390

91+
export type UpdateMessageType = {
92+
message: MessageResponse | LocalMessage;
93+
flush?: boolean;
94+
};
95+
8496
export type GetChannelsType = {
8597
cids: string[];
8698
userId: string;
@@ -117,6 +129,12 @@ export type DeleteReactionType = {
117129
flush?: boolean;
118130
};
119131

132+
export type DeleteMemberType = {
133+
cid: string;
134+
member: ChannelMemberResponse;
135+
flush?: boolean;
136+
};
137+
120138
export type DeleteMessageType = { id: string; flush?: boolean };
121139

122140
export type ChannelExistsType = { cid: string };
@@ -137,7 +155,9 @@ export interface OfflineDBApi {
137155
upsertChannelData: (options: UpsertChannelDataType) => Promise<ExecuteBatchQueriesType>;
138156
upsertReads: (options: UpsertReadsType) => Promise<ExecuteBatchQueriesType>;
139157
upsertMessages: (options: UpsertMessagesType) => Promise<ExecuteBatchQueriesType>;
158+
upsertMembers: (options: UpsertMembersType) => Promise<ExecuteBatchQueriesType>;
140159
updateReaction: (options: UpdateReactionType) => Promise<ExecuteBatchQueriesType>;
160+
updateMessage: (options: UpdateMessageType) => Promise<ExecuteBatchQueriesType>;
141161
getChannels: (options: GetChannelsType) => Promise<unknown>;
142162
getChannelsForQuery: (
143163
options: GetChannelsForQueryType,
@@ -151,6 +171,7 @@ export interface OfflineDBApi {
151171
getPendingTasks: (conditions?: GetPendingTasksType) => Promise<PendingTask[]>;
152172
deletePendingTask: (options: DeletePendingTaskType) => Promise<ExecuteBatchQueriesType>;
153173
deleteReaction: (options: DeleteReactionType) => Promise<ExecuteBatchQueriesType>;
174+
deleteMember: (options: DeleteMemberType) => Promise<ExecuteBatchQueriesType>;
154175
hardDeleteMessage: (options: DeleteMessageType) => Promise<ExecuteBatchQueriesType>;
155176
softDeleteMessage: (options: DeleteMessageType) => Promise<ExecuteBatchQueriesType>;
156177
resetDB: () => Promise<unknown>;
@@ -184,8 +205,12 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
184205

185206
abstract upsertMessages: OfflineDBApi['upsertMessages'];
186207

208+
abstract upsertMembers: OfflineDBApi['upsertMembers'];
209+
187210
abstract updateReaction: OfflineDBApi['updateReaction'];
188211

212+
abstract updateMessage: OfflineDBApi['updateMessage'];
213+
189214
abstract getChannels: OfflineDBApi['getChannels'];
190215

191216
abstract getChannelsForQuery: OfflineDBApi['getChannelsForQuery'];
@@ -208,6 +233,8 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
208233

209234
abstract deleteReaction: OfflineDBApi['deleteReaction'];
210235

236+
abstract deleteMember: OfflineDBApi['deleteMember'];
237+
211238
abstract hardDeleteMessage: OfflineDBApi['hardDeleteMessage'];
212239

213240
abstract softDeleteMessage: OfflineDBApi['softDeleteMessage'];
@@ -378,6 +405,55 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
378405
return [];
379406
};
380407

408+
public handleMemberEvent = async ({
409+
event,
410+
flush = true,
411+
}: {
412+
event: Event;
413+
flush?: boolean;
414+
}) => {
415+
const { member, cid, type } = event;
416+
417+
if (member && cid) {
418+
return await this.queriesWithChannelGuard(
419+
{ event, flush },
420+
async (flushOverride) => {
421+
if (type === 'member.removed') {
422+
return await this.deleteMember({ member, cid, flush: flushOverride });
423+
}
424+
425+
return await this.upsertMembers({
426+
cid,
427+
members: [member],
428+
flush: flushOverride,
429+
});
430+
},
431+
);
432+
}
433+
434+
return [];
435+
};
436+
437+
public handleMessageUpdatedEvent = async ({
438+
event,
439+
flush = true,
440+
}: {
441+
event: Event;
442+
flush?: boolean;
443+
}) => {
444+
const { message } = event;
445+
446+
if (message && !message.parent_id) {
447+
return await this.queriesWithChannelGuard(
448+
{ event, flush },
449+
async (flushOverride) =>
450+
await this.updateMessage({ message, flush: flushOverride }),
451+
);
452+
}
453+
454+
return [];
455+
};
456+
381457
public queueTask = async ({ task }: { task: PendingTask }) => {
382458
let response;
383459
try {
@@ -638,6 +714,14 @@ export class OfflineDBSyncManager {
638714
return await this.offlineDb.handleDeleteMessage({ event, flush });
639715
}
640716

717+
if (type === 'message.updated') {
718+
return this.offlineDb.handleMessageUpdatedEvent({ event, flush });
719+
}
720+
721+
if (type.startsWith('member.')) {
722+
return await this.offlineDb.handleMemberEvent({ event, flush });
723+
}
724+
641725
return [];
642726
};
643727

0 commit comments

Comments
 (0)