blob: fa72af958e4722383c29ec99db0fcdec6b908e81 [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
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-Raynauld6bbdacf2022-11-29 21:45:40 -050018import { Message } from 'jami-web-common';
19
20import { Contact } from './Contact';
simon06527b02022-10-01 15:01:47 -040021
22export interface ConversationMember {
23 contact: Contact;
24 role?: 'admin' | 'member' | 'invited' | 'banned' | 'left';
25}
26
27type ConversationInfos = Record<string, unknown>;
28
simon20076982022-10-11 15:04:13 -040029export class Conversation {
simon06527b02022-10-01 15:01:47 -040030 private readonly id: string | undefined;
31 private readonly accountId: string;
32 private readonly members: ConversationMember[];
simon5e32f742022-10-04 18:03:12 -040033 private messages: Message[];
34 private _infos: ConversationInfos;
simon06527b02022-10-01 15:01:47 -040035
36 constructor(id: string | undefined, accountId: string, members?: ConversationMember[]) {
37 this.id = id;
38 this.accountId = accountId;
39 this.members = members || [];
simon5e32f742022-10-04 18:03:12 -040040
41 this.messages = [];
42 this._infos = {};
simon06527b02022-10-01 15:01:47 -040043 }
44
simon5e32f742022-10-04 18:03:12 -040045 static from(accountId: string, object: any) {
simon06527b02022-10-01 15:01:47 -040046 const conversation = new Conversation(
47 object.id,
48 accountId,
simon5e32f742022-10-04 18:03:12 -040049 object.members.map((member: any) => {
simon06527b02022-10-01 15:01:47 -040050 member.contact = Contact.from(member.contact);
51 return member;
52 })
53 );
54 conversation.messages = object.messages;
idillonae655dd2022-10-14 18:11:02 -040055 conversation.infos = object.infos;
simon06527b02022-10-01 15:01:47 -040056 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
simon06527b02022-10-01 15:01:47 -040083 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
idillonae655dd2022-10-14 18:11:02 -0400114 get infos() {
115 return this._infos;
116 }
117
simon06527b02022-10-01 15:01:47 -0400118 set infos(infos: ConversationInfos) {
119 this._infos = infos;
120 }
simon06527b02022-10-01 15:01:47 -0400121}