2021-06-03 17:12:07 +02:00
|
|
|
import Setting, { Env } from './models/Setting';
|
2023-07-27 17:05:56 +02:00
|
|
|
import Logger, { TargetType, LoggerWrapper } from '@joplin/utils/Logger';
|
2020-11-05 18:58:23 +02:00
|
|
|
import shim from './shim';
|
2022-07-10 15:54:31 +02:00
|
|
|
const { setupProxySettings } = require('./shim-init-node');
|
2020-11-05 18:58:23 +02:00
|
|
|
import BaseService from './services/BaseService';
|
2023-09-24 21:21:58 +02:00
|
|
|
import reducer, { getNotesParent, serializeNotesParent, setStore, State } from './reducer';
|
2020-11-05 18:58:23 +02:00
|
|
|
import KeychainServiceDriver from './services/keychain/KeychainServiceDriver.node';
|
2021-08-11 17:23:37 +02:00
|
|
|
import KeychainServiceDriverDummy from './services/keychain/KeychainServiceDriver.dummy';
|
2023-10-31 17:05:28 +02:00
|
|
|
import { setLocale } from './locale';
|
2020-11-08 03:08:33 +02:00
|
|
|
import KvStore from './services/KvStore';
|
2020-12-28 13:48:47 +02:00
|
|
|
import SyncTargetJoplinServer from './SyncTargetJoplinServer';
|
2021-01-20 17:49:02 +02:00
|
|
|
import SyncTargetOneDrive from './SyncTargetOneDrive';
|
2021-05-13 18:57:37 +02:00
|
|
|
import { createStore, applyMiddleware, Store } from 'redux';
|
2020-11-05 18:58:23 +02:00
|
|
|
const { defaultState, stateUtils } = require('./reducer');
|
2021-01-29 20:45:11 +02:00
|
|
|
import JoplinDatabase from './JoplinDatabase';
|
2024-01-06 19:21:51 +02:00
|
|
|
import { cancelTimers as folderScreenUtilsCancelTimers, refreshFolders, scheduleRefreshFolders } from './folders-screen-utils';
|
2020-11-05 18:58:23 +02:00
|
|
|
const { DatabaseDriverNode } = require('./database-driver-node.js');
|
2021-01-22 19:41:11 +02:00
|
|
|
import BaseModel from './BaseModel';
|
|
|
|
import Folder from './models/Folder';
|
|
|
|
import BaseItem from './models/BaseItem';
|
|
|
|
import Note from './models/Note';
|
|
|
|
import Tag from './models/Tag';
|
2023-03-19 17:37:07 +02:00
|
|
|
import { splitCommandString } from '@joplin/utils';
|
2021-01-29 20:45:11 +02:00
|
|
|
import { reg } from './registry';
|
2021-01-22 19:41:11 +02:00
|
|
|
import time from './time';
|
2021-01-29 20:45:11 +02:00
|
|
|
import BaseSyncTarget from './BaseSyncTarget';
|
2024-01-03 20:02:05 +02:00
|
|
|
import reduxSharedMiddleware from './components/shared/reduxSharedMiddleware';
|
2018-03-09 22:59:12 +02:00
|
|
|
const os = require('os');
|
2023-11-14 20:47:52 +02:00
|
|
|
import dns = require('dns');
|
2023-10-31 17:05:28 +02:00
|
|
|
import fs = require('fs-extra');
|
2018-03-09 22:59:12 +02:00
|
|
|
const EventEmitter = require('events');
|
2020-11-10 17:59:30 +02:00
|
|
|
const syswidecas = require('./vendor/syswide-cas');
|
2021-08-16 16:20:14 +02:00
|
|
|
import SyncTargetRegistry from './SyncTargetRegistry';
|
2020-11-05 18:58:23 +02:00
|
|
|
const SyncTargetFilesystem = require('./SyncTargetFilesystem.js');
|
|
|
|
const SyncTargetNextcloud = require('./SyncTargetNextcloud.js');
|
|
|
|
const SyncTargetWebDAV = require('./SyncTargetWebDAV.js');
|
|
|
|
const SyncTargetDropbox = require('./SyncTargetDropbox.js');
|
|
|
|
const SyncTargetAmazonS3 = require('./SyncTargetAmazonS3.js');
|
2021-08-23 19:47:07 +02:00
|
|
|
import EncryptionService from './services/e2ee/EncryptionService';
|
2021-01-23 17:51:19 +02:00
|
|
|
import ResourceFetcher from './services/ResourceFetcher';
|
2024-01-05 16:15:47 +02:00
|
|
|
import SearchEngineUtils from './services/search/SearchEngineUtils';
|
|
|
|
import SearchEngine, { ProcessResultsRow } from './services/search/SearchEngine';
|
2021-01-23 17:51:19 +02:00
|
|
|
import RevisionService from './services/RevisionService';
|
|
|
|
import ResourceService from './services/ResourceService';
|
|
|
|
import DecryptionWorker from './services/DecryptionWorker';
|
2021-08-30 15:15:35 +02:00
|
|
|
import { loadKeychainServiceAndSettings } from './services/SettingUtils';
|
2021-01-23 17:51:19 +02:00
|
|
|
import MigrationService from './services/MigrationService';
|
2021-05-13 18:57:37 +02:00
|
|
|
import ShareService from './services/share/ShareService';
|
2021-05-09 18:12:47 +02:00
|
|
|
import handleSyncStartupOperation from './services/synchronizer/utils/handleSyncStartupOperation';
|
2021-06-03 17:12:07 +02:00
|
|
|
import SyncTargetJoplinCloud from './SyncTargetJoplinCloud';
|
2020-11-05 18:58:23 +02:00
|
|
|
const { toSystemSlashes } = require('./path-utils');
|
2020-10-09 19:35:46 +02:00
|
|
|
const { setAutoFreeze } = require('immer');
|
2021-08-12 17:54:10 +02:00
|
|
|
import { getEncryptionEnabled } from './services/synchronizer/syncInfoUtils';
|
2021-08-30 15:15:35 +02:00
|
|
|
import { loadMasterKeysFromSettings, migrateMasterPassword } from './services/e2ee/utils';
|
2021-08-16 19:05:22 +02:00
|
|
|
import SyncTargetNone from './SyncTargetNone';
|
2021-10-03 17:00:49 +02:00
|
|
|
import { setRSA } from './services/e2ee/ppk';
|
|
|
|
import RSA from './services/e2ee/RSA.node';
|
2022-02-09 20:04:27 +02:00
|
|
|
import Resource from './models/Resource';
|
2022-04-11 17:49:32 +02:00
|
|
|
import { ProfileConfig } from './services/profileConfig/types';
|
|
|
|
import initProfile from './services/profileConfig/initProfile';
|
2023-07-16 18:42:42 +02:00
|
|
|
import { parseShareCache } from './services/share/reducer';
|
2023-07-14 15:24:25 +02:00
|
|
|
import RotatingLogs from './RotatingLogs';
|
2023-12-13 21:24:58 +02:00
|
|
|
import { NoteEntity } from './services/database/types';
|
2023-10-31 17:05:28 +02:00
|
|
|
import { join } from 'path';
|
|
|
|
import processStartFlags, { MatchedStartFlags } from './utils/processStartFlags';
|
2023-07-14 15:24:25 +02:00
|
|
|
|
2020-11-25 11:40:54 +02:00
|
|
|
const appLogger: LoggerWrapper = Logger.create('App');
|
2020-11-19 17:25:02 +02:00
|
|
|
|
2020-11-05 18:58:23 +02:00
|
|
|
// const ntpClient = require('./vendor/ntp-client');
|
2020-09-12 00:57:06 +02:00
|
|
|
// ntpClient.dgram = require('dgram');
|
2020-09-09 01:34:27 +02:00
|
|
|
|
2021-08-11 17:23:37 +02:00
|
|
|
interface StartOptions {
|
|
|
|
keychainEnabled?: boolean;
|
2023-08-04 11:57:54 +02:00
|
|
|
setupGlobalLogger?: boolean;
|
2021-08-11 17:23:37 +02:00
|
|
|
}
|
2023-10-31 17:05:28 +02:00
|
|
|
export const safeModeFlagFilename = 'force-safe-mode-on-next-start';
|
2021-08-11 17:23:37 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
export default class BaseApplication {
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
private eventEmitter_: any;
|
|
|
|
private scheduleAutoAddResourcesIID_: any = null;
|
|
|
|
private database_: any = null;
|
2022-04-11 17:49:32 +02:00
|
|
|
private profileConfig_: ProfileConfig = null;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2023-06-30 10:07:03 +02:00
|
|
|
protected showStackTraces_ = false;
|
|
|
|
protected showPromptString_ = false;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
// Note: this is basically a cache of state.selectedFolderId. It should *only*
|
|
|
|
// be derived from the state and not set directly since that would make the
|
|
|
|
// state and UI out of sync.
|
2020-11-12 21:13:28 +02:00
|
|
|
private currentFolder_: any = null;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2021-05-13 18:57:37 +02:00
|
|
|
protected store_: Store<any> = null;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2023-07-14 15:24:25 +02:00
|
|
|
private rotatingLogs: RotatingLogs;
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
public constructor() {
|
2017-11-04 14:23:46 +02:00
|
|
|
this.eventEmitter_ = new EventEmitter();
|
2019-05-28 23:05:11 +02:00
|
|
|
this.decryptionWorker_resourceMetadataButNotBlobDecrypted = this.decryptionWorker_resourceMetadataButNotBlobDecrypted.bind(this);
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
public async destroy() {
|
2020-02-27 20:25:42 +02:00
|
|
|
if (this.scheduleAutoAddResourcesIID_) {
|
2020-10-09 19:35:46 +02:00
|
|
|
shim.clearTimeout(this.scheduleAutoAddResourcesIID_);
|
2020-02-27 20:25:42 +02:00
|
|
|
this.scheduleAutoAddResourcesIID_ = null;
|
|
|
|
}
|
|
|
|
await ResourceFetcher.instance().destroy();
|
|
|
|
await SearchEngine.instance().destroy();
|
|
|
|
await DecryptionWorker.instance().destroy();
|
2024-01-06 19:21:51 +02:00
|
|
|
await folderScreenUtilsCancelTimers();
|
2020-03-16 04:30:54 +02:00
|
|
|
await BaseItem.revisionService_.cancelTimers();
|
|
|
|
await ResourceService.instance().cancelTimers();
|
2020-02-22 13:25:16 +02:00
|
|
|
await reg.cancelTimers();
|
|
|
|
|
2020-02-27 20:25:42 +02:00
|
|
|
this.eventEmitter_.removeAllListeners();
|
2020-11-08 03:08:33 +02:00
|
|
|
KvStore.destroyInstance();
|
2020-03-16 04:30:54 +02:00
|
|
|
BaseModel.setDb(null);
|
2020-02-27 20:25:42 +02:00
|
|
|
reg.setDb(null);
|
|
|
|
|
2020-03-16 04:30:54 +02:00
|
|
|
BaseItem.revisionService_ = null;
|
|
|
|
RevisionService.instance_ = null;
|
|
|
|
ResourceService.instance_ = null;
|
2021-01-22 19:41:11 +02:00
|
|
|
ResourceService.isRunningInBackground_ = false;
|
|
|
|
// ResourceService.isRunningInBackground_ = false;
|
2020-03-16 04:30:54 +02:00
|
|
|
ResourceFetcher.instance_ = null;
|
|
|
|
EncryptionService.instance_ = null;
|
|
|
|
DecryptionWorker.instance_ = null;
|
|
|
|
|
2020-11-19 17:25:02 +02:00
|
|
|
appLogger.info('Base application terminated...');
|
2020-02-22 13:25:16 +02:00
|
|
|
this.eventEmitter_ = null;
|
|
|
|
this.decryptionWorker_resourceMetadataButNotBlobDecrypted = null;
|
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
public logger(): LoggerWrapper {
|
2020-11-19 17:25:02 +02:00
|
|
|
return appLogger;
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
public store() {
|
2017-11-04 14:23:46 +02:00
|
|
|
return this.store_;
|
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
public currentFolder() {
|
2017-11-04 14:23:46 +02:00
|
|
|
return this.currentFolder_;
|
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
public async refreshCurrentFolder() {
|
2017-11-04 14:23:46 +02:00
|
|
|
let newFolder = null;
|
2018-09-04 06:42:22 +02:00
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
if (this.currentFolder_) newFolder = await Folder.load(this.currentFolder_.id);
|
|
|
|
if (!newFolder) newFolder = await Folder.defaultFolder();
|
|
|
|
|
|
|
|
this.switchCurrentFolder(newFolder);
|
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
public switchCurrentFolder(folder: any) {
|
2017-12-07 20:12:46 +02:00
|
|
|
if (!this.hasGui()) {
|
2023-06-01 13:02:36 +02:00
|
|
|
this.currentFolder_ = { ...folder };
|
2018-03-09 22:59:12 +02:00
|
|
|
Setting.setValue('activeFolderId', folder ? folder.id : '');
|
2017-12-07 20:12:46 +02:00
|
|
|
} else {
|
|
|
|
this.dispatch({
|
2018-03-09 22:59:12 +02:00
|
|
|
type: 'FOLDER_SELECT',
|
|
|
|
id: folder ? folder.id : '',
|
2017-12-07 20:12:46 +02:00
|
|
|
});
|
|
|
|
}
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handles the initial flags passed to main script and
|
|
|
|
// returns the remaining args.
|
2023-06-30 10:11:26 +02:00
|
|
|
private async handleStartFlags_(argv: string[], setDefaults = true) {
|
2023-10-31 17:05:28 +02:00
|
|
|
const flags = await processStartFlags(argv, setDefaults);
|
2021-12-01 15:51:42 +02:00
|
|
|
|
2023-10-31 17:05:28 +02:00
|
|
|
if (flags.matched.showStackTraces) {
|
|
|
|
this.showStackTraces_ = true;
|
2017-11-16 20:51:11 +02:00
|
|
|
}
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2023-11-14 20:47:52 +02:00
|
|
|
// Work around issues with ipv6 resolution -- default to ipv4first.
|
|
|
|
// (possibly incorrect URL serialization see https://github.com/mswjs/msw/issues/1388#issuecomment-1241180921).
|
|
|
|
// See also https://github.com/node-fetch/node-fetch/issues/1624#issuecomment-1407717012
|
|
|
|
if (flags.matched.allowOverridingDnsResultOrder) {
|
|
|
|
dns.setDefaultResultOrder('ipv4first');
|
|
|
|
}
|
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
return {
|
2023-10-31 17:05:28 +02:00
|
|
|
matched: flags.matched,
|
|
|
|
argv: flags.argv,
|
2017-11-04 14:23:46 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2021-10-03 17:00:49 +02:00
|
|
|
public on(eventName: string, callback: Function) {
|
2017-11-04 14:23:46 +02:00
|
|
|
return this.eventEmitter_.on(eventName, callback);
|
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
public async exit(code = 0) {
|
2017-11-04 14:23:46 +02:00
|
|
|
await Setting.saveAll();
|
|
|
|
process.exit(code);
|
|
|
|
}
|
|
|
|
|
2023-06-30 10:11:26 +02:00
|
|
|
public async refreshNotes(state: any, useSelectedNoteId = false, noteHash = '') {
|
2017-11-12 19:37:04 +02:00
|
|
|
let parentType = state.notesParentType;
|
|
|
|
let parentId = null;
|
2018-09-04 06:42:22 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (parentType === 'Folder') {
|
2017-11-12 19:37:04 +02:00
|
|
|
parentId = state.selectedFolderId;
|
|
|
|
parentType = BaseModel.TYPE_FOLDER;
|
2018-03-09 22:59:12 +02:00
|
|
|
} else if (parentType === 'Tag') {
|
2017-11-12 19:37:04 +02:00
|
|
|
parentId = state.selectedTagId;
|
|
|
|
parentType = BaseModel.TYPE_TAG;
|
2018-03-09 22:59:12 +02:00
|
|
|
} else if (parentType === 'Search') {
|
2017-11-12 19:37:04 +02:00
|
|
|
parentId = state.selectedSearchId;
|
|
|
|
parentType = BaseModel.TYPE_SEARCH;
|
2020-02-22 13:25:16 +02:00
|
|
|
} else if (parentType === 'SmartFilter') {
|
|
|
|
parentId = state.selectedSmartFilterId;
|
|
|
|
parentType = BaseModel.TYPE_SMART_FILTER;
|
2017-11-12 19:37:04 +02:00
|
|
|
}
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2020-11-19 17:25:02 +02:00
|
|
|
appLogger.debug('Refreshing notes:', parentType, parentId);
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const options = {
|
2018-02-22 20:58:15 +02:00
|
|
|
order: stateUtils.notesOrder(state.settings),
|
2018-03-09 22:59:12 +02:00
|
|
|
uncompletedTodosOnTop: Setting.value('uncompletedTodosOnTop'),
|
2018-05-09 22:00:05 +02:00
|
|
|
showCompletedTodos: Setting.value('showCompletedTodos'),
|
2018-02-22 20:58:15 +02:00
|
|
|
caseInsensitive: true,
|
2017-11-04 14:23:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const source = JSON.stringify({
|
|
|
|
options: options,
|
|
|
|
parentId: parentId,
|
|
|
|
});
|
|
|
|
|
2023-12-13 21:24:58 +02:00
|
|
|
let notes: NoteEntity[] = [];
|
|
|
|
let highlightedWords: string[] = [];
|
|
|
|
let searchResults: ProcessResultsRow[] = [];
|
2017-11-04 14:23:46 +02:00
|
|
|
|
|
|
|
if (parentId) {
|
|
|
|
if (parentType === Folder.modelType()) {
|
|
|
|
notes = await Note.previews(parentId, options);
|
|
|
|
} else if (parentType === Tag.modelType()) {
|
2018-09-30 21:43:46 +02:00
|
|
|
notes = await Tag.notes(parentId, options);
|
2017-11-04 14:23:46 +02:00
|
|
|
} else if (parentType === BaseModel.TYPE_SEARCH) {
|
2018-12-10 20:58:49 +02:00
|
|
|
const search = BaseModel.byId(state.searches, parentId);
|
2023-12-13 21:24:58 +02:00
|
|
|
const response = await SearchEngineUtils.notesForQuery(search.query_pattern, true, { appendWildCards: true });
|
|
|
|
notes = response.notes;
|
|
|
|
searchResults = response.results;
|
2020-09-15 15:01:07 +02:00
|
|
|
const parsedQuery = await SearchEngine.instance().parseQuery(search.query_pattern);
|
2020-09-06 14:07:00 +02:00
|
|
|
highlightedWords = SearchEngine.instance().allParsedQueryTerms(parsedQuery);
|
2020-02-22 13:25:16 +02:00
|
|
|
} else if (parentType === BaseModel.TYPE_SMART_FILTER) {
|
|
|
|
notes = await Note.previews(parentId, options);
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 14:17:51 +02:00
|
|
|
this.store().dispatch({
|
|
|
|
type: 'SET_HIGHLIGHTED',
|
|
|
|
words: highlightedWords,
|
|
|
|
});
|
2020-09-06 14:07:00 +02:00
|
|
|
|
2023-12-13 21:24:58 +02:00
|
|
|
this.store().dispatch({
|
|
|
|
type: 'SEARCH_RESULTS_SET',
|
|
|
|
value: searchResults,
|
|
|
|
});
|
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
this.store().dispatch({
|
2018-03-09 22:59:12 +02:00
|
|
|
type: 'NOTE_UPDATE_ALL',
|
2017-11-04 14:23:46 +02:00
|
|
|
notes: notes,
|
|
|
|
notesSource: source,
|
|
|
|
});
|
|
|
|
|
2018-11-08 02:58:06 +02:00
|
|
|
if (useSelectedNoteId) {
|
|
|
|
this.store().dispatch({
|
|
|
|
type: 'NOTE_SELECT',
|
|
|
|
id: state.selectedNoteIds && state.selectedNoteIds.length ? state.selectedNoteIds[0] : null,
|
2019-09-09 19:16:00 +02:00
|
|
|
hash: noteHash,
|
2018-11-08 02:58:06 +02:00
|
|
|
});
|
|
|
|
} else {
|
2019-01-29 20:32:52 +02:00
|
|
|
const lastSelectedNoteIds = stateUtils.lastSelectedNoteIds(state);
|
|
|
|
const foundIds = [];
|
|
|
|
for (let i = 0; i < lastSelectedNoteIds.length; i++) {
|
|
|
|
const noteId = lastSelectedNoteIds[i];
|
|
|
|
let found = false;
|
|
|
|
for (let j = 0; j < notes.length; j++) {
|
|
|
|
if (notes[j].id === noteId) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) foundIds.push(noteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
let selectedNoteId = null;
|
|
|
|
if (foundIds.length) {
|
|
|
|
selectedNoteId = foundIds[0];
|
|
|
|
} else {
|
|
|
|
selectedNoteId = notes.length ? notes[0].id : null;
|
|
|
|
}
|
|
|
|
|
2018-11-08 02:58:06 +02:00
|
|
|
this.store().dispatch({
|
|
|
|
type: 'NOTE_SELECT',
|
2019-01-29 20:32:52 +02:00
|
|
|
id: selectedNoteId,
|
2018-11-08 02:58:06 +02:00
|
|
|
});
|
|
|
|
}
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
private resourceFetcher_downloadComplete(event: any) {
|
2019-05-22 16:56:07 +02:00
|
|
|
if (event.encrypted) {
|
2021-01-22 19:41:11 +02:00
|
|
|
void DecryptionWorker.instance().scheduleStart();
|
2019-05-22 16:56:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
private async decryptionWorker_resourceMetadataButNotBlobDecrypted() {
|
2020-03-16 04:30:54 +02:00
|
|
|
ResourceFetcher.instance().scheduleAutoAddResources();
|
2019-05-28 23:05:11 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public reducerActionToString(action: any) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const o = [action.type];
|
2018-03-09 22:59:12 +02:00
|
|
|
if ('id' in action) o.push(action.id);
|
|
|
|
if ('noteId' in action) o.push(action.noteId);
|
|
|
|
if ('folderId' in action) o.push(action.folderId);
|
|
|
|
if ('tagId' in action) o.push(action.tagId);
|
|
|
|
if ('tag' in action) o.push(action.tag.id);
|
|
|
|
if ('folder' in action) o.push(action.folder.id);
|
|
|
|
if ('notesSource' in action) o.push(JSON.stringify(action.notesSource));
|
|
|
|
return o.join(', ');
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
public hasGui() {
|
2017-11-05 02:17:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
public uiType() {
|
2018-03-09 22:59:12 +02:00
|
|
|
return this.hasGui() ? 'gui' : 'cli';
|
2017-12-07 20:12:46 +02:00
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
public generalMiddlewareFn() {
|
2020-11-12 21:13:28 +02:00
|
|
|
const middleware = (store: any) => (next: any) => (action: any) => {
|
2017-11-10 20:58:00 +02:00
|
|
|
return this.generalMiddleware(store, next, action);
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2017-11-10 20:58:00 +02:00
|
|
|
return middleware;
|
|
|
|
}
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
protected async applySettingsSideEffects(action: any = null) {
|
2020-11-12 21:13:28 +02:00
|
|
|
const sideEffects: any = {
|
2019-09-06 19:29:40 +02:00
|
|
|
'dateFormat': async () => {
|
|
|
|
time.setLocale(Setting.value('locale'));
|
|
|
|
time.setDateFormat(Setting.value('dateFormat'));
|
|
|
|
time.setTimeFormat(Setting.value('timeFormat'));
|
|
|
|
},
|
|
|
|
'net.ignoreTlsErrors': async () => {
|
|
|
|
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = Setting.value('net.ignoreTlsErrors') ? '0' : '1';
|
|
|
|
},
|
|
|
|
'net.customCertificates': async () => {
|
|
|
|
const caPaths = Setting.value('net.customCertificates').split(',');
|
|
|
|
for (let i = 0; i < caPaths.length; i++) {
|
|
|
|
const f = caPaths[i].trim();
|
|
|
|
if (!f) continue;
|
|
|
|
syswidecas.addCAs(f);
|
|
|
|
}
|
|
|
|
},
|
2022-07-10 15:54:31 +02:00
|
|
|
'net.proxyEnabled': async () => {
|
|
|
|
setupProxySettings({
|
|
|
|
maxConcurrentConnections: Setting.value('sync.maxConcurrentConnections'),
|
|
|
|
proxyTimeout: Setting.value('net.proxyTimeout'),
|
|
|
|
proxyEnabled: Setting.value('net.proxyEnabled'),
|
|
|
|
proxyUrl: Setting.value('net.proxyUrl'),
|
|
|
|
});
|
|
|
|
},
|
2021-08-12 17:54:10 +02:00
|
|
|
|
|
|
|
// Note: this used to run when "encryption.enabled" was changed, but
|
|
|
|
// now we run it anytime any property of the sync target info is
|
|
|
|
// changed. This is not optimal but:
|
|
|
|
// - The sync target info rarely changes.
|
|
|
|
// - All the calls below are cheap or do nothing if there's nothing
|
|
|
|
// to do.
|
|
|
|
'syncInfoCache': async () => {
|
2019-09-06 19:29:40 +02:00
|
|
|
if (this.hasGui()) {
|
2021-08-12 17:54:10 +02:00
|
|
|
appLogger.info('"syncInfoCache" was changed - setting up encryption related code');
|
|
|
|
|
|
|
|
await loadMasterKeysFromSettings(EncryptionService.instance());
|
2021-01-22 19:41:11 +02:00
|
|
|
void DecryptionWorker.instance().scheduleStart();
|
2019-09-06 19:29:40 +02:00
|
|
|
const loadedMasterKeyIds = EncryptionService.instance().loadedMasterKeyIds();
|
|
|
|
|
|
|
|
this.dispatch({
|
|
|
|
type: 'MASTERKEY_REMOVE_NOT_LOADED',
|
|
|
|
ids: loadedMasterKeyIds,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Schedule a sync operation so that items that need to be encrypted
|
|
|
|
// are sent to sync target.
|
2021-01-29 20:45:11 +02:00
|
|
|
void reg.scheduleSync();
|
2019-09-06 19:29:40 +02:00
|
|
|
}
|
|
|
|
},
|
2021-08-12 17:54:10 +02:00
|
|
|
|
2019-09-06 19:29:40 +02:00
|
|
|
'sync.interval': async () => {
|
|
|
|
if (this.hasGui()) reg.setupRecurrentSync();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
sideEffects['timeFormat'] = sideEffects['dateFormat'];
|
|
|
|
sideEffects['locale'] = sideEffects['dateFormat'];
|
2021-08-12 17:54:10 +02:00
|
|
|
sideEffects['encryption.passwordCache'] = sideEffects['syncInfoCache'];
|
2021-08-30 15:15:35 +02:00
|
|
|
sideEffects['encryption.masterPassword'] = sideEffects['syncInfoCache'];
|
2022-07-10 15:54:31 +02:00
|
|
|
sideEffects['sync.maxConcurrentConnections'] = sideEffects['net.proxyEnabled'];
|
|
|
|
sideEffects['sync.proxyTimeout'] = sideEffects['net.proxyEnabled'];
|
|
|
|
sideEffects['sync.proxyUrl'] = sideEffects['net.proxyEnabled'];
|
2019-09-06 19:29:40 +02:00
|
|
|
|
|
|
|
if (action) {
|
|
|
|
const effect = sideEffects[action.key];
|
|
|
|
if (effect) await effect();
|
|
|
|
} else {
|
|
|
|
for (const key in sideEffects) {
|
|
|
|
await sideEffects[key]();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
protected async generalMiddleware(store: any, next: any, action: any) {
|
2020-11-19 17:25:02 +02:00
|
|
|
// appLogger.debug('Reducer action', this.reducerActionToString(action));
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2017-11-10 20:58:00 +02:00
|
|
|
const result = next(action);
|
2018-02-22 20:58:15 +02:00
|
|
|
let refreshNotes = false;
|
2024-01-06 19:21:51 +02:00
|
|
|
let doRefreshFolders: boolean | string = false;
|
2018-11-08 02:58:06 +02:00
|
|
|
let refreshNotesUseSelectedNoteId = false;
|
2019-09-09 19:16:00 +02:00
|
|
|
let refreshNotesHash = '';
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2024-01-03 20:02:05 +02:00
|
|
|
await reduxSharedMiddleware(store, next, action, ((action: any) => { this.dispatch(action); }) as any);
|
2023-09-24 21:21:58 +02:00
|
|
|
const newState = store.getState() as State;
|
2018-05-09 11:49:31 +02:00
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
if (this.hasGui() && ['NOTE_UPDATE_ONE', 'NOTE_DELETE', 'FOLDER_UPDATE_ONE', 'FOLDER_DELETE'].indexOf(action.type) >= 0) {
|
2023-08-04 15:48:18 +02:00
|
|
|
if (!(await reg.syncTarget().syncStarted())) void reg.scheduleSync(15 * 1000, { syncSteps: ['update_remote', 'delete_remote'] });
|
2019-01-19 20:03:05 +02:00
|
|
|
SearchEngine.instance().scheduleSyncTables();
|
|
|
|
}
|
|
|
|
|
2019-03-10 23:16:05 +02:00
|
|
|
// Don't add FOLDER_UPDATE_ALL as refreshFolders() is calling it too, which
|
|
|
|
// would cause the sidebar to refresh all the time.
|
2019-07-29 15:43:53 +02:00
|
|
|
if (this.hasGui() && ['FOLDER_UPDATE_ONE'].indexOf(action.type) >= 0) {
|
2024-01-06 19:21:51 +02:00
|
|
|
doRefreshFolders = true;
|
2019-03-08 19:25:32 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
if (action.type === 'HISTORY_BACKWARD' || action.type === 'HISTORY_FORWARD') {
|
2020-05-15 13:13:42 +02:00
|
|
|
refreshNotes = true;
|
|
|
|
refreshNotesUseSelectedNoteId = true;
|
|
|
|
}
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
if (action.type === 'HISTORY_BACKWARD' || action.type === 'HISTORY_FORWARD' || action.type === 'FOLDER_SELECT' || action.type === 'FOLDER_DELETE' || action.type === 'FOLDER_AND_NOTE_SELECT' || (action.type === 'SEARCH_UPDATE' && newState.notesParentType === 'Folder')) {
|
2018-03-09 22:59:12 +02:00
|
|
|
Setting.setValue('activeFolderId', newState.selectedFolderId);
|
2017-11-10 20:58:00 +02:00
|
|
|
this.currentFolder_ = newState.selectedFolderId ? await Folder.load(newState.selectedFolderId) : null;
|
2018-02-22 20:58:15 +02:00
|
|
|
refreshNotes = true;
|
2018-11-08 02:58:06 +02:00
|
|
|
|
2019-09-09 19:16:00 +02:00
|
|
|
if (action.type === 'FOLDER_AND_NOTE_SELECT') {
|
|
|
|
refreshNotesUseSelectedNoteId = true;
|
|
|
|
refreshNotesHash = action.hash;
|
|
|
|
}
|
2018-02-22 20:58:15 +02:00
|
|
|
}
|
|
|
|
|
2023-09-24 21:21:58 +02:00
|
|
|
if (['HISTORY_BACKWARD', 'HISTORY_FORWARD', 'FOLDER_SELECT', 'TAG_SELECT', 'SMART_FILTER_SELECT', 'FOLDER_DELETE', 'FOLDER_AND_NOTE_SELECT'].includes(action.type) || (action.type === 'SEARCH_UPDATE' && newState.notesParentType === 'Folder')) {
|
|
|
|
Setting.setValue('notesParent', serializeNotesParent(getNotesParent(newState)));
|
|
|
|
}
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
if (this.hasGui() && (action.type === 'NOTE_IS_INSERTING_NOTES' && !action.value)) {
|
2020-05-27 18:21:46 +02:00
|
|
|
refreshNotes = true;
|
|
|
|
}
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
if (this.hasGui() && ((action.type === 'SETTING_UPDATE_ONE' && action.key === 'uncompletedTodosOnTop') || action.type === 'SETTING_UPDATE_ALL')) {
|
2018-02-22 20:58:15 +02:00
|
|
|
refreshNotes = true;
|
|
|
|
}
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
if (this.hasGui() && ((action.type === 'SETTING_UPDATE_ONE' && action.key === 'showCompletedTodos') || action.type === 'SETTING_UPDATE_ALL')) {
|
2018-05-09 22:00:05 +02:00
|
|
|
refreshNotes = true;
|
|
|
|
}
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
if (this.hasGui() && ((action.type === 'SETTING_UPDATE_ONE' && action.key.indexOf('notes.sortOrder') === 0) || action.type === 'SETTING_UPDATE_ALL')) {
|
2018-02-22 20:58:15 +02:00
|
|
|
refreshNotes = true;
|
|
|
|
}
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
if (action.type === 'SMART_FILTER_SELECT') {
|
2020-02-22 13:25:16 +02:00
|
|
|
refreshNotes = true;
|
|
|
|
refreshNotesUseSelectedNoteId = true;
|
|
|
|
}
|
|
|
|
|
2020-09-29 09:11:52 +02:00
|
|
|
// Should refresh the notes when:
|
|
|
|
// - A tag is selected, to show the notes for that tag
|
|
|
|
// - When a tag is updated so that when searching by tags, the search results are updated
|
|
|
|
// https://github.com/laurent22/joplin/issues/3754
|
|
|
|
if (['TAG_SELECT', 'TAG_DELETE', 'TAG_UPDATE_ONE', 'NOTE_TAG_REMOVE'].includes(action.type)) {
|
2018-02-22 20:58:15 +02:00
|
|
|
refreshNotes = true;
|
|
|
|
}
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
if (action.type === 'SEARCH_SELECT' || action.type === 'SEARCH_DELETE') {
|
2018-02-22 20:58:15 +02:00
|
|
|
refreshNotes = true;
|
2017-11-10 20:58:00 +02:00
|
|
|
}
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
if (action.type === 'NOTE_TAG_REMOVE') {
|
2020-02-10 00:08:24 +02:00
|
|
|
if (newState.notesParentType === 'Tag' && newState.selectedTagId === action.item.id) {
|
|
|
|
if (newState.notes.length === newState.selectedNoteIds.length) {
|
|
|
|
await this.refreshCurrentFolder();
|
|
|
|
refreshNotesUseSelectedNoteId = true;
|
|
|
|
}
|
|
|
|
refreshNotes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 20:58:15 +02:00
|
|
|
if (refreshNotes) {
|
2019-09-09 19:16:00 +02:00
|
|
|
await this.refreshNotes(newState, refreshNotesUseSelectedNoteId, refreshNotesHash);
|
2017-11-12 02:44:26 +02:00
|
|
|
}
|
|
|
|
|
2020-10-20 00:24:40 +02:00
|
|
|
if (action.type === 'NOTE_UPDATE_ONE') {
|
2020-11-19 18:21:24 +02:00
|
|
|
if (!action.changedFields.length ||
|
|
|
|
action.changedFields.includes('parent_id') ||
|
|
|
|
action.changedFields.includes('encryption_applied') ||
|
|
|
|
action.changedFields.includes('is_conflict')
|
|
|
|
) {
|
2024-01-06 19:21:51 +02:00
|
|
|
doRefreshFolders = true;
|
2020-10-20 00:24:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'NOTE_DELETE') {
|
2024-01-06 19:21:51 +02:00
|
|
|
doRefreshFolders = true;
|
2019-03-02 19:35:57 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
if (this.hasGui() && action.type === 'SETTING_UPDATE_ALL') {
|
2024-01-06 19:21:51 +02:00
|
|
|
doRefreshFolders = 'now';
|
2017-11-12 20:57:59 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
if (this.hasGui() && action.type === 'SETTING_UPDATE_ONE' && (
|
2020-01-18 15:46:04 +02:00
|
|
|
action.key.indexOf('folders.sortOrder') === 0 ||
|
2022-07-23 09:31:32 +02:00
|
|
|
action.key === 'showNoteCounts' ||
|
|
|
|
action.key === 'showCompletedTodos')) {
|
2024-01-06 19:21:51 +02:00
|
|
|
doRefreshFolders = 'now';
|
2019-11-11 08:14:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (this.hasGui() && action.type === 'SYNC_GOT_ENCRYPTED_ITEM') {
|
2021-01-22 19:41:11 +02:00
|
|
|
void DecryptionWorker.instance().scheduleStart();
|
2017-12-21 21:06:08 +02:00
|
|
|
}
|
|
|
|
|
2020-05-31 01:31:29 +02:00
|
|
|
if (this.hasGui() && action.type === 'SYNC_CREATED_OR_UPDATED_RESOURCE') {
|
2021-01-22 19:41:11 +02:00
|
|
|
void ResourceFetcher.instance().autoAddResources();
|
2018-10-08 20:11:53 +02:00
|
|
|
}
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
if (action.type === 'SETTING_UPDATE_ONE') {
|
2019-09-06 19:29:40 +02:00
|
|
|
await this.applySettingsSideEffects(action);
|
2022-07-23 09:31:32 +02:00
|
|
|
} else if (action.type === 'SETTING_UPDATE_ALL') {
|
2019-09-06 19:29:40 +02:00
|
|
|
await this.applySettingsSideEffects();
|
|
|
|
}
|
|
|
|
|
2024-01-06 19:21:51 +02:00
|
|
|
if (doRefreshFolders) {
|
|
|
|
if (doRefreshFolders === 'now') {
|
|
|
|
await refreshFolders((action: any) => this.dispatch(action));
|
2019-03-02 19:35:57 +02:00
|
|
|
} else {
|
2024-01-06 19:21:51 +02:00
|
|
|
await scheduleRefreshFolders((action: any) => this.dispatch(action));
|
2019-03-02 19:35:57 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-29 15:43:53 +02:00
|
|
|
return result;
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
public dispatch(action: any) {
|
2017-11-04 14:23:46 +02:00
|
|
|
if (this.store()) return this.store().dispatch(action);
|
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
public reducer(state: any = defaultState, action: any) {
|
2017-11-06 20:35:04 +02:00
|
|
|
return reducer(state, action);
|
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
public initRedux() {
|
2021-05-13 18:57:37 +02:00
|
|
|
this.store_ = createStore(this.reducer, applyMiddleware(this.generalMiddlewareFn() as any));
|
|
|
|
setStore(this.store_);
|
2022-04-11 17:49:32 +02:00
|
|
|
|
|
|
|
this.store_.dispatch({
|
|
|
|
type: 'PROFILE_CONFIG_SET',
|
|
|
|
value: this.profileConfig_,
|
|
|
|
});
|
|
|
|
|
2017-11-04 15:23:15 +02:00
|
|
|
BaseModel.dispatch = this.store().dispatch;
|
2017-11-24 01:10:55 +02:00
|
|
|
BaseSyncTarget.dispatch = this.store().dispatch;
|
2017-12-14 20:53:08 +02:00
|
|
|
DecryptionWorker.instance().dispatch = this.store().dispatch;
|
2018-11-14 00:25:23 +02:00
|
|
|
ResourceFetcher.instance().dispatch = this.store().dispatch;
|
2021-11-03 18:24:40 +02:00
|
|
|
ShareService.instance().initialize(this.store(), EncryptionService.instance());
|
2017-11-04 15:23:15 +02:00
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
public deinitRedux() {
|
2020-02-22 13:25:16 +02:00
|
|
|
this.store_ = null;
|
|
|
|
BaseModel.dispatch = function() {};
|
|
|
|
BaseSyncTarget.dispatch = function() {};
|
|
|
|
DecryptionWorker.instance().dispatch = function() {};
|
|
|
|
ResourceFetcher.instance().dispatch = function() {};
|
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
public async readFlagsFromFile(flagPath: string) {
|
2017-11-16 20:51:11 +02:00
|
|
|
if (!fs.existsSync(flagPath)) return {};
|
2018-03-09 22:59:12 +02:00
|
|
|
let flagContent = fs.readFileSync(flagPath, 'utf8');
|
2017-11-16 20:51:11 +02:00
|
|
|
if (!flagContent) return {};
|
|
|
|
|
|
|
|
flagContent = flagContent.trim();
|
|
|
|
|
2023-03-19 17:37:07 +02:00
|
|
|
let flags: any = splitCommandString(flagContent);
|
2018-03-09 22:59:12 +02:00
|
|
|
flags.splice(0, 0, 'cmd');
|
|
|
|
flags.splice(0, 0, 'node');
|
2017-11-16 20:51:11 +02:00
|
|
|
|
|
|
|
flags = await this.handleStartFlags_(flags, false);
|
2018-09-04 06:42:22 +02:00
|
|
|
|
2017-11-16 20:51:11 +02:00
|
|
|
return flags.matched;
|
|
|
|
}
|
|
|
|
|
2023-10-31 17:05:28 +02:00
|
|
|
public static determineProfileDir(initArgs: MatchedStartFlags) {
|
2020-05-25 01:30:29 +02:00
|
|
|
let output = '';
|
2018-05-14 12:08:33 +02:00
|
|
|
|
2020-05-25 01:30:29 +02:00
|
|
|
if (initArgs.profileDir) {
|
|
|
|
output = initArgs.profileDir;
|
|
|
|
} else if (process && process.env && process.env.PORTABLE_EXECUTABLE_DIR) {
|
|
|
|
output = `${process.env.PORTABLE_EXECUTABLE_DIR}/JoplinProfile`;
|
|
|
|
} else {
|
|
|
|
output = `${os.homedir()}/.config/${Setting.value('appName')}`;
|
|
|
|
}
|
2018-05-14 12:08:33 +02:00
|
|
|
|
2020-05-25 01:30:29 +02:00
|
|
|
return toSystemSlashes(output, 'linux');
|
2018-05-14 12:08:33 +02:00
|
|
|
}
|
|
|
|
|
2023-08-08 17:02:05 +02:00
|
|
|
protected startRotatingLogMaintenance(profileDir: string) {
|
|
|
|
this.rotatingLogs = new RotatingLogs(profileDir);
|
|
|
|
const processLogs = async () => {
|
|
|
|
try {
|
|
|
|
await this.rotatingLogs.cleanActiveLogFile();
|
|
|
|
await this.rotatingLogs.deleteNonActiveLogFiles();
|
|
|
|
} catch (error) {
|
|
|
|
appLogger.error(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
shim.setTimeout(() => { void processLogs(); }, 60000);
|
|
|
|
shim.setInterval(() => { void processLogs(); }, 24 * 60 * 60 * 1000);
|
|
|
|
}
|
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
public async start(argv: string[], options: StartOptions = null): Promise<any> {
|
2021-08-11 17:23:37 +02:00
|
|
|
options = {
|
|
|
|
keychainEnabled: true,
|
2023-08-04 11:57:54 +02:00
|
|
|
setupGlobalLogger: true,
|
2021-08-11 17:23:37 +02:00
|
|
|
...options,
|
|
|
|
};
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const startFlags = await this.handleStartFlags_(argv);
|
2017-11-04 15:23:15 +02:00
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
argv = startFlags.argv;
|
|
|
|
let initArgs = startFlags.matched;
|
|
|
|
if (argv.length) this.showPromptString_ = false;
|
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
let appName = initArgs.env === 'dev' ? 'joplindev' : 'joplin';
|
2018-03-09 22:59:12 +02:00
|
|
|
if (Setting.value('appId').indexOf('-desktop') >= 0) appName += '-desktop';
|
|
|
|
Setting.setConstant('appName', appName);
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
// https://immerjs.github.io/immer/docs/freezing
|
|
|
|
setAutoFreeze(initArgs.env === 'dev');
|
|
|
|
|
2023-10-31 17:05:28 +02:00
|
|
|
const rootProfileDir = BaseApplication.determineProfileDir(initArgs);
|
2022-04-11 17:49:32 +02:00
|
|
|
const { profileDir, profileConfig, isSubProfile } = await initProfile(rootProfileDir);
|
|
|
|
this.profileConfig_ = profileConfig;
|
|
|
|
|
2019-05-11 12:46:13 +02:00
|
|
|
const resourceDirName = 'resources';
|
2019-09-19 23:51:18 +02:00
|
|
|
const resourceDir = `${profileDir}/${resourceDirName}`;
|
|
|
|
const tempDir = `${profileDir}/tmp`;
|
2021-01-20 00:58:09 +02:00
|
|
|
const cacheDir = `${profileDir}/cache`;
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
Setting.setConstant('env', initArgs.env);
|
2019-05-11 12:46:13 +02:00
|
|
|
Setting.setConstant('resourceDirName', resourceDirName);
|
2018-03-09 22:59:12 +02:00
|
|
|
Setting.setConstant('resourceDir', resourceDir);
|
|
|
|
Setting.setConstant('tempDir', tempDir);
|
2021-01-24 17:51:35 +02:00
|
|
|
Setting.setConstant('pluginDataDir', `${profileDir}/plugin-data`);
|
2021-01-20 00:58:09 +02:00
|
|
|
Setting.setConstant('cacheDir', cacheDir);
|
2022-04-16 14:36:14 +02:00
|
|
|
Setting.setConstant('pluginDir', `${rootProfileDir}/plugins`);
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2021-08-16 19:05:22 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetNone);
|
2020-02-08 13:59:19 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetFilesystem);
|
|
|
|
SyncTargetRegistry.addClass(SyncTargetOneDrive);
|
|
|
|
SyncTargetRegistry.addClass(SyncTargetNextcloud);
|
|
|
|
SyncTargetRegistry.addClass(SyncTargetWebDAV);
|
|
|
|
SyncTargetRegistry.addClass(SyncTargetDropbox);
|
2020-07-15 11:22:55 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetAmazonS3);
|
2020-12-28 13:48:47 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetJoplinServer);
|
2021-06-03 17:12:07 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetJoplinCloud);
|
2020-02-08 13:59:19 +02:00
|
|
|
|
2020-07-22 20:03:31 +02:00
|
|
|
try {
|
|
|
|
await shim.fsDriver().remove(tempDir);
|
|
|
|
} catch (error) {
|
|
|
|
// Can't do anything in this case, not even log, since the logger
|
|
|
|
// is not yet ready. But normally it's not an issue if the temp
|
|
|
|
// dir cannot be deleted.
|
|
|
|
}
|
2018-06-18 20:56:07 +02:00
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
await fs.mkdirp(profileDir, 0o755);
|
|
|
|
await fs.mkdirp(resourceDir, 0o755);
|
|
|
|
await fs.mkdirp(tempDir, 0o755);
|
2021-01-20 00:58:09 +02:00
|
|
|
await fs.mkdirp(cacheDir, 0o755);
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2019-05-11 12:46:13 +02:00
|
|
|
// Clean up any remaining watched files (they start with "edit-")
|
|
|
|
await shim.fsDriver().removeAllThatStartWith(profileDir, 'edit-');
|
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
const extraFlags = await this.readFlagsFromFile(`${profileDir}/flags.txt`);
|
2023-06-01 13:02:36 +02:00
|
|
|
initArgs = { ...initArgs, ...extraFlags };
|
2017-11-16 20:51:11 +02:00
|
|
|
|
2023-08-04 11:57:54 +02:00
|
|
|
const globalLogger = Logger.globalLogger;
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2023-08-04 11:57:54 +02:00
|
|
|
if (options.setupGlobalLogger) {
|
|
|
|
globalLogger.addTarget(TargetType.File, { path: `${profileDir}/log.txt` });
|
|
|
|
if (Setting.value('appType') === 'desktop') {
|
|
|
|
globalLogger.addTarget(TargetType.Console);
|
|
|
|
}
|
|
|
|
globalLogger.setLevel(initArgs.logLevel);
|
2020-06-03 18:07:50 +02:00
|
|
|
}
|
2020-11-19 17:25:02 +02:00
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
reg.setLogger(Logger.create('') as Logger);
|
|
|
|
// reg.dispatch = () => {};
|
2020-11-19 17:25:02 +02:00
|
|
|
|
|
|
|
BaseService.logger_ = globalLogger;
|
2020-06-03 18:07:50 +02:00
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2020-11-19 17:25:02 +02:00
|
|
|
appLogger.info(`Profile directory: ${profileDir}`);
|
2022-04-11 17:49:32 +02:00
|
|
|
appLogger.info(`Root profile directory: ${rootProfileDir}`);
|
2017-11-04 14:23:46 +02:00
|
|
|
|
|
|
|
this.database_ = new JoplinDatabase(new DatabaseDriverNode());
|
2018-03-09 22:59:12 +02:00
|
|
|
this.database_.setLogExcludedQueryTypes(['SELECT']);
|
2020-11-19 17:25:02 +02:00
|
|
|
this.database_.setLogger(globalLogger);
|
2020-09-06 14:07:00 +02:00
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
await this.database_.open({ name: `${profileDir}/database.sqlite` });
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2019-05-26 20:39:07 +02:00
|
|
|
// if (Setting.value('env') === 'dev') await this.database_.clearForTesting();
|
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
reg.setDb(this.database_);
|
2020-03-16 04:30:54 +02:00
|
|
|
BaseModel.setDb(this.database_);
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2021-10-03 17:00:49 +02:00
|
|
|
setRSA(RSA);
|
|
|
|
|
2021-08-11 17:23:37 +02:00
|
|
|
await loadKeychainServiceAndSettings(options.keychainEnabled ? KeychainServiceDriver : KeychainServiceDriverDummy);
|
2021-08-30 15:15:35 +02:00
|
|
|
await migrateMasterPassword();
|
2021-05-09 18:12:47 +02:00
|
|
|
await handleSyncStartupOperation();
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2020-11-19 17:25:02 +02:00
|
|
|
appLogger.info(`Client ID: ${Setting.value('clientId')}`);
|
2019-10-10 23:23:11 +02:00
|
|
|
|
2023-07-16 18:42:42 +02:00
|
|
|
BaseItem.syncShareCache = parseShareCache(Setting.value('sync.shareCache'));
|
|
|
|
|
2023-03-14 13:46:21 +02:00
|
|
|
if (initArgs?.isSafeMode) {
|
|
|
|
Setting.setValue('isSafeMode', true);
|
|
|
|
}
|
|
|
|
|
2023-10-31 17:05:28 +02:00
|
|
|
const safeModeFlagFile = join(profileDir, safeModeFlagFilename);
|
|
|
|
if (await fs.pathExists(safeModeFlagFile) && fs.readFileSync(safeModeFlagFile, 'utf8') === 'true') {
|
|
|
|
appLogger.info(`Safe mode enabled because of file: ${safeModeFlagFile}`);
|
|
|
|
Setting.setValue('isSafeMode', true);
|
|
|
|
fs.removeSync(safeModeFlagFile);
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (Setting.value('firstStart')) {
|
2023-05-10 12:05:55 +02:00
|
|
|
const locale = shim.detectAndSetLocale(Setting);
|
|
|
|
reg.logger().info(`First start: detected locale as ${locale}`);
|
2021-10-18 13:37:25 +02:00
|
|
|
Setting.skipDefaultMigrations();
|
|
|
|
|
2018-03-26 19:33:55 +02:00
|
|
|
if (Setting.value('env') === 'dev') {
|
|
|
|
Setting.setValue('showTrayIcon', 0);
|
|
|
|
Setting.setValue('autoUpdateEnabled', 0);
|
|
|
|
Setting.setValue('sync.interval', 3600);
|
|
|
|
}
|
2018-04-16 19:18:28 +02:00
|
|
|
|
2018-03-26 19:33:55 +02:00
|
|
|
Setting.setValue('firstStart', 0);
|
2017-11-04 14:23:46 +02:00
|
|
|
} else {
|
2021-10-18 13:37:25 +02:00
|
|
|
Setting.applyDefaultMigrations();
|
2022-08-27 13:05:44 +02:00
|
|
|
Setting.applyUserSettingMigration();
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
2022-04-16 14:29:14 +02:00
|
|
|
setLocale(Setting.value('locale'));
|
|
|
|
|
2021-06-03 17:12:07 +02:00
|
|
|
if (Setting.value('env') === Env.Dev) {
|
2021-06-15 13:25:55 +02:00
|
|
|
// Setting.setValue('sync.10.path', 'https://api.joplincloud.com');
|
|
|
|
// Setting.setValue('sync.10.userContentPath', 'https://joplinusercontent.com');
|
|
|
|
Setting.setValue('sync.10.path', 'http://api.joplincloud.local:22300');
|
2022-12-24 14:30:24 +02:00
|
|
|
Setting.setValue('sync.10.userContentPath', 'http://joplinusercontent.local:22300');
|
2021-06-03 17:12:07 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 19:41:16 +02:00
|
|
|
// For now always disable fuzzy search due to performance issues:
|
|
|
|
// https://discourse.joplinapp.org/t/1-1-4-keyboard-locks-up-while-typing/11231/11
|
|
|
|
// https://discourse.joplinapp.org/t/serious-lagging-when-there-are-tens-of-thousands-of-notes/11215/23
|
|
|
|
Setting.setValue('db.fuzzySearchEnabled', 0);
|
2020-09-23 13:14:17 +02:00
|
|
|
|
2020-03-13 19:42:50 +02:00
|
|
|
if (Setting.value('encryption.shouldReencrypt') < 0) {
|
2020-03-14 02:52:28 +02:00
|
|
|
// We suggest re-encryption if the user has at least one notebook
|
|
|
|
// and if encryption is enabled. This code runs only when shouldReencrypt = -1
|
2020-03-13 19:42:50 +02:00
|
|
|
// which can be set by a maintenance script for example.
|
|
|
|
const folderCount = await Folder.count();
|
2021-08-12 17:54:10 +02:00
|
|
|
const itShould = getEncryptionEnabled() && !!folderCount ? Setting.SHOULD_REENCRYPT_YES : Setting.SHOULD_REENCRYPT_NO;
|
2020-03-13 19:42:50 +02:00
|
|
|
Setting.setValue('encryption.shouldReencrypt', itShould);
|
|
|
|
}
|
|
|
|
|
2019-05-12 02:10:46 +02:00
|
|
|
if ('welcomeDisabled' in initArgs) Setting.setValue('welcome.enabled', !initArgs.welcomeDisabled);
|
2022-04-11 17:49:32 +02:00
|
|
|
if (isSubProfile) Setting.setValue('welcome.enabled', false);
|
2019-05-12 02:10:46 +02:00
|
|
|
|
2018-09-28 20:24:57 +02:00
|
|
|
if (!Setting.value('api.token')) {
|
2021-01-22 19:41:11 +02:00
|
|
|
void EncryptionService.instance()
|
2021-04-07 20:27:16 +02:00
|
|
|
.generateApiToken()
|
2022-09-30 18:23:14 +02:00
|
|
|
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
.then((token: string) => {
|
2019-07-29 15:43:53 +02:00
|
|
|
Setting.setValue('api.token', token);
|
|
|
|
});
|
2018-09-28 20:24:57 +02:00
|
|
|
}
|
|
|
|
|
2018-09-05 00:03:21 +02:00
|
|
|
time.setDateFormat(Setting.value('dateFormat'));
|
|
|
|
time.setTimeFormat(Setting.value('timeFormat'));
|
|
|
|
|
2019-05-06 22:35:29 +02:00
|
|
|
BaseItem.revisionService_ = RevisionService.instance();
|
|
|
|
|
2019-06-08 00:11:08 +02:00
|
|
|
KvStore.instance().setDb(reg.db());
|
|
|
|
|
2017-12-14 02:23:32 +02:00
|
|
|
BaseItem.encryptionService_ = EncryptionService.instance();
|
2021-05-13 18:57:37 +02:00
|
|
|
BaseItem.shareService_ = ShareService.instance();
|
2022-02-09 20:04:27 +02:00
|
|
|
Resource.shareService_ = ShareService.instance();
|
2020-11-19 17:25:02 +02:00
|
|
|
DecryptionWorker.instance().setLogger(globalLogger);
|
2017-12-14 21:39:13 +02:00
|
|
|
DecryptionWorker.instance().setEncryptionService(EncryptionService.instance());
|
2019-06-08 00:11:08 +02:00
|
|
|
DecryptionWorker.instance().setKvStore(KvStore.instance());
|
2021-08-12 17:54:10 +02:00
|
|
|
await loadMasterKeysFromSettings(EncryptionService.instance());
|
2019-05-28 23:05:11 +02:00
|
|
|
DecryptionWorker.instance().on('resourceMetadataButNotBlobDecrypted', this.decryptionWorker_resourceMetadataButNotBlobDecrypted);
|
2017-12-14 02:23:32 +02:00
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
ResourceFetcher.instance().setFileApi(() => {
|
|
|
|
return reg.syncTarget().fileApi();
|
|
|
|
});
|
2020-11-19 17:25:02 +02:00
|
|
|
ResourceFetcher.instance().setLogger(globalLogger);
|
2019-05-22 16:56:07 +02:00
|
|
|
ResourceFetcher.instance().on('downloadComplete', this.resourceFetcher_downloadComplete);
|
2021-01-22 19:41:11 +02:00
|
|
|
void ResourceFetcher.instance().start();
|
2018-10-08 20:11:53 +02:00
|
|
|
|
2019-01-19 20:03:05 +02:00
|
|
|
SearchEngine.instance().setDb(reg.db());
|
|
|
|
SearchEngine.instance().setLogger(reg.logger());
|
|
|
|
SearchEngine.instance().scheduleSyncTables();
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const currentFolderId = Setting.value('activeFolderId');
|
2017-11-04 14:23:46 +02:00
|
|
|
let currentFolder = null;
|
|
|
|
if (currentFolderId) currentFolder = await Folder.load(currentFolderId);
|
|
|
|
if (!currentFolder) currentFolder = await Folder.defaultFolder();
|
2018-03-09 22:59:12 +02:00
|
|
|
Setting.setValue('activeFolderId', currentFolder ? currentFolder.id : '');
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2019-05-11 18:55:40 +02:00
|
|
|
await MigrationService.instance().run();
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
}
|