mattermost-mobile/app/actions/local/post.ts
Anurag Shivarathri 9dbdae22fd
[Gekidou MM-39707] CRT DB (#5948)
* Database init

* Naming fix

* naming misc

* Fix test

* Added Thread Tab columns, Team Threads Count table and other changes

* Test case fix

* Test cases fix ...... AGAIN

* TS fix

* Removed loaded_in_all_threads_tab, loaded_in_unreads_tab

* Removed TeamThreadsCount table, mention & message root counts & added loadedInGlobalThreads flag

* Type changes, added delete thread with post

* Removed unused type

* Reverted relationshio of post with thread

* Calling thread destroyPermanently from post

* Removed unused table name variables

* added THREAD constant table in post model and fixed a few comments

* Misc typo fix and code clean up

* Added test case and related to participant in user model

* test cases fix

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2022-03-03 22:47:29 +05:30

179 lines
5.4 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {postActionWithCookie} from '@actions/remote/post';
import {ActionType, Post} from '@constants';
import DatabaseManager from '@database/manager';
import {queryPostById} from '@queries/servers/post';
import {queryCurrentUserId} from '@queries/servers/system';
import {generateId} from '@utils/general';
import type PostModel from '@typings/database/models/servers/post';
import type UserModel from '@typings/database/models/servers/user';
export const sendAddToChannelEphemeralPost = async (serverUrl: string, user: UserModel, addedUsernames: string[], messages: string[], channeId: string, postRootId = '') => {
const operator = DatabaseManager.serverDatabases[serverUrl]?.operator;
if (!operator) {
return {error: `${serverUrl} database not found`};
}
const timestamp = Date.now();
const posts = addedUsernames.map((addedUsername, index) => {
const message = messages[index];
return {
id: generateId(),
user_id: user.id,
channel_id: channeId,
message,
type: Post.POST_TYPES.EPHEMERAL_ADD_TO_CHANNEL as PostType,
create_at: timestamp,
edit_at: 0,
update_at: timestamp,
delete_at: 0,
is_pinned: false,
original_id: '',
hashtags: '',
pending_post_id: '',
reply_count: 0,
metadata: {},
root_id: postRootId,
props: {
username: user.username,
addedUsername,
},
} as Post;
});
await operator.handlePosts({
actionType: ActionType.POSTS.RECEIVED_NEW,
order: posts.map((p) => p.id),
posts,
});
return {posts};
};
export const sendEphemeralPost = async (serverUrl: string, message: string, channeId: string, rootId = '', userId?: string) => {
const operator = DatabaseManager.serverDatabases[serverUrl]?.operator;
if (!operator) {
return {error: `${serverUrl} database not found`};
}
if (!channeId) {
return {error: 'channel Id not defined'};
}
let authorId = userId;
if (!authorId) {
authorId = await queryCurrentUserId(operator.database);
}
const timestamp = Date.now();
const post = {
id: generateId(),
user_id: authorId,
channel_id: channeId,
message,
type: Post.POST_TYPES.EPHEMERAL_ADD_TO_CHANNEL as PostType,
create_at: timestamp,
edit_at: 0,
update_at: timestamp,
delete_at: 0,
is_pinned: false,
original_id: '',
hashtags: '',
pending_post_id: '',
reply_count: 0,
metadata: {},
participants: null,
root_id: rootId,
props: {},
} as Post;
await operator.handlePosts({
actionType: ActionType.POSTS.RECEIVED_NEW,
order: [post.id],
posts: [post],
});
return {post};
};
export const removePost = async (serverUrl: string, post: PostModel | Post) => {
const operator = DatabaseManager.serverDatabases[serverUrl]?.operator;
if (!operator) {
return {error: `${serverUrl} database not found`};
}
if (post.type === Post.POST_TYPES.COMBINED_USER_ACTIVITY && post.props?.system_post_ids) {
const systemPostIds = post.props.system_post_ids as string[];
for await (const id of systemPostIds) {
const postModel = await queryPostById(operator.database, id);
if (postModel) {
await operator.database.write(async () => {
await postModel.destroyPermanently();
});
}
}
} else {
const postModel = await queryPostById(operator.database, post.id);
if (postModel) {
await operator.database.write(async () => {
await postModel.destroyPermanently();
});
}
}
return {post};
};
export const selectAttachmentMenuAction = (serverUrl: string, postId: string, actionId: string, selectedOption: string) => {
return postActionWithCookie(serverUrl, postId, actionId, '', selectedOption);
};
export const markPostAsDeleted = async (serverUrl: string, post: Post) => {
const operator = DatabaseManager.serverDatabases[serverUrl]?.operator;
if (!operator) {
return {error: `${serverUrl} database not found`};
}
const dbPost = await queryPostById(operator.database, post.id);
if (!dbPost) {
return {};
}
dbPost.prepareUpdate((p) => {
p.deleteAt = Date.now();
p.message = '';
p.metadata = null;
p.props = undefined;
});
operator.batchRecords([dbPost]);
return {post: dbPost};
};
export const processPostsFetched = async (serverUrl: string, actionType: string, data: PostResponse, fetchOnly = false) => {
const order = data.order;
const posts = Object.values(data.posts) as Post[];
const previousPostId = data.prev_post_id;
if (!fetchOnly) {
const operator = DatabaseManager.serverDatabases[serverUrl]?.operator;
if (operator) {
await operator.handlePosts({
actionType,
order,
posts,
previousPostId,
});
}
}
return {
posts,
order,
previousPostId,
};
};