[Gekidou] Groups + group membership schema (#6251)

* First pass at adding groups to mobile

* Reverts back and updates the group vars

* Cleans tests

* Missing created/updated/deleted fields in types, many-many ref fix

* Adds to manager

* PR Feedback

* Failing test

* Move FK out of comment, add indexes

* updated docs/database/server artefacts

Co-authored-by: Avinash Lingaloo <avinashlng1080@gmail.com>
This commit is contained in:
Shaz MJ
2022-05-19 17:54:39 +10:00
committed by GitHub
parent 7e27113f61
commit 6d6085ed4b
26 changed files with 770 additions and 47 deletions

View File

@@ -0,0 +1,83 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Q} from '@nozbe/watermelondb';
import {field, lazy} from '@nozbe/watermelondb/decorators';
import Model, {Associations} from '@nozbe/watermelondb/Model';
import {MM_TABLES} from '@constants/database';
import type ChannelModel from '@typings/database/models/servers/channel';
import type GroupInterface from '@typings/database/models/servers/group';
import type TeamModel from '@typings/database/models/servers/team';
import type UserModel from '@typings/database/models/servers/user';
const {
CHANNEL,
GROUP,
GROUP_CHANNEL,
GROUP_TEAM,
GROUP_MEMBERSHIP,
TEAM,
USER,
} = MM_TABLES.SERVER;
/**
* A Group is a collection of users that can be associated with a team or a channel
*/
export default class GroupModel extends Model implements GroupInterface {
/** table (name) : Group */
static table = GROUP;
/** associations : Describes every relationship to this table. */
static associations: Associations = {
/** Groups are associated with Channels (relationship N:N) through GROUP_CHANNEL */
[GROUP_CHANNEL]: {type: 'has_many', foreignKey: 'group_id'},
/** Groups are associated with Members (Users) (relationship N:N) through GROUP_MEMBERSHIP */
[GROUP_MEMBERSHIP]: {type: 'has_many', foreignKey: 'group_id'},
/** Groups are associated with Teams (relationship N:N) through GROUP_TEAM */
[GROUP_TEAM]: {type: 'has_many', foreignKey: 'group_id'},
};
/** name : The name for the group */
@field('name') name!: string;
/** display_name : The display name for the group */
@field('display_name') displayName!: string;
/** description : The display name for the group */
@field('description') description!: string;
/** remote_id : The source for the group (i.e. custom) */
@field('source') source!: string;
/** remote_id : The remote id for the group (i.e. in a shared channel) */
@field('remote_id') remoteId!: string;
/** created_at : The creation date for this row */
@field('created_at') createdAt!: number;
/** updated_at : The update date for this row */
@field('updated_at') updatedAt!: number;
/** deleted_at : The delete date for this row */
@field('deleted_at') deletedAt!: number;
/** channels : Retrieves all the channels that are associated to this group */
@lazy channels = this.collections.
get<ChannelModel>(CHANNEL).
query(Q.on(GROUP_CHANNEL, 'group_id', this.id));
/** teams : Retrieves all the teams that are associated to this group */
@lazy teams = this.collections.
get<TeamModel>(TEAM).
query(Q.on(GROUP_TEAM, 'group_id', this.id));
/** members : Retrieves all the members that are associated to this group */
@lazy members = this.collections.
get<UserModel>(USER).
query(Q.on(GROUP_MEMBERSHIP, 'group_id', this.id));
}

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} from '@nozbe/watermelondb/decorators';
import Model, {Associations} from '@nozbe/watermelondb/Model';
import {MM_TABLES} from '@constants/database';
import type ChannelModel from '@typings/database/models/servers/channel';
import type GroupModel from '@typings/database/models/servers/group';
import type GroupChannelInterface from '@typings/database/models/servers/group_channel';
const {CHANNEL, GROUP, GROUP_CHANNEL} = MM_TABLES.SERVER;
/**
* The GroupChannel model represents the 'association table' where many groups have channels and many channels are in
* groups (relationship type N:N)
*/
export default class GroupChannelModel extends Model implements GroupChannelInterface {
/** table (name) : GroupChannel */
static table = GROUP_CHANNEL;
/** associations : Describes every relationship to this table. */
static associations: Associations = {
/** A GroupChannel belongs to a Group */
[GROUP]: {type: 'belongs_to', key: 'group_id'},
/** A GroupChannel has a Channel */
[CHANNEL]: {type: 'belongs_to', key: 'channel_id'},
};
/** group_id : The foreign key to the related Group record */
@field('group_id') groupId!: string;
/** channel_id : The foreign key to the related Channel record */
@field('channel_id') channelId!: string;
/** created_at : The creation date for this row */
@field('created_at') createdAt!: number;
/** updated_at : The update date for this row */
@field('updated_at') updatedAt!: number;
/** deleted_at : The delete date for this row */
@field('deleted_at') deletedAt!: number;
/** group : The related group */
@immutableRelation(GROUP, 'group_id') group!: Relation<GroupModel>;
/** channel : The related channel */
@immutableRelation(CHANNEL, 'channel_id') channel!: Relation<ChannelModel>;
}

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} from '@nozbe/watermelondb/decorators';
import Model, {Associations} from '@nozbe/watermelondb/Model';
import {MM_TABLES} from '@constants/database';
import type GroupModel from '@typings/database/models/servers/group';
import type GroupMembershipInterface from '@typings/database/models/servers/group_membership';
import type UserModel from '@typings/database/models/servers/user';
const {USER, GROUP, GROUP_MEMBERSHIP} = MM_TABLES.SERVER;
/**
* The GroupMembership model represents the 'association table' where many groups have users and many users are in
* groups (relationship type N:N)
*/
export default class GroupMembershipModel extends Model implements GroupMembershipInterface {
/** table (name) : GroupMembership */
static table = GROUP_MEMBERSHIP;
/** associations : Describes every relationship to this table. */
static associations: Associations = {
/** A GroupMembership belongs to a Group */
[GROUP]: {type: 'belongs_to', key: 'group_id'},
/** A GroupMembership has a User */
[USER]: {type: 'belongs_to', key: 'user_id'},
};
/** group_id : The foreign key to the related Group record */
@field('group_id') groupId!: string;
/** user_id : The foreign key to the related User record */
@field('user_id') userId!: string;
/** created_at : The creation date for this row */
@field('created_at') createdAt!: number;
/** updated_at : The update date for this row */
@field('updated_at') updatedAt!: number;
/** deleted_at : The delete date for this row */
@field('deleted_at') deletedAt!: number;
/** group : The related group */
@immutableRelation(GROUP, 'group_id') group!: Relation<GroupModel>;
/** member : The related member */
@immutableRelation(USER, 'user_id') member!: Relation<UserModel>;
}

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} from '@nozbe/watermelondb/decorators';
import Model, {Associations} from '@nozbe/watermelondb/Model';
import {MM_TABLES} from '@constants/database';
import type GroupModel from '@typings/database/models/servers/group';
import type GroupTeamInterface from '@typings/database/models/servers/group_team';
import type TeamModel from '@typings/database/models/servers/team';
const {TEAM, GROUP, GROUP_TEAM} = MM_TABLES.SERVER;
/**
* The GroupTeam model represents the 'association table' where many groups have teams and many teams are in
* groups (relationship type N:N)
*/
export default class GroupTeamModel extends Model implements GroupTeamInterface {
/** table (name) : GroupTeam */
static table = GROUP_TEAM;
/** associations : Describes every relationship to this table. */
static associations: Associations = {
/** A GroupTeam belongs to a Group */
[GROUP]: {type: 'belongs_to', key: 'group_id'},
/** A GroupTeam has a Team */
[TEAM]: {type: 'belongs_to', key: 'team_id'},
};
/** group_id : The foreign key to the related Group record */
@field('group_id') groupId!: string;
/** team_id : The foreign key to the related Team record */
@field('team_id') teamId!: string;
/** created_at : The creation date for this row */
@field('created_at') createdAt!: number;
/** updated_at : The update date for this row */
@field('updated_at') updatedAt!: number;
/** deleted_at : The delete date for this row */
@field('deleted_at') deletedAt!: number;
/** group : The related group */
@immutableRelation(GROUP, 'group_id') group!: Relation<GroupModel>;
/** team : The related team */
@immutableRelation(TEAM, 'team_id') team!: Relation<TeamModel>;
}

View File

@@ -9,6 +9,10 @@ export {default as ChannelModel} from './channel';
export {default as CustomEmojiModel} from './custom_emoji';
export {default as DraftModel} from './draft';
export {default as FileModel} from './file';
export {default as GroupModel} from './group';
export {default as GroupChannelModel} from './group_channel';
export {default as GroupMembershipModel} from './group_membership';
export {default as GroupTeamModel} from './group_team';
export {default as MyChannelModel} from './my_channel';
export {default as MyChannelSettingsModel} from './my_channel_settings';
export {default as MyTeamModel} from './my_team';