* MM_30475 : ADDED default schema * MM_30475 : ADDED todo for field 'value' of default/Global entity * MM_30476 : Created schema for SERVER DB * MM_30476 : Server model [ IN PROGRESS ] * MM_30476 : Including types for group, groups_in_channel and role * MM_30476 : ADDED models for Group - @typings absolute path has been added to the tsconfig.json * MM_30476 : ADDED typings to current models * MM_30476 : ADDED typings to current models * MM_30476 : ADDED models related to TEAM section of the ERD * MM_30476 : ADDED models for User section of the ERD * MM_30476 : ADDED models for POST section of the ERD * MM_30476 : ADDED models for Channel section of the ERD * MM_30475 : Updated typings and references to MM_TABLES * MM_30476 : Verified all field names * MM_30476 : Verified every table associations * MM_30476 : Verified all relation fields * MM_30476 : Updated primary id of the main models We will override the wdb id at component level when we create a new records. This involves the models : channel, group, post, team and user. * MM_30476 : Including 1:1 relationship amongs some entities * MM_30476 : ADDED Schema Managers * The migration array will hold all the migration steps. * The initial app release (e.g. v2 )will have an empty array and subsequent releases (e.g. v2.1 ) will have the steps listed in that array. * On initialization, the database will perform the migration to accomodate for new columns/tables creation and while it will conserve the mobile phone's data, it will also make it conform to this new schema. * If a migration fails, the migration process will rollback any changes. This migration will be thoroughly tested in development before pushing it live. * Revert "MM_30476 : ADDED Schema Managers" This reverts commit a505bd5e11124e8eb8f258ce8dbb8168a535f7ae. * MM_30478 : Converted schema_manager into a function * MM_30478 : Updated schema manager and included patch for wdb * MM_30478: Updated watermelondb patch package * MM_30478 : Update function create_schema_manager to createSqliteAdaptorOptions * MM_30476 : Update constant name to reflect directory name * MM_30476 : Updated msgCount from my_channel model to message_count in server schema * MM_30482 : Added tests for schema_manager * MM_30482 : Database Manager [ IN PROGRESS ] * MM_30478 : Returning an sqliteAdapter instead of an object * MM_30476 : Apply suggestions from code review Co-authored-by: Elias Nahum <nahumhbl@gmail.com> * MM_30476 : Updated all imports as per instruction. * MM_30476 : Shortening object chains by destructuring * MM_30476 : Updated schema file structure * MM_30476 : Prettifying @typings folder * MM_30476 : Removing useless ids * MM_30476 : Prettify imports for decorators * MM_30476 : ADDED documentations and lazy queries to Channel and Channel_Info * MM_30476 : ADDED documentations for default schema * MM_30476 : Documentation [ IN PROGRESS ] - Following JSDoc syntax for single line comment - Removed redundant fields in the 'membership' tables and left only the @relation records. * MM_30476 : Documentations [ IN PROGRESS ] * MM_30476 : Documentations [ IN PROGRESS ] * MM_30476 : Documentations [ IN PROGRESS ] * MM_30476 : Documentations [ IN PROGRESS] Updated 1) my_team and team, 2) my_channel and channel, to each have 1:1 relationship with one another * MM_30476 : Updated all Typescript definitions * MM_30476 :Updated @relation to @immutableRelation * MM_30476 : Updated description for previous_post_id * MM_30478 : Updated patch package for wdb module * MM_30478: DB Manager [IN PROGRESS ] * MM_30478: DB Manager [IN PROGRESS] * MM_30478: DB Manager [IN PROGRESS] * MM_30478 : DB Manager [IN PROGRESS] * MM_30478 : Deleting .db file on iOS * MM_30478: Successfully deleting .db files and directory on iOS side * MM_30478 : Update definition for default/global * MM_30478 : Updated all models * MM_30478 : Doing a bit of house cleaning * MM_30478: Record of new server connection added to default/servers db * TS Definitely Typed Assignment issue is now FIXED * MM_30478 : TS Definitely Typed Assignment \n Removed all the constructors but error still in editor tabs. But this time the app is not crashing * MM_30478 : Attempt 1 [SUCCESSFUL] * MM_30478 : Removing useDefineForClassFields * MM_30478 : Retrieving the servers in a list + Improved the DB Manager and Babel config * MM_30478 : Updated babel.config.js * MM_30478 : Minor UI correction * MM_30478 : Jest and Typescript configuration * MM_30478 : A bit of housekeeping * MM_30478 : Installed WDB on Android * MM_30478 : Deletes new server record from default DB * MM_30478 : Returns subset of server db instances * MM_30478 : Code clean up * MM_30478 : Code clean up on db manager * MM_30478 : House keeping + Patch for WDB * MM_30478 : Android - Saving & Deleting in FilesDir [COMPLETED] * MM_30478 : Code clean up * MM_30478 : Code clean up * MM_30478 : Code clean up * MM_30478 : Test successful on Android device * MM_30478 : Rolling back change to jest.config.js * MM_30478 : Updated test to test_integration * MM_30478 : Fix imports * MM_30478 : Refactored the manual testscript * MM_30478 : Renamed database manager test file * MM_30478 : Code clean up * MM_30478 : Updated manual test file with a note. * MM_30482 : DataOperator [ IN PROGRESS ] * MM_30482 : DataOperator - setting up the factory [ IN PROGRESS ] * MM_30482: Code refactoring * MM_30482 : DataOperator - setting up the factory [ IN PROGRESS ] * MM_30482 : DataOperator - code clean up [ IN PROGRESS ] * MM_30482 : Minor code clean up * MM_30478 : Fixed JEST issue with TS * MM_30478 : Fixed JEST issue with TS * MM_30478 : Fixed JEST issue with TS * MM_30478 : Implementing JEST test cases * MM_30478 : Implementing JEST last test cases * MM_30478 : Jest fixing ts errors * MM_30478 : Database Manager Jest testing [ IN PROGRESS ] * MM_30482 - Fixing DataOperator [ IN PROGRESS ] * MM_30482 : Code clean up * MM_30482 - Creates multiple records [ IN PROGRESS ] * MM_30482 - Creates multiple records [ IN PROGRESS ] * MM_30482 : Update operation [ COMPLETED ] * MM_30482 : Code clean up * MM_30482 : Updated TS for Data Operator * Update mobile v2 detox deps * MM_30482 : Added factories for all isolated tables * MM_30482 : Refactored TS * MM_30482 : Refactored base factory * MM_30482 : Updated JSDoc for operateBaseRecord - Delete CASE * MM_30482 : Implementing test for Data Operator * MM_30482 : Completed tests for all isolated tables * MM_30482 : Renamed entity_factory into operators * MM_30482 : Fix all imports * MM_30482 : Update multiple records * MM_30482 : Edge case for existing records ( update instead of create ) * MM_30482 : Edge case - create instead of update * MM_30482 : Code clean up * MM_30482 : Code clean up * MM_30482 : Code clean up * MM_30482 : Code clean up * Update app/database/admin/data_operator/operators.ts Co-authored-by: Joseph Baylon <joseph.baylon@mattermost.com> * Update app/database/admin/data_operator/operators.ts Co-authored-by: Joseph Baylon <joseph.baylon@mattermost.com> * Update app/database/admin/data_operator/operators.ts Co-authored-by: Joseph Baylon <joseph.baylon@mattermost.com> * MM_30482 : Imposing usage of correct table name for isolated entities * MM_30482 : Code improvement as per Joseph reviews * MM_30482 : Updated tests to validate choice of operator service wrt tableName * MM_30482 : Updated PR as per suggestions * MM_30482 : Updated comments to follow jsdoc conventions * MM_33223 : Renamed DBInstance to DatabaseInstance * MM_33223 : ADDED Prettier * MM_33223 - Prettier formatting * MM_33223 : Prettier formatting * MM_33223 - Post section [ in progress ] * MM_33223 : PostsInThread [99% completed ] * MM_33223: Reaction entity completed * MM_33223: Added Reaction to the Post * MM_33223 : Refactored reactions utils * MM_33223 : Added previous post id to all posts * MM_33223 : Added File Metadata * MM_33223 : Code clean up * MM_33223 : Added PostMetadata * MM_33223 : Added Draft * MM_33223 - Removed Prettier * MM_33223 - Undo files changes due to Prettier * MM_33223 : Making use of MM eslint plugins * MM_33223 : PostsInChannel [ IN PROGRESS ] * MM_33223 : Including update_at in Post schema * MM_33223: Code clean up * MM_33223: Code clean up * MM_33223 : Code clean up * MM_33223: Testing Reaction [IN PROGRESS] * MM_33223 : Updated typings for RawCustomEmoji in Reactions * MM_33223 : Refactored DataOperator test * MM_33223 : Jest - handleReactions - Completed * MM_33223 : Jest - HandleDraft - Completed * MM_33223 : Jest - HandleFiles - Completed * MM_33223 : Refactored DataOperator-PostMetadata * MM_33223 : Jest - HandlePostMetadata - Completed * MM_33223 : Refactored posts into ordered and unordered * MM_33223 : Refactoring + Jest Utils [ IN PROGRESS ] * MM_33223 - Jest Utils - Completed * MM_33223 : Jest - Remaining operators - Completed * MM_33223 : Jest - Handler PostsInThread - Completed * MM_33223 : Jest - HandlePostsInChannel - Completed * MM_33223 : Refactored DataOperator class * MM_33223 : DataOperator test clean up * MM_33223 : DataOperator code clean up * MM_33223 : Jest - HandlePosts - Completed * MM_33223: JSDoc - Operators - Completed * MM_33223 : Refactoring file types.ts * MM_33223 : Refactored import statements * MM_33223 : Added @database alias * MM_33223 : Added missing JSDoc * MM_33223 : Minor code clean up * MM_33223 : Lint fixed * MM_33223 : Disable eslint rules for Notification * MM_33223 : Disable eslint rule for screens * Update app/database/admin/data_operator/index.ts Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com> * MM_33223 : Update data_operatator as per suggestion * MM_33226: Removed optType * MM_33226 : ADDED User Handler+Operator+Jest, Included update_at field for User * MM_33226 : Preference entity - Completed * MM_33226 : Team Membership entity - Completed * MM_33226 : Team Membership - Jest - Completed * MM_33226 : Removing duplicates for TeamMembership and Preferences * MM_33226 : Refactored Custom Emojis to remove duplicates * MM_33226 : Group Membership - Completed * MM_33226 : ChannelMembership - Completed * MM_33226 : Refactored some handlers whose response have no Ids * MM_33226 : Refactoring - in progress * MM_33226 : Refactoring - in progress * MM_33226 : Refactoring - in progress * MM_33226 : Code clean up * MM_33226 : Polishing Operator tests * MM_33226 : Removing redundant test cases * MM_33226 : Polishing Operators * MM_33226 : Testing for duplicate post id in Raw values * MM_33226 : Including some error-throwing in the Database Manager * MM_33226 : Merged in DataOperator/Post-section * MM_33226 : Fixing the merging issues * MM_33226 : fixing merge issues * MM_33226 : Code polishing * MM_33226 : Enabling user notify props comment * MM_33226 : Correcting type casting * MM_33226 : Correcting data operators * MM_33226 : Corrections * MM_33226 : Code clean up * MM_33226 : Rename oneOfField to fieldName * MM_33226 : Renaming comparators to Boolean name and oneOfField to fieldName * MM_33226 : Putting back custom emoji into handleIsolatedEntity * MM_33226 : Comparing simple arrays * MM_33226 : Renaming DiscardDuplicates to ProcessInputs * MM_33226 : Sort imports * MM_33226 : Types clean up Co-authored-by: Elias Nahum <nahumhbl@gmail.com> Co-authored-by: Avinash Lingaloo <> Co-authored-by: Joseph Baylon <joseph.baylon@mattermost.com> Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com>
663 lines
22 KiB
TypeScript
663 lines
22 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import {Q} from '@nozbe/watermelondb';
|
|
import Model from '@nozbe/watermelondb/Model';
|
|
|
|
import {MM_TABLES} from '@constants/database';
|
|
import {User} from '@database/server/models';
|
|
import App from '@typings/database/app';
|
|
import ChannelMembership from '@typings/database/channel_membership';
|
|
import CustomEmoji from '@typings/database/custom_emoji';
|
|
import {
|
|
DataFactoryArgs,
|
|
RawApp,
|
|
RawChannelMembership,
|
|
RawCustomEmoji,
|
|
RawDraft,
|
|
RawFile,
|
|
RawGlobal,
|
|
RawGroupMembership,
|
|
RawPost,
|
|
RawPostMetadata,
|
|
RawPostsInChannel,
|
|
RawPostsInThread,
|
|
RawPreference,
|
|
RawReaction,
|
|
RawRole,
|
|
RawServers,
|
|
RawSystem,
|
|
RawTeamMembership,
|
|
RawTermsOfService,
|
|
RawUser,
|
|
} from '@typings/database/database';
|
|
import Draft from '@typings/database/draft';
|
|
import {OperationType} from '@typings/database/enums';
|
|
import File from '@typings/database/file';
|
|
import Global from '@typings/database/global';
|
|
import GroupMembership from '@typings/database/group_membership';
|
|
import Post from '@typings/database/post';
|
|
import PostMetadata from '@typings/database/post_metadata';
|
|
import PostsInChannel from '@typings/database/posts_in_channel';
|
|
import PostsInThread from '@typings/database/posts_in_thread';
|
|
import Preference from '@typings/database/preference';
|
|
import Reaction from '@typings/database/reaction';
|
|
import Role from '@typings/database/role';
|
|
import Servers from '@typings/database/servers';
|
|
import System from '@typings/database/system';
|
|
import TeamMembership from '@typings/database/team_membership';
|
|
import TermsOfService from '@typings/database/terms_of_service';
|
|
|
|
const {APP, GLOBAL, SERVERS} = MM_TABLES.DEFAULT;
|
|
const {
|
|
CHANNEL_MEMBERSHIP,
|
|
CUSTOM_EMOJI,
|
|
DRAFT,
|
|
FILE,
|
|
GROUP_MEMBERSHIP,
|
|
POST,
|
|
POSTS_IN_CHANNEL,
|
|
POSTS_IN_THREAD,
|
|
POST_METADATA,
|
|
PREFERENCE,
|
|
REACTION,
|
|
ROLE,
|
|
SYSTEM,
|
|
TEAM_MEMBERSHIP,
|
|
TERMS_OF_SERVICE,
|
|
USER,
|
|
} = MM_TABLES.SERVER;
|
|
|
|
/**
|
|
* operateAppRecord: Prepares record of entity 'App' from the DEFAULT database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operateAppRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawApp;
|
|
const record = value.record as App;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
const generator = (app: App) => {
|
|
app._raw.id = isCreateAction ? app.id : record.id;
|
|
app.buildNumber = raw?.buildNumber;
|
|
app.createdAt = raw?.createdAt;
|
|
app.versionNumber = raw?.versionNumber;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
generator,
|
|
tableName: APP,
|
|
value,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operateGlobalRecord: Prepares record of entity 'Global' from the DEFAULT database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operateGlobalRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawGlobal;
|
|
const record = value.record as Global;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
const generator = (global: Global) => {
|
|
global._raw.id = isCreateAction ? global.id : record.id;
|
|
global.name = raw?.name;
|
|
global.value = raw?.value;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
generator,
|
|
tableName: GLOBAL,
|
|
value,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operateServersRecord: Prepares record of entity 'Servers' from the DEFAULT database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operateServersRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawServers;
|
|
const record = value.record as Servers;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
const generator = (servers: Servers) => {
|
|
servers._raw.id = isCreateAction ? servers.id : record.id;
|
|
servers.dbPath = raw?.dbPath;
|
|
servers.displayName = raw?.displayName;
|
|
servers.mentionCount = raw?.mentionCount;
|
|
servers.unreadCount = raw?.unreadCount;
|
|
servers.url = raw?.url;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: SERVERS,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operateCustomEmojiRecord: Prepares record of entity 'CustomEmoji' from the SERVER database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operateCustomEmojiRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawCustomEmoji;
|
|
const record = value.record as CustomEmoji;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
// id of emoji comes from server response
|
|
const generator = (emoji: CustomEmoji) => {
|
|
emoji._raw.id = isCreateAction ? (raw?.id ?? emoji.id) : record.id;
|
|
emoji.name = raw.name;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: CUSTOM_EMOJI,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operateRoleRecord: Prepares record of entity 'Role' from the SERVER database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operateRoleRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawRole;
|
|
const record = value.record as Role;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
// id of role comes from server response
|
|
const generator = (role: Role) => {
|
|
role._raw.id = isCreateAction ? (raw?.id ?? role.id) : record.id;
|
|
role.name = raw?.name;
|
|
role.permissions = raw?.permissions;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: ROLE,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operateSystemRecord: Prepares record of entity 'System' from the SERVER database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operateSystemRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawSystem;
|
|
const record = value.record as System;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
// id of system comes from server response
|
|
const generator = (system: System) => {
|
|
system._raw.id = isCreateAction ? (raw?.id ?? system.id) : record?.id;
|
|
system.name = raw?.name;
|
|
system.value = raw?.value;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: SYSTEM,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operateTermsOfServiceRecord: Prepares record of entity 'TermsOfService' from the SERVER database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operateTermsOfServiceRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawTermsOfService;
|
|
const record = value.record as TermsOfService;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
// id of TOS comes from server response
|
|
const generator = (tos: TermsOfService) => {
|
|
tos._raw.id = isCreateAction ? (raw?.id ?? tos.id) : record?.id;
|
|
tos.acceptedAt = raw?.acceptedAt;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: TERMS_OF_SERVICE,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operatePostRecord: Prepares record of entity 'Post' from the SERVER database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operatePostRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawPost;
|
|
const record = value.record as Post;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
// id of post comes from server response
|
|
const generator = (post: Post) => {
|
|
post._raw.id = isCreateAction ? (raw?.id ?? post.id) : record?.id;
|
|
post.channelId = raw?.channel_id;
|
|
post.createAt = raw?.create_at;
|
|
post.deleteAt = raw?.delete_at || raw?.delete_at === 0 ? raw?.delete_at : 0;
|
|
post.editAt = raw?.edit_at;
|
|
post.updateAt = raw?.update_at;
|
|
post.isPinned = raw!.is_pinned!;
|
|
post.message = Q.sanitizeLikeString(raw?.message);
|
|
post.userId = raw?.user_id;
|
|
post.originalId = raw?.original_id ?? '';
|
|
post.pendingPostId = raw?.pending_post_id ?? '';
|
|
post.previousPostId = raw?.prev_post_id ?? '';
|
|
post.rootId = raw?.root_id ?? '';
|
|
post.type = raw?.type ?? '';
|
|
post.props = raw?.props ?? {};
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: POST,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operatePostInThreadRecord: Prepares record of entity 'POSTS_IN_THREAD' from the SERVER database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operatePostInThreadRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawPostsInThread;
|
|
const record = value.record as PostsInThread;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
const generator = (postsInThread: PostsInThread) => {
|
|
postsInThread.postId = isCreateAction ? raw.post_id : record.id;
|
|
postsInThread.earliest = raw.earliest;
|
|
postsInThread.latest = raw.latest!;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: POSTS_IN_THREAD,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operateReactionRecord: Prepares record of entity 'REACTION' from the SERVER database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operateReactionRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawReaction;
|
|
const record = value.record as Reaction;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
// id of reaction comes from server response
|
|
const generator = (reaction: Reaction) => {
|
|
reaction._raw.id = isCreateAction ? (raw?.id ?? reaction.id) : record?.id;
|
|
reaction.userId = raw.user_id;
|
|
reaction.postId = raw.post_id;
|
|
reaction.emojiName = raw.emoji_name;
|
|
reaction.createAt = raw.create_at;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: REACTION,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operateFileRecord: Prepares record of entity 'FILE' from the SERVER database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operateFileRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawFile;
|
|
const record = value.record as File;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
// id of file comes from server response
|
|
const generator = (file: File) => {
|
|
file._raw.id = isCreateAction ? (raw?.id ?? file.id) : record?.id;
|
|
file.postId = raw.post_id;
|
|
file.name = raw.name;
|
|
file.extension = raw.extension;
|
|
file.size = raw.size;
|
|
file.mimeType = raw?.mime_type ?? '';
|
|
file.width = raw?.width ?? 0;
|
|
file.height = raw?.height ?? 0;
|
|
file.imageThumbnail = raw?.mini_preview ?? '';
|
|
file.localPath = raw?.localPath ?? '';
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: FILE,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operatePostMetadataRecord: Prepares record of entity 'POST_METADATA' from the SERVER database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operatePostMetadataRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawPostMetadata;
|
|
const record = value.record as PostMetadata;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
const generator = (postMeta: PostMetadata) => {
|
|
postMeta._raw.id = isCreateAction ? postMeta.id : record.id;
|
|
postMeta.data = raw.data;
|
|
postMeta.postId = raw.postId;
|
|
postMeta.type = raw.type;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: POST_METADATA,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operateDraftRecord: Prepares record of entity 'DRAFT' from the SERVER database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operateDraftRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const emptyFileInfo: FileInfo[] = [];
|
|
const raw = value.raw as RawDraft;
|
|
|
|
// Draft is client side only; plus you would only be creating/deleting one
|
|
const generator = (draft: Draft) => {
|
|
draft._raw.id = draft.id;
|
|
draft.rootId = raw?.root_id ?? '';
|
|
draft.message = raw?.message ?? '';
|
|
draft.channelId = raw?.channel_id ?? '';
|
|
draft.files = raw?.files ?? emptyFileInfo;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: DRAFT,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operatePostsInChannelRecord: Prepares record of entity 'POSTS_IN_CHANNEL' from the SERVER database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operatePostsInChannelRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawPostsInChannel;
|
|
|
|
const generator = (postsInChannel: PostsInChannel) => {
|
|
postsInChannel._raw.id = raw?.id ?? postsInChannel.id;
|
|
postsInChannel.channelId = raw.channel_id;
|
|
postsInChannel.earliest = raw.earliest;
|
|
postsInChannel.latest = raw.latest;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: POSTS_IN_CHANNEL,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operateUserRecord: Prepares record of entity 'USER' from the SERVER database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operateUserRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawUser;
|
|
const record = value.record as User;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
// id of user comes from server response
|
|
const generator = (user: User) => {
|
|
user._raw.id = isCreateAction ? (raw?.id ?? user.id) : record?.id;
|
|
user.authService = raw.auth_service;
|
|
user.deleteAt = raw.delete_at;
|
|
user.updateAt = raw.update_at;
|
|
user.email = raw.email;
|
|
user.firstName = raw.first_name;
|
|
user.isGuest = raw.roles.includes('system_guest');
|
|
user.lastName = raw.last_name;
|
|
user.lastPictureUpdate = raw.last_picture_update;
|
|
user.locale = raw.locale;
|
|
user.nickname = raw.nickname;
|
|
user.position = raw?.position ?? '';
|
|
user.roles = raw.roles;
|
|
user.username = raw.username;
|
|
user.notifyProps = raw.notify_props;
|
|
user.props = raw.props;
|
|
user.timezone = raw.timezone;
|
|
user.isBot = raw.is_bot;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: USER,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operatePreferenceRecord: Prepares record of entity 'PREFERENCE' from the SERVER database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operatePreferenceRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawPreference;
|
|
const record = value.record as Preference;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
// id of preference comes from server response
|
|
const generator = (preference: Preference) => {
|
|
preference._raw.id = isCreateAction ? (raw?.id ?? preference.id) : record?.id;
|
|
preference.category = raw.category;
|
|
preference.name = raw.name;
|
|
preference.userId = raw.user_id;
|
|
preference.value = raw.value;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: PREFERENCE,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operatePreferenceRecord: Prepares record of entity 'TEAM_MEMBERSHIP' from the SERVER database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operateTeamMembershipRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawTeamMembership;
|
|
const record = value.record as TeamMembership;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
// id of preference comes from server response
|
|
const generator = (teamMembership: TeamMembership) => {
|
|
teamMembership._raw.id = isCreateAction ? (raw?.id ?? teamMembership.id) : record?.id;
|
|
teamMembership.teamId = raw.team_id;
|
|
teamMembership.userId = raw.user_id;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: TEAM_MEMBERSHIP,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operateGroupMembershipRecord: Prepares record of entity 'GROUP_MEMBERSHIP' from the SERVER database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operateGroupMembershipRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawGroupMembership;
|
|
const record = value.record as GroupMembership;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
// id of preference comes from server response
|
|
const generator = (groupMember: GroupMembership) => {
|
|
groupMember._raw.id = isCreateAction ? (raw?.id ?? groupMember.id) : record?.id;
|
|
groupMember.groupId = raw.group_id;
|
|
groupMember.userId = raw.user_id;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: GROUP_MEMBERSHIP,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operateChannelMembershipRecord: Prepares record of entity 'CHANNEL_MEMBERSHIP' from the SERVER database for update or create actions.
|
|
* @param {DataFactoryArgs} operator
|
|
* @param {Database} operator.database
|
|
* @param {MatchExistingRecord} operator.value
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export const operateChannelMembershipRecord = async ({action, database, value}: DataFactoryArgs) => {
|
|
const raw = value.raw as RawChannelMembership;
|
|
const record = value.record as ChannelMembership;
|
|
const isCreateAction = action === OperationType.CREATE;
|
|
|
|
// id of preference comes from server response
|
|
const generator = (channelMember: ChannelMembership) => {
|
|
channelMember._raw.id = isCreateAction ? (raw?.id ?? channelMember.id) : record?.id;
|
|
channelMember.channelId = raw.channel_id;
|
|
channelMember.userId = raw.user_id;
|
|
};
|
|
|
|
return operateBaseRecord({
|
|
action,
|
|
database,
|
|
tableName: CHANNEL_MEMBERSHIP,
|
|
value,
|
|
generator,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* operateBaseRecord: This is the last step for each operator and depending on the 'action', it will either prepare an
|
|
* existing record for UPDATE or prepare a collection for CREATE
|
|
*
|
|
* @param {DataFactoryArgs} operatorBase
|
|
* @param {Database} operatorBase.database
|
|
* @param {string} operatorBase.tableName
|
|
* @param {MatchExistingRecord} operatorBase.value
|
|
* @param {((model: Model) => void)} operatorBase.generator
|
|
* @returns {Promise<any>}
|
|
*/
|
|
const operateBaseRecord = async ({action, database, tableName, value, generator}: DataFactoryArgs) => {
|
|
if (action === OperationType.UPDATE) {
|
|
// Two possible scenarios:
|
|
// 1. We are dealing with either duplicates here and if so, we'll update instead of create
|
|
// 2. This is just a normal update operation
|
|
|
|
const record = value.record as Model;
|
|
return record.prepareUpdate(() => generator!(record));
|
|
}
|
|
|
|
// Two possible scenarios
|
|
// 1. We don't have a record yet to update; so we create it
|
|
// 2. This is just a normal create operation
|
|
return database.collections.get(tableName!).prepareCreate(generator);
|
|
};
|