Skip to content

Commit 4deedf8

Browse files
committed
feat: add offline api for querying reactions
1 parent 6c1cb2a commit 4deedf8

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

src/client.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,16 +1792,38 @@ export class StreamChat {
17921792
sort: ReactionSort = [],
17931793
options: QueryReactionsOptions = {},
17941794
) {
1795-
// Make sure we wait for the connect promise if there is a pending one
1796-
await this.wsPromise;
1797-
1798-
// Return a list of channels
17991795
const payload = {
18001796
filter,
18011797
sort: normalizeQuerySort(sort),
18021798
...options,
18031799
};
18041800

1801+
if (this.offlineDb?.getReactions && !options.next) {
1802+
try {
1803+
const reactionsFromDb = await this.offlineDb.getReactions({
1804+
messageId: messageID,
1805+
filters: filter,
1806+
sort,
1807+
limit: options.limit,
1808+
});
1809+
1810+
if (reactionsFromDb) {
1811+
this.dispatchEvent({
1812+
type: 'offline_reactions.queried',
1813+
offlineReactions: reactionsFromDb as ReactionResponse[],
1814+
});
1815+
}
1816+
} catch (e) {
1817+
this.logger('warn', 'An error has occurred while querying offline reactions', {
1818+
error: e,
1819+
});
1820+
}
1821+
}
1822+
1823+
// TODO: Why is this here ? Looks not needed.
1824+
// Make sure we wait for the connect promise if there is a pending one
1825+
await this.wsPromise;
1826+
18051827
return await this.post<QueryReactionsAPIResponse>(
18061828
this.baseURL + '/messages/' + encodeURIComponent(messageID) + '/reactions',
18071829
payload,

src/events.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const EVENT_MAP = {
5656

5757
// local events
5858
'channels.queried': true,
59+
'offline_reactions.queried': true,
5960
'connection.changed': true,
6061
'connection.recovered': true,
6162
'transport.changed': true,

src/offline_support_api.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type {
55
ChannelFilters,
66
ChannelSort,
77
Event,
8+
ReactionFilters,
9+
ReactionSort,
810
} from './types';
911
import type { AxiosError } from 'axios';
1012
import type { StreamChat } from './client';
@@ -61,6 +63,13 @@ export type GetAppSettingsType = {
6163
userId: string;
6264
};
6365

66+
export type GetReactionsType = {
67+
messageId: string;
68+
filters?: Pick<ReactionFilters, 'type'>;
69+
sort?: ReactionSort;
70+
limit?: number;
71+
};
72+
6473
export type DeletePendingTaskType = { id: number };
6574

6675
export type DeleteReactionType = {
@@ -87,6 +96,7 @@ export interface OfflineDBApi {
8796
getAllChannelCids: () => Promise<string[]>;
8897
getLastSyncedAt: (options: GetLastSyncedAtType) => Promise<number | undefined>;
8998
getAppSettings: (options: GetAppSettingsType) => Promise<unknown>;
99+
getReactions: (options: GetReactionsType) => Promise<unknown>;
90100
executeSqlBatch: (queries: ExecuteBatchQueriesType) => Promise<unknown>;
91101
addPendingTask: (task: PendingTask) => Promise<() => Promise<void>>;
92102
getPendingTasks: (conditions?: GetPendingTasksType) => Promise<PendingTask[]>;
@@ -126,6 +136,8 @@ export abstract class AbstractOfflineDB implements OfflineDBApi {
126136

127137
abstract getAppSettings: OfflineDBApi['getAppSettings'];
128138

139+
abstract getReactions: OfflineDBApi['getReactions'];
140+
129141
abstract executeSqlBatch: OfflineDBApi['executeSqlBatch'];
130142

131143
abstract addPendingTask: OfflineDBApi['addPendingTask'];

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,7 @@ export type Event = CustomEventData & {
14161416
channels: ChannelAPIResponse[];
14171417
isLatestMessageSet?: boolean;
14181418
};
1419+
offlineReactions?: ReactionResponse[];
14191420
reaction?: ReactionResponse;
14201421
received_at?: string | Date;
14211422
shadow?: boolean;

0 commit comments

Comments
 (0)