mattermost-mobile/app/queries/servers/post.ts
Daniel Espino García 55324127e1
[Gekidou] Post input (#5844)
* Initial commit post input

* Fix message posting, add create direct channel and minor fixes

* Fix "is typing" and "react to last post" behaviour

* Some reordering, better handling of upload error, properly clear draft on send message, and fix minor progress bar misbehavior

* Add keyboard listener for shift-enter, add selection between video or photo while attaching, add alert when trying to attach more than you are allowed, add paste functionality, minor fixes and reordering

* Add library patch

* Fix lint

* Address feedback

* Address feedback

* Add missing negation

* Check for group name and fix typo on draft comparisons

* Address feedback

* Address feedback

* Address feedback

* Address feedback

* Fix several bugs

* Remove @app imports

* Address feedback

* fix post list & post draft layout on iOS

* Fix post draft cursor position

* Fix file upload route

* Allow to pick multiple images using the image picker

* accurately get the channel member count

* remove android cursor workaround

* Remove local const INPUT_LINE_HEIGHT

* move getPlaceHolder out of the component

* use substring instead of legacy substr for hardward keyboard

* Move onAppStateChange above the effects

* Fix camera action bottom sheet

* no need to memo SendButton

* properly use memberCount in sender handler

* Refactor how to get memberCount

* Fix queryRecentPostsInThread

* Remove unused isDirectChannelVisible && isGroupChannelVisible util functions

* rename errorBadUser to errorUnkownUser

* extract localized strings

* use ClientErrorProps instead of ClientError

* Minor improvements

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2022-02-03 08:59:15 -03:00

115 lines
4 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Database, Model, Q, Query, Relation} from '@nozbe/watermelondb';
import {MM_TABLES} from '@constants/database';
import type PostModel from '@typings/database/models/servers/post';
import type PostInChannelModel from '@typings/database/models/servers/posts_in_channel';
import type PostsInThreadModel from '@typings/database/models/servers/posts_in_thread';
const {SERVER: {POST, POSTS_IN_CHANNEL, POSTS_IN_THREAD}} = MM_TABLES;
export const prepareDeletePost = async (post: PostModel): Promise<Model[]> => {
const preparedModels: Model[] = [post.prepareDestroyPermanently()];
const relations: Array<Relation<Model> | Query<Model>> = [post.drafts, post.postsInThread];
for await (const relation of relations) {
try {
const model = await relation.fetch();
if (model) {
if (Array.isArray(model)) {
model.forEach((m) => preparedModels.push(m.prepareDestroyPermanently()));
} else {
preparedModels.push(model.prepareDestroyPermanently());
}
}
} catch {
// Record not found, do nothing
}
}
const associatedChildren: Array<Query<any>> = [post.files, post.reactions];
for await (const children of associatedChildren) {
const models = await children.fetch() as Model[];
models.forEach((model) => preparedModels.push(model.prepareDestroyPermanently()));
}
return preparedModels;
};
export const queryPostById = async (database: Database, postId: string) => {
try {
const userRecord = (await database.collections.get(MM_TABLES.SERVER.POST).find(postId)) as PostModel;
return userRecord;
} catch {
return undefined;
}
};
export const queryPostsInChannel = (database: Database, channelId: string): Promise<PostInChannelModel[]> => {
try {
return database.get<PostInChannelModel>(POSTS_IN_CHANNEL).query(
Q.where('channel_id', channelId),
Q.sortBy('latest', Q.desc),
).fetch();
} catch {
return Promise.resolve([]);
}
};
export const queryPostsInThread = (database: Database, rootId: string): Promise<PostsInThreadModel[]> => {
try {
return database.get<PostsInThreadModel>(POSTS_IN_THREAD).query(
Q.where('root_id', rootId),
Q.sortBy('latest', Q.desc),
).fetch();
} catch {
return Promise.resolve([]);
}
};
export const queryRecentPostsInThread = async (database: Database, rootId: string): Promise<PostModel[]> => {
try {
const chunks = await queryPostsInThread(database, rootId);
if (chunks.length) {
const recent = chunks[0];
const post = await queryPostById(database, rootId);
if (post) {
return queryPostsChunk(database, post.channelId, recent.earliest, recent.latest);
}
}
return Promise.resolve([]);
} catch {
return Promise.resolve([]);
}
};
export const queryPostsChunk = (database: Database, channelId: string, earliest: number, latest: number): Promise<PostModel[]> => {
try {
return database.get(POST).query(
Q.and(
Q.where('channel_id', channelId),
Q.where('create_at', Q.between(earliest, latest)),
Q.where('delete_at', Q.eq(0)),
),
Q.sortBy('create_at', Q.desc),
).fetch() as Promise<PostModel[]>;
} catch {
return Promise.resolve([]);
}
};
export const queryRecentPostsInChannel = async (database: Database, channelId: string): Promise<PostModel[]> => {
try {
const chunks = await queryPostsInChannel(database, channelId);
if (chunks.length) {
const recent = chunks[0];
return queryPostsChunk(database, channelId, recent.earliest, recent.latest);
}
return Promise.resolve([]);
} catch {
return Promise.resolve([]);
}
};