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