2022-04-12 19:29:30 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import type { AttachmentType } from '../types/Attachment';
|
2022-04-25 17:25:50 +00:00
|
|
|
import {
|
2022-08-04 00:38:41 +00:00
|
|
|
hasFailed,
|
2022-04-25 17:25:50 +00:00
|
|
|
hasNotResolved,
|
|
|
|
isDownloaded,
|
|
|
|
isGIF,
|
|
|
|
isVideo,
|
|
|
|
} from '../types/Attachment';
|
2022-04-12 19:29:30 +00:00
|
|
|
import { count } from './grapheme';
|
|
|
|
import { SECOND } from './durations';
|
|
|
|
|
|
|
|
const DEFAULT_DURATION = 5 * SECOND;
|
|
|
|
const MAX_VIDEO_DURATION = 30 * SECOND;
|
|
|
|
|
|
|
|
export async function getStoryDuration(
|
|
|
|
attachment: AttachmentType
|
2022-04-25 17:25:50 +00:00
|
|
|
): Promise<number | undefined> {
|
2022-08-04 00:38:41 +00:00
|
|
|
if (hasFailed(attachment)) {
|
|
|
|
return DEFAULT_DURATION;
|
|
|
|
}
|
|
|
|
|
2022-04-25 17:25:50 +00:00
|
|
|
if (!isDownloaded(attachment) || hasNotResolved(attachment)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-12 19:29:30 +00:00
|
|
|
if (isGIF([attachment]) || isVideo([attachment])) {
|
|
|
|
const videoEl = document.createElement('video');
|
|
|
|
if (!attachment.url) {
|
|
|
|
return DEFAULT_DURATION;
|
|
|
|
}
|
|
|
|
videoEl.src = attachment.url;
|
|
|
|
|
|
|
|
await new Promise<void>(resolve => {
|
|
|
|
function resolveAndRemove() {
|
|
|
|
resolve();
|
|
|
|
videoEl.removeEventListener('loadedmetadata', resolveAndRemove);
|
|
|
|
}
|
|
|
|
|
|
|
|
videoEl.addEventListener('loadedmetadata', resolveAndRemove);
|
|
|
|
});
|
|
|
|
|
|
|
|
const duration = Math.ceil(videoEl.duration * SECOND);
|
|
|
|
|
|
|
|
if (isGIF([attachment])) {
|
|
|
|
// GIFs: Loop gifs 3 times or play for 5 seconds, whichever is longer.
|
|
|
|
return Math.min(
|
|
|
|
Math.max(duration * 3, DEFAULT_DURATION),
|
|
|
|
MAX_VIDEO_DURATION
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Video max duration: 30 seconds
|
|
|
|
return Math.min(duration, MAX_VIDEO_DURATION);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attachment.textAttachment && attachment.textAttachment.text) {
|
2022-08-11 21:03:56 +00:00
|
|
|
// Minimum 5 seconds. +1 second for every 15 characters past the first
|
2022-04-12 19:29:30 +00:00
|
|
|
// 15 characters (round up).
|
|
|
|
// For text stories that include a link, +2 seconds to the playback time.
|
|
|
|
const length = count(attachment.textAttachment.text);
|
|
|
|
const additionalSeconds = (Math.ceil(length / 15) - 1) * SECOND;
|
|
|
|
const linkPreviewSeconds = attachment.textAttachment.preview
|
|
|
|
? 2 * SECOND
|
|
|
|
: 0;
|
2022-08-11 21:03:56 +00:00
|
|
|
return DEFAULT_DURATION + additionalSeconds + linkPreviewSeconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attachment.caption) {
|
|
|
|
const length = count(attachment.caption);
|
|
|
|
const additionalSeconds = (Math.ceil(length / 15) - 1) * SECOND;
|
|
|
|
return DEFAULT_DURATION + additionalSeconds;
|
2022-04-12 19:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return DEFAULT_DURATION;
|
|
|
|
}
|