Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [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 | */ |
| 18 | import { ConversationInfos, IConversation, IConversationMember, Message } from 'jami-web-common'; |
| 19 | |
| 20 | import { Contact } from './contact'; |
| 21 | |
| 22 | export interface ConversationMember extends IConversationMember { |
| 23 | contact: Contact; |
| 24 | } |
| 25 | |
| 26 | export class Conversation implements IConversation { |
| 27 | readonly id: string; |
| 28 | members: ConversationMember[]; |
| 29 | messages: Message[] = []; |
| 30 | infos: ConversationInfos = {}; |
| 31 | |
| 32 | constructor(id: string, members?: ConversationMember[]) { |
| 33 | this.id = id; |
| 34 | this.members = members ?? []; |
| 35 | } |
| 36 | |
| 37 | static fromInterface(conversationInterface: IConversation) { |
| 38 | const conversation = new Conversation( |
| 39 | conversationInterface.id, |
| 40 | conversationInterface.members.map((member) => { |
| 41 | const contact = Contact.fromInterface(member.contact); |
| 42 | return { contact } as ConversationMember; |
| 43 | }) |
| 44 | ); |
| 45 | |
| 46 | conversation.messages = conversationInterface.messages; |
| 47 | conversation.infos = conversationInterface.infos; |
| 48 | |
| 49 | return conversation; |
| 50 | } |
| 51 | |
| 52 | static fromSingleContact(contact: Contact) { |
| 53 | return new Conversation('', [{ contact } as ConversationMember]); |
| 54 | } |
| 55 | |
| 56 | getDisplayUri() { |
| 57 | return this.id ?? this.getFirstMember().contact.uri; |
| 58 | } |
| 59 | |
| 60 | getDisplayName() { |
| 61 | if (this.members.length !== 0) { |
| 62 | return this.getFirstMember().contact.registeredName; |
| 63 | } |
| 64 | return this.getDisplayUri(); |
| 65 | } |
| 66 | |
| 67 | getDisplayNameNoFallback() { |
| 68 | if (this.members.length !== 0) { |
| 69 | return this.getFirstMember().contact.registeredName; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | getFirstMember() { |
| 74 | return this.members[0]; |
| 75 | } |
| 76 | |
| 77 | addMessage(message: Message) { |
| 78 | if (this.messages.length === 0) { |
| 79 | this.messages.push(message); |
| 80 | } else if (message.id === this.messages[this.messages.length - 1].linearizedParent) { |
| 81 | this.messages.push(message); |
| 82 | } else if (message.linearizedParent === this.messages[0].id) { |
| 83 | this.messages.unshift(message); |
| 84 | } else { |
| 85 | console.log('Could not insert message', message.id); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | addMessages(messages: Message[]) { |
| 90 | for (const message of messages) { |
| 91 | this.addMessage(message); |
| 92 | } |
| 93 | } |
| 94 | } |