simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
Misha Krieger-Raynauld | 6bbdacf | 2022-11-29 21:45:40 -0500 | [diff] [blame] | 18 | import { Message } from 'jami-web-common'; |
| 19 | |
| 20 | import { Contact } from './Contact'; |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 21 | |
| 22 | export interface ConversationMember { |
| 23 | contact: Contact; |
| 24 | role?: 'admin' | 'member' | 'invited' | 'banned' | 'left'; |
| 25 | } |
| 26 | |
| 27 | type ConversationInfos = Record<string, unknown>; |
| 28 | |
simon | 2007698 | 2022-10-11 15:04:13 -0400 | [diff] [blame] | 29 | export class Conversation { |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 30 | private readonly id: string | undefined; |
| 31 | private readonly accountId: string; |
| 32 | private readonly members: ConversationMember[]; |
simon | 5e32f74 | 2022-10-04 18:03:12 -0400 | [diff] [blame] | 33 | private messages: Message[]; |
| 34 | private _infos: ConversationInfos; |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 35 | |
| 36 | constructor(id: string | undefined, accountId: string, members?: ConversationMember[]) { |
| 37 | this.id = id; |
| 38 | this.accountId = accountId; |
| 39 | this.members = members || []; |
simon | 5e32f74 | 2022-10-04 18:03:12 -0400 | [diff] [blame] | 40 | |
| 41 | this.messages = []; |
| 42 | this._infos = {}; |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 43 | } |
| 44 | |
simon | 5e32f74 | 2022-10-04 18:03:12 -0400 | [diff] [blame] | 45 | static from(accountId: string, object: any) { |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 46 | const conversation = new Conversation( |
| 47 | object.id, |
| 48 | accountId, |
simon | 5e32f74 | 2022-10-04 18:03:12 -0400 | [diff] [blame] | 49 | object.members.map((member: any) => { |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 50 | member.contact = Contact.from(member.contact); |
| 51 | return member; |
| 52 | }) |
| 53 | ); |
| 54 | conversation.messages = object.messages; |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 55 | conversation.infos = object.infos; |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 56 | return conversation; |
| 57 | } |
| 58 | static fromSingleContact(accountId: string, contact: Contact) { |
| 59 | return new Conversation(undefined, accountId, [{ contact }]); |
| 60 | } |
| 61 | |
| 62 | getId() { |
| 63 | return this.id; |
| 64 | } |
| 65 | |
| 66 | getAccountId() { |
| 67 | return this.accountId; |
| 68 | } |
| 69 | |
| 70 | getDisplayName() { |
| 71 | if (this.members.length !== 0) { |
| 72 | return this.members[0].contact.getDisplayName(); |
| 73 | } |
| 74 | return this.getDisplayUri(); |
| 75 | } |
| 76 | |
| 77 | getDisplayNameNoFallback() { |
| 78 | if (this.members.length !== 0) { |
| 79 | return this.members[0].contact.getDisplayNameNoFallback(); |
| 80 | } |
| 81 | } |
| 82 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 83 | getDisplayUri() { |
| 84 | return this.getId() || this.getFirstMember().contact.getUri(); |
| 85 | } |
| 86 | |
| 87 | getFirstMember() { |
| 88 | return this.members[0]; |
| 89 | } |
| 90 | |
| 91 | getMembers() { |
| 92 | return this.members; |
| 93 | } |
| 94 | |
| 95 | addMessage(message: Message) { |
| 96 | if (this.messages.length === 0) this.messages.push(message); |
| 97 | else if (message.id === this.messages[this.messages.length - 1].linearizedParent) { |
| 98 | this.messages.push(message); |
| 99 | } else if (message.linearizedParent === this.messages[0].id) { |
| 100 | this.messages.unshift(message); |
| 101 | } else { |
| 102 | console.log("Can't insert message " + message.id); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | addLoadedMessages(messages: Message[]) { |
| 107 | messages.forEach((message) => this.addMessage(message)); |
| 108 | } |
| 109 | |
| 110 | getMessages() { |
| 111 | return this.messages; |
| 112 | } |
| 113 | |
idillon | ae655dd | 2022-10-14 18:11:02 -0400 | [diff] [blame] | 114 | get infos() { |
| 115 | return this._infos; |
| 116 | } |
| 117 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 118 | set infos(infos: ConversationInfos) { |
| 119 | this._infos = infos; |
| 120 | } |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 121 | } |