signal-desktop/ts/util/shouldDownloadStory.ts

30 lines
810 B
TypeScript
Raw Normal View History

2022-03-29 01:10:08 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { ConversationAttributesType } from '../model-types.d';
2024-07-22 18:16:33 +00:00
import { DataReader } from '../sql/Client';
import { isMe } from './whatTypeOfConversation';
2022-03-29 01:10:08 +00:00
const MAX_NUM_STORIES_TO_PREFETCH = 5;
export async function shouldDownloadStory(
conversation: ConversationAttributesType
): Promise<boolean> {
if (isMe(conversation)) {
return true;
}
// We download the first time the user has posted a story
2022-03-29 01:10:08 +00:00
if (!conversation.hasPostedStory) {
return true;
}
const [storyReads, storyCounts] = await Promise.all([
2024-07-22 18:16:33 +00:00
DataReader.countStoryReadsByConversation(conversation.id),
DataReader.getStoryCount(conversation.id),
2022-03-29 01:10:08 +00:00
]);
return storyReads > 0 && storyCounts <= MAX_NUM_STORIES_TO_PREFETCH;
}