Skip to content

Commit 68bbf36

Browse files
Fix findStoryMessage for in-memory messages
Co-authored-by: Fedor Indutny <[email protected]>
1 parent ba8e588 commit 68bbf36

File tree

2 files changed

+68
-44
lines changed

2 files changed

+68
-44
lines changed

ts/messages/handleDataMessage.preload.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { drop } from '../util/drop.std.js';
4949
import { strictAssert } from '../util/assert.std.js';
5050
import { isAciString } from '../util/isAciString.std.js';
5151
import { copyFromQuotedMessage } from './copyQuote.preload.js';
52-
import { findStoryMessages } from '../util/findStoryMessage.preload.js';
52+
import { findStoryMessage } from '../util/findStoryMessage.preload.js';
5353
import { getRoomIdFromCallLink } from '../util/callLinksRingrtc.node.js';
5454
import { isNotNil } from '../util/isNotNil.std.js';
5555
import { normalizeServiceId } from '../types/ServiceId.std.js';
@@ -421,39 +421,17 @@ export async function handleDataMessage(
421421
});
422422
}
423423

424-
const [quote, storyQuotes] = await Promise.all([
424+
const [quote, storyQuote] = await Promise.all([
425425
initialMessage.quote
426426
? copyFromQuotedMessage(initialMessage.quote, conversation.id)
427427
: undefined,
428-
findStoryMessages(conversation.id, storyContext),
428+
findStoryMessage({
429+
conversation: conversation.attributes,
430+
senderId: sender.id,
431+
storyContext,
432+
}),
429433
]);
430434

431-
const storyQuote = storyQuotes.find(candidateQuote => {
432-
const sendStateByConversationId =
433-
candidateQuote.sendStateByConversationId || {};
434-
const sendState = sendStateByConversationId[sender.id];
435-
436-
const storyQuoteIsFromSelf =
437-
candidateQuote.sourceServiceId === itemStorage.user.getCheckedAci();
438-
439-
if (!storyQuoteIsFromSelf) {
440-
return true;
441-
}
442-
443-
// The sender is not a recipient for this story
444-
if (sendState === undefined) {
445-
return false;
446-
}
447-
448-
// Group replies are always allowed
449-
if (!isDirectConversation(conversation.attributes)) {
450-
return true;
451-
}
452-
453-
// For 1:1 stories, we need to check if they can be replied to
454-
return sendState.isAllowedToReplyToStory !== false;
455-
});
456-
457435
if (
458436
storyContext &&
459437
!storyQuote &&

ts/util/findStoryMessage.preload.ts

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,94 @@
33

44
import type {
55
ReadonlyMessageAttributesType,
6-
MessageAttributesType,
6+
ConversationAttributesType,
77
} from '../model-types.d.ts';
88
import { type AciString } from '../types/ServiceId.std.js';
99
import type { ProcessedStoryContext } from '../textsecure/Types.d.ts';
10-
import { DataReader } from '../sql/Client.preload.js';
1110
import { createLogger } from '../logging/log.std.js';
1211
import { getAuthorId } from '../messages/sources.preload.js';
12+
import { itemStorage } from '../textsecure/Storage.preload.js';
13+
import { isDirectConversation } from './whatTypeOfConversation.dom.js';
1314

1415
const log = createLogger('findStoryMessage');
1516

16-
export async function findStoryMessages(
17-
conversationId: string,
18-
storyContext?: ProcessedStoryContext
19-
): Promise<Array<MessageAttributesType>> {
17+
export type FindStoryMessageOptionsType = Readonly<{
18+
conversation: ConversationAttributesType;
19+
senderId: string;
20+
storyContext?: ProcessedStoryContext;
21+
}>;
22+
23+
export async function findStoryMessage({
24+
conversation,
25+
senderId,
26+
storyContext,
27+
}: FindStoryMessageOptionsType): Promise<
28+
ReadonlyMessageAttributesType | undefined
29+
> {
2030
if (!storyContext) {
21-
return [];
31+
return undefined;
2232
}
2333

2434
const { authorAci, sentTimestamp: sentAt } = storyContext;
2535

2636
if (!sentAt) {
27-
return [];
37+
return undefined;
2838
}
2939

3040
if (authorAci == null) {
31-
return [];
41+
return undefined;
3242
}
3343

3444
const ourConversationId =
3545
window.ConversationController.getOurConversationIdOrThrow();
46+
const ourAci = itemStorage.user.getCheckedAci();
47+
48+
const found = await window.MessageCache.findBySentAt(
49+
sentAt,
50+
({ attributes: candidate }) => {
51+
if (
52+
!isStoryAMatch(
53+
candidate,
54+
conversation.id,
55+
ourConversationId,
56+
authorAci,
57+
sentAt
58+
)
59+
) {
60+
return false;
61+
}
62+
63+
const sendStateByConversationId =
64+
candidate.sendStateByConversationId || {};
65+
const sendState = sendStateByConversationId[senderId];
66+
67+
const storyQuoteIsFromSelf = candidate.sourceServiceId === ourAci;
68+
69+
if (!storyQuoteIsFromSelf) {
70+
return true;
71+
}
72+
73+
// The sender is not a recipient for this story
74+
if (sendState === undefined) {
75+
return false;
76+
}
77+
78+
// Group replies are always allowed
79+
if (!isDirectConversation(conversation)) {
80+
return true;
81+
}
3682

37-
const messages = await DataReader.getMessagesBySentAt(sentAt);
38-
const found = messages.filter(item =>
39-
isStoryAMatch(item, conversationId, ourConversationId, authorAci, sentAt)
83+
// For 1:1 stories, we need to check if they can be replied to
84+
return sendState.isAllowedToReplyToStory !== false;
85+
}
4086
);
4187

42-
if (found.length === 0) {
88+
if (found == null) {
4389
log.info('findStoryMessages: message not found', sentAt);
44-
return [];
90+
return undefined;
4591
}
4692

47-
return found;
93+
return found.attributes;
4894
}
4995

5096
function isStoryAMatch(

0 commit comments

Comments
 (0)