forked from Ivasoft/mattermost-mobile
* Refactors thread and threads_in_team tables The convention is that only threads being in the ThreadsInTeam will be shown in a thread list, and that only threads marked as loaded_in_global_threads are being shown in the All threads tab in the list. So when a thread arrives through different means, whether it's a WS event, or just fetching a (*new) thread by opening it in a channel, etc... we'll need to check if it's newer than any of the existing threads in the all threads list. If it is it will be added in the ThreadsInTeam and will be marked as loaded_in_global_threads: true. If it's not newer but it is an unread thread it will still be added in the ThreadsInTeam but marked as loaded_in_global_threads: false. This commit refactors `loaded_in_global_threads` field from the Thread table to the ThreadsInTeam table so that the above is possible. * Update tests * Addresses review comments
70 lines
2.9 KiB
TypeScript
70 lines
2.9 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import {Query, Relation} from '@nozbe/watermelondb';
|
|
import {children, field, immutableRelation} from '@nozbe/watermelondb/decorators';
|
|
import Model, {Associations} from '@nozbe/watermelondb/Model';
|
|
|
|
import {MM_TABLES} from '@constants/database';
|
|
|
|
import type PostModel from '@typings/database/models/servers/post';
|
|
import type ThreadModelInterface from '@typings/database/models/servers/thread';
|
|
import type ThreadInTeamModel from '@typings/database/models/servers/thread_in_team';
|
|
import type ThreadParticipantModel from '@typings/database/models/servers/thread_participant';
|
|
|
|
const {POST, THREAD, THREAD_PARTICIPANT, THREADS_IN_TEAM} = MM_TABLES.SERVER;
|
|
|
|
/**
|
|
* The Thread model contains thread information of a post.
|
|
*/
|
|
export default class ThreadModel extends Model implements ThreadModelInterface {
|
|
/** table (name) : Thread */
|
|
static table = THREAD;
|
|
|
|
/** associations : Describes every relationship to this table. */
|
|
static associations: Associations = {
|
|
|
|
/** A THREAD is associated to one POST (relationship is 1:1) */
|
|
[POST]: {type: 'belongs_to', key: 'id'},
|
|
|
|
/** A THREAD can have multiple THREAD_PARTICIPANT. (relationship is 1:N)*/
|
|
[THREAD_PARTICIPANT]: {type: 'has_many', foreignKey: 'thread_id'},
|
|
|
|
/** A THREAD can have multiple THREADS_IN_TEAM. (relationship is 1:N)*/
|
|
[THREADS_IN_TEAM]: {type: 'has_many', foreignKey: 'team_id'},
|
|
};
|
|
|
|
/** last_reply_at : The timestamp of when user last replied to the thread. */
|
|
@field('last_reply_at') lastReplyAt!: number;
|
|
|
|
/** last_viewed_at : The timestamp of when user last viewed the thread. */
|
|
@field('last_viewed_at') lastViewedAt!: number;
|
|
|
|
/** reply_count : The total replies to the thread by all the participants. */
|
|
@field('reply_count') replyCount!: number;
|
|
|
|
/** is_following: If user is following the thread or not */
|
|
@field('is_following') isFollowing!: boolean;
|
|
|
|
/** unread_replies : The number of replies that have not been read by the user. */
|
|
@field('unread_replies') unreadReplies!: number;
|
|
|
|
/** unread_mentions : The number of mentions that have not been read by the user. */
|
|
@field('unread_mentions') unreadMentions!: number;
|
|
|
|
/** participants : All the participants associated with this Thread */
|
|
@children(THREAD_PARTICIPANT) participants!: Query<ThreadParticipantModel>;
|
|
|
|
/** threadsInTeam : All the threadsInTeam associated with this Thread */
|
|
@children(THREADS_IN_TEAM) threadsInTeam!: Query<ThreadInTeamModel>;
|
|
|
|
/** post : The root post of this thread */
|
|
@immutableRelation(POST, 'id') post!: Relation<PostModel>;
|
|
|
|
async destroyPermanently() {
|
|
await this.participants.destroyAllPermanently();
|
|
await this.threadsInTeam.destroyAllPermanently();
|
|
super.destroyPermanently();
|
|
}
|
|
}
|