diff --git a/lib/shared/dm-ops/join-thread-spec.js b/lib/shared/dm-ops/join-thread-spec.js --- a/lib/shared/dm-ops/join-thread-spec.js +++ b/lib/shared/dm-ops/join-thread-spec.js @@ -50,22 +50,56 @@ const currentThreadInfo = utilities.threadInfos[existingThreadDetails.threadID]; + if (currentThreadInfo && !currentThreadInfo.thick) { + return { + rawMessageInfos: [], + updateInfos: [], + }; + } const messageData = createMessageDataFromDMOperation(dmOperation); const joinThreadMessageInfos = [ rawMessageInfoFromMessageData(messageData, messageID), ]; - if (userIsMember(currentThreadInfo, joinerID)) { + const memberTimestamps = { ...currentThreadInfo?.timestamps?.members }; + if (!memberTimestamps[joinerID]) { + memberTimestamps[joinerID] = { + role: time, + subscription: existingThreadDetails.creationTime, + }; + } + + if (memberTimestamps[joinerID].role > time) { return { rawMessageInfos: joinThreadMessageInfos, updateInfos: [], }; } + memberTimestamps[joinerID] = { + ...memberTimestamps[joinerID], + role: time, + }; + const updateInfos: Array = []; const rawMessageInfos: Array = []; - if (viewerID === joinerID) { + + if (userIsMember(currentThreadInfo, joinerID)) { + rawMessageInfos.push(...joinThreadMessageInfos); + updateInfos.push({ + type: updateTypes.UPDATE_THREAD, + id: uuid.v4(), + time, + threadInfo: { + ...currentThreadInfo, + timestamps: { + ...currentThreadInfo.timestamps, + members: memberTimestamps, + }, + }, + }); + } else if (viewerID === joinerID) { const newThreadInfo = createThickRawThreadInfo( { ...existingThreadDetails, @@ -80,14 +114,18 @@ type: updateTypes.JOIN_THREAD, id: uuid.v4(), time, - threadInfo: newThreadInfo, + threadInfo: { + ...newThreadInfo, + timestamps: { + ...newThreadInfo.timestamps, + members: memberTimestamps, + }, + }, rawMessageInfos: joinThreadMessageInfos, truncationStatus: messageTruncationStatus.EXHAUSTIVE, rawEntryInfos: [], }); } else { - invariant(currentThreadInfo.thick, 'Thread should be thick'); - rawMessageInfos.push(...joinThreadMessageInfos); const defaultRoleID = values(currentThreadInfo.roles).find(role => roleIsDefaultRole(role), @@ -109,6 +147,10 @@ const updatedThreadInfo = { ...currentThreadInfo, members: [...currentThreadInfo.members, member], + timestamps: { + ...currentThreadInfo.timestamps, + members: memberTimestamps, + }, }; updateInfos.push({ type: updateTypes.UPDATE_THREAD, @@ -127,7 +169,10 @@ viewerID: string, utilities: ProcessDMOperationUtilities, ) { - if (utilities.threadInfos[dmOperation.existingThreadDetails.threadID]) { + if ( + utilities.threadInfos[dmOperation.existingThreadDetails.threadID] || + dmOperation.joinerID === viewerID + ) { return { isProcessingPossible: true }; } return {