Skip to content

Commit

Permalink
[lib] Check the store when searching for message edits
Browse files Browse the repository at this point in the history
Summary:
It can happen that when creating a thick sidebar, we don't yet have the source message persisted in the DB. In that case, we can try to find the message in Redux store.

https://linear.app/comm/issue/ENG-9034/creating-a-thick-sidebar-just-after-sending-a-message-doesnt-work

Depends on D13059

Test Plan:
Checked if creating a sidebar immediately after sending a message works (before this diff the sidebar isn't created and an exception is thrown). Also checked if creating a message -> editing -> creating a sidebar works (with 10 edits).

```
const tid = uuid.v4();
await this.props.processDMOps({
  type: 'create_thread',
  threadID: tid,
  creatorID: viewerID,
  time: Date.now(),
  threadType: threadTypes.LOCAL,
  memberIDs: [],
  roleID: uuid.v4(),
  newMessageID: uuid.v4(),
});
const mid = uuid.v4();
await this.props.processDMOps({
  type: 'send_text_message',
  threadID: tid,
  creatorID: viewerID,
  time: Date.now(),
  messageID: mid,
  text: `${text}-parent`,
});
for (let i = 0; i < 10; i++) {
  await this.props.processDMOps({
    type: 'send_edit_message',
    threadID: tid,
    creatorID: viewerID,
    time: Date.now(),
    messageID: uuid.v4(),
    targetMessageID: mid,
    text: `${text}-${i}`,
  });
}
const sid = uuid.v4();
await this.props.processDMOps({
  type: 'create_sidebar',
  threadID: sid,
  creatorID: viewerID,
  time: Date.now(),
  parentThreadID: tid,
  memberIDs: [],
  sourceMessageID: mid,
  roleID: uuid.v4(),
  newSidebarSourceMessageID: uuid.v4(),
  newCreateSidebarMessageID: uuid.v4(),
});
await this.props.processDMOps({
  type: 'send_text_message',
  threadID: sid,
  creatorID: viewerID,
  time: Date.now(),
  messageID: uuid.v4(),
  text: `${text}-sidebar`,
});
```

Reviewers: kamil, inka

Reviewed By: kamil

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D13122
  • Loading branch information
palys-swm committed Aug 26, 2024
1 parent ec66fa6 commit 508f32d
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions lib/hooks/latest-message-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { messageTypes } from '../types/message-types-enum.js';
import type { RawMessageInfo } from '../types/message-types.js';
import { getConfig } from '../utils/config.js';
import { translateClientDBMessageInfoToRawMessageInfo } from '../utils/message-ops-utils.js';
import { useSelector } from '../utils/redux-utils.js';

function useGetLatestMessageEdit(): (
messageID: string,
) => Promise<?RawMessageInfo> {
const { getRelatedMessages } = getConfig().sqliteAPI;
const messageStore = useSelector(state => state.messageStore);
return React.useCallback(
async (messageID: string) => {
const relatedDBMessages = await getRelatedMessages(messageID);
Expand All @@ -22,7 +24,31 @@ function useGetLatestMessageEdit(): (
// since the results are sorted on time
const originalMessage = relatedMessages.pop();
if (!originalMessage) {
return undefined;
// It is possible that this message isn't yet present in the DB but is
// already present in Redux store
const messageFromRedux = messageStore.messages[messageID];
if (!messageFromRedux) {
return undefined;
}
// Edits are only supported for text messages
if (messageFromRedux.type !== messageTypes.TEXT) {
return messageFromRedux;
}
for (const threadMessageID of messageStore.threads[
messageFromRedux.threadID
].messageIDs) {
const message = messageStore.messages[threadMessageID];
if (
message.type === messageTypes.EDIT_MESSAGE &&
message.targetMessageID === messageID
) {
return {
...messageFromRedux,
text: message.text,
};
}
}
return messageFromRedux;
}

if (originalMessage.id !== messageID) {
Expand Down Expand Up @@ -50,7 +76,7 @@ function useGetLatestMessageEdit(): (

return editedOriginalMessage;
},
[getRelatedMessages],
[getRelatedMessages, messageStore.messages, messageStore.threads],
);
}

Expand Down

0 comments on commit 508f32d

Please sign in to comment.