blob: baebd14fd58c19f5d3b399dcd7fea1064a15851f [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
57 isComment() {
58 return !!this.getParentMessageId;
59 }
60
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010061 toRawMessageOrGap() {
62 return {1: this.data};
63 }
64
65 static mapToMessageOrGapModels(rawArray) {
Adrià Vilanova Martínezb47ec062023-01-15 17:43:26 +010066 return rawArray.filter(mog => mog !== undefined).map(mog => {
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010067 if (mog[1]) return new MessageModel(mog[1]);
68 if (mog[2]) return new GapModel(mog[2]);
69 });
70 }
71
72 mergeCommentOrGapViews(a) {
73 this.commentsAndGaps = ThreadModel.mergeMessageOrGaps(
74 a.getCommentsAndGaps(), this.getCommentsAndGaps());
75 this.data[12] = this.commentsAndGaps.map(cog => cog.toRawMessageOrGap());
76 }
77}