blob: b515191f8e261ad913d00db5f8c2901015e31e52 [file] [log] [blame]
Adrià Vilanova Martínez3f26d432023-02-04 20:49:25 +01001import {kAdditionalInfoClass} from '../../contentScripts/communityConsole/flattenThreads/flattenThreads.js';
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +01002import GapModel from '../../models/Gap.js';
3import MessageModel from '../../models/Message.js';
4
Adrià Vilanova Martínez4fb615f2023-02-04 18:55:16 +01005const flattenThread = {
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +01006 urlRegex: /api\/ViewThread/i,
7 featureGated: true,
8 features: ['flattenthreads', 'flattenthreads_switch_enabled'],
9 isEnabled(options) {
10 return options['flattenthreads'] &&
11 options['flattenthreads_switch_enabled'];
12 },
13 async interceptor(_request, response) {
14 if (!response[1]?.[40]) return response;
15
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010016 // Do the actual flattening
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010017 const originalMogs =
18 MessageModel.mapToMessageOrGapModels(response[1][40] ?? []);
19 let extraMogs = [];
20 originalMogs.forEach(mog => {
21 if (mog instanceof GapModel) return;
22 const cogs = mog.getCommentsAndGaps();
23 extraMogs = extraMogs.concat(cogs);
24 mog.clearCommentsAndGaps();
25 });
26 const mogs = originalMogs.concat(extraMogs);
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010027
28 // Add some message data to the payload so the extension can show the parent
29 // comment/reply in the case of comments.
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010030 let prevReplyId;
31 let prevReplyParentId;
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010032 mogs.forEach(m => {
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010033 const info = this.getAdditionalInformation(
34 m, mogs, prevReplyId, prevReplyParentId);
35 prevReplyId = m.getId();
36 prevReplyParentId = info.parentId;
37
Adrià Vilanova Martínez3f26d432023-02-04 20:49:25 +010038 const extraInfoEl = document.createElement('code');
39 extraInfoEl.textContent = JSON.stringify(info);
40 extraInfoEl.setAttribute('style', 'display: none');
41 extraInfoEl.classList.add(kAdditionalInfoClass);
42 m.newPayload = m.getPayload() + extraInfoEl.outerHTML;
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010043 });
44 mogs.forEach(m => m.setPayload(m.newPayload));
45
Adrià Vilanova Martínez4fb615f2023-02-04 18:55:16 +010046 // Clear parent_message_id fields
47 mogs.forEach(m => m.clearParentMessageId());
48
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010049 // Sort the messages by date
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010050 mogs.sort((a, b) => {
51 const c = a instanceof MessageModel ? a.getCreatedMicroseconds() :
52 a.getStartTimestamp();
53 const d = b instanceof MessageModel ? b.getCreatedMicroseconds() :
54 b.getStartTimestamp();
55 const diff = c - d;
56 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
57 });
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010058
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010059 response[1][40] = mogs.map(mog => mog.toRawMessageOrGap());
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010060
Adrià Vilanova Martínez4fb615f2023-02-04 18:55:16 +010061 // Set last_message to the last message after sorting
62 if (response[1]?.[17]?.[3])
63 response[1][17][3] = response[1][40].slice(-1)?.[1];
64
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010065 // Set num_messages to the updated value, since we've flattened the replies.
66 response[1][8] = response[1][40].length;
67 return response;
68 },
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010069 getAdditionalInformation(message, mogs, prevReplyId, prevReplyParentId) {
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010070 const id = message.getId();
71 const parentId = message.getParentMessageId();
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010072 const authorName = message.getAuthor()?.[1]?.[1];
73 if (!parentId) {
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010074 return {
75 isComment: false,
76 id,
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010077 authorName,
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010078 };
79 }
80
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010081 let prevId;
Adrià Vilanova Martínezb47ec062023-01-15 17:43:26 +010082 if (parentId == prevReplyParentId && prevReplyParentId)
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010083 prevId = prevReplyId;
84 else
85 prevId = parentId;
86
Adrià Vilanova Martínez4fb615f2023-02-04 18:55:16 +010087 const prevMessage = prevId ? mogs.find(m => m.getId() == prevId) : null;
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010088
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010089 return {
90 isComment: true,
91 id,
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010092 authorName,
93 parentId,
94 prevMessage: {
95 id: prevId,
Adrià Vilanova Martínezb47ec062023-01-15 17:43:26 +010096 payload: prevMessage?.getPayload(),
97 author: prevMessage?.getAuthor(),
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010098 },
99 };
100 }
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100101};
102
Adrià Vilanova Martínez4fb615f2023-02-04 18:55:16 +0100103export default flattenThread;