2018-03-09 22:59:12 +02:00
|
|
|
const { createStore, applyMiddleware } = require('redux');
|
|
|
|
const { reducer, defaultState, stateUtils } = require('lib/reducer.js');
|
|
|
|
const { JoplinDatabase } = require('lib/joplin-database.js');
|
|
|
|
const { FoldersScreenUtils } = require('lib/folders-screen-utils.js');
|
|
|
|
const { DatabaseDriverNode } = require('lib/database-driver-node.js');
|
|
|
|
const BaseModel = require('lib/BaseModel.js');
|
|
|
|
const Folder = require('lib/models/Folder.js');
|
|
|
|
const BaseItem = require('lib/models/BaseItem.js');
|
|
|
|
const Note = require('lib/models/Note.js');
|
|
|
|
const Tag = require('lib/models/Tag.js');
|
|
|
|
const Setting = require('lib/models/Setting.js');
|
|
|
|
const { Logger } = require('lib/logger.js');
|
|
|
|
const { splitCommandString } = require('lib/string-utils.js');
|
|
|
|
const { reg } = require('lib/registry.js');
|
|
|
|
const { time } = require('lib/time-utils.js');
|
|
|
|
const BaseSyncTarget = require('lib/BaseSyncTarget.js');
|
|
|
|
const { shim } = require('lib/shim.js');
|
2019-10-10 23:23:11 +02:00
|
|
|
const { uuid } = require('lib/uuid.js');
|
2019-07-29 15:58:33 +02:00
|
|
|
const { _, setLocale } = require('lib/locale.js');
|
2018-05-09 11:49:31 +02:00
|
|
|
const reduxSharedMiddleware = require('lib/components/shared/reduxSharedMiddleware');
|
2018-03-09 22:59:12 +02:00
|
|
|
const os = require('os');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const JoplinError = require('lib/JoplinError');
|
|
|
|
const EventEmitter = require('events');
|
2018-06-20 02:18:58 +02:00
|
|
|
const syswidecas = require('syswide-cas');
|
2018-03-09 22:59:12 +02:00
|
|
|
const SyncTargetRegistry = require('lib/SyncTargetRegistry.js');
|
|
|
|
const SyncTargetFilesystem = require('lib/SyncTargetFilesystem.js');
|
|
|
|
const SyncTargetOneDrive = require('lib/SyncTargetOneDrive.js');
|
|
|
|
const SyncTargetOneDriveDev = require('lib/SyncTargetOneDriveDev.js');
|
|
|
|
const SyncTargetNextcloud = require('lib/SyncTargetNextcloud.js');
|
|
|
|
const SyncTargetWebDAV = require('lib/SyncTargetWebDAV.js');
|
2018-03-26 19:33:55 +02:00
|
|
|
const SyncTargetDropbox = require('lib/SyncTargetDropbox.js');
|
2018-03-09 22:59:12 +02:00
|
|
|
const EncryptionService = require('lib/services/EncryptionService');
|
2018-10-08 20:11:53 +02:00
|
|
|
const ResourceFetcher = require('lib/services/ResourceFetcher');
|
2018-12-16 19:32:42 +02:00
|
|
|
const SearchEngineUtils = require('lib/services/SearchEngineUtils');
|
2019-05-06 22:35:29 +02:00
|
|
|
const RevisionService = require('lib/services/RevisionService');
|
2018-03-09 22:59:12 +02:00
|
|
|
const DecryptionWorker = require('lib/services/DecryptionWorker');
|
2018-03-15 20:08:46 +02:00
|
|
|
const BaseService = require('lib/services/BaseService');
|
2019-01-19 20:03:05 +02:00
|
|
|
const SearchEngine = require('lib/services/SearchEngine');
|
2019-06-08 00:11:08 +02:00
|
|
|
const KvStore = require('lib/services/KvStore');
|
2019-05-11 18:55:40 +02:00
|
|
|
const MigrationService = require('lib/services/MigrationService');
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2017-11-24 21:21:30 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetFilesystem);
|
|
|
|
SyncTargetRegistry.addClass(SyncTargetOneDrive);
|
2017-11-24 21:47:24 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetOneDriveDev);
|
2018-01-25 21:01:14 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetNextcloud);
|
2018-02-02 01:40:05 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetWebDAV);
|
2018-03-26 19:33:55 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTargetDropbox);
|
2017-11-24 21:21:30 +02:00
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
class BaseApplication {
|
|
|
|
constructor() {
|
|
|
|
this.logger_ = new Logger();
|
|
|
|
this.dbLogger_ = new Logger();
|
|
|
|
this.eventEmitter_ = new EventEmitter();
|
|
|
|
|
2018-09-04 06:42:22 +02:00
|
|
|
// Note: this is basically a cache of state.selectedFolderId. It should *only*
|
2017-11-04 14:23:46 +02:00
|
|
|
// be derived from the state and not set directly since that would make the
|
|
|
|
// state and UI out of sync.
|
2018-09-04 06:42:22 +02:00
|
|
|
this.currentFolder_ = null;
|
2019-05-28 23:05:11 +02:00
|
|
|
|
|
|
|
this.decryptionWorker_resourceMetadataButNotBlobDecrypted = this.decryptionWorker_resourceMetadataButNotBlobDecrypted.bind(this);
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logger() {
|
|
|
|
return this.logger_;
|
|
|
|
}
|
|
|
|
|
|
|
|
store() {
|
|
|
|
return this.store_;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentFolder() {
|
|
|
|
return this.currentFolder_;
|
|
|
|
}
|
|
|
|
|
|
|
|
async refreshCurrentFolder() {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
switchCurrentFolder(folder) {
|
2017-12-07 20:12:46 +02:00
|
|
|
if (!this.hasGui()) {
|
|
|
|
this.currentFolder_ = Object.assign({}, 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.
|
2017-11-16 20:51:11 +02:00
|
|
|
async handleStartFlags_(argv, setDefaults = true) {
|
2017-11-04 14:23:46 +02:00
|
|
|
let matched = {};
|
|
|
|
argv = argv.slice(0);
|
|
|
|
argv.splice(0, 2); // First arguments are the node executable, and the node JS file
|
|
|
|
|
|
|
|
while (argv.length) {
|
|
|
|
let arg = argv[0];
|
|
|
|
let nextArg = argv.length >= 2 ? argv[1] : null;
|
2018-09-04 06:42:22 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (arg == '--profile') {
|
|
|
|
if (!nextArg) throw new JoplinError(_('Usage: %s', '--profile <dir-path>'), 'flagError');
|
2017-11-04 14:23:46 +02:00
|
|
|
matched.profileDir = nextArg;
|
|
|
|
argv.splice(0, 2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-12 02:10:46 +02:00
|
|
|
if (arg == '--no-welcome') {
|
|
|
|
matched.welcomeDisabled = true;
|
|
|
|
argv.splice(0, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (arg == '--env') {
|
|
|
|
if (!nextArg) throw new JoplinError(_('Usage: %s', '--env <dev|prod>'), 'flagError');
|
2017-11-04 14:23:46 +02:00
|
|
|
matched.env = nextArg;
|
|
|
|
argv.splice(0, 2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (arg == '--is-demo') {
|
|
|
|
Setting.setConstant('isDemo', true);
|
2017-11-04 14:23:46 +02:00
|
|
|
argv.splice(0, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (arg == '--open-dev-tools') {
|
|
|
|
Setting.setConstant('openDevTools', true);
|
2017-11-17 20:02:01 +02:00
|
|
|
argv.splice(0, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (arg == '--update-geolocation-disabled') {
|
2017-11-04 14:23:46 +02:00
|
|
|
Note.updateGeolocationEnabled_ = false;
|
|
|
|
argv.splice(0, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (arg == '--stack-trace-enabled') {
|
2017-11-04 14:23:46 +02:00
|
|
|
this.showStackTraces_ = true;
|
|
|
|
argv.splice(0, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (arg == '--log-level') {
|
|
|
|
if (!nextArg) throw new JoplinError(_('Usage: %s', '--log-level <none|error|warn|info|debug>'), 'flagError');
|
2017-11-04 14:23:46 +02:00
|
|
|
matched.logLevel = Logger.levelStringToId(nextArg);
|
|
|
|
argv.splice(0, 2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-02 16:51:33 +02:00
|
|
|
if (arg.indexOf('-psn') === 0) {
|
|
|
|
// Some weird flag passed by macOS - can be ignored.
|
|
|
|
// https://github.com/laurent22/joplin/issues/480
|
|
|
|
// https://stackoverflow.com/questions/10242115
|
|
|
|
argv.splice(0, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-03-12 23:41:13 +02:00
|
|
|
if (arg === '--enable-logging') {
|
|
|
|
// Electron-specific flag used for debugging - ignore it
|
|
|
|
argv.splice(0, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (arg.length && arg[0] == '-') {
|
|
|
|
throw new JoplinError(_('Unknown flag: %s', arg), 'flagError');
|
2017-11-04 14:23:46 +02:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:51:11 +02:00
|
|
|
if (setDefaults) {
|
|
|
|
if (!matched.logLevel) matched.logLevel = Logger.LEVEL_INFO;
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!matched.env) matched.env = 'prod';
|
2017-11-16 20:51:11 +02:00
|
|
|
}
|
2017-11-04 14:23:46 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
matched: matched,
|
|
|
|
argv: argv,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
on(eventName, callback) {
|
|
|
|
return this.eventEmitter_.on(eventName, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
async exit(code = 0) {
|
|
|
|
await Setting.saveAll();
|
|
|
|
process.exit(code);
|
|
|
|
}
|
|
|
|
|
2019-09-09 19:16:00 +02:00
|
|
|
async refreshNotes(state, 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;
|
|
|
|
}
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.logger().debug('Refreshing notes:', parentType, parentId);
|
2017-11-04 14:23:46 +02:00
|
|
|
|
|
|
|
let 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,
|
|
|
|
});
|
|
|
|
|
|
|
|
let notes = [];
|
|
|
|
|
|
|
|
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);
|
2019-01-19 20:03:05 +02:00
|
|
|
notes = await SearchEngineUtils.notesForQuery(search.query_pattern);
|
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
|
|
|
}
|
|
|
|
|
2019-05-22 16:56:07 +02:00
|
|
|
resourceFetcher_downloadComplete(event) {
|
|
|
|
if (event.encrypted) {
|
|
|
|
DecryptionWorker.instance().scheduleStart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 00:16:42 +02:00
|
|
|
async decryptionWorker_resourceMetadataButNotBlobDecrypted() {
|
2019-05-28 23:05:11 +02:00
|
|
|
this.scheduleAutoAddResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
scheduleAutoAddResources() {
|
|
|
|
if (this.scheduleAutoAddResourcesIID_) return;
|
|
|
|
|
|
|
|
this.scheduleAutoAddResourcesIID_ = setTimeout(() => {
|
|
|
|
this.scheduleAutoAddResourcesIID_ = null;
|
|
|
|
ResourceFetcher.instance().autoAddResources();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
reducerActionToString(action) {
|
|
|
|
let 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
|
|
|
}
|
|
|
|
|
2017-11-05 02:17:48 +02:00
|
|
|
hasGui() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-07 20:12:46 +02:00
|
|
|
uiType() {
|
2018-03-09 22:59:12 +02:00
|
|
|
return this.hasGui() ? 'gui' : 'cli';
|
2017-12-07 20:12:46 +02:00
|
|
|
}
|
|
|
|
|
2017-11-10 20:58:00 +02:00
|
|
|
generalMiddlewareFn() {
|
2019-07-29 15:43:53 +02:00
|
|
|
const middleware = store => next => action => {
|
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
|
|
|
|
2019-09-06 19:29:40 +02:00
|
|
|
async applySettingsSideEffects(action = null) {
|
|
|
|
const sideEffects = {
|
|
|
|
'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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'encryption.enabled': async () => {
|
|
|
|
if (this.hasGui()) {
|
|
|
|
await EncryptionService.instance().loadMasterKeysFromSettings();
|
|
|
|
DecryptionWorker.instance().scheduleStart();
|
|
|
|
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.
|
|
|
|
reg.scheduleSync();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'sync.interval': async () => {
|
|
|
|
if (this.hasGui()) reg.setupRecurrentSync();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
sideEffects['timeFormat'] = sideEffects['dateFormat'];
|
|
|
|
sideEffects['locale'] = sideEffects['dateFormat'];
|
2019-09-06 19:33:30 +02:00
|
|
|
sideEffects['encryption.activeMasterKeyId'] = sideEffects['encryption.enabled'];
|
|
|
|
sideEffects['encryption.passwordCache'] = sideEffects['encryption.enabled'];
|
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]();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 20:58:00 +02:00
|
|
|
async generalMiddleware(store, next, action) {
|
2019-05-28 23:05:11 +02:00
|
|
|
// this.logger().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);
|
|
|
|
const newState = store.getState();
|
2018-02-22 20:58:15 +02:00
|
|
|
let refreshNotes = false;
|
2019-03-02 19:35:57 +02:00
|
|
|
let refreshFolders = false;
|
2019-02-24 20:02:50 +02:00
|
|
|
// let refreshTags = 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
|
|
|
|
2019-02-24 20:02:50 +02:00
|
|
|
await reduxSharedMiddleware(store, next, action);
|
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) {
|
|
|
|
if (!(await reg.syncTarget().syncStarted())) reg.scheduleSync(30 * 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) {
|
2019-03-08 19:25:32 +02:00
|
|
|
refreshFolders = true;
|
|
|
|
}
|
|
|
|
|
2018-11-08 02:58:06 +02:00
|
|
|
if (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
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +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;
|
|
|
|
}
|
|
|
|
|
2018-05-09 22:00:05 +02:00
|
|
|
if (this.hasGui() && ((action.type == 'SETTING_UPDATE_ONE' && action.key == 'showCompletedTodos') || action.type == 'SETTING_UPDATE_ALL')) {
|
|
|
|
refreshNotes = true;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +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;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (action.type == 'TAG_SELECT' || action.type === 'TAG_DELETE') {
|
2018-02-22 20:58:15 +02:00
|
|
|
refreshNotes = true;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (action.type === 'NOTE_UPDATE_ONE') {
|
2019-03-02 19:35:57 +02:00
|
|
|
refreshFolders = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.hasGui() && ((action.type == 'SETTING_UPDATE_ONE' && action.key.indexOf('folders.sortOrder') === 0) || action.type == 'SETTING_UPDATE_ALL')) {
|
|
|
|
refreshFolders = 'now';
|
2017-11-12 20:57:59 +02:00
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (this.hasGui() && action.type === 'SYNC_GOT_ENCRYPTED_ITEM') {
|
2017-12-21 21:06:08 +02:00
|
|
|
DecryptionWorker.instance().scheduleStart();
|
|
|
|
}
|
|
|
|
|
2018-10-08 20:11:53 +02:00
|
|
|
if (this.hasGui() && action.type === 'SYNC_CREATED_RESOURCE') {
|
2019-05-22 16:56:07 +02:00
|
|
|
ResourceFetcher.instance().autoAddResources();
|
2018-10-08 20:11:53 +02:00
|
|
|
}
|
|
|
|
|
2019-09-06 19:29:40 +02:00
|
|
|
if (action.type == 'SETTING_UPDATE_ONE') {
|
|
|
|
await this.applySettingsSideEffects(action);
|
|
|
|
} else if (action.type == 'SETTING_UPDATE_ALL') {
|
|
|
|
await this.applySettingsSideEffects();
|
|
|
|
}
|
|
|
|
|
2019-03-02 19:35:57 +02:00
|
|
|
if (refreshFolders) {
|
|
|
|
if (refreshFolders === 'now') {
|
|
|
|
await FoldersScreenUtils.refreshFolders();
|
|
|
|
} else {
|
|
|
|
await FoldersScreenUtils.scheduleRefreshFolders();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
return result;
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(action) {
|
|
|
|
if (this.store()) return this.store().dispatch(action);
|
|
|
|
}
|
|
|
|
|
2017-11-06 20:35:04 +02:00
|
|
|
reducer(state = defaultState, action) {
|
|
|
|
return reducer(state, action);
|
|
|
|
}
|
|
|
|
|
2017-11-04 15:23:15 +02:00
|
|
|
initRedux() {
|
2017-11-10 20:58:00 +02:00
|
|
|
this.store_ = createStore(this.reducer, applyMiddleware(this.generalMiddlewareFn()));
|
2017-11-04 15:23:15 +02:00
|
|
|
BaseModel.dispatch = this.store().dispatch;
|
|
|
|
FoldersScreenUtils.dispatch = this.store().dispatch;
|
2017-11-06 23:11:15 +02:00
|
|
|
reg.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;
|
2017-11-04 15:23:15 +02:00
|
|
|
}
|
|
|
|
|
2017-11-16 20:51:11 +02:00
|
|
|
async readFlagsFromFile(flagPath) {
|
|
|
|
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();
|
|
|
|
|
|
|
|
let flags = 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;
|
|
|
|
}
|
|
|
|
|
2018-05-14 12:08:33 +02:00
|
|
|
determineProfileDir(initArgs) {
|
|
|
|
if (initArgs.profileDir) return initArgs.profileDir;
|
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
if (process && process.env && process.env.PORTABLE_EXECUTABLE_DIR) return `${process.env.PORTABLE_EXECUTABLE_DIR}/JoplinProfile`;
|
2018-05-14 12:08:33 +02:00
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
return `${os.homedir()}/.config/${Setting.value('appName')}`;
|
2018-05-14 12:08:33 +02:00
|
|
|
}
|
|
|
|
|
2018-05-23 13:14:38 +02:00
|
|
|
async testing() {
|
2018-05-23 15:25:59 +02:00
|
|
|
const markdownUtils = require('lib/markdownUtils');
|
2018-05-23 13:14:38 +02:00
|
|
|
const ClipperServer = require('lib/ClipperServer');
|
|
|
|
const server = new ClipperServer();
|
|
|
|
const HtmlToMd = require('lib/HtmlToMd');
|
|
|
|
const service = new HtmlToMd();
|
|
|
|
const html = await shim.fsDriver().readFile('/mnt/d/test.html');
|
2018-05-23 15:25:59 +02:00
|
|
|
let markdown = service.parse(html, { baseUrl: 'https://duckduckgo.com/' });
|
2018-05-23 13:14:38 +02:00
|
|
|
console.info(markdown);
|
|
|
|
console.info('--------------------------------------------------');
|
|
|
|
|
2018-05-23 15:25:59 +02:00
|
|
|
const imageUrls = markdownUtils.extractImageUrls(markdown);
|
|
|
|
let result = await server.downloadImages_(imageUrls);
|
|
|
|
result = await server.createResourcesFromPaths_(result);
|
2018-05-23 13:14:38 +02:00
|
|
|
console.info(result);
|
2018-05-23 15:25:59 +02:00
|
|
|
markdown = server.replaceImageUrlsByResources_(markdown, result);
|
2018-05-23 13:14:38 +02:00
|
|
|
console.info('--------------------------------------------------');
|
|
|
|
console.info(markdown);
|
|
|
|
console.info('--------------------------------------------------');
|
|
|
|
}
|
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
async start(argv) {
|
2018-03-07 21:11:55 +02:00
|
|
|
let 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;
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
let appName = initArgs.env == 'dev' ? 'joplindev' : 'joplin';
|
|
|
|
if (Setting.value('appId').indexOf('-desktop') >= 0) appName += '-desktop';
|
|
|
|
Setting.setConstant('appName', appName);
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2018-05-14 12:08:33 +02:00
|
|
|
const profileDir = this.determineProfileDir(initArgs);
|
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`;
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
Setting.setConstant('env', initArgs.env);
|
|
|
|
Setting.setConstant('profileDir', profileDir);
|
2019-09-19 23:51:18 +02:00
|
|
|
Setting.setConstant('templateDir', `${profileDir}/templates`);
|
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);
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2018-06-18 20:56:07 +02:00
|
|
|
await shim.fsDriver().remove(tempDir);
|
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
await fs.mkdirp(profileDir, 0o755);
|
|
|
|
await fs.mkdirp(resourceDir, 0o755);
|
|
|
|
await fs.mkdirp(tempDir, 0o755);
|
|
|
|
|
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`);
|
2017-11-16 20:51:11 +02:00
|
|
|
initArgs = Object.assign(initArgs, extraFlags);
|
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
this.logger_.addTarget('file', { path: `${profileDir}/log.txt` });
|
2019-05-11 18:53:56 +02:00
|
|
|
if (Setting.value('env') === 'dev') this.logger_.addTarget('console', { level: Logger.LEVEL_WARN });
|
2017-11-04 14:23:46 +02:00
|
|
|
this.logger_.setLevel(initArgs.logLevel);
|
|
|
|
|
|
|
|
reg.setLogger(this.logger_);
|
2019-09-13 00:16:42 +02:00
|
|
|
reg.dispatch = () => {};
|
2017-11-04 14:23:46 +02:00
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
this.dbLogger_.addTarget('file', { path: `${profileDir}/log-database.txt` });
|
2017-11-04 14:23:46 +02:00
|
|
|
this.dbLogger_.setLevel(initArgs.logLevel);
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (Setting.value('env') === 'dev') {
|
2018-12-28 22:40:29 +02:00
|
|
|
this.dbLogger_.setLevel(Logger.LEVEL_INFO);
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
this.logger_.info(`Profile directory: ${profileDir}`);
|
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']);
|
2017-11-04 14:23:46 +02:00
|
|
|
this.database_.setLogger(this.dbLogger_);
|
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_);
|
|
|
|
BaseModel.db_ = this.database_;
|
|
|
|
|
|
|
|
await Setting.load();
|
|
|
|
|
2019-10-10 23:23:11 +02:00
|
|
|
if (!Setting.value('clientId')) Setting.setValue('clientId', uuid.create());
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (Setting.value('firstStart')) {
|
2017-11-04 14:23:46 +02:00
|
|
|
const locale = shim.detectAndSetLocale(Setting);
|
2019-09-19 23:51:18 +02:00
|
|
|
reg.logger().info(`First start: detected locale as ${locale}`);
|
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 {
|
2018-03-09 22:59:12 +02:00
|
|
|
setLocale(Setting.value('locale'));
|
2017-11-04 14:23:46 +02:00
|
|
|
}
|
|
|
|
|
2019-05-12 02:10:46 +02:00
|
|
|
if ('welcomeDisabled' in initArgs) Setting.setValue('welcome.enabled', !initArgs.welcomeDisabled);
|
|
|
|
|
2018-09-28 20:24:57 +02:00
|
|
|
if (!Setting.value('api.token')) {
|
2019-07-29 15:43:53 +02:00
|
|
|
EncryptionService.instance()
|
|
|
|
.randomHexString(64)
|
|
|
|
.then(token => {
|
|
|
|
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());
|
|
|
|
|
2018-03-15 20:08:46 +02:00
|
|
|
BaseService.logger_ = this.logger_;
|
2017-12-14 21:39:13 +02:00
|
|
|
EncryptionService.instance().setLogger(this.logger_);
|
2017-12-14 02:23:32 +02:00
|
|
|
BaseItem.encryptionService_ = EncryptionService.instance();
|
2017-12-14 21:39:13 +02:00
|
|
|
DecryptionWorker.instance().setLogger(this.logger_);
|
|
|
|
DecryptionWorker.instance().setEncryptionService(EncryptionService.instance());
|
2019-06-08 00:11:08 +02:00
|
|
|
DecryptionWorker.instance().setKvStore(KvStore.instance());
|
2017-12-14 02:23:32 +02:00
|
|
|
await EncryptionService.instance().loadMasterKeysFromSettings();
|
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();
|
|
|
|
});
|
2018-10-08 20:11:53 +02:00
|
|
|
ResourceFetcher.instance().setLogger(this.logger_);
|
2019-05-22 16:56:07 +02:00
|
|
|
ResourceFetcher.instance().on('downloadComplete', this.resourceFetcher_downloadComplete);
|
2018-10-08 20:11:53 +02:00
|
|
|
ResourceFetcher.instance().start();
|
|
|
|
|
2019-01-19 20:03:05 +02:00
|
|
|
SearchEngine.instance().setDb(reg.db());
|
|
|
|
SearchEngine.instance().setLogger(reg.logger());
|
|
|
|
SearchEngine.instance().scheduleSyncTables();
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
let 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();
|
|
|
|
|
2017-11-04 14:23:46 +02:00
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 06:42:22 +02:00
|
|
|
module.exports = { BaseApplication };
|