Adrià Vilanova Martínez | 3f26d43 | 2023-02-04 20:49:25 +0100 | [diff] [blame] | 1 | import {kAdditionalInfoClass} from '../../contentScripts/communityConsole/flattenThreads/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'; |
| 4 | |
Adrià Vilanova Martínez | 4fb615f | 2023-02-04 18:55:16 +0100 | [diff] [blame] | 5 | const flattenThread = { |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 6 | 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ínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 16 | // Do the actual flattening |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 17 | 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ínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 27 | |
| 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ínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 30 | let prevReplyId; |
| 31 | let prevReplyParentId; |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 32 | mogs.forEach(m => { |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 33 | const info = this.getAdditionalInformation( |
| 34 | m, mogs, prevReplyId, prevReplyParentId); |
| 35 | prevReplyId = m.getId(); |
| 36 | prevReplyParentId = info.parentId; |
| 37 | |
Adrià Vilanova Martínez | 3f26d43 | 2023-02-04 20:49:25 +0100 | [diff] [blame] | 38 | 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ínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 43 | }); |
| 44 | mogs.forEach(m => m.setPayload(m.newPayload)); |
| 45 | |
Adrià Vilanova Martínez | 4fb615f | 2023-02-04 18:55:16 +0100 | [diff] [blame] | 46 | // Clear parent_message_id fields |
| 47 | mogs.forEach(m => m.clearParentMessageId()); |
| 48 | |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 49 | // Sort the messages by date |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 50 | 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ínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 58 | |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 59 | response[1][40] = mogs.map(mog => mog.toRawMessageOrGap()); |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 60 | |
Adrià Vilanova Martínez | 4fb615f | 2023-02-04 18:55:16 +0100 | [diff] [blame] | 61 | // 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ínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 65 | // 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ínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 69 | getAdditionalInformation(message, mogs, prevReplyId, prevReplyParentId) { |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 70 | const id = message.getId(); |
| 71 | const parentId = message.getParentMessageId(); |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 72 | const authorName = message.getAuthor()?.[1]?.[1]; |
| 73 | if (!parentId) { |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 74 | return { |
| 75 | isComment: false, |
| 76 | id, |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 77 | authorName, |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 78 | }; |
| 79 | } |
| 80 | |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 81 | let prevId; |
Adrià Vilanova Martínez | b47ec06 | 2023-01-15 17:43:26 +0100 | [diff] [blame] | 82 | if (parentId == prevReplyParentId && prevReplyParentId) |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 83 | prevId = prevReplyId; |
| 84 | else |
| 85 | prevId = parentId; |
| 86 | |
Adrià Vilanova Martínez | 4fb615f | 2023-02-04 18:55:16 +0100 | [diff] [blame] | 87 | const prevMessage = prevId ? mogs.find(m => m.getId() == prevId) : null; |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 88 | |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 89 | return { |
| 90 | isComment: true, |
| 91 | id, |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 92 | authorName, |
| 93 | parentId, |
| 94 | prevMessage: { |
| 95 | id: prevId, |
Adrià Vilanova Martínez | b47ec06 | 2023-01-15 17:43:26 +0100 | [diff] [blame] | 96 | payload: prevMessage?.getPayload(), |
| 97 | author: prevMessage?.getAuthor(), |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 98 | }, |
| 99 | }; |
| 100 | } |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 101 | }; |
| 102 | |
Adrià Vilanova Martínez | 4fb615f | 2023-02-04 18:55:16 +0100 | [diff] [blame] | 103 | export default flattenThread; |