blob: 2ddf37d295f9f4ec20a8a779406faac58ce4a60c [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
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +010020 const thread = new ThreadModel(response[1]);
21
22 if (!thread.getForumId() || !thread.getId()) {
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010023 console.error(
24 '[loadMoreThread] Couldn\'t find forum id and thread id for:',
Adrià Vilanova Martínezc928a342023-01-15 20:03:10 +010025 request.$TWPTRequestURL);
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010026 return response;
27 }
28
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +010029 const mogs = thread.getMessageOrGapModels();
30 thread.setRawCommentsAndGaps(
31 await this.loadGaps(thread.getForumId(), thread.getId(), mogs, 0));
32
33 response[1] = thread.toRawThread();
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010034 return response;
35 },
36 loadGaps(forumId, threadId, mogs, it) {
37 if (it >= 10) {
38 return Promise.reject(new Error(
39 'loadGaps has been called for more than 10 times, ' +
40 'which means we\'ve entered an infinite loop.'));
41 }
42
43 const messageOrGapPromises = [];
Adrià Vilanova Martínezb47ec062023-01-15 17:43:26 +010044 messageOrGapPromises.push(
45 Promise.resolve(mogs.filter(mog => mog !== undefined)));
46 mogs.forEach(mog => {
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010047 if (mog instanceof GapModel) {
48 messageOrGapPromises.push(this.loadGap(forumId, threadId, mog));
49 }
50 if (mog instanceof MessageModel) {
51 mog.getCommentsAndGaps().forEach(cog => {
52 if (cog instanceof GapModel) {
53 messageOrGapPromises.push(this.loadGap(forumId, threadId, cog));
54 }
55 });
56 }
Adrià Vilanova Martínezb47ec062023-01-15 17:43:26 +010057 });
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010058
59 return Promise.all(messageOrGapPromises).then(res => {
60 // #!if !production
61 console.time('mergeMessages');
62 // #!endif
63 const mogs = ThreadModel.mergeMessageOrGapsMultiarray(res);
64 // #!if !production
65 console.timeEnd('mergeMessages');
66 // #!endif
Adrià Vilanova Martínezb47ec062023-01-15 17:43:26 +010067
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010068 if (mogs.some(mog => {
69 return mog instanceof GapModel ||
70 mog.getCommentsAndGaps().some(cog => cog instanceof GapModel);
71 })) {
72 return this.loadGaps(forumId, threadId, mogs, it + 1);
73 }
74 return mogs.map(message => message.toRawMessageOrGap());
75 });
76 },
77 loadGap(forumId, threadId, gap) {
78 return CCApi(
79 'ViewThread', {
80 1: forumId,
81 2: threadId,
82 3: {
83 // options
84 1: {
85 // pagination
86 2: gap.getCount(), // maxNum
87 7: {
88 // targetRange
89 1: gap.getStartMicroseconds(), // startMicroseconds
90 2: gap.getEndMicroseconds(), // endMicroseconds
91 3: gap.getParentId(), // parentId
92 },
93 },
94 5: true, // withUserProfile
95 10: true, // withPromotedMessages
96 },
97 },
98 /* authenticated = */ true, authuser)
99 .then(res => {
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +0100100 const thread = new ThreadModel(res[1]);
101 return thread.getMessageOrGapModels();
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100102 });
103 }
104};
105
106export default loadMoreThread;