forked from Ivasoft/mattermost-mobile
[Gekidou] fix database schema and models (#5553)
* fix database schema and models * fix types
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
import type {ChainPostsArgs, RecordPair, SanitizePostsArgs} from '@typings/database/database';
|
||||
import type {ChainPostsArgs, SanitizePostsArgs} from '@typings/database/database';
|
||||
|
||||
/**
|
||||
* sanitizePosts: Creates arrays of ordered and unordered posts. Unordered posts are those posts that are not
|
||||
@@ -32,29 +32,36 @@ export const sanitizePosts = ({posts, orders}: SanitizePostsArgs) => {
|
||||
* 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 {Post[]} chainPosts.rawPosts
|
||||
* @param {string[]} chainPosts.order
|
||||
* @param {Post[]} chainPosts.posts
|
||||
* @param {string} chainPosts.previousPostId
|
||||
* @returns {Post[]}
|
||||
*/
|
||||
export const createPostsChain = ({orders, rawPosts, previousPostId = ''}: ChainPostsArgs) => {
|
||||
const posts: RecordPair[] = [];
|
||||
export const createPostsChain = ({order, posts, previousPostId = ''}: ChainPostsArgs) => {
|
||||
return order.reduce((result, id, index) => {
|
||||
const post = posts.find((p) => p.id === id);
|
||||
|
||||
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]}});
|
||||
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[]);
|
||||
};
|
||||
|
||||
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;
|
||||
});
|
||||
|
||||
return posts;
|
||||
// The first element (beginning of chain)
|
||||
const firstPost = sortedPosts[0];
|
||||
const lastPost = sortedPosts[sortedPosts.length - 1];
|
||||
|
||||
return {firstPost, lastPost};
|
||||
};
|
||||
|
||||
@@ -17,7 +17,7 @@ const {REACTION} = MM_TABLES.SERVER;
|
||||
* @param {Database} sanitizeReactions.database
|
||||
* @param {string} sanitizeReactions.post_id
|
||||
* @param {RawReaction[]} sanitizeReactions.rawReactions
|
||||
* @returns {Promise<{createReactions: RawReaction[], createEmojis: {name: string}[], deleteReactions: Reaction[]}>}
|
||||
* @returns {Promise<{createReactions: RawReaction[], deleteReactions: Reaction[]}>}
|
||||
*/
|
||||
export const sanitizeReactions = async ({database, post_id, rawReactions}: SanitizeReactionsArgs) => {
|
||||
const reactions = (await database.collections.
|
||||
@@ -30,28 +30,18 @@ export const sanitizeReactions = async ({database, post_id, rawReactions}: Sanit
|
||||
|
||||
const createReactions: RecordPair[] = [];
|
||||
|
||||
const emojiSet = new Set();
|
||||
|
||||
for (let i = 0; i < rawReactions.length; i++) {
|
||||
const rawReaction = rawReactions[i];
|
||||
const raw = rawReactions[i];
|
||||
|
||||
// Do we have a similar value of rawReaction in the REACTION table?
|
||||
const idxPresent = reactions.findIndex((value) => {
|
||||
return (
|
||||
value.userId === rawReaction.user_id &&
|
||||
value.emojiName === rawReaction.emoji_name
|
||||
);
|
||||
});
|
||||
// If the reaction is not present let's add it to the db
|
||||
const exists = reactions.find((r) => (
|
||||
r.userId === raw.user_id &&
|
||||
r.emojiName === raw.emoji_name));
|
||||
|
||||
if (idxPresent === -1) {
|
||||
// So, we don't have a similar Reaction object. That one is new...so we'll create it
|
||||
createReactions.push({record: undefined, raw: rawReaction});
|
||||
|
||||
// If that reaction is new, that implies that the emoji might also be new
|
||||
emojiSet.add(rawReaction.emoji_name);
|
||||
if (exists) {
|
||||
similarObjects.push(exists);
|
||||
} else {
|
||||
// we have a similar object in both reactions and rawReactions; we'll pop it out from both arrays
|
||||
similarObjects.push(reactions[idxPresent]);
|
||||
createReactions.push({raw});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,9 +50,5 @@ export const sanitizeReactions = async ({database, post_id, rawReactions}: Sanit
|
||||
filter((reaction) => !similarObjects.includes(reaction)).
|
||||
map((outCast) => outCast.prepareDestroyPermanently());
|
||||
|
||||
const createEmojis = Array.from(emojiSet).map((emoji) => {
|
||||
return {name: emoji};
|
||||
});
|
||||
|
||||
return {createReactions, createEmojis, deleteReactions};
|
||||
return {createReactions, deleteReactions};
|
||||
};
|
||||
|
||||
@@ -22,46 +22,46 @@ describe('DataOperator: Utils tests', () => {
|
||||
it('=> createPostsChain: should link posts amongst each other based on order array', () => {
|
||||
const previousPostId = 'prev_xxyuoxmehne';
|
||||
const chainedOfPosts = createPostsChain({
|
||||
orders: mockedPosts.order,
|
||||
rawPosts: Object.values(mockedPosts.posts) as Post[],
|
||||
order: mockedPosts.order,
|
||||
posts: Object.values(mockedPosts.posts) as Post[],
|
||||
previousPostId,
|
||||
});
|
||||
|
||||
// eslint-disable-next-line max-nested-callbacks
|
||||
const post1 = chainedOfPosts.find((post) => {
|
||||
const p = post.raw as Post;
|
||||
const p = post;
|
||||
return p.id === '8swgtrrdiff89jnsiwiip3y1eoe';
|
||||
})?.raw as Post;
|
||||
});
|
||||
|
||||
expect(post1).toBeTruthy();
|
||||
expect(post1?.prev_post_id).toBe(previousPostId);
|
||||
expect(post1?.prev_post_id).toBe('8fcnk3p1jt8mmkaprgajoxz115a');
|
||||
|
||||
// eslint-disable-next-line max-nested-callbacks
|
||||
const post2 = chainedOfPosts.find((post) => {
|
||||
const p = post.raw as Post;
|
||||
const p = post;
|
||||
return p.id === '8fcnk3p1jt8mmkaprgajoxz115a';
|
||||
})?.raw as Post;
|
||||
});
|
||||
|
||||
expect(post2).toBeTruthy();
|
||||
expect(post2!.prev_post_id).toBe('8swgtrrdiff89jnsiwiip3y1eoe');
|
||||
expect(post2!.prev_post_id).toBe('3y3w3a6gkbg73bnj3xund9o5ic');
|
||||
|
||||
// eslint-disable-next-line max-nested-callbacks
|
||||
const post3 = chainedOfPosts.find((post) => {
|
||||
const p = post.raw as Post;
|
||||
const p = post;
|
||||
return p.id === '3y3w3a6gkbg73bnj3xund9o5ic';
|
||||
})?.raw as Post;
|
||||
});
|
||||
|
||||
expect(post3).toBeTruthy();
|
||||
expect(post3?.prev_post_id).toBe('8fcnk3p1jt8mmkaprgajoxz115a');
|
||||
expect(post3?.prev_post_id).toBe('4btbnmticjgw7ewd3qopmpiwqw');
|
||||
|
||||
// eslint-disable-next-line max-nested-callbacks
|
||||
const post4 = chainedOfPosts.find((post) => {
|
||||
const p = post.raw as Post;
|
||||
const p = post;
|
||||
return p.id === '4btbnmticjgw7ewd3qopmpiwqw';
|
||||
})?.raw as Post;
|
||||
});
|
||||
|
||||
expect(post4).toBeTruthy();
|
||||
expect(post4!.prev_post_id).toBe('3y3w3a6gkbg73bnj3xund9o5ic');
|
||||
expect(post4!.prev_post_id).toBe(previousPostId);
|
||||
});
|
||||
|
||||
it('=> sanitizeReactions: should triage between reactions that needs creation/deletion and emojis to be created', async () => {
|
||||
@@ -76,14 +76,17 @@ describe('DataOperator: Utils tests', () => {
|
||||
|
||||
// we commit one Reaction to our database
|
||||
const prepareRecords = await server?.operator.handleReactions({
|
||||
reactions: [
|
||||
{
|
||||
user_id: 'beqkgo4wzbn98kjzjgc1p5n91o',
|
||||
post_id: '8ww8kb1dbpf59fu4d5xhu5nf5w',
|
||||
emoji_name: 'tada_will_be_removed',
|
||||
create_at: 1601558322701,
|
||||
},
|
||||
],
|
||||
postsReactions: [{
|
||||
post_id: '8ww8kb1dbpf59fu4d5xhu5nf5w',
|
||||
reactions: [
|
||||
{
|
||||
user_id: 'beqkgo4wzbn98kjzjgc1p5n91o',
|
||||
post_id: '8ww8kb1dbpf59fu4d5xhu5nf5w',
|
||||
emoji_name: 'tada_will_be_removed',
|
||||
create_at: 1601558322701,
|
||||
},
|
||||
],
|
||||
}],
|
||||
prepareRecordsOnly: true,
|
||||
}) as ReactionModel[];
|
||||
|
||||
@@ -95,7 +98,6 @@ describe('DataOperator: Utils tests', () => {
|
||||
|
||||
const {
|
||||
createReactions,
|
||||
createEmojis,
|
||||
deleteReactions,
|
||||
} = await sanitizeReactions({
|
||||
database: server!.database,
|
||||
@@ -108,7 +110,5 @@ describe('DataOperator: Utils tests', () => {
|
||||
expect(deleteReactions[0].emojiName).toBe('tada_will_be_removed');
|
||||
|
||||
expect(createReactions.length).toBe(3);
|
||||
|
||||
expect(createEmojis.length).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user