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 | |
| 57 | isComment() { |
| 58 | return !!this.getParentMessageId; |
| 59 | } |
| 60 | |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 61 | toRawMessageOrGap() { |
| 62 | return {1: this.data}; |
| 63 | } |
| 64 | |
| 65 | static mapToMessageOrGapModels(rawArray) { |
Adrià Vilanova Martínez | b47ec06 | 2023-01-15 17:43:26 +0100 | [diff] [blame] | 66 | return rawArray.filter(mog => mog !== undefined).map(mog => { |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 67 | 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 | } |