Skip to content

Commit 640a15d

Browse files
committed
feat: add pending tasks for sending messages
1 parent ba4cf09 commit 640a15d

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

src/channel.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export class Channel {
191191
*
192192
* @return {Promise<SendMessageAPIResponse>} The Server Response
193193
*/
194-
async sendMessage(message: Message, options?: SendMessageOptions) {
194+
async _sendMessage(message: Message, options?: SendMessageOptions) {
195195
return await this.getClient().post<SendMessageAPIResponse>(
196196
this._channelURL() + '/message',
197197
{
@@ -201,6 +201,33 @@ export class Channel {
201201
);
202202
}
203203

204+
async sendMessage(message: Message, options?: SendMessageOptions) {
205+
console.log('TO SEND: ', message);
206+
try {
207+
const offlineDb = this.getClient().offlineDb;
208+
if (offlineDb) {
209+
const messageId = message.id;
210+
if (messageId) {
211+
return (await offlineDb.queueTask({
212+
task: {
213+
channelId: this.id as string,
214+
channelType: this.type,
215+
messageId,
216+
payload: [message, options],
217+
type: 'send-message',
218+
},
219+
})) as SendMessageAPIResponse;
220+
}
221+
}
222+
} catch (error) {
223+
this._client.logger('error', `offlineDb:send-message`, {
224+
tags: ['channel', 'offlineDb'],
225+
error,
226+
});
227+
}
228+
return await this._sendMessage(message, options);
229+
}
230+
204231
sendFile(
205232
uri: string | NodeJS.ReadableStream | Buffer | File,
206233
name?: string,

src/offline_support_api.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
ChannelSort,
99
Event,
1010
LocalMessage,
11+
Message,
1112
MessageResponse,
1213
PollResponse,
1314
ReactionFilters,
@@ -623,7 +624,10 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
623624
return response;
624625
};
625626

626-
private executeTask = async ({ task }: { task: PendingTask }) => {
627+
private executeTask = async (
628+
{ task }: { task: PendingTask },
629+
isPendingTask = false,
630+
) => {
627631
if (task.type === 'delete-message') {
628632
return await this.client._deleteMessage(...task.payload);
629633
}
@@ -632,6 +636,7 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
632636

633637
if (channelType && channelId) {
634638
const channel = this.client.channel(channelType, channelId);
639+
// TODO: Think about this a bit better. Do we really need to watch or is it okay if we don't ?
635640
if (!channel.initialized) {
636641
await channel.watch();
637642
}
@@ -643,6 +648,14 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
643648
if (task.type === 'delete-reaction') {
644649
return await channel._deleteReaction(...task.payload);
645650
}
651+
652+
if (task.type === 'send-message') {
653+
const newMessage = await channel._sendMessage(...task.payload);
654+
if (isPendingTask) {
655+
channel.state.addMessageSorted(newMessage.message, true);
656+
}
657+
return newMessage;
658+
}
646659
}
647660

648661
throw new Error('Invalid task type');
@@ -651,15 +664,17 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
651664
public executePendingTasks = async () => {
652665
const queue = await this.getPendingTasks();
653666
for (const task of queue) {
654-
console.log('[OFFLINE]: ', task);
655667
if (!task.id) {
656668
continue;
657669
}
658670

659671
try {
660-
await this.executeTask({
661-
task,
662-
});
672+
await this.executeTask(
673+
{
674+
task,
675+
},
676+
true,
677+
);
663678
} catch (e) {
664679
const error = e as AxiosError<APIErrorResponse>;
665680
if (error?.response?.data?.code === 4) {
@@ -701,6 +716,7 @@ export type PendingTaskTypes = {
701716
deleteMessage: 'delete-message';
702717
deleteReaction: 'delete-reaction';
703718
sendReaction: 'send-reaction';
719+
sendMessage: 'send-message';
704720
};
705721

706722
// TODO: Please rethink the definition of PendingTasks as it seems awkward
@@ -722,8 +738,16 @@ export type PendingTask = {
722738
payload: Parameters<Channel['deleteReaction']>;
723739
type: PendingTaskTypes['deleteReaction'];
724740
}
741+
| {
742+
payload: Parameters<Channel['sendMessage']>;
743+
type: PendingTaskTypes['sendMessage'];
744+
}
725745
);
726746

747+
export type PendingTaskExtraData = {
748+
message?: Message;
749+
};
750+
727751
/**
728752
* DBSyncManager has the responsibility to sync the channel states
729753
* within local database whenever possible.

0 commit comments

Comments
 (0)