signal-desktop/ts/util/findAndFormatContact.ts

48 lines
1.1 KiB
TypeScript
Raw Normal View History

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { ConversationType } from '../state/ducks/conversations';
import { format, isValidNumber } from '../types/PhoneNumber';
const PLACEHOLDER_CONTACT: ConversationType = {
acceptedMessageRequest: false,
2021-11-02 23:01:13 +00:00
badges: [],
id: 'placeholder-contact',
isMe: false,
sharedGroupNames: [],
2023-03-30 00:03:25 +00:00
title: window.i18n('icu:unknownContact'),
type: 'direct',
};
export function findAndFormatContact(identifier?: string): ConversationType {
if (!identifier) {
return PLACEHOLDER_CONTACT;
}
2021-06-23 19:16:29 +00:00
const contactModel = window.ConversationController.get(
identifier.toLowerCase()
2021-06-23 19:16:29 +00:00
);
if (contactModel) {
return contactModel.format();
}
const regionCode = window.storage.get('regionCode');
if (!isValidNumber(identifier, { regionCode })) {
return PLACEHOLDER_CONTACT;
}
const phoneNumber = format(identifier, { ourRegionCode: regionCode });
return {
acceptedMessageRequest: false,
2021-11-02 23:01:13 +00:00
badges: [],
id: 'phone-only',
isMe: false,
phoneNumber,
sharedGroupNames: [],
title: phoneNumber,
type: 'direct',
};
}