Skip to content

Commit

Permalink
[Gekidou] fix database schema and models (#5553)
Browse files Browse the repository at this point in the history
* fix database schema and models

* fix types
  • Loading branch information
enahum authored Jul 20, 2021
1 parent bd0f22f commit 324dbbd
Show file tree
Hide file tree
Showing 60 changed files with 779 additions and 552 deletions.
2 changes: 1 addition & 1 deletion app/actions/remote/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export const loadMe = async (serverUrl: string, {deviceToken, user}: LoadMeArgs)

const myTeams = teamUnreads.map((unread) => {
const matchingTeam = teamMemberships.find((team) => team.team_id === unread.team_id);
return {team_id: unread.team_id, roles: matchingTeam?.roles ?? '', is_unread: unread.msg_count > 0, mentions_count: unread.mention_count};
return {id: unread.team_id, roles: matchingTeam?.roles ?? '', is_unread: unread.msg_count > 0, mentions_count: unread.mention_count};
});

const myTeamRecords = operator.handleMyTeam({
Expand Down
16 changes: 16 additions & 0 deletions app/constants/action_type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import keyMirror from '@utils/key_mirror';

export const POSTS = keyMirror({
RECEIVED_IN_CHANNEL: null,
RECEIVED_SINCE: null,
RECEIVED_AFTER: null,
RECEIVED_BEFORE: null,
RECEIVED_NEW: null,
});

export default {
POSTS,
};
1 change: 1 addition & 0 deletions app/constants/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default {
LOGS_PAGE_SIZE_DEFAULT: 10000,
PROFILE_CHUNK_SIZE: 100,
CHANNELS_CHUNK_SIZE: 50,
POST_CHUNK_SIZE: 60,
TEAMS_CHUNK_SIZE: 50,
SEARCH_TIMEOUT_MILLISECONDS: 100,
STATUS_INTERVAL: 60000,
Expand Down
4 changes: 4 additions & 0 deletions app/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import ActionType from './action_type';
import Attachment from './attachment';
import Database from './database';
import Device from './device';
Expand All @@ -9,13 +10,15 @@ import General from './general';
import List from './list';
import Navigation from './navigation';
import Network from './network';
import Permissions from './permissions';
import Preferences from './preferences';
import Screens from './screens';
import SSO, {REDIRECT_URL_SCHEME, REDIRECT_URL_SCHEME_DEV} from './sso';
import View, {Upgrade} from './view';
import WebsocketEvents from './websocket';

export {
ActionType,
Attachment,
Database,
Device,
Expand All @@ -24,6 +27,7 @@ export {
List,
Navigation,
Network,
Permissions,
Preferences,
REDIRECT_URL_SCHEME,
REDIRECT_URL_SCHEME_DEV,
Expand Down
84 changes: 84 additions & 0 deletions app/constants/permissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
export default {
INVITE_USER: 'invite_user',
ADD_USER_TO_TEAM: 'add_user_to_team',
USE_SLASH_COMMANDS: 'use_slash_commands',
MANAGE_SLASH_COMMANDS: 'manage_slash_commands',
MANAGE_OTHERS_SLASH_COMMANDS: 'manage_others_slash_commands',
CREATE_PUBLIC_CHANNEL: 'create_public_channel',
CREATE_PRIVATE_CHANNEL: 'create_private_channel',
MANAGE_PUBLIC_CHANNEL_MEMBERS: 'manage_public_channel_members',
MANAGE_PRIVATE_CHANNEL_MEMBERS: 'manage_private_channel_members',
ASSIGN_SYSTEM_ADMIN_ROLE: 'assign_system_admin_role',
MANAGE_ROLES: 'manage_roles',
MANAGE_TEAM_ROLES: 'manage_team_roles',
MANAGE_CHANNEL_ROLES: 'manage_channel_roles',
MANAGE_SYSTEM: 'manage_system',
CREATE_DIRECT_CHANNEL: 'create_direct_channel',
CREATE_GROUP_CHANNEL: 'create_group_channel',
MANAGE_PUBLIC_CHANNEL_PROPERTIES: 'manage_public_channel_properties',
MANAGE_PRIVATE_CHANNEL_PROPERTIES: 'manage_private_channel_properties',
LIST_PUBLIC_TEAMS: 'list_public_teams',
JOIN_PUBLIC_TEAMS: 'join_public_teams',
LIST_PRIVATE_TEAMS: 'list_private_teams',
JOIN_PRIVATE_TEAMS: 'join_private_teams',
LIST_TEAM_CHANNELS: 'list_team_channels',
JOIN_PUBLIC_CHANNELS: 'join_public_channels',
DELETE_PUBLIC_CHANNEL: 'delete_public_channel',
CONVERT_PUBLIC_CHANNEL_TO_PRIVATE: 'convert_public_channel_to_private',
DELETE_PRIVATE_CHANNEL: 'delete_private_channel',
EDIT_OTHER_USERS: 'edit_other_users',
READ_CHANNEL: 'read_channel',
READ_PUBLIC_CHANNEL: 'read_public_channel',
ADD_REACTION: 'add_reaction',
REMOVE_REACTION: 'remove_reaction',
REMOVE_OTHERS_REACTIONS: 'remove_others_reactions',
PERMANENT_DELETE_USER: 'permanent_delete_user',
UPLOAD_FILE: 'upload_file',
GET_PUBLIC_LINK: 'get_public_link',
MANAGE_WEBHOOKS: 'manage_webhooks',
MANAGE_OTHERS_WEBHOOKS: 'manage_others_webhooks',
MANAGE_INCOMING_WEBHOOKS: 'manage_incoming_webhooks',
MANAGE_OTHERS_INCOMING_WEBHOOKS: 'manage_others_incoming_webhooks',
MANAGE_OUTGOING_WEBHOOKS: 'manage_outgoing_webhooks',
MANAGE_OTHERS_OUTGOING_WEBHOOKS: 'manage_others_outgoing_webhooks',
MANAGE_OAUTH: 'manage_oauth',
MANAGE_SYSTEM_WIDE_OAUTH: 'manage_system_wide_oauth',
CREATE_POST: 'create_post',
CREATE_POST_PUBLIC: 'create_post_public',
EDIT_POST: 'edit_post',
EDIT_OTHERS_POSTS: 'edit_others_posts',
DELETE_POST: 'delete_post',
DELETE_OTHERS_POSTS: 'delete_others_posts',
REMOVE_USER_FROM_TEAM: 'remove_user_from_team',
CREATE_TEAM: 'create_team',
MANAGE_TEAM: 'manage_team',
IMPORT_TEAM: 'import_team',
VIEW_TEAM: 'view_team',
LIST_USERS_WITHOUT_TEAM: 'list_users_without_team',
CREATE_USER_ACCESS_TOKEN: 'create_user_access_token',
READ_USER_ACCESS_TOKEN: 'read_user_access_token',
REVOKE_USER_ACCESS_TOKEN: 'revoke_user_access_token',
MANAGE_JOBS: 'manage_jobs',
MANAGE_EMOJIS: 'manage_emojis',
MANAGE_OTHERS_EMOJIS: 'manage_others_emojis',
CREATE_EMOJIS: 'create_emojis',
DELETE_EMOJIS: 'delete_emojis',
DELETE_OTHERS_EMOJIS: 'delete_others_emojis',
VIEW_MEMBERS: 'view_members',
INVITE_GUEST: 'invite_guest',
PROMOTE_GUEST: 'promote_guest',
DEMOTE_TO_GUEST: 'demote_to_guest',
USE_CHANNEL_MENTIONS: 'use_channel_mentions',
USE_GROUP_MENTIONS: 'use_group_mentions',

CHANNEL_MODERATED_PERMISSIONS: {
CREATE_POST: 'create_post',
CREATE_REACTIONS: 'create_reactions',
MANAGE_MEMBERS: 'manage_members',
USE_CHANNEL_MENTIONS: 'use_channel_mentions',
},
MANAGE_BOTS: 'manage_bots',
MANAGE_OTHERS_BOTS: 'manage_others_bots',
};
2 changes: 0 additions & 2 deletions app/constants/view.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {Platform} from 'react-native';
import DeviceInfo from 'react-native-device-info';

import keyMirror from '@utils/key_mirror';
Expand Down Expand Up @@ -111,7 +110,6 @@ const RequiredServer = {
export default {
...ViewTypes,
RequiredServer,
POST_VISIBILITY_CHUNK_SIZE: Platform.OS === 'android' ? 15 : 60,
FEATURE_TOGGLE_PREFIX: 'feature_enabled_',
EMBED_PREVIEW: 'embed_preview',
LINK_PREVIEW_DISPLAY: 'link_previews',
Expand Down
2 changes: 1 addition & 1 deletion app/database/models/server/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class ChannelModel extends Model {
[MY_CHANNEL_SETTINGS]: {type: 'has_many', foreignKey: 'channel_id'},

/** A CHANNEL can be associated with multiple POSTS_IN_CHANNEL (relationship is 1:N) */
[POSTS_IN_CHANNEL]: {type: 'has_many', foreignKey: 'channel_id'},
[POSTS_IN_CHANNEL]: {type: 'has_many', foreignKey: 'id'},

/** A CHANNEL can contain multiple POST (relationship is 1:N) */
[POST]: {type: 'has_many', foreignKey: 'channel_id'},
Expand Down
7 changes: 2 additions & 5 deletions app/database/models/server/channel_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ export default class ChannelInfoModel extends Model {
static associations: Associations = {

/** A CHANNEL is associated with only one CHANNEL_INFO (relationship is 1:1) */
[CHANNEL]: {type: 'belongs_to', key: 'channel_id'},
[CHANNEL]: {type: 'belongs_to', key: 'id'},
};

/** channel_id : The foreign key from CHANNEL */
@field('channel_id') channelId!: string;

/** guest_count : The number of guest in this channel */
@field('guest_count') guestCount!: number;

Expand All @@ -46,5 +43,5 @@ export default class ChannelInfoModel extends Model {
@field('purpose') purpose!: string;

/** channel : The lazy query property to the record from CHANNEL table */
@immutableRelation(CHANNEL, 'channel_id') channel!: Relation<ChannelModel>;
@immutableRelation(CHANNEL, 'id') channel!: Relation<ChannelModel>;
}
7 changes: 2 additions & 5 deletions app/database/models/server/my_channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ export default class MyChannelModel extends Model {
static associations: Associations = {

/** A CHANNEL can be associated to only one record from the MY_CHANNEL table (relationship is 1:1) */
[CHANNEL]: {type: 'belongs_to', key: 'channel_id'},
[CHANNEL]: {type: 'belongs_to', key: 'id'},
};

/** channel_id : The foreign key to the related Channel record */
@field('channel_id') channelId!: string;

/** last_post_at : The timestamp for any last post on this channel */
@field('last_post_at') lastPostAt!: number;

Expand All @@ -44,5 +41,5 @@ export default class MyChannelModel extends Model {
@field('roles') roles!: string;

/** channel : The relation pointing to the CHANNEL table */
@immutableRelation(CHANNEL, 'channel_id') channel!: Relation<ChannelModel>;
@immutableRelation(CHANNEL, 'id') channel!: Relation<ChannelModel>;
}
9 changes: 3 additions & 6 deletions app/database/models/server/my_channel_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// See LICENSE.txt for license information.

import {Relation} from '@nozbe/watermelondb';
import {field, immutableRelation, json} from '@nozbe/watermelondb/decorators';
import {immutableRelation, json} from '@nozbe/watermelondb/decorators';
import Model, {Associations} from '@nozbe/watermelondb/Model';

import {MM_TABLES} from '@constants/database';
Expand All @@ -24,15 +24,12 @@ export default class MyChannelSettingsModel extends Model {
static associations: Associations = {

/** A CHANNEL is related to only one MY_CHANNEL_SETTINGS (relationship is 1:1) */
[CHANNEL]: {type: 'belongs_to', key: 'channel_id'},
[CHANNEL]: {type: 'belongs_to', key: 'id'},
};

/** channel_id : The foreign key to the related CHANNEL record */
@field('channel_id') channelId!: string;

/** notify_props : Configurations with regards to this channel */
@json('notify_props', safeParseJSON) notifyProps!: ChannelNotifyProps;

/** channel : The relation pointing to the CHANNEL table */
@immutableRelation(CHANNEL, 'channel_id') channel!: Relation<ChannelModel>;
@immutableRelation(CHANNEL, 'id') channel!: Relation<ChannelModel>;
}
7 changes: 2 additions & 5 deletions app/database/models/server/my_team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class MyTeamModel extends Model {
static associations: Associations = {

/** TEAM and MY_TEAM have a 1:1 relationship. */
[TEAM]: {type: 'belongs_to', key: 'team_id'},
[TEAM]: {type: 'belongs_to', key: 'id'},
};

/** is_unread : Boolean flag for unread messages on team level */
Expand All @@ -34,9 +34,6 @@ export default class MyTeamModel extends Model {
/** roles : The different permissions that this user has in the team, concatenated together with comma to form a single string. */
@field('roles') roles!: string;

/** team_id : The foreign key of the 'parent' Team record */
@field('team_id') teamId!: string;

/** team : The relation to the TEAM, that this user belongs to */
@relation(MY_TEAM, 'team_id') team!: Relation<TeamModel>;
@relation(MY_TEAM, 'id') team!: Relation<TeamModel>;
}
4 changes: 2 additions & 2 deletions app/database/models/server/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ export default class PostModel extends Model {
[FILE]: {type: 'has_many', foreignKey: 'post_id'},

/** A POST can have multiple POSTS_IN_THREAD. (relationship is 1:N)*/
[POSTS_IN_THREAD]: {type: 'has_many', foreignKey: 'post_id'},
[POSTS_IN_THREAD]: {type: 'has_many', foreignKey: 'id'},

/** A POST can have POST_METADATA. (relationship is 1:1)*/
[POST_METADATA]: {type: 'has_many', foreignKey: 'post_id'},
[POST_METADATA]: {type: 'has_many', foreignKey: 'id'},

/** A POST can have multiple REACTION. (relationship is 1:N)*/
[REACTION]: {type: 'has_many', foreignKey: 'post_id'},
Expand Down
9 changes: 3 additions & 6 deletions app/database/models/server/post_metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// See LICENSE.txt for license information.

import {Relation} from '@nozbe/watermelondb';
import {field, immutableRelation, json} from '@nozbe/watermelondb/decorators';
import {immutableRelation, json} from '@nozbe/watermelondb/decorators';
import Model, {Associations} from '@nozbe/watermelondb/Model';

import {MM_TABLES} from '@constants/database';
Expand All @@ -23,15 +23,12 @@ export default class PostMetadataModel extends Model {
static associations: Associations = {

/** A POST can have multiple POST_METADATA.(relationship is 1:N)*/
[POST]: {type: 'belongs_to', key: 'post_id'},
[POST]: {type: 'belongs_to', key: 'id'},
};

/** post_id : The foreign key of the parent POST model */
@field('post_id') postId!: string;

/** data : Different types of data ranging from embeds to images. */
@json('data', safeParseJSON) data!: PostMetadata;

/** post: The record representing the POST parent. */
@immutableRelation(POST, 'post_id') post!: Relation<PostModel>;
@immutableRelation(POST, 'id') post!: Relation<PostModel>;
}
7 changes: 2 additions & 5 deletions app/database/models/server/posts_in_channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@ export default class PostsInChannelModel extends Model {
static associations: Associations = {

/** A CHANNEL can have multiple POSTS_IN_CHANNEL. (relationship is 1:N)*/
[CHANNEL]: {type: 'belongs_to', key: 'channel_id'},
[CHANNEL]: {type: 'belongs_to', key: 'id'},
};

/** channel_id : The foreign key of the related parent channel */
@field('channel_id') channelId!: string;

/** earliest : The earliest timestamp of the post in that channel */
@field('earliest') earliest!: number;

/** latest : The latest timestamp of the post in that channel */
@field('latest') latest!: number;

/** channel : The parent record of the channel for those posts */
@immutableRelation(CHANNEL, 'channel_id') channel!: Relation<ChannelModel>;
@immutableRelation(CHANNEL, 'id') channel!: Relation<ChannelModel>;
}
9 changes: 3 additions & 6 deletions app/database/models/server/posts_in_thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export default class PostsInThreadModel extends Model {
/** associations : Describes every relationship to this table. */
static associations: Associations = {

/** A POST can have multiple POSTS_IN_THREAD.(relationship is 1:N)*/
[POST]: {type: 'belongs_to', key: 'post_id'},
/** A POST can have a POSTS_IN_THREAD.(relationship is 1:1)*/
[POST]: {type: 'belongs_to', key: 'id'},
};

/** earliest : Lower bound of a timestamp range */
Expand All @@ -32,9 +32,6 @@ export default class PostsInThreadModel extends Model {
/** latest : Upper bound of a timestamp range */
@field('latest') latest!: number;

/** post_id : The foreign key of the related Post model */
@field('post_id') postId!: string;

/** post : The related record to the parent Post model */
@immutableRelation(POST, 'post_id') post!: Relation<PostModel>;
@immutableRelation(POST, 'id') post!: Relation<PostModel>;
}
9 changes: 3 additions & 6 deletions app/database/models/server/team_channel_history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// See LICENSE.txt for license information.

import {Relation} from '@nozbe/watermelondb';
import {field, immutableRelation, json} from '@nozbe/watermelondb/decorators';
import {immutableRelation, json} from '@nozbe/watermelondb/decorators';
import Model, {Associations} from '@nozbe/watermelondb/Model';

import {MM_TABLES} from '@constants/database';
Expand All @@ -24,15 +24,12 @@ export default class TeamChannelHistoryModel extends Model {
static associations: Associations = {

/** A TEAM and TEAM_CHANNEL_HISTORY share a 1:1 relationship */
[TEAM]: {type: 'belongs_to', key: 'team_id'},
[TEAM]: {type: 'belongs_to', key: 'id'},
};

/** team_id : The foreign key to the related Team record */
@field('team_id') teamId!: string;

/** channel_ids : An array containing the last 5 channels visited within this team order by recency */
@json('channel_ids', safeParseJSON) channelIds!: string[];

/** team : The related record from the parent Team model */
@immutableRelation(TEAM, 'team_id') team!: Relation<TeamModel>;
@immutableRelation(TEAM, 'id') team!: Relation<TeamModel>;
}
2 changes: 1 addition & 1 deletion app/database/operator/base_data_operator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class BaseDataOperator {
* @param {(existing: Model, newElement: RawValue) => boolean} inputsArg.findMatchingRecordBy
* @returns {Promise<{ProcessRecordResults}>}
*/
processRecords = async ({createOrUpdateRawValues, deleteRawValues = [], tableName, findMatchingRecordBy, fieldName}: ProcessRecordsArgs): Promise<ProcessRecordResults> => {
processRecords = async ({createOrUpdateRawValues = [], deleteRawValues = [], tableName, findMatchingRecordBy, fieldName}: ProcessRecordsArgs): Promise<ProcessRecordResults> => {
const getRecords = async (rawValues: RawValue[]) => {
// We will query a table where one of its fields can match a range of values. Hence, here we are extracting all those potential values.
const columnValues: string[] = getRangeOfValues({fieldName, raws: rawValues});
Expand Down
Loading

0 comments on commit 324dbbd

Please sign in to comment.