blob: 452b1fb885c7d5ddc122a22d4b546c2537ca1549 [file] [log] [blame]
Adrià Vilanova Martínez3c13a762024-11-09 23:03:32 +01001import {kAdditionalInfoClass} from '../../features/flattenThreads/core/flattenThreads.js';
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +01002import GapModel from '../../models/Gap.js';
3import MessageModel from '../../models/Message.js';
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +01004import StartupDataModel from '../../models/StartupData.js';
5import ThreadModel from '../../models/Thread.js';
6
7const currentUser = StartupDataModel.buildFromCCDOM().getCurrentUserModel();
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +01008
Adrià Vilanova Martínez4fb615f2023-02-04 18:55:16 +01009const flattenThread = {
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010010 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
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010022 // Do the actual flattening
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +010023 const originalMogs = thread.getMessageOrGapModels();
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010024 let extraMogs = [];
25 originalMogs.forEach(mog => {
26 if (mog instanceof GapModel) return;
27 const cogs = mog.getCommentsAndGaps();
28 extraMogs = extraMogs.concat(cogs);
29 mog.clearCommentsAndGaps();
30 });
31 const mogs = originalMogs.concat(extraMogs);
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010032
33 // Add some message data to the payload so the extension can show the parent
34 // comment/reply in the case of comments.
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010035 let prevReplyId;
36 let prevReplyParentId;
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010037 mogs.forEach(m => {
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010038 const info = this.getAdditionalInformation(
39 m, mogs, prevReplyId, prevReplyParentId);
40 prevReplyId = m.getId();
41 prevReplyParentId = info.parentId;
42
Adrià Vilanova Martínez3f26d432023-02-04 20:49:25 +010043 const extraInfoEl = document.createElement('code');
44 extraInfoEl.textContent = JSON.stringify(info);
45 extraInfoEl.setAttribute('style', 'display: none');
46 extraInfoEl.classList.add(kAdditionalInfoClass);
47 m.newPayload = m.getPayload() + extraInfoEl.outerHTML;
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010048 });
49 mogs.forEach(m => m.setPayload(m.newPayload));
50
Adrià Vilanova Martínez4fb615f2023-02-04 18:55:16 +010051 // Clear parent_message_id fields
52 mogs.forEach(m => m.clearParentMessageId());
53
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010054 // Sort the messages by date
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010055 mogs.sort((a, b) => {
56 const c = a instanceof MessageModel ? a.getCreatedMicroseconds() :
57 a.getStartTimestamp();
58 const d = b instanceof MessageModel ? b.getCreatedMicroseconds() :
59 b.getStartTimestamp();
60 const diff = c - d;
61 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
62 });
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010063
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +010064 thread.setRawCommentsAndGaps(mogs.map(mog => mog.toRawMessageOrGap()));
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010065
Adrià Vilanova Martínez4fb615f2023-02-04 18:55:16 +010066 // Set last_message to the last message after sorting
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +010067 thread.setLastMessage(thread.getRawCommentsAndGaps().slice(-1)?.[1]);
Adrià Vilanova Martínez4fb615f2023-02-04 18:55:16 +010068
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010069 // Set num_messages to the updated value, since we've flattened the replies.
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +010070 thread.setNumMessages(thread.getRawCommentsAndGaps().length);
71
72 response[1] = thread.toRawThread();
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010073 return response;
74 },
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010075 getAdditionalInformation(message, mogs, prevReplyId, prevReplyParentId) {
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010076 const id = message.getId();
77 const parentId = message.getParentMessageId();
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010078 const authorName = message.getAuthor()?.[1]?.[1];
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +010079 const canComment = message.canComment(currentUser);
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010080 if (!parentId) {
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010081 return {
82 isComment: false,
83 id,
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010084 authorName,
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +010085 canComment,
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010086 };
87 }
88
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010089 let prevId;
Adrià Vilanova Martínezb47ec062023-01-15 17:43:26 +010090 if (parentId == prevReplyParentId && prevReplyParentId)
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010091 prevId = prevReplyId;
92 else
93 prevId = parentId;
94
Adrià Vilanova Martínez4fb615f2023-02-04 18:55:16 +010095 const prevMessage = prevId ? mogs.find(m => m.getId() == prevId) : null;
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010096
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010097 return {
98 isComment: true,
99 id,
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100100 authorName,
101 parentId,
102 prevMessage: {
103 id: prevId,
Adrià Vilanova Martínezb47ec062023-01-15 17:43:26 +0100104 payload: prevMessage?.getPayload(),
105 author: prevMessage?.getAuthor(),
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +0100106 },
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +0100107 canComment,
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +0100108 };
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +0100109 },
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100110};
111
Adrià Vilanova Martínez4fb615f2023-02-04 18:55:16 +0100112export default flattenThread;