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';
|
|
|
|
|
|
|
|
import dataInterface from '../sql/Client';
|
2022-08-10 18:48:52 +00:00
|
|
|
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> {
|
2022-08-10 18:48:52 +00:00
|
|
|
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([
|
|
|
|
dataInterface.countStoryReadsByConversation(conversation.id),
|
|
|
|
dataInterface.getStoryCount(conversation.id),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return storyReads > 0 && storyCounts <= MAX_NUM_STORIES_TO_PREFETCH;
|
|
|
|
}
|