blob: 0e9d6fb3cde072db9b6bdda5069cb446f0877a12 [file] [log] [blame]
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +01001import ItemMetadataState from './enums/ItemMetadataState.js';
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +01002import GapModel from './Gap.js';
3import ThreadModel from './Thread.js';
4
5export default class MessageModel {
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +01006 constructor(data, thread) {
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +01007 this.data = data ?? {};
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +01008 this.thread = thread ?? new ThreadModel();
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +01009 this.commentsAndGaps = null;
10 }
11
12 getCreatedTimestamp() {
13 return this.data[1]?.[1]?.[2] ?? null;
14 }
15
16 getCreatedMicroseconds() {
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010017 let a = this.getCreatedTimestamp();
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010018 if (a === null) a = '0';
19 return BigInt(a);
20 }
21
22 getRawCommentsAndGaps() {
23 return this.data[12] ?? [];
24 }
25
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +010026 #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ínez2d9be8d2022-12-28 00:50:14 +010034 getCommentsAndGaps() {
35 if (this.commentsAndGaps === null)
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +010036 this.commentsAndGaps = this.#getMessageOrGapModels();
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010037 return this.commentsAndGaps;
38 }
39
40 clearCommentsAndGaps() {
41 this.commentsAndGaps = [];
42 this.data[12] = [];
43 }
44
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010045 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ínez4fb615f2023-02-04 18:55:16 +010066 clearParentMessageId() {
67 if (!this.data[1]) return;
68 delete this.data[1][37];
69 }
70
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +010071 isDeleted() {
72 return this.data[5]?.[3] ?? null;
73 }
74
75 getState() {
76 return this.data[5]?.[1] ?? null;
77 }
78
Adrià Vilanova Martínez19f6a652023-11-15 19:38:50 +010079 getEndPendingStateTimestampMicros() {
80 return this.data[1]?.[17] ?? null;
81 }
82
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +010083 isTakenDown() {
84 return [
85 ItemMetadataState.AUTOMATED_ABUSE_TAKE_DOWN_DELETE,
86 ItemMetadataState.MANUAL_PROFILE_TAKE_DOWN_SUSPEND,
87 ItemMetadataState.AUTOMATED_ABUSE_TAKE_DOWN_HIDE,
88 ItemMetadataState.MANUAL_TAKE_DOWN_DELETE,
89 ItemMetadataState.MANUAL_TAKE_DOWN_HIDE,
90 ].includes(this.getState());
91 }
92
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010093 isComment() {
94 return !!this.getParentMessageId;
95 }
96
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010097 toRawMessageOrGap() {
98 return {1: this.data};
99 }
100
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100101 mergeCommentOrGapViews(a) {
102 this.commentsAndGaps = ThreadModel.mergeMessageOrGaps(
103 a.getCommentsAndGaps(), this.getCommentsAndGaps());
104 this.data[12] = this.commentsAndGaps.map(cog => cog.toRawMessageOrGap());
105 }
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +0100106
107 /**
108 * The following method is based on logic written by Googlers in the TW
109 * frontend and thus is not included as part of the MIT license.
110 *
111 * Source:
112 * module$exports$google3$customer_support$content$ui$client$tailwind$models$message_model$message_model.MessageModel.prototype.canComment
113 */
114 canComment(currentUser) {
115 if (this.isDeleted()) return false;
116 if (this.isTakenDown()) return false;
117 if (currentUser.isAccountDisabled()) return false;
118 if (this.thread.isLocked() &&
119 !currentUser.isAtLeastCommunityManager(this.thread.getForumId())) {
120 return false;
121 }
122 if (this.thread.isSoftLocked() && !currentUser.isAtLeastSilverRole() &&
123 !this.thread.isAuthoredByUser()) {
124 return false;
125 }
126 return true;
127 }
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100128}