Skip to content

Commit

Permalink
feat: push preferences (#1431)
Browse files Browse the repository at this point in the history
Co-authored-by: Thierry Schellenbach <[email protected]>
Co-authored-by: Tommaso Barbugli <[email protected]>
Co-authored-by: Oliver Lazoroski <[email protected]>
  • Loading branch information
4 people authored Jan 27, 2025
1 parent 4ae4617 commit c9861fd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
PartialUpdateMemberAPIResponse,
AIState,
MessageOptions,
PushPreference,
} from './types';
import { Role } from './permissions';
import { DEFAULT_QUERY_CHANNEL_MESSAGE_LIST_PAGE_SIZE } from './constants';
Expand Down Expand Up @@ -97,6 +98,7 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
lastTypingEvent: Date | null;
isTyping: boolean;
disconnected: boolean;
push_preferences?: PushPreference;

/**
* constructor - Create a channel
Expand Down
14 changes: 14 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ import {
PollVote,
PollVoteData,
PollVotesAPIResponse,
PushPreference,
PushProvider,
PushProviderConfig,
PushProviderID,
Expand Down Expand Up @@ -201,6 +202,7 @@ import {
UpdatePollAPIResponse,
UpdatePollOptionAPIResponse,
UpdateSegmentData,
UpsertPushPreferencesResponse,
UserCustomEvent,
UserFilters,
UserOptions,
Expand Down Expand Up @@ -1666,6 +1668,7 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
c.data = channelState.channel;
c.offlineMode = offlineMode;
c.initialized = !offlineMode;
c.push_preferences = channelState.push_preferences;

let updatedMessagesSet;
if (skipInitialization === undefined) {
Expand Down Expand Up @@ -1805,6 +1808,17 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
return await this.post<GetUnreadCountBatchAPIResponse>(this.baseURL + '/unread_batch', { user_ids: userIDs });
}

/**
* setPushPreferences - Applies the list of push preferences.
*
* @param {PushPreference[]} A list of push preferences.
*
* @return {<UpsertPushPreferencesResponse>}
*/
async setPushPreferences(preferences: PushPreference[]) {
return await this.post<UpsertPushPreferencesResponse>(this.baseURL + '/push_preferences', { preferences });
}

/**
* removeDevice - Removes the device with the given id. Clientside users can only delete their own devices
*
Expand Down
23 changes: 23 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ export type ChannelAPIResponse<StreamChatGenerics extends ExtendableGenerics = D
hidden?: boolean;
membership?: ChannelMemberResponse<StreamChatGenerics> | null;
pending_messages?: PendingMessageResponse<StreamChatGenerics>[];
push_preferences?: PushPreference;
read?: ReadResponse<StreamChatGenerics>[];
threads?: ThreadResponse[];
watcher_count?: number;
Expand Down Expand Up @@ -633,6 +634,27 @@ export type GetUnreadCountAPIResponse = APIResponse & {
total_unread_threads_count: number;
};

export type ChatLevelPushPreference = 'all' | 'none' | 'mentions' | (string & {});

export type PushPreference = {
callLevel?: 'all' | 'none' | (string & {});
chatLevel?: ChatLevelPushPreference;
disabledUntil?: string; // snooze till this time
removeDisable?: boolean; // Temporary flag for resetting disabledUntil
};

export type ChannelPushPreference = {
chatLevel?: ChatLevelPushPreference; // "all", "none", "mentions", or other custom strings
disabledUntil?: string;
removeDisable?: boolean; // Temporary flag for resetting disabledUntil
};

export type UpsertPushPreferencesResponse = APIResponse & {
// Mapping of user IDs to their push preferences
userChannelPreferences: Record<string, Record<string, ChannelPushPreference>>;
userPreferences: Record<string, PushPreference>; // Mapping of user -> channel id -> push preferences
};

export type GetUnreadCountBatchAPIResponse = APIResponse & {
counts_by_user: { [userId: string]: GetUnreadCountAPIResponse };
};
Expand Down Expand Up @@ -773,6 +795,7 @@ export type OwnUserBase<StreamChatGenerics extends ExtendableGenerics = DefaultG
unread_threads: number;
invisible?: boolean;
privacy_settings?: PrivacySettings;
push_preferences?: PushPreference;
roles?: string[];
};

Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export function isOwnUserBaseProperty(property: string) {
invisible: true,
privacy_settings: true,
roles: true,
push_preferences: true,
};

return ownUserBaseProperties[property as keyof OwnUserBase];
Expand Down

0 comments on commit c9861fd

Please sign in to comment.