mirror of
https://github.com/zulip/zulip-desktop.git
synced 2025-10-25 00:53:39 +00:00
typescript: Migrate domain-util to typescript.
This commit is contained in:
committed by
Akash Nimare
parent
8ef39553b8
commit
c6c36b698d
@@ -1,27 +1,30 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { app, dialog } = require('electron').remote;
|
import fs from 'fs';
|
||||||
const fs = require('fs');
|
import path from 'path';
|
||||||
const path = require('path');
|
import JsonDB from 'node-json-db';
|
||||||
const JsonDB = require('node-json-db');
|
import * as request from 'request';
|
||||||
const request = require('request');
|
import * as escape from 'escape-html';
|
||||||
const escape = require('escape-html');
|
|
||||||
|
|
||||||
const Logger = require('./logger-util');
|
import Logger = require('./logger-util');
|
||||||
|
import electron = require('electron');
|
||||||
|
|
||||||
const RequestUtil = require(__dirname + '/../utils/request-util.js');
|
import RequestUtil = require('./request-util.js');
|
||||||
const Messages = require(__dirname + '/../../../resources/messages.js');
|
import Messages = require('../../../resources/messages');
|
||||||
|
|
||||||
|
const { app, dialog } = electron.remote;
|
||||||
|
|
||||||
const logger = new Logger({
|
const logger = new Logger({
|
||||||
file: `domain-util.log`,
|
file: `domain-util.log`,
|
||||||
timestamp: true
|
timestamp: true
|
||||||
});
|
});
|
||||||
|
|
||||||
let instance = null;
|
let instance: null | DomainUtil = null;
|
||||||
|
|
||||||
const defaultIconUrl = '../renderer/img/icon.png';
|
const defaultIconUrl = '../renderer/img/icon.png';
|
||||||
|
|
||||||
class DomainUtil {
|
class DomainUtil {
|
||||||
|
db: any;
|
||||||
constructor() {
|
constructor() {
|
||||||
if (instance) {
|
if (instance) {
|
||||||
return instance;
|
return instance;
|
||||||
@@ -42,7 +45,7 @@ class DomainUtil {
|
|||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
getDomains() {
|
getDomains(): any {
|
||||||
this.reloadDB();
|
this.reloadDB();
|
||||||
if (this.db.getData('/').domains === undefined) {
|
if (this.db.getData('/').domains === undefined) {
|
||||||
return [];
|
return [];
|
||||||
@@ -51,18 +54,18 @@ class DomainUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getDomain(index) {
|
getDomain(index: number): any {
|
||||||
this.reloadDB();
|
this.reloadDB();
|
||||||
return this.db.getData(`/domains[${index}]`);
|
return this.db.getData(`/domains[${index}]`);
|
||||||
}
|
}
|
||||||
|
|
||||||
updateDomain(index, server) {
|
updateDomain(index: number, server: object): void {
|
||||||
this.reloadDB();
|
this.reloadDB();
|
||||||
this.db.push(`/domains[${index}]`, server, true);
|
this.db.push(`/domains[${index}]`, server, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
addDomain(server) {
|
addDomain(server: any): Promise<void> {
|
||||||
const ignoreCerts = server.ignoreCerts;
|
const { ignoreCerts } = server;
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
if (server.icon) {
|
if (server.icon) {
|
||||||
this.saveServerIcon(server, ignoreCerts).then(localIconUrl => {
|
this.saveServerIcon(server, ignoreCerts).then(localIconUrl => {
|
||||||
@@ -80,18 +83,18 @@ class DomainUtil {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
removeDomains() {
|
removeDomains(): void {
|
||||||
this.db.delete('/domains');
|
this.db.delete('/domains');
|
||||||
this.reloadDB();
|
this.reloadDB();
|
||||||
}
|
}
|
||||||
|
|
||||||
removeDomain(index) {
|
removeDomain(index: number): void {
|
||||||
this.db.delete(`/domains[${index}]`);
|
this.db.delete(`/domains[${index}]`);
|
||||||
this.reloadDB();
|
this.reloadDB();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if domain is already added
|
// Check if domain is already added
|
||||||
duplicateDomain(domain) {
|
duplicateDomain(domain: any): boolean {
|
||||||
domain = this.formatUrl(domain);
|
domain = this.formatUrl(domain);
|
||||||
const servers = this.getDomains();
|
const servers = this.getDomains();
|
||||||
for (const i in servers) {
|
for (const i in servers) {
|
||||||
@@ -102,30 +105,30 @@ class DomainUtil {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async checkCertError(domain, serverConf, error, silent) {
|
async checkCertError(domain: object, serverConf: any, error: string, silent: boolean): Promise<string | object> {
|
||||||
if (silent) {
|
if (silent) {
|
||||||
// since getting server settings has already failed
|
// since getting server settings has already failed
|
||||||
return serverConf;
|
return serverConf;
|
||||||
} else {
|
} else {
|
||||||
// Report error to sentry to get idea of possible certificate errors
|
// Report error to sentry to get idea of possible certificate errors
|
||||||
// users get when adding the servers
|
// users get when adding the servers
|
||||||
logger.reportSentry(new Error(error));
|
logger.reportSentry(new Error(error).toString());
|
||||||
const certErrorMessage = Messages.certErrorMessage(domain, error);
|
const certErrorMessage = Messages.certErrorMessage(domain, error);
|
||||||
const certErrorDetail = Messages.certErrorDetail();
|
const certErrorDetail = Messages.certErrorDetail();
|
||||||
|
|
||||||
const response = await dialog.showMessageBox({
|
const response = await (dialog.showMessageBox({
|
||||||
type: 'warning',
|
type: 'warning',
|
||||||
buttons: ['Yes', 'No'],
|
buttons: ['Yes', 'No'],
|
||||||
defaultId: 1,
|
defaultId: 1,
|
||||||
message: certErrorMessage,
|
message: certErrorMessage,
|
||||||
detail: certErrorDetail
|
detail: certErrorDetail
|
||||||
});
|
}) as any); // TODO: TypeScript - Figure this out
|
||||||
if (response === 0) {
|
if (response === 0) {
|
||||||
// set ignoreCerts parameter to true in case user responds with yes
|
// set ignoreCerts parameter to true in case user responds with yes
|
||||||
serverConf.ignoreCerts = true;
|
serverConf.ignoreCerts = true;
|
||||||
try {
|
try {
|
||||||
return await this.getServerSettings(domain, serverConf.ignoreCerts);
|
return await this.getServerSettings(domain, serverConf.ignoreCerts);
|
||||||
} catch (err) {
|
} catch (_) {
|
||||||
if (error === Messages.noOrgsError(domain)) {
|
if (error === Messages.noOrgsError(domain)) {
|
||||||
throw new Error(error);
|
throw new Error(error);
|
||||||
}
|
}
|
||||||
@@ -139,7 +142,7 @@ class DomainUtil {
|
|||||||
|
|
||||||
// ignoreCerts parameter helps in fetching server icon and
|
// ignoreCerts parameter helps in fetching server icon and
|
||||||
// other server details when user chooses to ignore certificate warnings
|
// other server details when user chooses to ignore certificate warnings
|
||||||
async checkDomain(domain, ignoreCerts = false, silent = false) {
|
async checkDomain(domain: any, ignoreCerts = false, silent = false): Promise<any> {
|
||||||
if (!silent && this.duplicateDomain(domain)) {
|
if (!silent && this.duplicateDomain(domain)) {
|
||||||
// Do not check duplicate in silent mode
|
// Do not check duplicate in silent mode
|
||||||
throw new Error('This server has been added.');
|
throw new Error('This server has been added.');
|
||||||
@@ -168,25 +171,21 @@ class DomainUtil {
|
|||||||
|
|
||||||
const certsError = error.toString().includes('certificate');
|
const certsError = error.toString().includes('certificate');
|
||||||
if (domain.indexOf(whitelistDomains) >= 0 || certsError) {
|
if (domain.indexOf(whitelistDomains) >= 0 || certsError) {
|
||||||
try {
|
return this.checkCertError(domain, serverConf, error, silent);
|
||||||
return await this.checkCertError(domain, serverConf, error, silent);
|
|
||||||
} catch (err) {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
throw Messages.invalidZulipServerError(domain);
|
throw Messages.invalidZulipServerError(domain);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getServerSettings(domain, ignoreCerts = false) {
|
getServerSettings(domain: any, ignoreCerts = false): Promise<object | string> {
|
||||||
const serverSettingsOptions = {
|
const serverSettingsOptions = {
|
||||||
url: domain + '/api/v1/server_settings',
|
url: domain + '/api/v1/server_settings',
|
||||||
...RequestUtil.requestOptions(domain, ignoreCerts)
|
...RequestUtil.requestOptions(domain, ignoreCerts)
|
||||||
};
|
};
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
request(serverSettingsOptions, (error, response) => {
|
request(serverSettingsOptions, (error: string, response: any) => {
|
||||||
if (!error && response.statusCode === 200) {
|
if (!error && response.statusCode === 200) {
|
||||||
const data = JSON.parse(response.body);
|
const data = JSON.parse(response.body);
|
||||||
if (data.hasOwnProperty('realm_icon') && data.realm_icon) {
|
if (data.hasOwnProperty('realm_icon') && data.realm_icon) {
|
||||||
@@ -208,7 +207,7 @@ class DomainUtil {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
saveServerIcon(server, ignoreCerts = false) {
|
saveServerIcon(server: any, ignoreCerts = false): Promise<string> {
|
||||||
const url = server.icon;
|
const url = server.icon;
|
||||||
const domain = server.url;
|
const domain = server.url;
|
||||||
|
|
||||||
@@ -222,8 +221,8 @@ class DomainUtil {
|
|||||||
const filePath = this.generateFilePath(url);
|
const filePath = this.generateFilePath(url);
|
||||||
const file = fs.createWriteStream(filePath);
|
const file = fs.createWriteStream(filePath);
|
||||||
try {
|
try {
|
||||||
request(serverIconOptions).on('response', response => {
|
request(serverIconOptions).on('response', (response: any) => {
|
||||||
response.on('error', err => {
|
response.on('error', (err: string) => {
|
||||||
logger.log('Could not get server icon.');
|
logger.log('Could not get server icon.');
|
||||||
logger.log(err);
|
logger.log(err);
|
||||||
logger.reportSentry(err);
|
logger.reportSentry(err);
|
||||||
@@ -232,7 +231,7 @@ class DomainUtil {
|
|||||||
response.pipe(file).on('finish', () => {
|
response.pipe(file).on('finish', () => {
|
||||||
resolve(filePath);
|
resolve(filePath);
|
||||||
});
|
});
|
||||||
}).on('error', err => {
|
}).on('error', (err: string) => {
|
||||||
logger.log('Could not get server icon.');
|
logger.log('Could not get server icon.');
|
||||||
logger.log(err);
|
logger.log(err);
|
||||||
logger.reportSentry(err);
|
logger.reportSentry(err);
|
||||||
@@ -247,9 +246,9 @@ class DomainUtil {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
updateSavedServer(url, index) {
|
updateSavedServer(url: string, index: number): void {
|
||||||
// Does not promise successful update
|
// Does not promise successful update
|
||||||
const ignoreCerts = this.getDomain(index).ignoreCerts;
|
const { ignoreCerts } = this.getDomain(index);
|
||||||
this.checkDomain(url, ignoreCerts, true).then(newServerConf => {
|
this.checkDomain(url, ignoreCerts, true).then(newServerConf => {
|
||||||
this.saveServerIcon(newServerConf, ignoreCerts).then(localIconUrl => {
|
this.saveServerIcon(newServerConf, ignoreCerts).then(localIconUrl => {
|
||||||
newServerConf.icon = localIconUrl;
|
newServerConf.icon = localIconUrl;
|
||||||
@@ -259,7 +258,7 @@ class DomainUtil {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
reloadDB() {
|
reloadDB(): void {
|
||||||
const domainJsonPath = path.join(app.getPath('userData'), 'config/domain.json');
|
const domainJsonPath = path.join(app.getPath('userData'), 'config/domain.json');
|
||||||
try {
|
try {
|
||||||
const file = fs.readFileSync(domainJsonPath, 'utf8');
|
const file = fs.readFileSync(domainJsonPath, 'utf8');
|
||||||
@@ -280,7 +279,7 @@ class DomainUtil {
|
|||||||
this.db = new JsonDB(domainJsonPath, true, true);
|
this.db = new JsonDB(domainJsonPath, true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
generateFilePath(url) {
|
generateFilePath(url: string): string {
|
||||||
const dir = `${app.getPath('userData')}/server-icons`;
|
const dir = `${app.getPath('userData')}/server-icons`;
|
||||||
const extension = path.extname(url).split('?')[0];
|
const extension = path.extname(url).split('?')[0];
|
||||||
|
|
||||||
@@ -299,7 +298,7 @@ class DomainUtil {
|
|||||||
return `${dir}/${hash >>> 0}${extension}`;
|
return `${dir}/${hash >>> 0}${extension}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
formatUrl(domain) {
|
formatUrl(domain: any): string {
|
||||||
const hasPrefix = (domain.indexOf('http') === 0);
|
const hasPrefix = (domain.indexOf('http') === 0);
|
||||||
if (hasPrefix) {
|
if (hasPrefix) {
|
||||||
return domain;
|
return domain;
|
||||||
Reference in New Issue
Block a user