blob: 88e191646cf54800d551f9a7373e154817836779 [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 */
simon20076982022-10-11 15:04:13 -040018export class Contact {
simon06527b02022-10-01 15:01:47 -040019 private readonly uri: string;
simon5e32f742022-10-04 18:03:12 -040020 private readonly displayName: string | undefined;
21 private registeredName: string | undefined;
simon06527b02022-10-01 15:01:47 -040022
23 constructor(uri: string) {
simond47ef9e2022-09-28 22:24:28 -040024 this.uri = uri;
simon5e32f742022-10-04 18:03:12 -040025 this.displayName = undefined;
26 this.registeredName = undefined;
simond47ef9e2022-09-28 22:24:28 -040027 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -040028
simon5e32f742022-10-04 18:03:12 -040029 static from(object: any) {
simond47ef9e2022-09-28 22:24:28 -040030 const contact = new Contact(object.uri);
31 if (object.registeredName) contact.setRegisteredName(object.registeredName);
32 return contact;
33 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -040034
simond47ef9e2022-09-28 22:24:28 -040035 getUri() {
36 return this.uri;
37 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -040038
simond47ef9e2022-09-28 22:24:28 -040039 getRegisteredName() {
40 return this.registeredName;
41 }
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040042
simon06527b02022-10-01 15:01:47 -040043 setRegisteredName(name: string | undefined) {
simond47ef9e2022-09-28 22:24:28 -040044 this.registeredName = name;
45 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -040046
simond47ef9e2022-09-28 22:24:28 -040047 isRegisteredNameResolved() {
48 return this.registeredName !== undefined;
49 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -040050
simond47ef9e2022-09-28 22:24:28 -040051 getDisplayName() {
52 return this.getDisplayNameNoFallback() || this.getUri();
53 }
Adrien Béraud150b4782021-04-21 19:40:59 -040054
simond47ef9e2022-09-28 22:24:28 -040055 getDisplayNameNoFallback() {
56 return this.displayName || this.getRegisteredName();
57 }
Adrien Béraud150b4782021-04-21 19:40:59 -040058
simond47ef9e2022-09-28 22:24:28 -040059 async getObject() {
60 return {
61 uri: this.uri,
62 registeredName: await this.registeredName,
63 };
64 }
Adrien Béraud0cb76c92021-04-07 19:59:08 -040065}