blob: 65eb42c237f7f9a0941ff26c09e37818b1996715 [file] [log] [blame]
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +01001import GapModel from '../../models/Gap.js';
2import MessageModel from '../../models/Message.js';
3
4const loadMoreThread = {
5 urlRegex: /api\/ViewThread/i,
6 featureGated: true,
7 features: ['flattenthreads', 'flattenthreads_switch_enabled'],
8 isEnabled(options) {
9 return options['flattenthreads'] &&
10 options['flattenthreads_switch_enabled'];
11 },
12 async interceptor(_request, response) {
13 if (!response[1]?.[40]) return response;
14
15 const originalMogs =
16 MessageModel.mapToMessageOrGapModels(response[1][40] ?? []);
17 let extraMogs = [];
18 originalMogs.forEach(mog => {
19 if (mog instanceof GapModel) return;
20 const cogs = mog.getCommentsAndGaps();
21 extraMogs = extraMogs.concat(cogs);
22 mog.clearCommentsAndGaps();
23 });
24 const mogs = originalMogs.concat(extraMogs);
25 mogs.sort((a, b) => {
26 const c = a instanceof MessageModel ? a.getCreatedMicroseconds() :
27 a.getStartTimestamp();
28 const d = b instanceof MessageModel ? b.getCreatedMicroseconds() :
29 b.getStartTimestamp();
30 const diff = c - d;
31 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
32 });
33 response[1][40] = mogs.map(mog => mog.toRawMessageOrGap());
34 // Set num_messages to the updated value, since we've flattened the replies.
35 response[1][8] = response[1][40].length;
36 return response;
37 },
38};
39
40export default loadMoreThread;