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 | */ |
simon | 2007698 | 2022-10-11 15:04:13 -0400 | [diff] [blame] | 18 | export class Contact { |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 19 | private readonly uri: string; |
simon | 5e32f74 | 2022-10-04 18:03:12 -0400 | [diff] [blame] | 20 | private readonly displayName: string | undefined; |
| 21 | private registeredName: string | undefined; |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 22 | |
| 23 | constructor(uri: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 24 | this.uri = uri; |
simon | 5e32f74 | 2022-10-04 18:03:12 -0400 | [diff] [blame] | 25 | this.displayName = undefined; |
| 26 | this.registeredName = undefined; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 27 | } |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 28 | |
simon | 5e32f74 | 2022-10-04 18:03:12 -0400 | [diff] [blame] | 29 | static from(object: any) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 30 | const contact = new Contact(object.uri); |
| 31 | if (object.registeredName) contact.setRegisteredName(object.registeredName); |
| 32 | return contact; |
| 33 | } |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 34 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 35 | getUri() { |
| 36 | return this.uri; |
| 37 | } |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 38 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 39 | getRegisteredName() { |
| 40 | return this.registeredName; |
| 41 | } |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 42 | |
simon | 06527b0 | 2022-10-01 15:01:47 -0400 | [diff] [blame] | 43 | setRegisteredName(name: string | undefined) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 44 | this.registeredName = name; |
| 45 | } |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 46 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 47 | isRegisteredNameResolved() { |
| 48 | return this.registeredName !== undefined; |
| 49 | } |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 50 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 51 | getDisplayName() { |
| 52 | return this.getDisplayNameNoFallback() || this.getUri(); |
| 53 | } |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 54 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 55 | getDisplayNameNoFallback() { |
| 56 | return this.displayName || this.getRegisteredName(); |
| 57 | } |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 58 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 59 | async getObject() { |
| 60 | return { |
| 61 | uri: this.uri, |
| 62 | registeredName: await this.registeredName, |
| 63 | }; |
| 64 | } |
Adrien Béraud | 0cb76c9 | 2021-04-07 19:59:08 -0400 | [diff] [blame] | 65 | } |