[Gekidou] Extract common observers to queries (#5984)

* Extract common observers to queries

* Separate also queries and more agressive refactoring

* Use query to avoid throws from findAndObserve

* Fix minor error

* Address feedback

* Address feedback

* Address feedback

* Fix model types

* Address feedback
This commit is contained in:
Daniel Espino García
2022-03-23 13:19:29 +01:00
committed by GitHub
parent 3e94958ab0
commit 7c642b1e80
179 changed files with 1615 additions and 2011 deletions

View File

@@ -8,7 +8,7 @@ import {addFilesToDraft} from '@actions/local/draft';
import {PROGRESS_TIME_TO_STORE} from '@constants/files';
import DatabaseManager from '@database/manager';
import ServerDataOperator from '@database/operator/server_data_operator';
import {queryDraft} from '@queries/servers/drafts';
import {getDraft} from '@queries/servers/drafts';
import TestHelper from '@test/test_helper';
import {exportedForTesting} from '.';
@@ -94,7 +94,7 @@ describe('draft upload manager', () => {
// Wait for other promises (on complete write) to finish
await new Promise(process.nextTick);
const draft = await queryDraft(operator.database, channelId, rootId);
const draft = await getDraft(operator.database, channelId, rootId);
expect(draft?.files.length).toBe(1);
expect(draft?.files[0].id).toBe(fileServerId);
@@ -121,7 +121,7 @@ describe('draft upload manager', () => {
await new Promise(process.nextTick);
// There has been progress, but we are not storing in to the database since the app is still active.
let draft = await queryDraft(operator.database, channelId, rootId);
let draft = await getDraft(operator.database, channelId, rootId);
expect(draft?.files.length).toBe(1);
expect(draft?.files[0].bytesRead).toBeUndefined();
@@ -131,7 +131,7 @@ describe('draft upload manager', () => {
await new Promise(process.nextTick);
// After a failure, we store the progress on the database, so we can resume from the point before failure.
draft = await queryDraft(operator.database, channelId, rootId);
draft = await getDraft(operator.database, channelId, rootId);
expect(draft?.files.length).toBe(1);
expect(draft?.files[0].bytesRead).toBe(bytesRead);
expect(draft?.files[0].failed).toBe(true);
@@ -196,7 +196,7 @@ describe('draft upload manager', () => {
for (let i = 0; i < 3; i++) {
// eslint-disable-next-line no-await-in-loop
const draft = await queryDraft(operator.database, channelIds[i], rootIds[i]);
const draft = await getDraft(operator.database, channelIds[i], rootIds[i]);
expect(draft?.files.length).toBe(1);
expect(draft?.files[0].bytesRead).toBe(bytesReads[i]);
}
@@ -213,7 +213,7 @@ describe('draft upload manager', () => {
for (let i = 0; i < 3; i++) {
// eslint-disable-next-line no-await-in-loop
const draft = await queryDraft(operator.database, channelIds[i], rootIds[i]);
const draft = await getDraft(operator.database, channelIds[i], rootIds[i]);
expect(draft?.files.length).toBe(1);
expect(draft?.files[0].bytesRead).toBe(bytesReads[i]);
}
@@ -231,7 +231,7 @@ describe('draft upload manager', () => {
for (let i = 0; i < 3; i++) {
// eslint-disable-next-line no-await-in-loop
const draft = await queryDraft(operator.database, channelIds[i], rootIds[i]);
const draft = await getDraft(operator.database, channelIds[i], rootIds[i]);
expect(draft?.files.length).toBe(1);
expect(draft?.files[0].bytesRead).toBe(bytesReadsStore[i]);
}
@@ -245,7 +245,7 @@ describe('draft upload manager', () => {
for (let i = 0; i < 3; i++) {
// eslint-disable-next-line no-await-in-loop
const draft = await queryDraft(operator.database, channelIds[i], rootIds[i]);
const draft = await getDraft(operator.database, channelIds[i], rootIds[i]);
expect(draft?.files.length).toBe(1);
expect(draft?.files[0].bytesRead).toBe(bytesReadsStore[i]);
}
@@ -270,7 +270,7 @@ describe('draft upload manager', () => {
// Wait for other promises (on complete write) to finish
await new Promise(process.nextTick);
const draft = await queryDraft(operator.database, channelId, rootId);
const draft = await getDraft(operator.database, channelId, rootId);
expect(draft?.files.length).toBe(1);
expect(draft?.files[0].id).toBeUndefined();
expect(draft?.files[0].failed).toBe(true);
@@ -294,7 +294,7 @@ describe('draft upload manager', () => {
// Wait for other promises (on complete write) to finish
await new Promise(process.nextTick);
const draft = await queryDraft(operator.database, channelId, rootId);
const draft = await getDraft(operator.database, channelId, rootId);
expect(draft?.files.length).toBe(1);
expect(draft?.files[0].id).toBeUndefined();
expect(draft?.files[0].failed).toBe(true);
@@ -318,7 +318,7 @@ describe('draft upload manager', () => {
// Wait for other promises (on complete write) to finish
await new Promise(process.nextTick);
const draft = await queryDraft(operator.database, channelId, rootId);
const draft = await getDraft(operator.database, channelId, rootId);
expect(draft?.files.length).toBe(1);
expect(draft?.files[0].id).toBeUndefined();
expect(draft?.files[0].failed).toBe(true);

View File

@@ -16,7 +16,7 @@ import {getLaunchPropsFromDeepLink, relaunchApp} from '@init/launch';
import NetworkManager from '@init/network_manager';
import PushNotifications from '@init/push_notifications';
import WebsocketManager from '@init/websocket_manager';
import {queryCurrentUser} from '@queries/servers/user';
import {getCurrentUser} from '@queries/servers/user';
import EphemeralStore from '@store/ephemeral_store';
import {LaunchType} from '@typings/launch';
import {deleteFileCache} from '@utils/file';
@@ -158,7 +158,7 @@ class GlobalEventHandler {
resetLocale = async () => {
if (Object.keys(DatabaseManager.serverDatabases).length) {
const serverDatabase = await DatabaseManager.getActiveServerDatabase();
const user = await queryCurrentUser(serverDatabase!);
const user = await getCurrentUser(serverDatabase!);
resetMomentLocale(user?.locale);
} else {
resetMomentLocale();

View File

@@ -9,8 +9,8 @@ import {appEntry, pushNotificationEntry, upgradeEntry} from '@actions/remote/ent
import {Screens} from '@constants';
import DatabaseManager from '@database/manager';
import {getActiveServerUrl, getServerCredentials, removeServerCredentials} from '@init/credentials';
import {queryThemeForCurrentTeam} from '@queries/servers/preference';
import {queryCurrentUserId} from '@queries/servers/system';
import {getThemeForCurrentTeam} from '@queries/servers/preference';
import {getCurrentUserId} from '@queries/servers/system';
import {goToScreen, resetToHome, resetToSelectServer} from '@screens/navigation';
import EphemeralStore from '@store/ephemeral_store';
import {DeepLinkChannel, DeepLinkDM, DeepLinkGM, DeepLinkPermalink, DeepLinkType, DeepLinkWithData, LaunchProps, LaunchType} from '@typings/launch';
@@ -81,8 +81,8 @@ const launchApp = async (props: LaunchProps, resetNavigation = true) => {
const database = DatabaseManager.serverDatabases[serverUrl]?.database;
let hasCurrentUser = false;
if (database) {
EphemeralStore.theme = await queryThemeForCurrentTeam(database);
const currentUserId = await queryCurrentUserId(database);
EphemeralStore.theme = await getThemeForCurrentTeam(database);
const currentUserId = await getCurrentUserId(database);
hasCurrentUser = Boolean(currentUserId);
}

View File

@@ -22,7 +22,7 @@ import DatabaseManager from '@database/manager';
import {DEFAULT_LOCALE, getLocalizedMessage, t} from '@i18n';
import NativeNotifications from '@notifications';
import {queryServerName} from '@queries/app/servers';
import {queryCurrentChannelId} from '@queries/servers/system';
import {getCurrentChannelId} from '@queries/servers/system';
import {showOverlay} from '@screens/navigation';
import EphemeralStore from '@store/ephemeral_store';
import {isTablet} from '@utils/helpers';
@@ -134,7 +134,7 @@ class PushNotifications {
const isTabletDevice = await isTablet();
const activeServerUrl = await getActiveServerUrl();
const displayName = await queryServerName(DatabaseManager.appDatabase!.database, serverUrl);
const channelId = await queryCurrentChannelId(database);
const channelId = await getCurrentChannelId(database);
let serverName;
if (serverUrl !== activeServerUrl && Object.keys(DatabaseManager.serverDatabases).length > 1) {
serverName = displayName;

View File

@@ -12,7 +12,7 @@ import {handleClose, handleEvent, handleFirstConnect, handleReconnect} from '@ac
import WebSocketClient from '@client/websocket';
import {General} from '@constants';
import DatabaseManager from '@database/manager';
import {queryCurrentUserId} from '@queries/servers/system';
import {getCurrentUserId} from '@queries/servers/system';
import {queryAllUsers} from '@queries/servers/user';
import type {ServerCredential} from '@typings/credentials';
@@ -157,13 +157,8 @@ class WebsocketManager {
return;
}
const currentUserId = await queryCurrentUserId(database.database);
const users = await queryAllUsers(database.database);
const userIds = users.map((u) => u.id).filter((id) => id !== currentUserId);
if (!userIds.length) {
return;
}
const currentUserId = await getCurrentUserId(database.database);
const userIds = (await queryAllUsers(database.database).fetchIds()).filter((id) => id !== currentUserId);
fetchStatusByIds(serverUrl, userIds);
};