Skip to content

Commit 504f60c

Browse files
committed
fix: resolved missing reactions in open groups, fixed mutation cache logic
1 parent dd37133 commit 504f60c

File tree

9 files changed

+45
-51
lines changed

9 files changed

+45
-51
lines changed

ts/data/data.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,11 @@ async function getMessageBySenderAndSentAt({
446446
}
447447

448448
async function getMessageByServerId(
449+
conversationId: string,
449450
serverId: number,
450451
skipTimerInit: boolean = false
451452
): Promise<MessageModel | null> {
452-
const message = await channels.getMessageByServerId(serverId);
453+
const message = await channels.getMessageByServerId(conversationId, serverId);
453454
if (!message) {
454455
return null;
455456
}

ts/models/conversation.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,6 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
735735
reaction,
736736
sender: UserUtils.getOurPubKeyStrFromCache(),
737737
you: true,
738-
isOpenGroup: false,
739738
});
740739
return;
741740
}
@@ -752,7 +751,6 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
752751
reaction,
753752
sender: UserUtils.getOurPubKeyStrFromCache(),
754753
you: true,
755-
isOpenGroup: false,
756754
});
757755
return;
758756
}

ts/node/sql.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,10 +1040,14 @@ function getMessageBySenderAndSentAt({ source, sentAt }: { source: string; sentA
10401040
return map(rows, row => jsonToObject(row.json));
10411041
}
10421042

1043-
function getMessageByServerId(serverId: number) {
1043+
// serverIds are not unique so we need the conversationId
1044+
function getMessageByServerId(conversationId: string, serverId: number) {
10441045
const row = assertGlobalInstance()
1045-
.prepare(`SELECT * FROM ${MESSAGES_TABLE} WHERE serverId = $serverId;`)
1046+
.prepare(
1047+
`SELECT * FROM ${MESSAGES_TABLE} WHERE conversationId = $conversationId AND serverId = $serverId;`
1048+
)
10461049
.get({
1050+
conversationId,
10471051
serverId,
10481052
});
10491053

ts/receiver/dataMessage.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ async function handleSwarmMessage(
309309
reaction: rawDataMessage.reaction,
310310
sender: msgModel.get('source'),
311311
you: isUsFromCache(msgModel.get('source')),
312-
isOpenGroup: false,
313312
});
314313
if (
315314
convoToAddMessageTo.isPrivate() &&

ts/session/apis/open_group_api/sogsv3/sogsV3ClearReaction.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import AbortController from 'abort-controller';
22
import { OpenGroupReactionResponse } from '../../../../types/Reaction';
33
import { Reactions } from '../../../../util/reactions';
44
import { OpenGroupRequestCommonType } from '../opengroupV2/ApiUtil';
5+
import { getOpenGroupV2ConversationId } from '../utils/OpenGroupUtils';
56
import {
67
batchFirstSubIsSuccess,
78
batchGlobalIsSuccess,
@@ -26,7 +27,8 @@ export const clearSogsReactionByServerId = async (
2627
serverId: number,
2728
roomInfos: OpenGroupRequestCommonType
2829
): Promise<boolean> => {
29-
const { supported, conversation } = await hasReactionSupport(serverId);
30+
const converationId = getOpenGroupV2ConversationId(roomInfos.serverUrl, roomInfos.roomId);
31+
const { supported, conversation } = await hasReactionSupport(converationId, serverId);
3032
if (!supported) {
3133
return false;
3234
}
@@ -51,7 +53,7 @@ export const clearSogsReactionByServerId = async (
5153
addToMutationCache(cacheEntry);
5254

5355
// Since responses can take a long time we immediately update the moderators's UI and if there is a problem it is overwritten by handleOpenGroupMessageReactions later.
54-
await Reactions.handleClearReaction(serverId, reaction);
56+
await Reactions.handleClearReaction(converationId, serverId, reaction);
5557

5658
const options: Array<OpenGroupBatchRow> = [
5759
{

ts/session/apis/open_group_api/sogsv3/sogsV3MutationCache.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { filter, findIndex, remove } from 'lodash';
77
import { Reactions } from '../../../../util/reactions';
88
import { OpenGroupReactionMessageV4 } from '../opengroupV2/OpenGroupServerPoller';
9+
import { getOpenGroupV2ConversationId } from '../utils/OpenGroupUtils';
910

1011
export enum ChangeType {
1112
REACTIONS = 0,
@@ -143,6 +144,10 @@ export async function processMessagesUsingCache(
143144
}
144145

145146
message.reactions = updatedReactions;
146-
await Reactions.handleOpenGroupMessageReactions(message.reactions, message.id);
147+
await Reactions.handleOpenGroupMessageReactions(
148+
getOpenGroupV2ConversationId(server, room),
149+
message.id,
150+
message.reactions
151+
);
147152
return message;
148153
}

ts/session/apis/open_group_api/sogsv3/sogsV3SendReaction.ts

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,15 @@ import { Reactions } from '../../../../util/reactions';
1212
import { OnionSending } from '../../../onions/onionSend';
1313
import { ToastUtils, UserUtils } from '../../../utils';
1414
import { OpenGroupPollingUtils } from '../opengroupV2/OpenGroupPollingUtils';
15+
import { getOpenGroupV2ConversationId } from '../utils/OpenGroupUtils';
1516
import { getUsBlindedInThatServer } from './knownBlindedkeys';
1617
import { batchGlobalIsSuccess, parseBatchGlobalStatusCode } from './sogsV3BatchPoll';
17-
import {
18-
addToMutationCache,
19-
ChangeType,
20-
SogsV3Mutation,
21-
updateMutationCache,
22-
} from './sogsV3MutationCache';
2318

2419
export const hasReactionSupport = async (
20+
conversationId: string,
2521
serverId: number
2622
): Promise<{ supported: boolean; conversation: ConversationModel | null }> => {
27-
const found = await Data.getMessageByServerId(serverId);
23+
const found = await Data.getMessageByServerId(conversationId, serverId);
2824
if (!found) {
2925
window.log.warn(`Open Group Message ${serverId} not found in db`);
3026
return { supported: false, conversation: null };
@@ -57,7 +53,10 @@ export const sendSogsReactionOnionV4 = async (
5753
throw new Error(`Could not find sogs pubkey of url:${serverUrl}`);
5854
}
5955

60-
const { supported, conversation } = await hasReactionSupport(reaction.id);
56+
const { supported, conversation } = await hasReactionSupport(
57+
getOpenGroupV2ConversationId(serverUrl, room),
58+
reaction.id
59+
);
6160
if (!supported) {
6261
return false;
6362
}
@@ -82,27 +81,13 @@ export const sendSogsReactionOnionV4 = async (
8281
const method = reaction.action === Action.REACT ? 'PUT' : 'DELETE';
8382
const serverPubkey = allValidRoomInfos[0].serverPublicKey;
8483

85-
const cacheEntry: SogsV3Mutation = {
86-
server: serverUrl,
87-
room: room,
88-
changeType: ChangeType.REACTIONS,
89-
seqno: null,
90-
metadata: {
91-
messageId: reaction.id,
92-
emoji,
93-
action: reaction.action === Action.REACT ? 'ADD' : 'REMOVE',
94-
},
95-
};
96-
97-
addToMutationCache(cacheEntry);
98-
9984
// Since responses can take a long time we immediately update the sender's UI and if there is a problem it is overwritten by handleOpenGroupMessageReactions later.
10085
const me = UserUtils.getOurPubKeyStrFromCache();
10186
await Reactions.handleMessageReaction({
10287
reaction,
10388
sender: blinded ? getUsBlindedInThatServer(conversation) || me : me,
10489
you: true,
105-
isOpenGroup: true,
90+
openGroupConversationId: getOpenGroupV2ConversationId(serverUrl, room),
10691
});
10792

10893
// reaction endpoint requires an empty dict {}
@@ -137,9 +122,5 @@ export const sendSogsReactionOnionV4 = async (
137122

138123
const success = Boolean(reaction.action === Action.REACT ? rawMessage.added : rawMessage.removed);
139124

140-
if (success) {
141-
updateMutationCache(cacheEntry, rawMessage.seqno);
142-
}
143-
144125
return success;
145126
};

ts/test/session/unit/reactions/ReactionMessage_test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ describe('ReactionMessage', () => {
5656
reaction: reaction as SignalService.DataMessage.IReaction,
5757
sender: ourNumber,
5858
you: true,
59-
isOpenGroup: false,
6059
});
6160

6261
expect(updatedMessage?.get('reacts'), 'original message should have reacts').to.not.be
@@ -89,7 +88,6 @@ describe('ReactionMessage', () => {
8988
reaction: reaction as SignalService.DataMessage.IReaction,
9089
sender: ourNumber,
9190
you: true,
92-
isOpenGroup: false,
9391
});
9492

9593
expect(updatedMessage?.get('reacts'), 'original message reacts should be undefined').to.be

ts/util/reactions.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ function hitRateLimit(): boolean {
3838
*/
3939
const getMessageByReaction = async (
4040
reaction: SignalService.DataMessage.IReaction,
41-
isOpenGroup: boolean
41+
openGroupConversationId?: string
4242
): Promise<MessageModel | null> => {
4343
let originalMessage = null;
4444
const originalMessageId = Number(reaction.id);
4545
const originalMessageAuthor = reaction.author;
4646

47-
if (isOpenGroup) {
48-
originalMessage = await Data.getMessageByServerId(originalMessageId);
47+
if (openGroupConversationId && !isEmpty(openGroupConversationId)) {
48+
originalMessage = await Data.getMessageByServerId(openGroupConversationId, originalMessageId);
4949
} else {
5050
const collection = await Data.getMessagesBySentAt(originalMessageId);
5151
originalMessage = collection.find((item: MessageModel) => {
@@ -152,19 +152,19 @@ const handleMessageReaction = async ({
152152
reaction,
153153
sender,
154154
you,
155-
isOpenGroup,
155+
openGroupConversationId,
156156
}: {
157157
reaction: SignalService.DataMessage.IReaction;
158158
sender: string;
159159
you: boolean;
160-
isOpenGroup: boolean;
160+
openGroupConversationId?: string;
161161
}) => {
162162
if (!reaction.emoji) {
163163
window?.log?.warn(`There is no emoji for the reaction ${reaction}.`);
164164
return;
165165
}
166166

167-
const originalMessage = await getMessageByReaction(reaction, isOpenGroup);
167+
const originalMessage = await getMessageByReaction(reaction, openGroupConversationId);
168168
if (!originalMessage) {
169169
return;
170170
}
@@ -240,10 +240,12 @@ const handleMessageReaction = async ({
240240
* Handles updating the UI when clearing all reactions for a certain emoji
241241
* Only usable by moderators in opengroups and runs on their client
242242
*/
243-
const handleClearReaction = async (serverId: number, emoji: string) => {
244-
const originalMessage = await Data.getMessageByServerId(serverId);
243+
const handleClearReaction = async (conversationId: string, serverId: number, emoji: string) => {
244+
const originalMessage = await Data.getMessageByServerId(conversationId, serverId);
245245
if (!originalMessage) {
246-
window?.log?.warn(`Cannot find the original reacted message ${serverId}.`);
246+
window?.log?.warn(
247+
`Cannot find the original reacted message ${serverId} in conversation ${conversationId}.`
248+
);
247249
return;
248250
}
249251

@@ -265,14 +267,18 @@ const handleClearReaction = async (serverId: number, emoji: string) => {
265267

266268
/**
267269
* Handles all message reaction updates/responses for opengroups
270+
* serverIds are not unique so we need the conversationId
268271
*/
269272
const handleOpenGroupMessageReactions = async (
270-
reactions: OpenGroupReactionList,
271-
serverId: number
273+
conversationId: string,
274+
serverId: number,
275+
reactions: OpenGroupReactionList
272276
) => {
273-
const originalMessage = await Data.getMessageByServerId(serverId);
277+
const originalMessage = await Data.getMessageByServerId(conversationId, serverId);
274278
if (!originalMessage) {
275-
window?.log?.warn(`Cannot find the original reacted message ${serverId}.`);
279+
window?.log?.warn(
280+
`Cannot find the original reacted message ${serverId} in conversation ${conversationId}.`
281+
);
276282
return;
277283
}
278284

0 commit comments

Comments
 (0)