Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 5 additions & 33 deletions packages/api/src/EmbeddedChatApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,34 +550,16 @@ export default class EmbeddedChatApi {

/**
* @param {boolean} anonymousMode
* @param {Object} options This object should include query or fields.
* query - json object which accepts MongoDB query operators.
* fields - json object with properties that have either 1 or 0 to include them or exclude them
* @param {boolean} isChannelPrivate
* @returns messages
*/
async getMessages(
anonymousMode = false,
options: {
query?: object | undefined;
field?: object | undefined;
} = {
query: undefined,
field: undefined,
},
isChannelPrivate = false
) {
async getMessages(anonymousMode = false, isChannelPrivate = false) {
const roomType = isChannelPrivate ? "groups" : "channels";
const endp = anonymousMode ? "anonymousread" : "messages";
const query = options?.query
? `&query=${JSON.stringify(options.query)}`
: "";
const field = options?.field
? `&field=${JSON.stringify(options.field)}`
: "";
try {
const { userId, authToken } = (await this.auth.getCurrentUser()) || {};
const messages = await fetch(
`${this.host}/api/v1/${roomType}.${endp}?roomId=${this.rid}${query}${field}`,
`${this.host}/api/v1/${roomType}.${endp}?roomId=${this.rid}`,
{
headers: {
"Content-Type": "application/json",
Expand All @@ -589,36 +571,26 @@ export default class EmbeddedChatApi {
);
return await messages.json();
} catch (err) {
console.log(err);
console.error(err);
}
}

async getOlderMessages(
anonymousMode = false,
options: {
query?: object | undefined;
field?: object | undefined;
offset?: number;
} = {
query: undefined,
field: undefined,
offset: 50,
},
isChannelPrivate = false
) {
const roomType = isChannelPrivate ? "groups" : "channels";
const endp = anonymousMode ? "anonymousread" : "messages";
const query = options?.query
? `&query=${JSON.stringify(options.query)}`
: "";
const field = options?.field
? `&field=${JSON.stringify(options.field)}`
: "";
const offset = options?.offset ? options.offset : 0;
try {
const { userId, authToken } = (await this.auth.getCurrentUser()) || {};
const messages = await fetch(
`${this.host}/api/v1/${roomType}.${endp}?roomId=${this.rid}${query}${field}&offset=${offset}`,
`${this.host}/api/v1/${roomType}.${endp}?roomId=${this.rid}&offset=${offset}`,
{
headers: {
"Content-Type": "application/json",
Expand Down
18 changes: 8 additions & 10 deletions packages/react/src/hooks/useFetchChatData.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,18 @@ const useFetchChatData = (showRoles) => {
return;
}

const { messages, count } = await RCInstance.getMessages(
const result = await RCInstance.getMessages(
anonymousMode,
ECOptions?.enableThreads
? {
query: {
tmid: {
$exists: false,
},
},
}
: undefined,
anonymousMode ? false : isChannelPrivate
);

let { messages } = result || {};
const { count } = result || {};

if (messages && ECOptions?.enableThreads) {
messages = messages.filter((msg) => !msg.tmid);
}

if (messages) {
setMessages(messages.filter((message) => message._hidden !== true));
setMessagesOffset(count);
Expand Down
17 changes: 7 additions & 10 deletions packages/react/src/views/ChatBody/ChatBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,15 @@ const ChatBody = ({
try {
const olderMessages = await RCInstance.getOlderMessages(
anonymousMode,
ECOptions?.enableThreads
? {
query: {
tmid: {
$exists: false,
},
},
offset,
}
: undefined,
{ offset },
anonymousMode ? false : isChannelPrivate
);

if (ECOptions?.enableThreads && olderMessages?.messages) {
olderMessages.messages = olderMessages.messages.filter(
(msg) => !msg.tmid
);
}
const messageList = messageListRef.current;
if (olderMessages?.messages?.length) {
const previousScrollHeight = messageList.scrollHeight;
Expand Down