2021-06-22 23:16:50 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationType } from '../state/ducks/conversations';
|
2021-06-22 23:16:50 +00:00
|
|
|
import { format, isValidNumber } from '../types/PhoneNumber';
|
|
|
|
|
2022-11-19 08:31:18 +00:00
|
|
|
const PLACEHOLDER_CONTACT: ConversationType = {
|
2021-06-22 23:16:50 +00:00
|
|
|
acceptedMessageRequest: false,
|
2021-11-02 23:01:13 +00:00
|
|
|
badges: [],
|
2021-06-22 23:16:50 +00:00
|
|
|
id: 'placeholder-contact',
|
|
|
|
isMe: false,
|
|
|
|
sharedGroupNames: [],
|
2023-03-30 00:03:25 +00:00
|
|
|
title: window.i18n('icu:unknownContact'),
|
2021-06-22 23:16:50 +00:00
|
|
|
type: 'direct',
|
|
|
|
};
|
|
|
|
|
2022-11-19 08:31:18 +00:00
|
|
|
export function findAndFormatContact(identifier?: string): ConversationType {
|
2021-06-22 23:16:50 +00:00
|
|
|
if (!identifier) {
|
|
|
|
return PLACEHOLDER_CONTACT;
|
|
|
|
}
|
|
|
|
|
2021-06-23 19:16:29 +00:00
|
|
|
const contactModel = window.ConversationController.get(
|
2021-07-14 00:46:02 +00:00
|
|
|
identifier.toLowerCase()
|
2021-06-23 19:16:29 +00:00
|
|
|
);
|
2021-06-22 23:16:50 +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: [],
|
2021-06-22 23:16:50 +00:00
|
|
|
id: 'phone-only',
|
|
|
|
isMe: false,
|
|
|
|
phoneNumber,
|
|
|
|
sharedGroupNames: [],
|
|
|
|
title: phoneNumber,
|
|
|
|
type: 'direct',
|
|
|
|
};
|
|
|
|
}
|