mirror of
https://github.com/mattermost/mattermost-mobile.git
synced 2026-11-23 03:48:16 +00:00
* add SIP outbound phone calls from tel: links Tapping a tel: link can now dial the number through Mattermost when the server enables SIP outbound, reusing the LiveKit call flow. The user can still choose the device's phone app instead. * show the dialed number and call outcome during SIP phone calls The call screen and call bar now show the dialed number instead of the Calls bot, with ringback until the phone answers. The caller is alerted when the call goes unanswered, is declined or fails. * add phone call card and profile phone dialing for SIP calls phone call posts get their own card with "Call again", and users can dial the phone numbers on a person's profile from the DM header and the profile. * validate phone call responses, retry failed phone attribute fetches, and await sheet dismissal * wait for the bottom sheet to close in phone dialing tests * show phone call card as ended once the call is gone but the post has no end_at yet * merge both card state functions into one * adds log for handleCallUserJoined early return * alert when placing a phone call while already in one
102 lines
2.9 KiB
TypeScript
102 lines
2.9 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||
// See LICENSE.txt for license information.
|
||
|
||
export type PhoneNumberMatch = {
|
||
index: number;
|
||
length: number;
|
||
raw: string;
|
||
href: string;
|
||
}
|
||
|
||
// tel:/E.164 (+ and 7–15 digits) or NANP grouped numbers (separators required).
|
||
const PHONE_NUMBER_PATTERN = /(?:tel:(?:\+[1-9](?:[\s.\-()]*\d){6,14}|[+]?(?:[\s.\-()]*\d){7,15})|\+[1-9](?:[\s.\-()]*\d){6,14}|(?:\+?1[\s.\-]?)?(?:\(\d{3}\)[\s.\-]?|\d{3}[\s.\-])\d{3}[\s.\-]\d{4})/i;
|
||
|
||
const MIN_PHONE_DIGITS = 7;
|
||
const MAX_PHONE_DIGITS = 15;
|
||
|
||
// Hyphens and dots continue a digit run (`+123456789012345-6`). Spaces and
|
||
// parentheses are left as boundaries so a following number still matches.
|
||
const PHONE_CONTINUATION = /[.\-]/;
|
||
|
||
export function isTelHref(href: string): boolean {
|
||
return href.toLowerCase().startsWith('tel:');
|
||
}
|
||
|
||
export function getTelNumber(href: string): string {
|
||
const [rawNumber] = href.replace(/^tel:/i, '').split(';');
|
||
try {
|
||
return decodeURIComponent(rawNumber).trim();
|
||
} catch {
|
||
return rawNumber.trim();
|
||
}
|
||
}
|
||
|
||
export function toTelHref(raw: string): string {
|
||
const withoutScheme = raw.replace(/^tel:/i, '');
|
||
const hasPlus = withoutScheme.trimStart().startsWith('+');
|
||
const digits = withoutScheme.replace(/\D/g, '');
|
||
return `tel:${hasPlus ? '+' : ''}${digits}`;
|
||
}
|
||
|
||
function isDigit(char: string) {
|
||
return char >= '0' && char <= '9';
|
||
}
|
||
|
||
function isValidPhoneBoundary(text: string, index: number, direction: -1 | 1) {
|
||
const start = direction === -1 ? index - 1 : index;
|
||
if (start < 0 || start >= text.length) {
|
||
return true;
|
||
}
|
||
|
||
if ((/[0-9A-Za-z]/).test(text.charAt(start))) {
|
||
return false;
|
||
}
|
||
|
||
let i = start;
|
||
while (i >= 0 && i < text.length && PHONE_CONTINUATION.test(text.charAt(i))) {
|
||
i += direction;
|
||
}
|
||
|
||
if (i < 0 || i >= text.length) {
|
||
return true;
|
||
}
|
||
|
||
return !isDigit(text.charAt(i));
|
||
}
|
||
|
||
function isValidDigitCount(href: string): boolean {
|
||
const digits = href.replace(/^tel:\+?/, '');
|
||
return digits.length >= MIN_PHONE_DIGITS && digits.length <= MAX_PHONE_DIGITS;
|
||
}
|
||
|
||
export function findPhoneNumbers(text: string): PhoneNumberMatch[] {
|
||
if (!text) {
|
||
return [];
|
||
}
|
||
|
||
const matches: PhoneNumberMatch[] = [];
|
||
const regex = new RegExp(PHONE_NUMBER_PATTERN.source, 'gi');
|
||
let result = regex.exec(text);
|
||
|
||
while (result) {
|
||
const raw = result[0];
|
||
const index = result.index;
|
||
|
||
if (isValidPhoneBoundary(text, index, -1) && isValidPhoneBoundary(text, index + raw.length, 1)) {
|
||
const href = toTelHref(raw);
|
||
if (isValidDigitCount(href)) {
|
||
matches.push({
|
||
index,
|
||
length: raw.length,
|
||
raw,
|
||
href,
|
||
});
|
||
}
|
||
}
|
||
|
||
result = regex.exec(text);
|
||
}
|
||
|
||
return matches;
|
||
}
|