Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit 1d268dc

Browse files
authored
Merge pull request #3969 from withspectrum/2.4.38
2.4.38
2 parents 3894b38 + 9e1e2c1 commit 1d268dc

File tree

21 files changed

+358
-201
lines changed

21 files changed

+358
-201
lines changed

api/authentication.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const GITHUB_OAUTH_CLIENT_ID = IS_PROD
4949
const init = () => {
5050
// Setup use serialization
5151
passport.serializeUser((user, done) => {
52-
done(null, JSON.stringify(user));
52+
done(null, typeof user === 'string' ? user : JSON.stringify(user));
5353
});
5454

5555
// NOTE(@mxstbr): `data` used to be just the userID, but is now the full user data

api/loaders/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
import {
1313
__createThreadLoader,
1414
__createThreadParticipantsLoader,
15-
__createThreadMessageCountLoader,
1615
} from './thread';
1716
import { __createNotificationLoader } from './notification';
1817
import {
@@ -59,7 +58,6 @@ const createLoaders = (options?: DataLoaderOptions) => ({
5958
),
6059
thread: __createThreadLoader(options),
6160
threadParticipants: __createThreadParticipantsLoader(options),
62-
threadMessageCount: __createThreadMessageCountLoader(options),
6361
notification: __createNotificationLoader(options),
6462
channel: __createChannelLoader(options),
6563
channelMemberCount: __createChannelMemberCountLoader(options),

api/loaders/thread.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ export const __createThreadParticipantsLoader = createLoader(
1414
'group'
1515
);
1616

17-
export const __createThreadMessageCountLoader = createLoader(
18-
threadIds => getMessageCountInThreads(threadIds),
19-
'group'
20-
);
21-
2217
export default () => {
2318
throw new Error(
2419
'⚠️ Do not import loaders directly, get them from the GraphQL context instead! ⚠️'
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
exports.up = function(r, conn) {
2+
return r
3+
.table('threads')
4+
.update(
5+
{
6+
messageCount: r
7+
.table('messages')
8+
.getAll(r.row('id'), { index: 'threadId' })
9+
.count()
10+
.default(0),
11+
reactionCount: r
12+
.table('threadReactions')
13+
.getAll(r.row('id'), { index: 'threadId' })
14+
.count()
15+
.default(0),
16+
},
17+
{
18+
nonAtomic: true,
19+
}
20+
)
21+
.run(conn)
22+
.then(() => {
23+
return Promise.all([
24+
r
25+
.table('threads')
26+
.indexCreate('messageCount')
27+
.run(conn),
28+
r
29+
.table('threads')
30+
.indexCreate('reactionCount')
31+
.run(conn),
32+
]);
33+
})
34+
.catch(err => console.error(err));
35+
};
36+
37+
exports.down = function(r, conn) {
38+
return r
39+
.table('threads')
40+
.update({
41+
messageCount: r.literal(),
42+
reactionCount: r.literal(),
43+
})
44+
.run(conn)
45+
.then(() => {
46+
return Promise.all([
47+
r
48+
.table('threads')
49+
.indexDrop('messageCount')
50+
.run(conn),
51+
r
52+
.table('threads')
53+
.indexDrop('reactionCount')
54+
.run(conn),
55+
]);
56+
});
57+
};

api/migrations/seed/default/threads.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ module.exports = [
4747
],
4848
modifiedAt: new Date(DATE),
4949
lastActive: new Date(DATE),
50+
messageCount: 4,
51+
reactionCount: 0,
5052
},
5153
{
5254
id: 'thread-2',
@@ -77,6 +79,8 @@ module.exports = [
7779
],
7880
modifiedAt: new Date(DATE + 1),
7981
lastActive: new Date(DATE + 1),
82+
messageCount: 4,
83+
reactionCount: 0,
8084
},
8185
{
8286
id: 'thread-3',
@@ -107,6 +111,8 @@ module.exports = [
107111
],
108112
modifiedAt: new Date(DATE + 2),
109113
lastActive: new Date(DATE + 2),
114+
messageCount: 0,
115+
reactionCount: 0,
110116
},
111117

112118
{
@@ -138,6 +144,8 @@ module.exports = [
138144
],
139145
modifiedAt: new Date(DATE),
140146
lastActive: new Date(DATE),
147+
messageCount: 0,
148+
reactionCount: 0,
141149
},
142150
{
143151
id: 'thread-5',
@@ -168,6 +176,8 @@ module.exports = [
168176
],
169177
modifiedAt: new Date(DATE + 1),
170178
lastActive: new Date(DATE + 1),
179+
messageCount: 0,
180+
reactionCount: 0,
171181
},
172182
{
173183
id: 'thread-6',
@@ -198,6 +208,8 @@ module.exports = [
198208
],
199209
modifiedAt: new Date(DATE + 2),
200210
lastActive: new Date(DATE + 2),
211+
messageCount: 0,
212+
reactionCount: 0,
201213
},
202214
{
203215
id: 'thread-7',
@@ -229,6 +241,8 @@ module.exports = [
229241
modifiedAt: new Date(DATE + 2),
230242
lastActive: new Date(DATE + 2),
231243
deletedAt: new Date(DATE + 3),
244+
messageCount: 0,
245+
reactionCount: 0,
232246
},
233247
{
234248
id: 'thread-8',
@@ -259,6 +273,8 @@ module.exports = [
259273
],
260274
modifiedAt: new Date(DATE + 2),
261275
lastActive: new Date(DATE + 2),
276+
messageCount: 0,
277+
reactionCount: 0,
262278
},
263279
{
264280
id: 'thread-9',
@@ -320,6 +336,8 @@ module.exports = [
320336
],
321337
modifiedAt: new Date(DATE + 2),
322338
lastActive: new Date(DATE + 2),
339+
messageCount: 0,
340+
reactionCount: 0,
323341
},
324342
{
325343
id: 'thread-11',
@@ -349,6 +367,8 @@ module.exports = [
349367
modifiedAt: new Date(DATE + 2),
350368
lastActive: new Date(DATE + 2),
351369
deletedAt: new Date(DATE + 3),
370+
messageCount: 0,
371+
reactionCount: 0,
352372
},
353373

354374
{
@@ -381,6 +401,8 @@ module.exports = [
381401
modifiedAt: new Date(DATE + 2),
382402
lastActive: new Date(DATE + 2),
383403
deletedAt: new Date(DATE),
404+
messageCount: 0,
405+
reactionCount: 0,
384406
},
385407

386408
{
@@ -412,5 +434,7 @@ module.exports = [
412434
],
413435
modifiedAt: new Date(DATE + 2),
414436
lastActive: new Date(DATE + 2),
437+
messageCount: 0,
438+
reactionCount: 0,
415439
},
416440
];

api/models/message.js

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
} from 'shared/bull/queues';
99
import { NEW_DOCUMENTS } from './utils';
1010
import { createChangefeed } from 'shared/changefeed-utils';
11-
import { setThreadLastActive } from './thread';
11+
import {
12+
setThreadLastActive,
13+
incrementMessageCount,
14+
decrementMessageCount,
15+
} from './thread';
1216
import { events } from 'shared/analytics';
1317
import { trackQueue } from 'shared/bull/queues';
1418

@@ -135,35 +139,36 @@ export const storeMessage = (message: Message, userId: string): Promise<Message>
135139
)
136140
.run()
137141
.then(result => result.changes[0].new_val)
138-
.then(message => {
142+
.then(async message => {
139143
if (message.threadType === 'directMessageThread') {
140-
trackQueue.add({
141-
userId,
142-
event: events.DIRECT_MESSAGE_SENT,
143-
context: { messageId: message.id },
144-
});
145-
146-
sendDirectMessageNotificationQueue.add({ message, userId });
144+
await Promise.all([
145+
trackQueue.add({
146+
userId,
147+
event: events.DIRECT_MESSAGE_SENT,
148+
context: { messageId: message.id },
149+
}),
150+
sendDirectMessageNotificationQueue.add({ message, userId }),
151+
])
147152
}
148153

149154
if (message.threadType === 'story') {
150-
sendMessageNotificationQueue.add({ message });
151-
152-
_adminProcessToxicMessageQueue.add({ message });
153-
155+
await Promise.all([
156+
sendMessageNotificationQueue.add({ message }),
154157
processReputationEventQueue.add({
155158
userId,
156159
type: 'message created',
157160
entityId: message.threadId,
158-
});
159-
161+
}),
160162
trackQueue.add({
161163
userId,
162164
event: events.MESSAGE_SENT,
163165
context: { messageId: message.id },
164-
});
166+
}),
167+
_adminProcessToxicMessageQueue.add({ message }),
165168

166-
setThreadLastActive(message.threadId, message.timestamp);
169+
setThreadLastActive(message.threadId, message.timestamp),
170+
incrementMessageCount(message.threadId)
171+
])
167172
}
168173

169174
return message;
@@ -216,23 +221,27 @@ export const deleteMessage = (userId: string, messageId: string) => {
216221
)
217222
.run()
218223
.then(result => result.changes[0].new_val || result.changes[0].old_val)
219-
.then(message => {
224+
.then(async message => {
220225
const event =
221226
message.threadType === 'story'
222227
? events.MESSAGE_DELETED
223228
: events.DIRECT_MESSAGE_DELETED;
224229

225-
trackQueue.add({
226-
userId,
227-
event,
228-
context: { messageId },
229-
});
230-
231-
processReputationEventQueue.add({
232-
userId,
233-
type: 'message deleted',
234-
entityId: messageId,
235-
});
230+
await Promise.all([
231+
trackQueue.add({
232+
userId,
233+
event,
234+
context: { messageId },
235+
}),
236+
processReputationEventQueue.add({
237+
userId,
238+
type: 'message deleted',
239+
entityId: messageId,
240+
}),
241+
message.threadType === 'story'
242+
? decrementMessageCount(message.threadId)
243+
: Promise.resolve(),
244+
]);
236245

237246
return message;
238247
});
@@ -267,7 +276,9 @@ export const deleteMessagesInThread = async (threadId: string, userId: string) =
267276
})
268277
.run();
269278

270-
return await Promise.all([...trackingPromises, deletePromise]);
279+
return await Promise.all([...trackingPromises, deletePromise]).then(() => {
280+
return Promise.all(Array.from({ length: messages.length }).map(() => decrementMessageCount(threadId)))
281+
});
271282
};
272283

273284
export const userHasMessagesInThread = (threadId: string, userId: string) => {

api/models/thread.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,58 @@ export const moveThread = (id: string, channelId: string, userId: string) => {
660660
});
661661
};
662662

663+
export const incrementMessageCount = (threadId: string) => {
664+
return db
665+
.table('threads')
666+
.get(threadId)
667+
.update({
668+
messageCount: db
669+
.row('messageCount')
670+
.default(0)
671+
.add(1),
672+
})
673+
.run();
674+
};
675+
676+
export const decrementMessageCount = (threadId: string) => {
677+
return db
678+
.table('threads')
679+
.get(threadId)
680+
.update({
681+
messageCount: db
682+
.row('messageCount')
683+
.default(1)
684+
.sub(1),
685+
})
686+
.run();
687+
};
688+
689+
export const incrementReactionCount = (threadId: string) => {
690+
return db
691+
.table('threads')
692+
.get(threadId)
693+
.update({
694+
reactionCount: db
695+
.row('reactionCount')
696+
.default(0)
697+
.add(1),
698+
})
699+
.run();
700+
};
701+
702+
export const decrementReactionCount = (threadId: string) => {
703+
return db
704+
.table('threads')
705+
.get(threadId)
706+
.update({
707+
reactionCount: db
708+
.row('reactionCount')
709+
.default(1)
710+
.sub(1),
711+
})
712+
.run();
713+
};
714+
663715
const hasChanged = (field: string) =>
664716
db
665717
.row('old_val')(field)

0 commit comments

Comments
 (0)