/
Reactions.ts
221 lines (188 loc) · 6.63 KB
/
Reactions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2020-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* eslint-disable max-classes-per-file */
import { Collection, Model } from 'backbone';
import type { ConversationModel } from '../models/conversations';
import type { MessageModel } from '../models/messages';
import type {
MessageAttributesType,
ReactionAttributesType,
} from '../model-types.d';
import * as log from '../logging/log';
import { getContactId, getContact } from '../messages/helpers';
import { isDirectConversation, isMe } from '../util/whatTypeOfConversation';
import { isOutgoing, isStory } from '../state/selectors/message';
import { getMessageIdForLogging } from '../util/idForLogging';
export class ReactionModel extends Model<ReactionAttributesType> {}
let singleton: Reactions | undefined;
export class Reactions extends Collection<ReactionModel> {
static getSingleton(): Reactions {
if (!singleton) {
singleton = new Reactions();
}
return singleton;
}
forMessage(message: MessageModel): Array<ReactionModel> {
if (isOutgoing(message.attributes)) {
const outgoingReactions = this.filter(
item => item.get('targetTimestamp') === message.get('sent_at')
);
if (outgoingReactions.length > 0) {
log.info('Found early reaction for outgoing message');
this.remove(outgoingReactions);
return outgoingReactions;
}
}
const senderId = getContactId(message.attributes);
const sentAt = message.get('sent_at');
const reactionsBySource = this.filter(re => {
const targetSender = window.ConversationController.lookupOrCreate({
uuid: re.get('targetAuthorUuid'),
});
const targetTimestamp = re.get('targetTimestamp');
return targetSender?.id === senderId && targetTimestamp === sentAt;
});
if (reactionsBySource.length > 0) {
log.info('Found early reaction for message');
this.remove(reactionsBySource);
return reactionsBySource;
}
return [];
}
private async findMessage(
targetTimestamp: number,
targetConversationId: string
): Promise<MessageAttributesType | undefined> {
const messages = await window.Signal.Data.getMessagesBySentAt(
targetTimestamp
);
return messages.find(m => {
const contact = getContact(m);
if (!contact) {
return false;
}
const mcid = contact.get('id');
return mcid === targetConversationId;
});
}
async onReaction(
reaction: ReactionModel,
generatedMessage: MessageModel
): Promise<void> {
try {
// The conversation the target message was in; we have to find it in the database
// to to figure that out.
const targetAuthorConversation =
window.ConversationController.lookupOrCreate({
uuid: reaction.get('targetAuthorUuid'),
});
const targetConversationId = targetAuthorConversation?.id;
if (!targetConversationId) {
throw new Error(
'onReaction: No conversationId returned from lookupOrCreate!'
);
}
const fromConversation = window.ConversationController.get(
generatedMessage.get('conversationId')
);
let targetConversation: ConversationModel | undefined | null;
const targetMessageCheck = await this.findMessage(
reaction.get('targetTimestamp'),
targetConversationId
);
if (!targetMessageCheck) {
log.info(
'No message for reaction',
reaction.get('targetAuthorUuid'),
reaction.get('targetTimestamp')
);
return;
}
if (
fromConversation &&
isStory(targetMessageCheck) &&
isDirectConversation(fromConversation.attributes) &&
!isMe(fromConversation.attributes)
) {
targetConversation = fromConversation;
} else {
targetConversation =
await window.ConversationController.getConversationForTargetMessage(
targetConversationId,
reaction.get('targetTimestamp')
);
}
if (!targetConversation) {
log.info(
'No target conversation for reaction',
reaction.get('targetAuthorUuid'),
reaction.get('targetTimestamp')
);
return undefined;
}
// awaiting is safe since `onReaction` is never called from inside the queue
await targetConversation.queueJob('Reactions.onReaction', async () => {
log.info('Handling reaction for', reaction.get('targetTimestamp'));
// Thanks TS.
if (!targetConversation) {
return;
}
// Message is fetched inside the conversation queue so we have the
// most recent data
const targetMessage = await this.findMessage(
reaction.get('targetTimestamp'),
targetConversationId
);
if (!targetMessage) {
return;
}
const message = window.MessageController.register(
targetMessage.id,
targetMessage
);
// Use the generated message in ts/background.ts to create a message
// if the reaction is targetted at a story.
if (isStory(targetMessage)) {
generatedMessage.set({
storyId: targetMessage.id,
storyReaction: {
emoji: reaction.get('emoji'),
targetAuthorUuid: reaction.get('targetAuthorUuid'),
targetTimestamp: reaction.get('targetTimestamp'),
},
});
// Note: generatedMessage comes with an id, so we have to force this save
await Promise.all([
window.Signal.Data.saveMessage(generatedMessage.attributes, {
ourUuid: window.textsecure.storage.user
.getCheckedUuid()
.toString(),
forceSave: true,
}),
generatedMessage.hydrateStoryContext(message),
]);
log.info('Reactions.onReaction adding reaction to story', {
reactionMessageId: getMessageIdForLogging(
generatedMessage.attributes
),
storyId: getMessageIdForLogging(targetMessage),
targetTimestamp: reaction.get('targetTimestamp'),
timestamp: reaction.get('timestamp'),
});
const messageToAdd = window.MessageController.register(
generatedMessage.id,
generatedMessage
);
targetConversation.addSingleMessage(messageToAdd);
}
await message.handleReaction(reaction);
this.remove(reaction);
});
} catch (error) {
log.error(
'Reactions.onReaction error:',
error && error.stack ? error.stack : error
);
}
}
}