Chore: Migrate method getSettings to typescript (#3703)
* chore: migrate getSettings to typescript and and some types * chore: remove this and add current to code * chore: add current
This commit is contained in:
parent
219462ba3d
commit
370798f100
|
@ -9,4 +9,22 @@ export interface ISettings {
|
||||||
_updatedAt?: Date;
|
_updatedAt?: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface IPreparedSettings {
|
||||||
|
_id: string;
|
||||||
|
value: string;
|
||||||
|
enterprise: boolean;
|
||||||
|
valueAsString?: string;
|
||||||
|
valueAsBoolean?: boolean;
|
||||||
|
valueAsNumber?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ISettingsIcon {
|
||||||
|
_id: string;
|
||||||
|
value: {
|
||||||
|
defaultUrl: string;
|
||||||
|
url?: string;
|
||||||
|
};
|
||||||
|
enterprise: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export type TSettingsModel = ISettings & Model;
|
export type TSettingsModel = ISettings & Model;
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
|
||||||
import { Q } from '@nozbe/watermelondb';
|
import { Q } from '@nozbe/watermelondb';
|
||||||
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
||||||
|
|
||||||
import { addSettings, clearSettings } from '../../actions/settings';
|
import { addSettings, clearSettings } from '../../actions/settings';
|
||||||
import RocketChat from '../rocketchat';
|
|
||||||
import { store as reduxStore } from '../auxStore';
|
|
||||||
import settings from '../../constants/settings';
|
|
||||||
import log from '../../utils/log';
|
|
||||||
import database from '../database';
|
|
||||||
import fetch from '../../utils/fetch';
|
|
||||||
import { DEFAULT_AUTO_LOCK } from '../../constants/localAuthentication';
|
import { DEFAULT_AUTO_LOCK } from '../../constants/localAuthentication';
|
||||||
|
import settings from '../../constants/settings';
|
||||||
|
import { IPreparedSettings, ISettingsIcon } from '../../definitions';
|
||||||
|
import fetch from '../../utils/fetch';
|
||||||
|
import log from '../../utils/log';
|
||||||
|
import { store as reduxStore } from '../auxStore';
|
||||||
|
import database from '../database';
|
||||||
|
import RocketChat from '../rocketchat';
|
||||||
|
import sdk from '../rocketchat/services/sdk';
|
||||||
import protectedFunction from './helpers/protectedFunction';
|
import protectedFunction from './helpers/protectedFunction';
|
||||||
|
|
||||||
const serverInfoKeys = [
|
const serverInfoKeys = [
|
||||||
|
@ -41,7 +43,7 @@ const loginSettings = [
|
||||||
'Accounts_Iframe_api_method'
|
'Accounts_Iframe_api_method'
|
||||||
];
|
];
|
||||||
|
|
||||||
const serverInfoUpdate = async (serverInfo, iconSetting) => {
|
const serverInfoUpdate = async (serverInfo: IPreparedSettings[], iconSetting: ISettingsIcon) => {
|
||||||
const serversDB = database.servers;
|
const serversDB = database.servers;
|
||||||
const serverId = reduxStore.getState().server.server;
|
const serverId = reduxStore.getState().server.server;
|
||||||
const serversCollection = serversDB.get('servers');
|
const serversCollection = serversDB.get('servers');
|
||||||
|
@ -73,7 +75,7 @@ const serverInfoUpdate = async (serverInfo, iconSetting) => {
|
||||||
return { ...allSettings, autoLockTime: DEFAULT_AUTO_LOCK };
|
return { ...allSettings, autoLockTime: DEFAULT_AUTO_LOCK };
|
||||||
}
|
}
|
||||||
// if Force_Screen_Lock_After > 0 and forceScreenLock is enabled, use it
|
// if Force_Screen_Lock_After > 0 and forceScreenLock is enabled, use it
|
||||||
if (setting.valueAsNumber > 0 && forceScreenLock) {
|
if (setting.valueAsNumber && setting.valueAsNumber > 0 && forceScreenLock) {
|
||||||
return { ...allSettings, autoLockTime: setting.valueAsNumber };
|
return { ...allSettings, autoLockTime: setting.valueAsNumber };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,7 +93,7 @@ const serverInfoUpdate = async (serverInfo, iconSetting) => {
|
||||||
info = { ...info, iconURL };
|
info = { ...info, iconURL };
|
||||||
}
|
}
|
||||||
|
|
||||||
await serversDB.action(async () => {
|
await serversDB.write(async () => {
|
||||||
try {
|
try {
|
||||||
await server.update(record => {
|
await server.update(record => {
|
||||||
Object.assign(record, info);
|
Object.assign(record, info);
|
||||||
|
@ -102,7 +104,7 @@ const serverInfoUpdate = async (serverInfo, iconSetting) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function getLoginSettings({ server }) {
|
export async function getLoginSettings({ server }: { server: string }): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const settingsParams = JSON.stringify(loginSettings);
|
const settingsParams = JSON.stringify(loginSettings);
|
||||||
const result = await fetch(`${server}/api/v1/settings.public?query={"_id":{"$in":${settingsParams}}}`).then(response =>
|
const result = await fetch(`${server}/api/v1/settings.public?query={"_id":{"$in":${settingsParams}}}`).then(response =>
|
||||||
|
@ -111,14 +113,14 @@ export async function getLoginSettings({ server }) {
|
||||||
|
|
||||||
if (result.success && result.settings.length) {
|
if (result.success && result.settings.length) {
|
||||||
reduxStore.dispatch(clearSettings());
|
reduxStore.dispatch(clearSettings());
|
||||||
reduxStore.dispatch(addSettings(this.parseSettings(this._prepareSettings(result.settings))));
|
reduxStore.dispatch(addSettings(RocketChat.parseSettings(RocketChat._prepareSettings(result.settings))));
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log(e);
|
log(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function setSettings() {
|
export async function setSettings(): Promise<void> {
|
||||||
const db = database.active;
|
const db = database.active;
|
||||||
const settingsCollection = db.get('settings');
|
const settingsCollection = db.get('settings');
|
||||||
const settingsRecords = await settingsCollection.query().fetch();
|
const settingsRecords = await settingsCollection.query().fetch();
|
||||||
|
@ -133,17 +135,19 @@ export async function setSettings() {
|
||||||
reduxStore.dispatch(addSettings(RocketChat.parseSettings(parsed.slice(0, parsed.length))));
|
reduxStore.dispatch(addSettings(RocketChat.parseSettings(parsed.slice(0, parsed.length))));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function subscribeSettings() {
|
export function subscribeSettings(): void {
|
||||||
return RocketChat.subscribe('stream-notify-all', 'public-settings-changed');
|
return RocketChat.subscribe('stream-notify-all', 'public-settings-changed');
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function () {
|
type IData = ISettingsIcon | IPreparedSettings;
|
||||||
|
|
||||||
|
export default async function (): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const db = database.active;
|
const db = database.active;
|
||||||
const settingsParams = Object.keys(settings).filter(key => !loginSettings.includes(key));
|
const settingsParams = Object.keys(settings).filter(key => !loginSettings.includes(key));
|
||||||
// RC 0.60.0
|
// RC 0.60.0
|
||||||
const result = await fetch(
|
const result = await fetch(
|
||||||
`${this.sdk.client.host}/api/v1/settings.public?query={"_id":{"$in":${JSON.stringify(settingsParams)}}}&count=${
|
`${sdk.current.client.host}/api/v1/settings.public?query={"_id":{"$in":${JSON.stringify(settingsParams)}}}&count=${
|
||||||
settingsParams.length
|
settingsParams.length
|
||||||
}`
|
}`
|
||||||
).then(response => response.json());
|
).then(response => response.json());
|
||||||
|
@ -151,33 +155,32 @@ export default async function () {
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const data = result.settings || [];
|
const data: IData[] = result.settings || [];
|
||||||
const filteredSettings = this._prepareSettings(data);
|
const filteredSettings: IPreparedSettings[] = RocketChat._prepareSettings(data);
|
||||||
const filteredSettingsIds = filteredSettings.map(s => s._id);
|
const filteredSettingsIds = filteredSettings.map(s => s._id);
|
||||||
|
|
||||||
reduxStore.dispatch(addSettings(this.parseSettings(filteredSettings)));
|
reduxStore.dispatch(addSettings(RocketChat.parseSettings(filteredSettings)));
|
||||||
|
|
||||||
// filter server info
|
// filter server info
|
||||||
const serverInfo = filteredSettings.filter(i1 => serverInfoKeys.includes(i1._id));
|
const serverInfo = filteredSettings.filter(i1 => serverInfoKeys.includes(i1._id));
|
||||||
const iconSetting = data.find(item => item._id === 'Assets_favicon_512');
|
const iconSetting = data.find(icon => icon._id === 'Assets_favicon_512');
|
||||||
try {
|
try {
|
||||||
await serverInfoUpdate(serverInfo, iconSetting);
|
await serverInfoUpdate(serverInfo, iconSetting as ISettingsIcon);
|
||||||
} catch {
|
} catch {
|
||||||
// Server not found
|
// Server not found
|
||||||
}
|
}
|
||||||
|
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
const settingsCollection = db.get('settings');
|
const settingsCollection = db.get('settings');
|
||||||
const allSettingsRecords = await settingsCollection.query(Q.where('id', Q.oneOf(filteredSettingsIds))).fetch();
|
const allSettingsRecords = await settingsCollection.query(Q.where('id', Q.oneOf(filteredSettingsIds))).fetch();
|
||||||
|
|
||||||
// filter settings
|
// filter settings
|
||||||
let settingsToCreate = filteredSettings.filter(i1 => !allSettingsRecords.find(i2 => i1._id === i2.id));
|
const settingsToCreate = filteredSettings.filter(i1 => !allSettingsRecords.find(i2 => i1._id === i2.id));
|
||||||
let settingsToUpdate = allSettingsRecords.filter(i1 => filteredSettings.find(i2 => i1.id === i2._id));
|
const settingsToUpdate = allSettingsRecords.filter(i1 => filteredSettings.find(i2 => i1.id === i2._id));
|
||||||
|
|
||||||
// Create
|
// Create
|
||||||
settingsToCreate = settingsToCreate.map(setting =>
|
const settingsToCreateMapped = settingsToCreate.map(setting =>
|
||||||
settingsCollection.prepareCreate(
|
settingsCollection.prepareCreate(
|
||||||
protectedFunction(s => {
|
protectedFunction((s: any) => {
|
||||||
s._raw = sanitizedRaw({ id: setting._id }, settingsCollection.schema);
|
s._raw = sanitizedRaw({ id: setting._id }, settingsCollection.schema);
|
||||||
Object.assign(s, setting);
|
Object.assign(s, setting);
|
||||||
})
|
})
|
||||||
|
@ -185,16 +188,16 @@ export default async function () {
|
||||||
);
|
);
|
||||||
|
|
||||||
// Update
|
// Update
|
||||||
settingsToUpdate = settingsToUpdate.map(setting => {
|
const settingsToUpdateMapped = settingsToUpdate.map(setting => {
|
||||||
const newSetting = filteredSettings.find(s => s._id === setting.id);
|
const newSetting = filteredSettings.find(s => s._id === setting.id);
|
||||||
return setting.prepareUpdate(
|
return setting.prepareUpdate(
|
||||||
protectedFunction(s => {
|
protectedFunction((s: any) => {
|
||||||
Object.assign(s, newSetting);
|
Object.assign(s, newSetting);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
const allRecords = [...settingsToCreate, ...settingsToUpdate];
|
const allRecords = [...settingsToCreateMapped, ...settingsToUpdateMapped];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await db.batch(...allRecords);
|
await db.batch(...allRecords);
|
Loading…
Reference in New Issue