mattermost-mobile/app/queries/servers/system.ts
Avinash Lingaloo e8ce78f39d
MM-36721 - [GEKIDOU] Porting Markdown components (#5586)
* Started with Channel Post List

* Added markdown hashtag

* Added TouchableWithFeedback component

* Added utils/bottom_sheet

* Removed BottomSheet in favor of future SlideUpPanel

* Added markdown_block_quote

* Added markdown_list_item

* Added markdown_list

* Added MarkDownTableCell component

* Markdown_table - in progress - need to verify TS

* Added markdown_table

* Update Podfile.lock

* Added deep_linking constant

* Added utils/draft

* Update config to include ExperimentalNormalizeMarkdownLinks

* Added markdown_link

* Added markdown_table_row

* Added ProgressiveImage and RetriableImage components and images utils

* Converted Retriable component to functional component

* Added type definition for commonmark

* Continuing with markdown TS

* Markdown - Typing props [ in progress ]

* Fix boolean flag with mardown block quote

* Adding observable config to markdown_link

* TS Fixes [ in progress ]

* TS fixes

* TS fixes - TextStyles

* Update markdown.tsx

* TS fixes on markdown

* TS Fixes - AtMention component

* AtMention [ IN PROGRESS ]

* Add markdown support

* Fix emoji and jumboEmoji on iOS

* Fix handleMyTeam operator

* Fix navigation style based on theme

* Fix iOS MattermostManaged deleteDatabse return error type

* wrap setNavigationStackStyles under a requestAnimationFrame

* Add preventDoubleTap to channel mention

* Increase double tap to 750ms

* Fix handleReceivedPostsInChannel chunk query

* Set initial navigation theme

* Swizzle FastImage on iOS

* fix preventDoubleTap test

Co-authored-by: Avinash Lingaloo <>
Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2021-08-02 20:30:17 +04:00

169 lines
5.2 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Database} from '@nozbe/watermelondb';
import {MM_TABLES, SYSTEM_IDENTIFIERS} from '@constants/database';
import type ServerDataOperator from '@database/operator/server_data_operator';
import type SystemModel from '@typings/database/models/servers/system';
type PrepareCommonSystemValuesArgs = {
config?: ClientConfig;
currentChannelId?: string;
currentTeamId?: string;
currentUserId?: string;
license?: ClientLicense;
}
const {SERVER: {SYSTEM}} = MM_TABLES;
export const queryCurrentChannelId = async (serverDatabase: Database) => {
try {
const currentChannelId = await serverDatabase.get(SYSTEM).find(SYSTEM_IDENTIFIERS.CURRENT_CHANNEL_ID) as SystemModel;
return (currentChannelId?.value || '') as string;
} catch {
return '';
}
};
export const queryCurrentTeamId = async (serverDatabase: Database) => {
try {
const currentTeamId = await serverDatabase.get(SYSTEM).find(SYSTEM_IDENTIFIERS.CURRENT_TEAM_ID) as SystemModel;
return (currentTeamId?.value || '') as string;
} catch {
return '';
}
};
export const queryCurrentUserId = async (serverDatabase: Database) => {
try {
const currentUserId = await serverDatabase.get(SYSTEM).find(SYSTEM_IDENTIFIERS.CURRENT_USER_ID) as SystemModel;
return (currentUserId?.value || '') as string;
} catch {
return '';
}
};
export const queryCommonSystemValues = async (serverDatabase: Database) => {
const systemRecords = (await serverDatabase.collections.get(SYSTEM).query().fetch()) as SystemModel[];
let config = {};
let license = {};
let currentChannelId = '';
let currentTeamId = '';
let currentUserId = '';
systemRecords.forEach((systemRecord) => {
switch (systemRecord.id) {
case SYSTEM_IDENTIFIERS.CONFIG:
config = systemRecord.value;
break;
case SYSTEM_IDENTIFIERS.CURRENT_CHANNEL_ID:
currentChannelId = systemRecord.value;
break;
case SYSTEM_IDENTIFIERS.CURRENT_TEAM_ID:
currentTeamId = systemRecord.value;
break;
case SYSTEM_IDENTIFIERS.CURRENT_USER_ID:
currentUserId = systemRecord.value;
break;
case SYSTEM_IDENTIFIERS.LICENSE:
license = systemRecord.value as ClientLicense;
break;
}
});
return {
currentChannelId,
currentTeamId,
currentUserId,
config: (config as ClientConfig),
license: (license as ClientLicense),
};
};
export const queryWebSocketLastDisconnected = async (serverDatabase: Database) => {
try {
const websocketLastDisconnected = await serverDatabase.get(SYSTEM).find(SYSTEM_IDENTIFIERS.WEBSOCKET) as SystemModel;
return (parseInt(websocketLastDisconnected?.value || 0, 10) || 0);
} catch {
return 0;
}
};
export const prepareCommonSystemValues = (
operator: ServerDataOperator, values: PrepareCommonSystemValuesArgs) => {
try {
const {config, currentChannelId, currentTeamId, currentUserId, license} = values;
const systems: IdValue[] = [];
if (config !== undefined) {
systems.push({
id: SYSTEM_IDENTIFIERS.CONFIG,
value: JSON.stringify(config),
});
}
if (license !== undefined) {
systems.push({
id: SYSTEM_IDENTIFIERS.LICENSE,
value: JSON.stringify(license),
});
}
if (currentUserId !== undefined) {
systems.push({
id: SYSTEM_IDENTIFIERS.CURRENT_USER_ID,
value: currentUserId,
});
}
if (currentTeamId !== undefined) {
systems.push({
id: SYSTEM_IDENTIFIERS.CURRENT_TEAM_ID,
value: currentTeamId,
});
}
if (currentChannelId !== undefined) {
systems.push({
id: SYSTEM_IDENTIFIERS.CURRENT_CHANNEL_ID,
value: currentChannelId,
});
}
return operator.handleSystem({
systems,
prepareRecordsOnly: true,
});
} catch {
return undefined;
}
};
export const setCurrentChannelId = async (operator: ServerDataOperator, channelId: string) => {
try {
const models = await prepareCommonSystemValues(operator, {currentChannelId: channelId});
if (models) {
await operator.batchRecords(models);
}
return {currentChannelId: channelId};
} catch (error) {
return {error};
}
};
export const setCurrentTeamAndChannelId = async (operator: ServerDataOperator, teamId?: string, channelId?: string) => {
try {
const models = await prepareCommonSystemValues(operator, {
currentChannelId: channelId,
currentTeamId: teamId,
});
if (models) {
await operator.batchRecords(models);
}
return {currentChannelId: channelId};
} catch (error) {
return {error};
}
};