2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2021-05-03 23:24:40 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import humanizeDuration from 'humanize-duration';
|
2022-09-09 18:35:00 +00:00
|
|
|
import type { Unit } from 'humanize-duration';
|
|
|
|
import { isNumber } from 'lodash';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LocalizerType } from '../types/Util';
|
2022-11-16 20:18:02 +00:00
|
|
|
import { SECOND, DurationInSeconds } from './durations';
|
2021-05-03 23:24:40 +00:00
|
|
|
|
|
|
|
const SECONDS_PER_WEEK = 604800;
|
2022-11-16 20:18:02 +00:00
|
|
|
export const DEFAULT_DURATIONS_IN_SECONDS: ReadonlyArray<DurationInSeconds> = [
|
|
|
|
DurationInSeconds.ZERO,
|
|
|
|
DurationInSeconds.fromWeeks(4),
|
|
|
|
DurationInSeconds.fromWeeks(1),
|
|
|
|
DurationInSeconds.fromDays(1),
|
|
|
|
DurationInSeconds.fromHours(8),
|
|
|
|
DurationInSeconds.fromHours(1),
|
|
|
|
DurationInSeconds.fromMinutes(5),
|
|
|
|
DurationInSeconds.fromSeconds(30),
|
2021-05-03 23:24:40 +00:00
|
|
|
];
|
|
|
|
|
2022-11-16 20:18:02 +00:00
|
|
|
export const DEFAULT_DURATIONS_SET: ReadonlySet<DurationInSeconds> = new Set(
|
2021-06-01 20:45:43 +00:00
|
|
|
DEFAULT_DURATIONS_IN_SECONDS
|
|
|
|
);
|
|
|
|
|
|
|
|
export type FormatOptions = {
|
|
|
|
capitalizeOff?: boolean;
|
2022-09-09 18:35:00 +00:00
|
|
|
largest?: number; // how many units to show (the largest n)
|
2021-06-01 20:45:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export function format(
|
|
|
|
i18n: LocalizerType,
|
2022-11-16 20:18:02 +00:00
|
|
|
dirtySeconds?: DurationInSeconds,
|
2022-09-09 18:35:00 +00:00
|
|
|
{ capitalizeOff = false, largest }: FormatOptions = {}
|
2021-06-01 20:45:43 +00:00
|
|
|
): string {
|
2021-05-03 23:24:40 +00:00
|
|
|
let seconds = Math.abs(dirtySeconds || 0);
|
|
|
|
if (!seconds) {
|
2023-03-30 00:03:25 +00:00
|
|
|
return capitalizeOff
|
|
|
|
? i18n('icu:off')
|
|
|
|
: i18n('icu:disappearingMessages__off');
|
2021-05-03 23:24:40 +00:00
|
|
|
}
|
|
|
|
seconds = Math.max(Math.floor(seconds), 1);
|
|
|
|
|
2022-09-21 23:57:44 +00:00
|
|
|
// locale strings coming from electron use a dash as separator
|
|
|
|
// but humanizeDuration uses an underscore
|
2022-09-27 21:01:06 +00:00
|
|
|
const locale: string = i18n.getLocale().replace(/-/g, '_');
|
2022-09-21 23:57:44 +00:00
|
|
|
|
2021-05-03 23:24:40 +00:00
|
|
|
const localeWithoutRegion: string = locale.split('_', 1)[0];
|
|
|
|
const fallbacks: Array<string> = [];
|
|
|
|
if (localeWithoutRegion !== locale) {
|
|
|
|
fallbacks.push(localeWithoutRegion);
|
|
|
|
}
|
|
|
|
if (localeWithoutRegion === 'nb' || localeWithoutRegion === 'nn') {
|
|
|
|
fallbacks.push('no');
|
|
|
|
}
|
|
|
|
if (localeWithoutRegion !== 'en') {
|
|
|
|
fallbacks.push('en');
|
|
|
|
}
|
|
|
|
|
2022-09-27 21:01:06 +00:00
|
|
|
// humanizeDuration only supports zh_CN and zh_TW
|
|
|
|
if (locale === 'zh_HK') {
|
|
|
|
fallbacks.push('zh_TW');
|
|
|
|
}
|
|
|
|
|
2022-09-09 18:35:00 +00:00
|
|
|
const allUnits: Array<Unit> = ['y', 'mo', 'w', 'd', 'h', 'm', 's'];
|
|
|
|
|
|
|
|
const defaultUnits: Array<Unit> =
|
|
|
|
seconds % SECONDS_PER_WEEK === 0 ? ['w'] : ['d', 'h', 'm', 's'];
|
|
|
|
|
2022-11-16 20:18:02 +00:00
|
|
|
return humanizeDuration(seconds * SECOND, {
|
2023-01-01 11:41:40 +00:00
|
|
|
// if we have an explicit `largest` specified,
|
2022-09-09 18:35:00 +00:00
|
|
|
// allow it to pick from all the units
|
|
|
|
units: largest ? allUnits : defaultUnits,
|
|
|
|
largest,
|
2021-05-03 23:24:40 +00:00
|
|
|
language: locale,
|
|
|
|
...(fallbacks.length ? { fallbacks } : {}),
|
|
|
|
});
|
|
|
|
}
|
2022-09-09 18:35:00 +00:00
|
|
|
|
|
|
|
// normally we would not have undefineds all over,
|
|
|
|
// but most use-cases start out with undefineds
|
|
|
|
export function calculateExpirationTimestamp({
|
|
|
|
expireTimer,
|
|
|
|
expirationStartTimestamp,
|
|
|
|
}: {
|
2023-01-05 21:58:13 +00:00
|
|
|
expireTimer?: DurationInSeconds | null;
|
|
|
|
expirationStartTimestamp?: number | null;
|
2022-09-09 18:35:00 +00:00
|
|
|
}): number | undefined {
|
|
|
|
return isNumber(expirationStartTimestamp) && isNumber(expireTimer)
|
2022-11-16 20:18:02 +00:00
|
|
|
? expirationStartTimestamp + DurationInSeconds.toMillis(expireTimer)
|
2022-09-09 18:35:00 +00:00
|
|
|
: undefined;
|
|
|
|
}
|