signal-desktop/ts/util/callLinks.ts

119 lines
3 KiB
TypeScript
Raw Normal View History

2024-02-22 21:19:50 +00:00
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2024-07-24 21:35:36 +00:00
import { v4 as generateUuid } from 'uuid';
2024-06-10 15:23:43 +00:00
import * as RemoteConfig from '../RemoteConfig';
2024-04-01 19:19:35 +00:00
import * as Bytes from '../Bytes';
import type { CallLinkConversationType, CallLinkType } from '../types/CallLink';
import { CallLinkRestrictions } from '../types/CallLink';
2024-02-22 21:19:50 +00:00
import type { LocalizerType } from '../types/Util';
2024-06-10 15:23:43 +00:00
import { isTestOrMockEnvironment } from '../environment';
import { getColorForCallLink } from './getColorForCallLink';
2024-07-24 21:35:36 +00:00
import {
AdhocCallStatus,
CallDirection,
CallType,
type CallHistoryDetails,
CallMode,
2024-07-24 21:35:36 +00:00
} from '../types/CallDisposition';
2024-10-16 18:08:47 +00:00
import { isBeta, isProduction } from './version';
2024-02-22 21:19:50 +00:00
export const CALL_LINK_DEFAULT_STATE: Pick<
CallLinkType,
'name' | 'restrictions' | 'revoked' | 'expiration' | 'storageNeedsSync'
> = {
2024-04-25 17:09:05 +00:00
name: '',
restrictions: CallLinkRestrictions.Unknown,
revoked: false,
expiration: null,
2024-09-04 18:06:06 +00:00
storageNeedsSync: false,
2024-04-25 17:09:05 +00:00
};
export function getKeyFromCallLink(callLink: string): string {
const url = new URL(callLink);
if (url == null) {
throw new Error('Failed to parse call link URL');
}
const hash = url.hash.slice(1);
const hashParams = new URLSearchParams(hash);
return hashParams.get('key') || '';
}
2024-06-10 15:23:43 +00:00
export function isCallLinksCreateEnabled(): boolean {
if (isTestOrMockEnvironment()) {
return true;
}
2024-10-16 18:08:47 +00:00
const version = window.getVersion();
if (isProduction(version)) {
return RemoteConfig.getValue('desktop.calling.adhoc.create') === 'TRUE';
}
if (isBeta(version)) {
return (
RemoteConfig.getValue('desktop.calling.adhoc.create.beta') === 'TRUE'
);
}
return true;
2024-06-10 15:23:43 +00:00
}
2024-02-22 21:19:50 +00:00
export function callLinkToConversation(
callLink: CallLinkType,
i18n: LocalizerType
): CallLinkConversationType {
const { roomId, name, rootKey } = callLink;
2024-02-22 21:19:50 +00:00
return {
id: roomId,
type: 'callLink',
color: getColorForCallLink(rootKey),
2024-02-22 21:19:50 +00:00
isMe: false,
title: name || i18n('icu:calling__call-link-default-title'),
sharedGroupNames: [],
acceptedMessageRequest: true,
badges: [],
};
}
2024-04-01 19:19:35 +00:00
2024-04-25 17:09:05 +00:00
export function getPlaceholderCallLinkConversation(
roomId: string,
i18n: LocalizerType
): CallLinkConversationType {
return {
id: roomId,
type: 'callLink',
isMe: false,
title: i18n('icu:calling__call-link-default-title'),
sharedGroupNames: [],
acceptedMessageRequest: true,
badges: [],
};
}
2024-06-10 15:23:43 +00:00
export function toAdminKeyBytes(adminKey: string): Buffer {
return Buffer.from(adminKey, 'base64');
2024-04-25 17:09:05 +00:00
}
export function fromAdminKeyBytes(adminKey: Uint8Array): string {
return Bytes.toBase64(adminKey);
}
2024-07-24 21:35:36 +00:00
export function toCallHistoryFromUnusedCallLink(
callLink: CallLinkType
): CallHistoryDetails {
return {
callId: generateUuid(),
peerId: callLink.roomId,
ringerId: null,
startedById: null,
2024-07-24 21:35:36 +00:00
mode: CallMode.Adhoc,
type: CallType.Adhoc,
direction: CallDirection.Incoming,
timestamp: Date.now(),
endedTimestamp: null,
2024-07-24 21:35:36 +00:00
status: AdhocCallStatus.Pending,
};
}