blob: 7aae3926e31256c76128a884e78daf946709dca0 [file] [log] [blame]
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +01001import GapModel from './Gap.js';
2import ThreadModel from './Thread.js';
3
4export default class MessageModel {
5 constructor(data) {
6 this.data = data ?? {};
7 this.commentsAndGaps = null;
8 }
9
10 getCreatedTimestamp() {
11 return this.data[1]?.[1]?.[2] ?? null;
12 }
13
14 getCreatedMicroseconds() {
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010015 let a = this.getCreatedTimestamp();
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010016 if (a === null) a = '0';
17 return BigInt(a);
18 }
19
20 getRawCommentsAndGaps() {
21 return this.data[12] ?? [];
22 }
23
24 getCommentsAndGaps() {
25 if (this.commentsAndGaps === null)
26 this.commentsAndGaps =
27 MessageModel.mapToMessageOrGapModels(this.getRawCommentsAndGaps());
28 return this.commentsAndGaps;
29 }
30
31 clearCommentsAndGaps() {
32 this.commentsAndGaps = [];
33 this.data[12] = [];
34 }
35
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010036 getPayload() {
37 return this.data[1]?.[4] ?? null;
38 }
39
40 setPayload(value) {
41 if (!this.data[1]) this.data[1] = [];
42 this.data[1][4] = value;
43 }
44
45 getId() {
46 return this.data[1]?.[1]?.[1] ?? null;
47 }
48
49 getAuthor() {
50 return this.data[3] ?? null;
51 }
52
53 getParentMessageId() {
54 return this.data[1]?.[37] ?? null;
55 }
56
Adrià Vilanova Martínez4fb615f2023-02-04 18:55:16 +010057 clearParentMessageId() {
58 if (!this.data[1]) return;
59 delete this.data[1][37];
60 }
61
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010062 isComment() {
63 return !!this.getParentMessageId;
64 }
65
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010066 toRawMessageOrGap() {
67 return {1: this.data};
68 }
69
70 static mapToMessageOrGapModels(rawArray) {
Adrià Vilanova Martínezb47ec062023-01-15 17:43:26 +010071 return rawArray.filter(mog => mog !== undefined).map(mog => {
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010072 if (mog[1]) return new MessageModel(mog[1]);
73 if (mog[2]) return new GapModel(mog[2]);
74 });
75 }
76
77 mergeCommentOrGapViews(a) {
78 this.commentsAndGaps = ThreadModel.mergeMessageOrGaps(
79 a.getCommentsAndGaps(), this.getCommentsAndGaps());
80 this.data[12] = this.commentsAndGaps.map(cog => cog.toRawMessageOrGap());
81 }
82}