stream-chat
Advanced tools
Comparing version 8.15.0 to 8.16.0
@@ -312,3 +312,5 @@ /// <reference types="node" /> | ||
*/ | ||
keystroke(parent_id?: string): Promise<void>; | ||
keystroke(parent_id?: string, options?: { | ||
user_id: string; | ||
}): Promise<void>; | ||
/** | ||
@@ -319,3 +321,5 @@ * stopTyping - Sets last typing to null and sends the typing.stop event | ||
*/ | ||
stopTyping(parent_id?: string): Promise<void>; | ||
stopTyping(parent_id?: string, options?: { | ||
user_id: string; | ||
}): Promise<void>; | ||
/** | ||
@@ -322,0 +326,0 @@ * lastMessage - return the last message, takes into account that last few messages might not be perfectly sorted |
@@ -31,2 +31,3 @@ export declare const EVENT_MAP: { | ||
'notification.removed_from_channel': boolean; | ||
'notification.thread_message_new': boolean; | ||
'reaction.deleted': boolean; | ||
@@ -33,0 +34,0 @@ 'reaction.new': boolean; |
@@ -6,2 +6,3 @@ export * from './base64'; | ||
export * from './channel_state'; | ||
export * from './thread'; | ||
export * from './connection'; | ||
@@ -14,3 +15,3 @@ export * from './events'; | ||
export * from './types'; | ||
export { isOwnUser, chatCodes, logChatPromiseExecution } from './utils'; | ||
export { isOwnUser, chatCodes, logChatPromiseExecution, formatMessage } from './utils'; | ||
//# sourceMappingURL=index.d.ts.map |
/// <reference types="node" /> | ||
import FormData from 'form-data'; | ||
import { AscDesc, ExtendableGenerics, DefaultGenerics, OwnUserResponse, UserResponse } from './types'; | ||
import { AscDesc, ExtendableGenerics, DefaultGenerics, OwnUserResponse, UserResponse, MessageResponse, FormatMessageResponse } from './types'; | ||
import { AxiosRequestConfig } from 'axios'; | ||
@@ -47,2 +47,11 @@ /** | ||
export declare const axiosParamsSerializer: AxiosRequestConfig['paramsSerializer']; | ||
/** | ||
* formatMessage - Takes the message object. Parses the dates, sets __html | ||
* and sets the status to received if missing. Returns a message object | ||
* | ||
* @param {MessageResponse<StreamChatGenerics>} message a message object | ||
* | ||
*/ | ||
export declare function formatMessage<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics>(message: MessageResponse<StreamChatGenerics>): FormatMessageResponse<StreamChatGenerics>; | ||
export declare function addToMessageList<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics>(messages: Array<FormatMessageResponse<StreamChatGenerics>>, message: FormatMessageResponse<StreamChatGenerics>, timestampChanged?: boolean, sortBy?: 'pinned_at' | 'created_at', addIfDoesNotExist?: boolean): FormatMessageResponse<StreamChatGenerics>[]; | ||
//# sourceMappingURL=utils.d.ts.map |
{ | ||
"name": "stream-chat", | ||
"version": "8.15.0", | ||
"version": "8.16.0", | ||
"description": "JS SDK for the Stream Chat API", | ||
@@ -5,0 +5,0 @@ "author": "GetStream", |
@@ -15,2 +15,3 @@ import { Channel } from './channel'; | ||
} from './types'; | ||
import { addToMessageList } from './utils'; | ||
@@ -445,60 +446,3 @@ type ChannelReadStatus<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = Record< | ||
) { | ||
const addMessageToList = addIfDoesNotExist || timestampChanged; | ||
let messageArr = messages; | ||
// if created_at has changed, message should be filtered and re-inserted in correct order | ||
// slow op but usually this only happens for a message inserted to state before actual response with correct timestamp | ||
if (timestampChanged) { | ||
messageArr = messageArr.filter((msg) => !(msg.id && message.id === msg.id)); | ||
} | ||
// Get array length after filtering | ||
const messageArrayLength = messageArr.length; | ||
// for empty list just concat and return unless it's an update or deletion | ||
if (messageArrayLength === 0 && addMessageToList) { | ||
return messageArr.concat(message); | ||
} else if (messageArrayLength === 0) { | ||
return [...messageArr]; | ||
} | ||
const messageTime = (message[sortBy] as Date).getTime(); | ||
const messageIsNewest = (messageArr[messageArrayLength - 1][sortBy] as Date).getTime() < messageTime; | ||
// if message is newer than last item in the list concat and return unless it's an update or deletion | ||
if (messageIsNewest && addMessageToList) { | ||
return messageArr.concat(message); | ||
} else if (messageIsNewest) { | ||
return [...messageArr]; | ||
} | ||
// find the closest index to push the new message | ||
let left = 0; | ||
let middle = 0; | ||
let right = messageArrayLength - 1; | ||
while (left <= right) { | ||
middle = Math.floor((right + left) / 2); | ||
if ((messageArr[middle][sortBy] as Date).getTime() <= messageTime) left = middle + 1; | ||
else right = middle - 1; | ||
} | ||
// message already exists and not filtered due to timestampChanged, update and return | ||
if (!timestampChanged && message.id) { | ||
if (messageArr[left] && message.id === messageArr[left].id) { | ||
messageArr[left] = message; | ||
return [...messageArr]; | ||
} | ||
if (messageArr[left - 1] && message.id === messageArr[left - 1].id) { | ||
messageArr[left - 1] = message; | ||
return [...messageArr]; | ||
} | ||
} | ||
// Do not add updated or deleted messages to the list if they do not already exist | ||
// or have a timestamp change. | ||
if (addMessageToList) { | ||
messageArr.splice(left, 0, message); | ||
} | ||
return [...messageArr]; | ||
return addToMessageList(messages, message, timestampChanged, sortBy, addIfDoesNotExist); | ||
} | ||
@@ -505,0 +449,0 @@ |
@@ -644,3 +644,3 @@ import { ChannelState } from './channel_state'; | ||
*/ | ||
async keystroke(parent_id?: string) { | ||
async keystroke(parent_id?: string, options?: { user_id: string }) { | ||
if (!this.getConfig()?.typing_events) { | ||
@@ -659,2 +659,3 @@ return; | ||
parent_id, | ||
...(options || {}), | ||
} as Event<StreamChatGenerics>); | ||
@@ -669,3 +670,3 @@ } | ||
*/ | ||
async stopTyping(parent_id?: string) { | ||
async stopTyping(parent_id?: string, options?: { user_id: string }) { | ||
if (!this.getConfig()?.typing_events) { | ||
@@ -679,2 +680,3 @@ return; | ||
parent_id, | ||
...(options || {}), | ||
} as Event<StreamChatGenerics>); | ||
@@ -681,0 +683,0 @@ } |
@@ -31,2 +31,3 @@ export const EVENT_MAP = { | ||
'notification.removed_from_channel': true, | ||
'notification.thread_message_new': true, | ||
'reaction.deleted': true, | ||
@@ -33,0 +34,0 @@ 'reaction.new': true, |
@@ -6,2 +6,3 @@ export * from './base64'; | ||
export * from './channel_state'; | ||
export * from './thread'; | ||
export * from './connection'; | ||
@@ -14,2 +15,2 @@ export * from './events'; | ||
export * from './types'; | ||
export { isOwnUser, chatCodes, logChatPromiseExecution } from './utils'; | ||
export { isOwnUser, chatCodes, logChatPromiseExecution, formatMessage } from './utils'; |
102
src/utils.ts
import FormData from 'form-data'; | ||
import { AscDesc, ExtendableGenerics, DefaultGenerics, OwnUserBase, OwnUserResponse, UserResponse } from './types'; | ||
import { | ||
AscDesc, | ||
ExtendableGenerics, | ||
DefaultGenerics, | ||
OwnUserBase, | ||
OwnUserResponse, | ||
UserResponse, | ||
MessageResponse, | ||
FormatMessageResponse, | ||
} from './types'; | ||
import { AxiosRequestConfig } from 'axios'; | ||
@@ -266,1 +275,92 @@ | ||
}; | ||
/** | ||
* formatMessage - Takes the message object. Parses the dates, sets __html | ||
* and sets the status to received if missing. Returns a message object | ||
* | ||
* @param {MessageResponse<StreamChatGenerics>} message a message object | ||
* | ||
*/ | ||
export function formatMessage<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics>( | ||
message: MessageResponse<StreamChatGenerics>, | ||
): FormatMessageResponse<StreamChatGenerics> { | ||
return { | ||
...message, | ||
/** | ||
* @deprecated please use `html` | ||
*/ | ||
__html: message.html, | ||
// parse the date.. | ||
pinned_at: message.pinned_at ? new Date(message.pinned_at) : null, | ||
created_at: message.created_at ? new Date(message.created_at) : new Date(), | ||
updated_at: message.updated_at ? new Date(message.updated_at) : new Date(), | ||
status: message.status || 'received', | ||
}; | ||
} | ||
export function addToMessageList<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics>( | ||
messages: Array<FormatMessageResponse<StreamChatGenerics>>, | ||
message: FormatMessageResponse<StreamChatGenerics>, | ||
timestampChanged = false, | ||
sortBy: 'pinned_at' | 'created_at' = 'created_at', | ||
addIfDoesNotExist = true, | ||
) { | ||
const addMessageToList = addIfDoesNotExist || timestampChanged; | ||
let messageArr = messages; | ||
// if created_at has changed, message should be filtered and re-inserted in correct order | ||
// slow op but usually this only happens for a message inserted to state before actual response with correct timestamp | ||
if (timestampChanged) { | ||
messageArr = messageArr.filter((msg) => !(msg.id && message.id === msg.id)); | ||
} | ||
// Get array length after filtering | ||
const messageArrayLength = messageArr.length; | ||
// for empty list just concat and return unless it's an update or deletion | ||
if (messageArrayLength === 0 && addMessageToList) { | ||
return messageArr.concat(message); | ||
} else if (messageArrayLength === 0) { | ||
return [...messageArr]; | ||
} | ||
const messageTime = (message[sortBy] as Date).getTime(); | ||
const messageIsNewest = (messageArr[messageArrayLength - 1][sortBy] as Date).getTime() < messageTime; | ||
// if message is newer than last item in the list concat and return unless it's an update or deletion | ||
if (messageIsNewest && addMessageToList) { | ||
return messageArr.concat(message); | ||
} else if (messageIsNewest) { | ||
return [...messageArr]; | ||
} | ||
// find the closest index to push the new message | ||
let left = 0; | ||
let middle = 0; | ||
let right = messageArrayLength - 1; | ||
while (left <= right) { | ||
middle = Math.floor((right + left) / 2); | ||
if ((messageArr[middle][sortBy] as Date).getTime() <= messageTime) left = middle + 1; | ||
else right = middle - 1; | ||
} | ||
// message already exists and not filtered due to timestampChanged, update and return | ||
if (!timestampChanged && message.id) { | ||
if (messageArr[left] && message.id === messageArr[left].id) { | ||
messageArr[left] = message; | ||
return [...messageArr]; | ||
} | ||
if (messageArr[left - 1] && message.id === messageArr[left - 1].id) { | ||
messageArr[left - 1] = message; | ||
return [...messageArr]; | ||
} | ||
} | ||
// Do not add updated or deleted messages to the list if they do not already exist | ||
// or have a timestamp change. | ||
if (addMessageToList) { | ||
messageArr.splice(left, 0, message); | ||
} | ||
return [...messageArr]; | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5366796
64
56025