Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: threads state fixes #1219

Merged
merged 18 commits into from
Feb 23, 2024
Merged
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
24 changes: 18 additions & 6 deletions src/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,31 @@ export class Thread<StreamChatGenerics extends ExtendableGenerics = DefaultGener
this.channel = t.channel;
this._channel = client.channel(t.channel.type, t.channel.id);
this._client = client;
for (const r of t.read) {
this.read[r.user.id] = {
...r,
last_read: new Date(r.last_read),
};
if (t.read) {
for (const r of t.read) {
this.read[r.user.id] = {
...r,
last_read: new Date(r.last_read),
};
}
}
}

getClient(): StreamChat<StreamChatGenerics> {
return this._client;
}

/**
* addReply - Adds or updates a latestReplies to the thread
*
* @param {MessageResponse<StreamChatGenerics>} message reply message to be added.
*/
addReply(message: MessageResponse<StreamChatGenerics>) {
this.latestReplies = addToMessageList(this.latestReplies, formatMessage(message));
if (message.parent_id !== this.message.id) {
throw new Error('Message does not belong to this thread');
}

this.latestReplies = addToMessageList(this.latestReplies, formatMessage(message), true);
}

updateReply(message: MessageResponse<StreamChatGenerics>) {
Expand All @@ -77,6 +88,7 @@ export class Thread<StreamChatGenerics extends ExtendableGenerics = DefaultGener

if (message.parent_id && message.parent_id === this.message.id) {
this.updateReply(message);
return;
}

if (!message.parent_id && message.id === this.message.id) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ export type OwnUserBase<StreamChatGenerics extends ExtendableGenerics = DefaultG
total_unread_count: number;
unread_channels: number;
unread_count: number;
unread_threads: number;
invisible?: boolean;
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 @@ -84,6 +84,7 @@ export function isOwnUserBaseProperty(property: string) {
total_unread_count: true,
unread_channels: true,
unread_count: true,
unread_threads: true,
invisible: true,
roles: true,
};
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test-utils/generateMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const generateMsg = (msg = {}) => {
const date = msg.date || new Date().toISOString();
return {
id: uuidv4(),
text: 'x',
text: uuidv4(),
html: '<p>x</p>\n',
type: 'regular',
user: { id: 'id' },
Expand Down
21 changes: 21 additions & 0 deletions test/unit/test-utils/generateThread.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { v4 as uuidv4 } from 'uuid';
import { generateUser } from './generateUser';

export const generateThread = (channel, parent, opts = {}) => {
return {
parent_message_id: parent.id,
parent_message: parent,
channel,
title: 'title',
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
channel_cid: channel.cid,
last_message_at: new Date().toISOString(),
deleted_at: '',
read: [],
reply_count: 0,
latest_replies: [],
thread_participants: [],
...opts,
};
};
70 changes: 70 additions & 0 deletions test/unit/thread.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import chai from 'chai';
import { v4 as uuidv4 } from 'uuid';

import { generateChannel } from './test-utils/generateChannel';
import { generateMember } from './test-utils/generateMember';
import { generateMsg } from './test-utils/generateMessage';
import { generateUser } from './test-utils/generateUser';
import { getClientWithUser } from './test-utils/getClient';
import { getOrCreateChannelApi } from './test-utils/getOrCreateChannelApi';
import sinon from 'sinon';
import { mockChannelQueryResponse } from './test-utils/mockChannelQueryResponse';

import { ChannelState, StreamChat, Thread } from '../../src';
import { generateThread } from './test-utils/generateThread';

const expect = chai.expect;

describe.only('Thread', () => {
describe('addReply', async () => {
let client;
let channel;
let parent;
let thread;

beforeEach(() => {
client = new StreamChat('apiKey');
client.userID = 'observer';
channel = generateChannel().channel;
parent = generateMsg();
thread = new Thread(client, generateThread(channel, parent));
});
it('should throw error if the message is not a reply to the parent', async () => {
const reply = generateMsg({
status: 'pending',
parent_id: 'some_other_id',
});
expect(() => thread.addReply(reply)).to.throw('Message does not belong to this thread');
});

it('should add reply to the thread', async () => {
const reply1 = generateMsg({
status: 'pending',
parent_id: parent.id,
});

thread.addReply(reply1);
expect(thread.latestReplies).to.have.length(1);
expect(thread.latestReplies[0].status).to.equal('pending');

reply1.status = 'received';
thread.addReply(reply1);
expect(thread.latestReplies).to.have.length(1);
expect(thread.latestReplies[0].status).to.equal('received');

const reply2 = generateMsg({
status: 'pending',
parent_id: parent.id,
});

thread.addReply(reply2);
expect(thread.latestReplies).to.have.length(2);
expect(thread.latestReplies[1].status).to.equal('pending');

reply2.status = 'received';
thread.addReply(reply2);
expect(thread.latestReplies).to.have.length(2);
expect(thread.latestReplies[1].status).to.equal('received');
});
});
});
Loading