Files
mattermost-mobile/app/database/models/server/thread.ts
Kyriakos Z 3326f34933 [Gekidou HOTFIX] fixes saving threads in the DB (#6132)
* [Gekidou HOTFIX] fixes saving threads in the DB

`loadedInGlobalThreads` cannot be a property of the thread, the DB will
fail with unknown column upon saving.

This commit makes it an argument of handleThreads and
handleThreadInTeam.

Fixes wrong foreignKey in THREAD table associations.

* Fixes errors

* Fixes error
2022-04-06 17:37:17 +03:00

73 lines
3.0 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: 'thread_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;
/** viewed_at : The timestamp showing when the user's last opened this thread (this is used for the new line message indicator) */
@field('viewed_at') viewedAt!: 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();
}
}