mattermost-mobile/app/database/operator/utils/post.ts
Elias Nahum 2621d921d6
[Gekidou] Performance & UI fixes (#6122)
* Fix search bar style

* MM-42983 Align mention badge on channel list item

* MM-42985 polish channel intro UI

* MM-42984 update fonts on 'Show' bottom sheet

* MM-42982 Increase tap area of back button on channel view

* Set StatusBar style based on theme sidebarBg

* Reconnect other WS after 20 seconds insted of 5

* Only fetch missing profiles for DM/GMs

* Database prepare records optimization

* Do not use fallbackUsername for storing GM/DM in the database

* fix filter for fetching missing dm/gm

* remove use of indexOf inside another loop

* remove use of includes inside another loop

* remove use of find inside another loop

* Add missing translations

* disable Flipper on iOS

* Remove Flipper code from AppDelegate

* feedback review

* trim DM display name to make sure is not empty

* fixing missing direct channels display name

* UI/UX feedback

* fix WS channel viewed and mark as unread

* Remove duplicate emojis from reaction bar
2022-04-04 17:09:26 -04:00

72 lines
2.2 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import type {ChainPostsArgs, 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 {Post[]} sanitizePosts.posts
* @param {string[]} sanitizePosts.orders
*/
export const sanitizePosts = ({posts, orders}: SanitizePostsArgs) => {
const orderedPosts: Post[] = [];
const unOrderedPosts: Post[] = [];
const ordersSet = new Set(orders);
posts.forEach((post) => {
if (post?.id && ordersSet.has(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.order
* @param {Post[]} chainPosts.posts
* @param {string} chainPosts.previousPostId
* @returns {Post[]}
*/
export const createPostsChain = ({order, posts, previousPostId = ''}: ChainPostsArgs) => {
const postsByIds = posts.reduce((result: Record<string, Post>, p) => {
result[p.id] = p;
return result;
}, {});
return order.reduce((result, id, index) => {
const post = postsByIds[id];
if (post) {
if (index === order.length - 1) {
result.push({...post, prev_post_id: previousPostId});
} else {
result.push({...post, prev_post_id: order[index + 1]});
}
}
return result;
}, [] as Post[]).reverse();
};
export const getPostListEdges = (posts: Post[]) => {
// Sort a clone of 'posts' array by create_at
const sortedPosts = [...posts].sort((a, b) => {
return a.create_at - b.create_at;
});
// The first element (beginning of chain)
const firstPost = sortedPosts[0];
const lastPost = sortedPosts[sortedPosts.length - 1];
return {firstPost, lastPost};
};