2022-12-05 22:46:54 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import type {
|
|
|
|
ConversationAttributesType,
|
|
|
|
ConversationRenderInfoType,
|
|
|
|
} from '../model-types.d';
|
|
|
|
import { combineNames } from './combineNames';
|
|
|
|
import { getRegionCodeForNumber } from './libphonenumberUtil';
|
|
|
|
import { isDirectConversation } from './whatTypeOfConversation';
|
|
|
|
|
|
|
|
export function getTitle(
|
|
|
|
attributes: ConversationRenderInfoType,
|
|
|
|
options?: { isShort?: boolean }
|
|
|
|
): string {
|
|
|
|
const title = getTitleNoDefault(attributes, options);
|
|
|
|
if (title) {
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDirectConversation(attributes)) {
|
|
|
|
return window.i18n('unknownContact');
|
|
|
|
}
|
|
|
|
return window.i18n('unknownGroup');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getTitleNoDefault(
|
|
|
|
attributes: ConversationRenderInfoType,
|
|
|
|
{ isShort = false }: { isShort?: boolean } = {}
|
|
|
|
): string | undefined {
|
|
|
|
if (!isDirectConversation(attributes)) {
|
|
|
|
return attributes.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { username } = attributes;
|
|
|
|
|
|
|
|
return (
|
|
|
|
(isShort ? attributes.systemGivenName : undefined) ||
|
2023-02-02 18:03:51 +00:00
|
|
|
getSystemName(attributes) ||
|
2022-12-05 22:46:54 +00:00
|
|
|
(isShort ? attributes.profileName : undefined) ||
|
|
|
|
getProfileName(attributes) ||
|
|
|
|
getNumber(attributes) ||
|
2023-02-09 19:18:57 +00:00
|
|
|
username
|
2022-12-05 22:46:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-02 18:03:51 +00:00
|
|
|
// Note that the used attributes field should match the ones we listen for
|
|
|
|
// change on in ConversationModel (see `ConversationModel#maybeClearUsername`)
|
|
|
|
export function canHaveUsername(
|
|
|
|
attributes: Pick<
|
|
|
|
ConversationAttributesType,
|
|
|
|
'id' | 'type' | 'name' | 'profileName' | 'profileFamilyName' | 'e164'
|
|
|
|
>,
|
|
|
|
ourConversationId: string | undefined
|
|
|
|
): boolean {
|
|
|
|
if (!isDirectConversation(attributes)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ourConversationId === attributes.id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
!getSystemName(attributes) &&
|
|
|
|
!getProfileName(attributes) &&
|
|
|
|
!getNumber(attributes)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
export function getProfileName(
|
|
|
|
attributes: Pick<
|
|
|
|
ConversationAttributesType,
|
|
|
|
'profileName' | 'profileFamilyName' | 'type'
|
|
|
|
>
|
|
|
|
): string | undefined {
|
|
|
|
if (isDirectConversation(attributes)) {
|
|
|
|
return combineNames(attributes.profileName, attributes.profileFamilyName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2023-02-02 18:03:51 +00:00
|
|
|
export function getSystemName(
|
|
|
|
attributes: Pick<
|
|
|
|
ConversationAttributesType,
|
|
|
|
'systemGivenName' | 'systemFamilyName' | 'type'
|
|
|
|
>
|
|
|
|
): string | undefined {
|
|
|
|
if (isDirectConversation(attributes)) {
|
|
|
|
return combineNames(
|
|
|
|
attributes.systemGivenName,
|
|
|
|
attributes.systemFamilyName
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2022-12-05 22:46:54 +00:00
|
|
|
export function getNumber(
|
|
|
|
attributes: Pick<ConversationAttributesType, 'e164' | 'type'>
|
|
|
|
): string {
|
|
|
|
if (!isDirectConversation(attributes)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
const { e164 } = attributes;
|
|
|
|
if (!e164) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return renderNumber(e164);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function renderNumber(e164: string): string {
|
|
|
|
try {
|
|
|
|
const parsedNumber = window.libphonenumberInstance.parse(e164);
|
|
|
|
const regionCode = getRegionCodeForNumber(e164);
|
|
|
|
if (regionCode === window.storage.get('regionCode')) {
|
|
|
|
return window.libphonenumberInstance.format(
|
|
|
|
parsedNumber,
|
|
|
|
window.libphonenumberFormat.NATIONAL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return window.libphonenumberInstance.format(
|
|
|
|
parsedNumber,
|
|
|
|
window.libphonenumberFormat.INTERNATIONAL
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
return e164;
|
|
|
|
}
|
|
|
|
}
|