(
thread: SerializedThread,
{
threadId,
messageId,
currentUser,
type,
active,
}: {
threadId: string;
messageId: string;
currentUser: SerializedUser | null;
type: string;
active: boolean;
}
)
| 6 | } from '@linen/types'; |
| 7 | |
| 8 | export function addReactionToThread( |
| 9 | thread: SerializedThread, |
| 10 | { |
| 11 | threadId, |
| 12 | messageId, |
| 13 | currentUser, |
| 14 | type, |
| 15 | active, |
| 16 | }: { |
| 17 | threadId: string; |
| 18 | messageId: string; |
| 19 | currentUser: SerializedUser | null; |
| 20 | type: string; |
| 21 | active: boolean; |
| 22 | } |
| 23 | ): SerializedThread { |
| 24 | if (!thread || !currentUser) { |
| 25 | return thread; |
| 26 | } |
| 27 | if (thread.id === threadId) { |
| 28 | return { |
| 29 | ...thread, |
| 30 | messages: thread.messages.map((message) => { |
| 31 | if (message.id === messageId) { |
| 32 | const reaction = message.reactions.find( |
| 33 | (reaction) => reaction.type === type |
| 34 | ); |
| 35 | |
| 36 | if (!reaction) { |
| 37 | return { |
| 38 | ...message, |
| 39 | reactions: [ |
| 40 | ...(message.reactions |
| 41 | .map((reaction) => { |
| 42 | if ( |
| 43 | (type === ':thumbsup:' && |
| 44 | reaction.type === ':thumbsdown:') || |
| 45 | (type === ':thumbsdown:' && |
| 46 | reaction.type === ':thumbsup:') |
| 47 | ) { |
| 48 | const userIds = reaction.users.map(({ id }) => id); |
| 49 | if (userIds.includes(currentUser.id)) { |
| 50 | if (reaction.count === 1) { |
| 51 | return null; |
| 52 | } |
| 53 | return { |
| 54 | ...reaction, |
| 55 | count: reaction.count - 1, |
| 56 | users: reaction.users.filter( |
| 57 | ({ id }) => id !== currentUser.id |
| 58 | ), |
| 59 | }; |
| 60 | } |
| 61 | } |
| 62 | return reaction; |
| 63 | }) |
| 64 | .filter(Boolean) as SerializedReaction[]), |
| 65 | { type, count: 1, users: [currentUser] }, |
no test coverage detected