[Gekidou] Sidebar Categories (Database only) (#5909)

This commit is contained in:
Shaz Amjad
2022-02-01 00:30:10 +11:00
committed by GitHub
parent aa84ccd808
commit fc29b4b974
24 changed files with 743 additions and 6 deletions

View File

@@ -0,0 +1,105 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Relation, Query, Q} from '@nozbe/watermelondb';
import {children, field, immutableRelation, lazy} from '@nozbe/watermelondb/decorators';
import Model, {Associations} from '@nozbe/watermelondb/Model';
import {map, distinctUntilChanged} from 'rxjs';
import {MM_TABLES} from '@constants/database';
import type CategoryInterface from '@typings/database/models/servers/category';
import type CategoryChannelModel from '@typings/database/models/servers/category_channel';
import type ChannelModel from '@typings/database/models/servers/channel';
import type MyChannelModel from '@typings/database/models/servers/my_channel';
import type TeamModel from '@typings/database/models/servers/team';
const {
CATEGORY,
CATEGORY_CHANNEL,
CHANNEL,
MY_CHANNEL,
TEAM,
} = MM_TABLES.SERVER;
/**
* A Category holds channels for a given user in a team
*/
export default class CategoryModel extends Model implements CategoryInterface {
/** table (name) : Category */
static table = CATEGORY;
/** associations : Describes every relationship to this table. */
static associations: Associations = {
/** A CATEGORY has a 1:N relationship with CHANNEL. A CATEGORY can possess multiple channels */
[CATEGORY_CHANNEL]: {type: 'has_many', foreignKey: 'category_id'},
/** A TEAM can be associated to CATEGORY (relationship is 1:N) */
[TEAM]: {type: 'belongs_to', key: 'team_id'},
};
/** display_name : The display name for the category */
@field('display_name') displayName!: string;
/** type : The type of category ('channels' | 'direct_messages' | 'favorites' | 'custom') */
@field('type') type!: CategoryType;
/** sort_order : The index on which to sort and display categories */
@field('sort_order') sortOrder!: number;
/** sorting : The type of sorting applied to the category channels (alpha, recent, manual) */
@field('sorting') sorting!: CategorySorting;
/** collapsed : Boolean flag indicating if the category is collapsed */
@field('collapsed') collapsed!: boolean;
/** muted : Boolean flag indicating if the category is muted */
@field('muted') muted!: boolean;
/** teamId : The team in which this category lives */
@field('team_id') teamId!: string;
/** team : Retrieves information about the team that this category is a part of. */
@immutableRelation(TEAM, 'id') team!: Relation<TeamModel>;
/** categoryChannels : All the CategoryChannels associated with this team */
@children(CATEGORY_CHANNEL) categoryChannels!: Query<CategoryChannelModel>;
/** categoryChannelsBySortOrder : Retrieves assocated category channels sorted by sort_order */
@lazy categoryChannelsBySortOrder = this.categoryChannels.collection.query(Q.sortBy('sort_order', Q.asc));
/** channels : Retrieves all the channels that are part of this category */
@lazy channels = this.collections.
get<ChannelModel>(CHANNEL).
query(
Q.experimentalJoinTables([CHANNEL, CATEGORY_CHANNEL]),
Q.on(CATEGORY_CHANNEL,
Q.and(
Q.on(CHANNEL, Q.where('delete_at', Q.eq(0))),
Q.where('category_id', this.id),
),
),
Q.sortBy('display_name'),
);
/** myChannels : Retrieves all myChannels that are part of this category */
@lazy myChannels = this.collections.
get<MyChannelModel>(MY_CHANNEL).
query(
Q.experimentalJoinTables([CHANNEL, CATEGORY_CHANNEL]),
Q.on(CATEGORY_CHANNEL,
Q.and(
Q.on(CHANNEL, Q.where('delete_at', Q.eq(0))),
Q.where('category_id', this.id),
),
),
Q.sortBy('last_post_at', Q.desc),
);
/** hasChannels : Returns a boolean indicating if the category has channels */
@lazy hasChannels = this.categoryChannels.observeCount().pipe(
map((c) => c > 0),
distinctUntilChanged(),
);
}

View File

@@ -0,0 +1,54 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Relation} from '@nozbe/watermelondb';
import {field, immutableRelation, relation} from '@nozbe/watermelondb/decorators';
import Model, {Associations} from '@nozbe/watermelondb/Model';
import {MM_TABLES} from '@constants/database';
import type CategoryModel from '@typings/database/models/servers/category';
import type CategoryChannelInterface from '@typings/database/models/servers/category_channel';
import type ChannelModel from '@typings/database/models/servers/channel';
const {CATEGORY_CHANNEL, CATEGORY, MY_CHANNEL, CHANNEL} = MM_TABLES.SERVER;
/**
* The CategoryChannel model represents the 'association table' where many categories have channels and many channels are in
* categories (relationship type N:N)
*/
export default class CategoryChannelModel extends Model implements CategoryChannelInterface {
/** table (name) : CategoryChannel */
static table = CATEGORY_CHANNEL;
/** associations : Describes every relationship to this table. */
static associations: Associations = {
/** A CategoryChannel belongs to a CATEGORY */
[CATEGORY]: {type: 'belongs_to', key: 'category_id'},
/** A CategoryChannel has a Channel */
[CHANNEL]: {type: 'belongs_to', key: 'channel_id'},
/** A CategoryChannel has a MyChannel */
[MY_CHANNEL]: {type: 'belongs_to', key: 'channel_id'},
};
/** category_id : The foreign key to the related Category record */
@field('category_id') categoryId!: string;
/** channel_id : The foreign key to the related Channel record */
@field('channel_id') channelId!: string;
/* sort_order: The sort order for the channel in category */
@field('sort_order') sortOrder!: number;
/** category : The related category */
@relation(CATEGORY, 'category_id') category!: Relation<CategoryModel>;
/** channel : The related channel */
@immutableRelation(CHANNEL, 'channel_id') channel!: Relation<ChannelModel>;
/** myChannel : The related myChannel */
@immutableRelation(MY_CHANNEL, 'channel_id') myChannel!: Relation<ChannelModel>;
}

View File

@@ -19,6 +19,7 @@ import type TeamModel from '@typings/database/models/servers/team';
import type UserModel from '@typings/database/models/servers/user';
const {
CATEGORY_CHANNEL,
CHANNEL,
CHANNEL_INFO,
CHANNEL_MEMBERSHIP,
@@ -45,6 +46,9 @@ export default class ChannelModel extends Model {
/** A CHANNEL can be associated with multiple CHANNEL_MEMBERSHIP (relationship is 1:N) */
[CHANNEL_MEMBERSHIP]: {type: 'has_many', foreignKey: 'channel_id'},
/** A CHANNEL can be associated with multiple CATEGORY_CHANNEL (relationship is 1:N) */
[CATEGORY_CHANNEL]: {type: 'has_many', foreignKey: 'channel_id'},
/** A CHANNEL can be associated with multiple DRAFT (relationship is 1:N) */
[DRAFT]: {type: 'has_many', foreignKey: 'channel_id'},

View File

@@ -1,6 +1,8 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
export {default as CategoryModel} from './category';
export {default as CategoryChannelModel} from './category_channel';
export {default as ChannelInfoModel} from './channel_info';
export {default as ChannelMembershipModel} from './channel_membership';
export {default as ChannelModel} from './channel';

View File

@@ -9,7 +9,7 @@ import {MM_TABLES} from '@constants/database';
import type ChannelModel from '@typings/database/models/servers/channel';
const {CHANNEL, MY_CHANNEL} = MM_TABLES.SERVER;
const {CATEGORY_CHANNEL, CHANNEL, MY_CHANNEL} = MM_TABLES.SERVER;
/**
* MyChannel is an extension of the Channel model but it lists only the Channels the app's user belongs to
@@ -20,6 +20,7 @@ export default class MyChannelModel extends Model {
static associations: Associations = {
[CHANNEL]: {type: 'belongs_to', key: 'id'},
[CATEGORY_CHANNEL]: {type: 'has_many', foreignKey: 'channel_id'},
};
/** last_post_at : The timestamp for any last post on this channel */