Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 1 | import {CCApi} from '../../common/api.js'; |
| 2 | import {getAuthUser} from '../../common/communityConsoleUtils.js'; |
| 3 | import GapModel from '../../models/Gap.js'; |
Adrià Vilanova Martínez | 647f6e1 | 2024-12-05 15:34:40 +0100 | [diff] [blame^] | 4 | import MessageModel from '../../models/Message'; |
| 5 | import ThreadModel from '../../models/Thread'; |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 6 | |
| 7 | const authuser = getAuthUser(); |
| 8 | |
| 9 | const 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ínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 20 | const thread = new ThreadModel(response[1]); |
| 21 | |
| 22 | if (!thread.getForumId() || !thread.getId()) { |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 23 | console.error( |
| 24 | '[loadMoreThread] Couldn\'t find forum id and thread id for:', |
Adrià Vilanova Martínez | c928a34 | 2023-01-15 20:03:10 +0100 | [diff] [blame] | 25 | request.$TWPTRequestURL); |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 26 | return response; |
| 27 | } |
| 28 | |
Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 29 | 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ínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 34 | 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ínez | b47ec06 | 2023-01-15 17:43:26 +0100 | [diff] [blame] | 44 | messageOrGapPromises.push( |
| 45 | Promise.resolve(mogs.filter(mog => mog !== undefined))); |
| 46 | mogs.forEach(mog => { |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 47 | 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ínez | b47ec06 | 2023-01-15 17:43:26 +0100 | [diff] [blame] | 57 | }); |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 58 | |
| 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ínez | b47ec06 | 2023-01-15 17:43:26 +0100 | [diff] [blame] | 67 | |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 68 | 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ínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 100 | const thread = new ThreadModel(res[1]); |
| 101 | return thread.getMessageOrGapModels(); |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 102 | }); |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | export default loadMoreThread; |