diff --git a/src/channel.ts b/src/channel.ts index f584628edf..c82230b9c9 100644 --- a/src/channel.ts +++ b/src/channel.ts @@ -60,6 +60,7 @@ import { MessageOptions, PushPreference, UpdateChannelOptions, + CustomChannelType, } from './types'; import { Role } from './permissions'; import { DEFAULT_QUERY_CHANNEL_MESSAGE_LIST_PAGE_SIZE } from './constants'; @@ -71,8 +72,8 @@ export class Channel { _client: StreamChat; type: string; id: string | undefined; - data: ChannelData | ChannelResponse | undefined; - _data: ChannelData | ChannelResponse; + data: Partial | undefined; + _data: Partial; cid: string; /** */ listeners: { [key: string]: (string | EventHandler)[] }; @@ -371,12 +372,13 @@ export class Channel { * @return {Promise} The server response */ async update( - channelData: Partial | Partial = {}, + channelData: Partial = {}, updateMessage?: Message, options?: ChannelUpdateOptions, ) { // Strip out reserved names that will result in API errors. - const reserved = [ + // TODO: this needs to be typed better + const reserved: Exclude[] = [ 'config', 'cid', 'created_by', @@ -388,8 +390,8 @@ export class Channel { 'last_message_at', 'own_capabilities', ]; + reserved.forEach((key) => { - // @ts-expect-error certain keys don't exist delete channelData[key]; }); @@ -412,7 +414,6 @@ export class Channel { const areCapabilitiesChanged = [...(data.channel.own_capabilities || [])].sort().join() !== - // @ts-expect-error capabilities do not exist on channel [...(Array.isArray(this.data?.own_capabilities) ? (this.data?.own_capabilities as string[]) : [])].sort().join(); this.data = data.channel; // If the capabiltities are changed, we trigger the `capabilities.changed` event. @@ -420,7 +421,6 @@ export class Channel { this.getClient().dispatchEvent({ type: 'capabilities.changed', cid: this.cid, - // @ts-expect-error capabilities do not exist on channel own_capabilities: data.channel.own_capabilities, }); } @@ -1050,7 +1050,6 @@ export class Channel { if (message.user?.id && this.getClient().userMuteStatus(message.user.id)) return false; // Return false if channel doesn't allow read events. - // @ts-expect-error capabilities if (Array.isArray(this.data?.own_capabilities) && !this.data?.own_capabilities.includes('read-events')) { return false; } @@ -1185,8 +1184,7 @@ export class Channel { const areCapabilitiesChanged = [...(state.channel.own_capabilities || [])].sort().join() !== - // @ts-expect-error capabilities - [...(Array.isArray(this.data?.own_capabilities) ? (this.data?.own_capabilities as string[]) : [])].sort().join(); + [...(this.data && Array.isArray(this.data?.own_capabilities) ? this.data.own_capabilities : [])].sort().join(); this.data = state.channel; this.offlineMode = false; @@ -1194,7 +1192,6 @@ export class Channel { this.getClient().dispatchEvent({ type: 'capabilities.changed', cid: this.cid, - // @ts-expect-error capabilities own_capabilities: state.channel.own_capabilities, }); } @@ -1569,16 +1566,13 @@ export class Channel { } case 'channel.updated': if (event.channel) { - // @ts-expect-error frozen const isFrozenChanged = event.channel?.frozen !== undefined && event.channel.frozen !== channel.data?.frozen; if (isFrozenChanged) { this.query({ state: false, messages: { limit: 0 }, watchers: { limit: 0 } }); } channel.data = { ...event.channel, - // @ts-expect-error hidden hidden: event.channel?.hidden ?? channel.data?.hidden, - // @ts-expect-error capabilities own_capabilities: event.channel?.own_capabilities ?? channel.data?.own_capabilities, }; } @@ -1612,9 +1606,7 @@ export class Channel { if (!event.user?.id) break; channelState.members[event.user.id] = { ...(channelState.members[event.user.id] || {}), - // @ts-expect-error shadow shadow_banned: !!event.shadow, - // @ts-expect-error shadow banned: !event.shadow, user: { ...(channelState.members[event.user.id]?.user || {}), ...event.user }, }; diff --git a/src/channel_state.ts b/src/channel_state.ts index 8ed44ce8dd..938254a5bf 100644 --- a/src/channel_state.ts +++ b/src/channel_state.ts @@ -280,11 +280,7 @@ export class ChannelState { this.pinnedMessages = result; } - addReaction( - reaction: ReactionResponse, - message?: MessageResponse, - enforce_unique?: boolean, - ) { + addReaction(reaction: ReactionResponse, message?: MessageResponse, enforce_unique?: boolean) { if (!message) return; const messageWithReaction = message; this._updateMessage(message, (msg) => { @@ -313,10 +309,7 @@ export class ChannelState { return ownReactions; } - _removeOwnReactionFromMessage( - ownReactions: ReactionResponse[] | null | undefined, - reaction: ReactionResponse, - ) { + _removeOwnReactionFromMessage(ownReactions: ReactionResponse[] | null | undefined, reaction: ReactionResponse) { if (ownReactions) { return ownReactions.filter((item) => item.user_id !== reaction.user_id || item.type !== reaction.type); } @@ -333,13 +326,7 @@ export class ChannelState { return messageWithReaction; } - _updateQuotedMessageReferences({ - message, - remove, - }: { - message: MessageResponse; - remove?: boolean; - }) { + _updateQuotedMessageReferences({ message, remove }: { message: MessageResponse; remove?: boolean }) { const parseMessage = (m: ReturnType) => (({ ...m, @@ -382,9 +369,7 @@ export class ChannelState { pinned?: boolean; show_in_channel?: boolean; }, - updateFunc: ( - msg: ReturnType, - ) => ReturnType, + updateFunc: (msg: ReturnType) => ReturnType, ) { const { parent_id, show_in_channel, pinned } = message; @@ -495,10 +480,7 @@ export class ChannelState { * @param {UserResponse} user */ updateUserMessages = (user: UserResponse) => { - const _updateUserMessages = ( - messages: Array>, - user: UserResponse, - ) => { + const _updateUserMessages = (messages: Array>, user: UserResponse) => { for (let i = 0; i < messages.length; i++) { const m = messages[i]; if (m.user?.id === user.id) { @@ -701,10 +683,7 @@ export class ChannelState { addIfDoesNotExist = true, messageSetToAddToIfDoesNotExist: MessageSetType = 'current', ) { - let messagesToAdd: ( - | MessageResponse - | ReturnType - )[] = newMessages; + let messagesToAdd: (MessageResponse | ReturnType)[] = newMessages; let targetMessageSetIndex!: number; if (addIfDoesNotExist) { const overlappingMessageSetIndices = this.messageSets diff --git a/src/client.ts b/src/client.ts index 0aa51a25b6..72590b3f72 100644 --- a/src/client.ts +++ b/src/client.ts @@ -115,7 +115,6 @@ import { ListImportsResponse, Logger, MarkChannelsReadOptions, - Message, MessageFilters, MessageFlagsFilters, MessageFlagsPaginationOptions, @@ -1250,12 +1249,11 @@ export class StreamChat { } /** Updating only available properties in _user object. */ - for (const key in event.user) { - if (_user && key in _user) { - const updateKey = key as keyof typeof event.user; + for (const key in _user) { + const updateKey = key as keyof typeof _user; - // TODO: revisit - // @ts-expect-error ??? + if (updateKey in event.user) { + // @ts-expect-error it has an issue with this, not sure why _user[updateKey] = event.user[updateKey]; } } @@ -2612,8 +2610,7 @@ export class StreamChat { throw Error('Please specify the message id when calling updateMessage'); } - // FIXME: brother in fucking christ - const clonedMessage: Partial = { ...message }; + const clonedMessage: Partial = { ...message }; delete clonedMessage.id; const reservedMessageFields: Array = [ @@ -2628,7 +2625,6 @@ export class StreamChat { 'type', 'updated_at', 'user', - '__html', ]; reservedMessageFields.forEach(function (item) { diff --git a/src/connection.ts b/src/connection.ts index 0b69497bbc..0f6296d9e8 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -310,7 +310,10 @@ export class StableWSConnection { this.client.insightMetrics.wsConsecutiveFailures++; this.client.insightMetrics.wsTotalFailures++; - const insights = buildWsFatalInsight((this as unknown) as StableWSConnection, convertErrorToJson(error as Error)); + const insights = buildWsFatalInsight( + (this as unknown) as StableWSConnection, + convertErrorToJson(error as Error), + ); postInsights?.('ws_fatal', insights); } throw error; diff --git a/src/moderation.ts b/src/moderation.ts index 1a35f87223..40b0f9fd83 100644 --- a/src/moderation.ts +++ b/src/moderation.ts @@ -102,13 +102,10 @@ export class Moderation { * @returns */ async muteUser(targetID: string, options: ModerationMuteOptions = {}) { - return await this.client.post( - this.client.baseURL + '/api/v2/moderation/mute', - { - target_ids: [targetID], - ...options, - }, - ); + return await this.client.post(this.client.baseURL + '/api/v2/moderation/mute', { + target_ids: [targetID], + ...options, + }); } /** diff --git a/src/poll.ts b/src/poll.ts index 15f6b4f0d0..ebcfe80445 100644 --- a/src/poll.ts +++ b/src/poll.ts @@ -48,22 +48,13 @@ type PollVoteCastedRemoved = PollVoteEvent & { type: 'poll.vote_removed'; }; -const isPollUpdatedEvent = (e: Event): e is PollUpdatedEvent => - e.type === 'poll.updated'; -const isPollClosedEventEvent = (e: Event): e is PollClosedEvent => - e.type === 'poll.closed'; -const isPollVoteCastedEvent = (e: Event): e is PollVoteCastedEvent => - e.type === 'poll.vote_casted'; -const isPollVoteChangedEvent = ( - e: Event, -): e is PollVoteCastedChanged => e.type === 'poll.vote_changed'; -const isPollVoteRemovedEvent = ( - e: Event, -): e is PollVoteCastedRemoved => e.type === 'poll.vote_removed'; - -export const isVoteAnswer = ( - vote: PollVote | PollAnswer, -): vote is PollAnswer => !!(vote as PollAnswer)?.answer_text; +const isPollUpdatedEvent = (e: Event): e is PollUpdatedEvent => e.type === 'poll.updated'; +const isPollClosedEventEvent = (e: Event): e is PollClosedEvent => e.type === 'poll.closed'; +const isPollVoteCastedEvent = (e: Event): e is PollVoteCastedEvent => e.type === 'poll.vote_casted'; +const isPollVoteChangedEvent = (e: Event): e is PollVoteCastedChanged => e.type === 'poll.vote_changed'; +const isPollVoteRemovedEvent = (e: Event): e is PollVoteCastedRemoved => e.type === 'poll.vote_removed'; + +export const isVoteAnswer = (vote: PollVote | PollAnswer): vote is PollAnswer => !!(vote as PollAnswer)?.answer_text; export type PollAnswersQueryParams = { filter?: QueryVotesFilters; @@ -79,7 +70,8 @@ export type PollOptionVotesQueryParams = { type OptionId = string; -export type PollState = CustomPollType & +// FIXME: ideally this would work without the unknown record... +export type PollState = (CustomPollType & Record) & Omit & { lastActivityAt: Date; // todo: would be ideal to get this from the BE maxVotedOptionIds: OptionId[]; @@ -381,9 +373,7 @@ function getOwnVotesByOptionId(ownVotes: PollVote[]) { }, {}); } -export function extractPollData( - pollResponse: PollResponse, -): PollData { +export function extractPollData(pollResponse: PollResponse): PollData { return { allow_answers: pollResponse.allow_answers, allow_user_suggested_options: pollResponse.allow_user_suggested_options, diff --git a/src/store.ts b/src/store.ts index 71334a3e57..e717b0a2e7 100644 --- a/src/store.ts +++ b/src/store.ts @@ -6,11 +6,7 @@ function isPatch(value: T | Patch): value is Patch { return typeof value === 'function'; } -// eslint-disable-next-line @typescript-eslint/no-empty-interface -interface EmptyInterface {} -type StateShape = Record | EmptyInterface; - -export class StateStore { +export class StateStore> { private handlerSet = new Set>(); private static logCount = 5; diff --git a/src/token_manager.ts b/src/token_manager.ts index 2ced62c380..e569b95d9c 100644 --- a/src/token_manager.ts +++ b/src/token_manager.ts @@ -1,7 +1,7 @@ import { Secret } from 'jsonwebtoken'; import { UserFromToken, JWTServerToken, JWTUserToken } from './signing'; import { isFunction } from './utils'; -import { TokenOrProvider, UserResponse } from './types'; +import { TokenOrProvider, UserResponse } from './types'; /** * TokenManager diff --git a/src/types.ts b/src/types.ts index b2b2317d17..5da033005a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1253,6 +1253,7 @@ export type Event = CustomEventType & { message_id?: string; mode?: string; online?: boolean; + own_capabilities?: string[]; parent_id?: string; poll?: PollResponse; poll_vote?: PollVote | PollAnswer; @@ -1262,6 +1263,7 @@ export type Event = CustomEventType & { }; reaction?: ReactionResponse; received_at?: string | Date; + shadow?: boolean; team?: string; thread?: ThreadResponse; // @deprecated number of all unread messages across all current user's unread channels, equals unread_count @@ -2619,8 +2621,7 @@ export type ReservedMessageFields = | 'reply_count' | 'type' | 'updated_at' - | 'user' - | '__html'; + | 'user'; export type UpdatedMessage = Omit & { mentioned_users?: string[] }; diff --git a/src/utils.ts b/src/utils.ts index 87ef37ba16..223bb4dae3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -721,9 +721,7 @@ const messagePaginationLinear = ({ return newPagination; }; -export const messageSetPagination = ( - params: MessagePaginationUpdatedParams, -) => { +export const messageSetPagination = (params: MessagePaginationUpdatedParams) => { if (params.parentSet.messages.length < params.returnedPage.length) { params.logger?.('error', 'Corrupted message set state: parent set size < returned page size'); return params.parentSet.pagination;