blob: 05dc7954aa7cd32b76847c75d0bfb20acba16c6d [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
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ínez412b7582022-12-30 01:35:30 +010089 isComment() {
90 return !!this.getParentMessageId;
91 }
92
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010093 toRawMessageOrGap() {
94 return {1: this.data};
95 }
96
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010097 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ínez5dd7a6f2023-03-04 18:32:27 +0100102
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ínez2d9be8d2022-12-28 00:50:14 +0100124}