Adrià Vilanova Martínez | 3c13a76 | 2024-11-09 23:03:32 +0100 | [diff] [blame^] | 1 | import {kAdditionalInfoClass} from '../../features/flattenThreads/core/flattenThreads.js'; |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 2 | import GapModel from '../../models/Gap.js'; |
| 3 | import MessageModel from '../../models/Message.js'; |
Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 4 | import StartupDataModel from '../../models/StartupData.js'; |
| 5 | import ThreadModel from '../../models/Thread.js'; |
| 6 | |
| 7 | const currentUser = StartupDataModel.buildFromCCDOM().getCurrentUserModel(); |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 8 | |
Adrià Vilanova Martínez | 4fb615f | 2023-02-04 18:55:16 +0100 | [diff] [blame] | 9 | const flattenThread = { |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 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 | |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 22 | // Do the actual flattening |
Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 23 | const originalMogs = thread.getMessageOrGapModels(); |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 24 | 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ínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 32 | |
| 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ínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 35 | let prevReplyId; |
| 36 | let prevReplyParentId; |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 37 | mogs.forEach(m => { |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 38 | const info = this.getAdditionalInformation( |
| 39 | m, mogs, prevReplyId, prevReplyParentId); |
| 40 | prevReplyId = m.getId(); |
| 41 | prevReplyParentId = info.parentId; |
| 42 | |
Adrià Vilanova Martínez | 3f26d43 | 2023-02-04 20:49:25 +0100 | [diff] [blame] | 43 | 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ínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 48 | }); |
| 49 | mogs.forEach(m => m.setPayload(m.newPayload)); |
| 50 | |
Adrià Vilanova Martínez | 4fb615f | 2023-02-04 18:55:16 +0100 | [diff] [blame] | 51 | // Clear parent_message_id fields |
| 52 | mogs.forEach(m => m.clearParentMessageId()); |
| 53 | |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 54 | // Sort the messages by date |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 55 | 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ínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 63 | |
Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 64 | thread.setRawCommentsAndGaps(mogs.map(mog => mog.toRawMessageOrGap())); |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 65 | |
Adrià Vilanova Martínez | 4fb615f | 2023-02-04 18:55:16 +0100 | [diff] [blame] | 66 | // Set last_message to the last message after sorting |
Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 67 | thread.setLastMessage(thread.getRawCommentsAndGaps().slice(-1)?.[1]); |
Adrià Vilanova Martínez | 4fb615f | 2023-02-04 18:55:16 +0100 | [diff] [blame] | 68 | |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 69 | // Set num_messages to the updated value, since we've flattened the replies. |
Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 70 | thread.setNumMessages(thread.getRawCommentsAndGaps().length); |
| 71 | |
| 72 | response[1] = thread.toRawThread(); |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 73 | return response; |
| 74 | }, |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 75 | getAdditionalInformation(message, mogs, prevReplyId, prevReplyParentId) { |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 76 | const id = message.getId(); |
| 77 | const parentId = message.getParentMessageId(); |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 78 | const authorName = message.getAuthor()?.[1]?.[1]; |
Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 79 | const canComment = message.canComment(currentUser); |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 80 | if (!parentId) { |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 81 | return { |
| 82 | isComment: false, |
| 83 | id, |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 84 | authorName, |
Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 85 | canComment, |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 86 | }; |
| 87 | } |
| 88 | |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 89 | let prevId; |
Adrià Vilanova Martínez | b47ec06 | 2023-01-15 17:43:26 +0100 | [diff] [blame] | 90 | if (parentId == prevReplyParentId && prevReplyParentId) |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 91 | prevId = prevReplyId; |
| 92 | else |
| 93 | prevId = parentId; |
| 94 | |
Adrià Vilanova Martínez | 4fb615f | 2023-02-04 18:55:16 +0100 | [diff] [blame] | 95 | const prevMessage = prevId ? mogs.find(m => m.getId() == prevId) : null; |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 96 | |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 97 | return { |
| 98 | isComment: true, |
| 99 | id, |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 100 | authorName, |
| 101 | parentId, |
| 102 | prevMessage: { |
| 103 | id: prevId, |
Adrià Vilanova Martínez | b47ec06 | 2023-01-15 17:43:26 +0100 | [diff] [blame] | 104 | payload: prevMessage?.getPayload(), |
| 105 | author: prevMessage?.getAuthor(), |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 106 | }, |
Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 107 | canComment, |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 108 | }; |
Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 109 | }, |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 110 | }; |
| 111 | |
Adrià Vilanova Martínez | 4fb615f | 2023-02-04 18:55:16 +0100 | [diff] [blame] | 112 | export default flattenThread; |