mirror of
https://github.com/mattermost/mattermost-mobile.git
synced 2026-11-13 22:50:19 +00:00
84 lines
3.5 KiB
TypeScript
84 lines
3.5 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import {chunk} from 'lodash';
|
|
|
|
import {prepareModelsForChannelPosts} from '@actions/local/post';
|
|
import {ActionType} from '@constants';
|
|
import DatabaseManager from '@database/manager';
|
|
import {removeDuplicatesModels} from '@helpers/database';
|
|
|
|
import {fetchPostAuthors, fetchPostsForChannel} from './post';
|
|
|
|
import type {Model} from '@nozbe/watermelondb';
|
|
|
|
/**
|
|
* processChannelPostsByTeam is to fetch posts for each channel in a team, and prepare the models for the posts.
|
|
* Then once all the models are prepared, write them into the database all at once.
|
|
* Writing posts channel by channel is time consuming, and has caused the UI to lock up significantly.
|
|
* @param serverUrl string the server URL
|
|
* @param channelIds string[]
|
|
* @param skipAuthors boolean
|
|
* @param groupLabel string
|
|
* @param isCRTEnabled boolean
|
|
*/
|
|
export async function processChannelPostsByTeam(
|
|
serverUrl: string,
|
|
channelIds: string[],
|
|
skipAuthors = false,
|
|
groupLabel?: RequestGroupLabel,
|
|
isCRTEnabled = false,
|
|
): Promise<void> {
|
|
const prepareModelsPromises: Array<Promise<Model[]>> = [];
|
|
const allPosts: Post[] = [];
|
|
|
|
const chunks = chunk(channelIds, 10);
|
|
for await (const channelIdsChunk of chunks) {
|
|
const channelPromises = channelIdsChunk.map((channelId) =>
|
|
|
|
// hard-coding true for skipAuthors because we want to fetch authors
|
|
// and upsert later to avoid unique constraint errors
|
|
fetchPostsForChannel(serverUrl, channelId, true, true, groupLabel),
|
|
);
|
|
const chunkResults = await Promise.allSettled(channelPromises);
|
|
|
|
for (const [i, result] of chunkResults.entries()) {
|
|
if (result.status === 'fulfilled') {
|
|
const {posts, order, previousPostId, authors, actionType} = result.value;
|
|
if (posts?.length) {
|
|
const channelId = channelIdsChunk[i];
|
|
allPosts.push(...posts);
|
|
prepareModelsPromises.push(
|
|
prepareModelsForChannelPosts(
|
|
serverUrl,
|
|
channelId,
|
|
posts,
|
|
|
|
// Use the action type fetchPostsForChannel resolved (RECEIVED_SINCE for a
|
|
// since-fetch, RECEIVED_IN_CHANNEL for an initial page). A since-fetch is a
|
|
// contiguous continuation of the existing chunk, so it must EXTEND it rather
|
|
// than create a disjoint interval — otherwise posts newer than the chunk
|
|
// create an orphaned interval that can hide the channel (MM-66467).
|
|
actionType ?? ActionType.POSTS.RECEIVED_IN_CHANNEL,
|
|
order || [],
|
|
previousPostId || '',
|
|
authors || [],
|
|
isCRTEnabled,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (prepareModelsPromises.length) {
|
|
const {operator} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
|
|
|
|
const models = await Promise.all(prepareModelsPromises);
|
|
await operator.batchRecords(removeDuplicatesModels(models.flat()), 'processTeamChannels');
|
|
}
|
|
if (!skipAuthors && allPosts.length) {
|
|
await fetchPostAuthors(serverUrl, allPosts, false, groupLabel);
|
|
}
|
|
}
|