Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 1 | import GapModel from './Gap.js'; |
| 2 | import ThreadModel from './Thread.js'; |
| 3 | |
| 4 | export 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ínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 15 | let a = this.getCreatedTimestamp(); |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 16 | 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ínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 36 | 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ínez | 4fb615f | 2023-02-04 18:55:16 +0100 | [diff] [blame] | 57 | clearParentMessageId() { |
| 58 | if (!this.data[1]) return; |
| 59 | delete this.data[1][37]; |
| 60 | } |
| 61 | |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 62 | isComment() { |
| 63 | return !!this.getParentMessageId; |
| 64 | } |
| 65 | |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 66 | toRawMessageOrGap() { |
| 67 | return {1: this.data}; |
| 68 | } |
| 69 | |
| 70 | static mapToMessageOrGapModels(rawArray) { |
Adrià Vilanova Martínez | b47ec06 | 2023-01-15 17:43:26 +0100 | [diff] [blame] | 71 | return rawArray.filter(mog => mog !== undefined).map(mog => { |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 72 | 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 | } |