forked from Ivasoft/mattermost-mobile
* MM_30482: Imported database and types /database folder * MM_30482: Imported database and types /database folder * MM_30482 : All tests are passing * MM_30482 : Updating patch package for watermelon db * MM_30482 : Fixing CI issue * MM_30482 : Updating TS complaint * Update index.ts * MM_30482 : Code clean up Co-authored-by: Avinash Lingaloo <>
61 lines
2.1 KiB
TypeScript
61 lines
2.1 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import {ChainPostsArgs, RawPost, RecordPair, SanitizePostsArgs} from '@typings/database/database';
|
|
|
|
/**
|
|
* sanitizePosts: Creates arrays of ordered and unordered posts. Unordered posts are those posts that are not
|
|
* present in the orders array
|
|
* @param {SanitizePostsArgs} sanitizePosts
|
|
* @param {RawPost[]} sanitizePosts.posts
|
|
* @param {string[]} sanitizePosts.orders
|
|
*/
|
|
export const sanitizePosts = ({posts, orders}: SanitizePostsArgs) => {
|
|
const orderedPosts: RawPost[] = [];
|
|
const unOrderedPosts: RawPost[] = [];
|
|
|
|
posts.forEach((post) => {
|
|
if (post?.id && orders.includes(post.id)) {
|
|
orderedPosts.push(post);
|
|
} else {
|
|
unOrderedPosts.push(post);
|
|
}
|
|
});
|
|
|
|
return {
|
|
postsOrdered: orderedPosts,
|
|
postsUnordered: unOrderedPosts,
|
|
};
|
|
};
|
|
|
|
/**
|
|
* createPostsChain: Basically creates the 'chain of posts' using the 'orders' array; each post is linked to the other
|
|
* by the previous_post_id field.
|
|
* @param {ChainPostsArgs} chainPosts
|
|
* @param {string[]} chainPosts.orders
|
|
* @param {RawPost[]} chainPosts.rawPosts
|
|
* @param {string} chainPosts.previousPostId
|
|
* @returns {RawPost[]}
|
|
*/
|
|
export const createPostsChain = ({orders, rawPosts, previousPostId = ''}: ChainPostsArgs) => {
|
|
const posts: RecordPair[] = [];
|
|
|
|
rawPosts.forEach((post) => {
|
|
const postId = post.id;
|
|
const orderIndex = orders.findIndex((order) => {
|
|
return order === postId;
|
|
});
|
|
|
|
if (orderIndex === -1) {
|
|
// This case will not occur as we are using 'ordered' posts for this step. However, if this happens, that
|
|
// implies that we might be dealing with an unordered post and in which case we do not action on it.
|
|
} else if (orderIndex === 0) {
|
|
posts.push({record: undefined, raw: {...post, prev_post_id: previousPostId}});
|
|
} else {
|
|
posts.push({record: undefined, raw: {...post, prev_post_id: orders[orderIndex - 1]}});
|
|
}
|
|
});
|
|
|
|
return posts;
|
|
};
|