Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 1 | import ItemMetadataState from './enums/ItemMetadataState.js'; |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 2 | import GapModel from './Gap.js'; |
| 3 | import ThreadModel from './Thread.js'; |
| 4 | |
| 5 | export default class MessageModel { |
Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 6 | constructor(data, thread) { |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 7 | this.data = data ?? {}; |
Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 8 | this.thread = thread ?? new ThreadModel(); |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 9 | this.commentsAndGaps = null; |
| 10 | } |
| 11 | |
| 12 | getCreatedTimestamp() { |
| 13 | return this.data[1]?.[1]?.[2] ?? null; |
| 14 | } |
| 15 | |
| 16 | getCreatedMicroseconds() { |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 17 | let a = this.getCreatedTimestamp(); |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 18 | if (a === null) a = '0'; |
| 19 | return BigInt(a); |
| 20 | } |
| 21 | |
| 22 | getRawCommentsAndGaps() { |
| 23 | return this.data[12] ?? []; |
| 24 | } |
| 25 | |
Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 26 | #getMessageOrGapModels() { |
| 27 | const rawMogs = this.getRawCommentsAndGaps(); |
| 28 | return rawMogs.filter(mog => mog !== undefined).map(mog => { |
| 29 | if (mog[1]) return new MessageModel(mog[1], this.thread); |
| 30 | if (mog[2]) return new GapModel(mog[2], this.thread); |
| 31 | }); |
| 32 | } |
| 33 | |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 34 | getCommentsAndGaps() { |
| 35 | if (this.commentsAndGaps === null) |
Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 36 | this.commentsAndGaps = this.#getMessageOrGapModels(); |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 37 | return this.commentsAndGaps; |
| 38 | } |
| 39 | |
| 40 | clearCommentsAndGaps() { |
| 41 | this.commentsAndGaps = []; |
| 42 | this.data[12] = []; |
| 43 | } |
| 44 | |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 45 | getPayload() { |
| 46 | return this.data[1]?.[4] ?? null; |
| 47 | } |
| 48 | |
| 49 | setPayload(value) { |
| 50 | if (!this.data[1]) this.data[1] = []; |
| 51 | this.data[1][4] = value; |
| 52 | } |
| 53 | |
| 54 | getId() { |
| 55 | return this.data[1]?.[1]?.[1] ?? null; |
| 56 | } |
| 57 | |
| 58 | getAuthor() { |
| 59 | return this.data[3] ?? null; |
| 60 | } |
| 61 | |
| 62 | getParentMessageId() { |
| 63 | return this.data[1]?.[37] ?? null; |
| 64 | } |
| 65 | |
Adrià Vilanova Martínez | 4fb615f | 2023-02-04 18:55:16 +0100 | [diff] [blame] | 66 | clearParentMessageId() { |
| 67 | if (!this.data[1]) return; |
| 68 | delete this.data[1][37]; |
| 69 | } |
| 70 | |
Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 71 | isDeleted() { |
| 72 | return this.data[5]?.[3] ?? null; |
| 73 | } |
| 74 | |
| 75 | getState() { |
| 76 | return this.data[5]?.[1] ?? null; |
| 77 | } |
| 78 | |
| 79 | isTakenDown() { |
| 80 | return [ |
| 81 | ItemMetadataState.AUTOMATED_ABUSE_TAKE_DOWN_DELETE, |
| 82 | ItemMetadataState.MANUAL_PROFILE_TAKE_DOWN_SUSPEND, |
| 83 | ItemMetadataState.AUTOMATED_ABUSE_TAKE_DOWN_HIDE, |
| 84 | ItemMetadataState.MANUAL_TAKE_DOWN_DELETE, |
| 85 | ItemMetadataState.MANUAL_TAKE_DOWN_HIDE, |
| 86 | ].includes(this.getState()); |
| 87 | } |
| 88 | |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 89 | isComment() { |
| 90 | return !!this.getParentMessageId; |
| 91 | } |
| 92 | |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 93 | toRawMessageOrGap() { |
| 94 | return {1: this.data}; |
| 95 | } |
| 96 | |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 97 | mergeCommentOrGapViews(a) { |
| 98 | this.commentsAndGaps = ThreadModel.mergeMessageOrGaps( |
| 99 | a.getCommentsAndGaps(), this.getCommentsAndGaps()); |
| 100 | this.data[12] = this.commentsAndGaps.map(cog => cog.toRawMessageOrGap()); |
| 101 | } |
Adrià Vilanova Martínez | 5dd7a6f | 2023-03-04 18:32:27 +0100 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * The following method is based on logic written by Googlers in the TW |
| 105 | * frontend and thus is not included as part of the MIT license. |
| 106 | * |
| 107 | * Source: |
| 108 | * module$exports$google3$customer_support$content$ui$client$tailwind$models$message_model$message_model.MessageModel.prototype.canComment |
| 109 | */ |
| 110 | canComment(currentUser) { |
| 111 | if (this.isDeleted()) return false; |
| 112 | if (this.isTakenDown()) return false; |
| 113 | if (currentUser.isAccountDisabled()) return false; |
| 114 | if (this.thread.isLocked() && |
| 115 | !currentUser.isAtLeastCommunityManager(this.thread.getForumId())) { |
| 116 | return false; |
| 117 | } |
| 118 | if (this.thread.isSoftLocked() && !currentUser.isAtLeastSilverRole() && |
| 119 | !this.thread.isAuthoredByUser()) { |
| 120 | return false; |
| 121 | } |
| 122 | return true; |
| 123 | } |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 124 | } |