blob: abea5056c33f626efa4d6f04f894b7570f00d200 [file] [log] [blame]
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +01001import {CCApi} from '../../common/api.js';
2import {getAuthUser} from '../../common/communityConsoleUtils.js';
3import GapModel from '../../models/Gap.js';
4import MessageModel from '../../models/Message.js';
5import ThreadModel from '../../models/Thread.js';
6
7const authuser = getAuthUser();
8
9const loadMoreThread = {
10 urlRegex: /api\/ViewThread/i,
11 featureGated: true,
12 features: ['flattenthreads', 'flattenthreads_switch_enabled'],
13 isEnabled(options) {
14 return options['flattenthreads'] &&
15 options['flattenthreads_switch_enabled'];
16 },
17 async interceptor(request, response) {
18 if (!response[1]?.[40]) return response;
19
20 const forumId = response[1]?.[2]?.[1]?.[3];
21 const threadId = response[1]?.[2]?.[1]?.[1];
22 if (!forumId || !threadId) {
23 console.error(
24 '[loadMoreThread] Couldn\'t find forum id and thread id for:',
25 request.$TWPTRequestUrl);
26 return response;
27 }
28
29 const mogs = MessageModel.mapToMessageOrGapModels(response[1]?.[40] ?? []);
30 response[1][40] = await this.loadGaps(forumId, threadId, mogs, 0);
31 return response;
32 },
33 loadGaps(forumId, threadId, mogs, it) {
34 if (it >= 10) {
35 return Promise.reject(new Error(
36 'loadGaps has been called for more than 10 times, ' +
37 'which means we\'ve entered an infinite loop.'));
38 }
39
40 const messageOrGapPromises = [];
Adrià Vilanova Martínezb47ec062023-01-15 17:43:26 +010041 messageOrGapPromises.push(
42 Promise.resolve(mogs.filter(mog => mog !== undefined)));
43 mogs.forEach(mog => {
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010044 if (mog instanceof GapModel) {
45 messageOrGapPromises.push(this.loadGap(forumId, threadId, mog));
46 }
47 if (mog instanceof MessageModel) {
48 mog.getCommentsAndGaps().forEach(cog => {
49 if (cog instanceof GapModel) {
50 messageOrGapPromises.push(this.loadGap(forumId, threadId, cog));
51 }
52 });
53 }
Adrià Vilanova Martínezb47ec062023-01-15 17:43:26 +010054 });
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010055
56 return Promise.all(messageOrGapPromises).then(res => {
57 // #!if !production
58 console.time('mergeMessages');
59 // #!endif
60 const mogs = ThreadModel.mergeMessageOrGapsMultiarray(res);
61 // #!if !production
62 console.timeEnd('mergeMessages');
63 // #!endif
Adrià Vilanova Martínezb47ec062023-01-15 17:43:26 +010064
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010065 if (mogs.some(mog => {
66 return mog instanceof GapModel ||
67 mog.getCommentsAndGaps().some(cog => cog instanceof GapModel);
68 })) {
69 return this.loadGaps(forumId, threadId, mogs, it + 1);
70 }
71 return mogs.map(message => message.toRawMessageOrGap());
72 });
73 },
74 loadGap(forumId, threadId, gap) {
75 return CCApi(
76 'ViewThread', {
77 1: forumId,
78 2: threadId,
79 3: {
80 // options
81 1: {
82 // pagination
83 2: gap.getCount(), // maxNum
84 7: {
85 // targetRange
86 1: gap.getStartMicroseconds(), // startMicroseconds
87 2: gap.getEndMicroseconds(), // endMicroseconds
88 3: gap.getParentId(), // parentId
89 },
90 },
91 5: true, // withUserProfile
92 10: true, // withPromotedMessages
93 },
94 },
95 /* authenticated = */ true, authuser)
96 .then(res => {
97 return MessageModel.mapToMessageOrGapModels(res[1]?.[40] ?? []);
98 });
99 }
100};
101
102export default loadMoreThread;