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() { |
| 15 | const a = this.getCreatedTimestamp(); |
| 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 | |
| 36 | toRawMessageOrGap() { |
| 37 | return {1: this.data}; |
| 38 | } |
| 39 | |
| 40 | static mapToMessageOrGapModels(rawArray) { |
| 41 | return rawArray.map(mog => { |
| 42 | if (mog[1]) return new MessageModel(mog[1]); |
| 43 | if (mog[2]) return new GapModel(mog[2]); |
| 44 | }); |
| 45 | } |
| 46 | |
| 47 | mergeCommentOrGapViews(a) { |
| 48 | this.commentsAndGaps = ThreadModel.mergeMessageOrGaps( |
| 49 | a.getCommentsAndGaps(), this.getCommentsAndGaps()); |
| 50 | this.data[12] = this.commentsAndGaps.map(cog => cog.toRawMessageOrGap()); |
| 51 | } |
| 52 | } |