2018-03-09 22:59:12 +02:00
|
|
|
const BaseModel = require('lib/BaseModel.js');
|
|
|
|
const { sprintf } = require('sprintf-js');
|
|
|
|
const BaseItem = require('lib/models/BaseItem.js');
|
2018-03-13 01:40:43 +02:00
|
|
|
const ItemChange = require('lib/models/ItemChange.js');
|
2018-03-09 22:59:12 +02:00
|
|
|
const Setting = require('lib/models/Setting.js');
|
|
|
|
const { shim } = require('lib/shim.js');
|
|
|
|
const { time } = require('lib/time-utils.js');
|
|
|
|
const { _ } = require('lib/locale.js');
|
|
|
|
const moment = require('moment');
|
|
|
|
const lodash = require('lodash');
|
2017-05-10 21:21:09 +02:00
|
|
|
|
2017-06-15 20:18:48 +02:00
|
|
|
class Note extends BaseItem {
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2017-05-10 21:51:43 +02:00
|
|
|
static tableName() {
|
2018-03-09 22:59:12 +02:00
|
|
|
return 'notes';
|
2017-05-10 21:51:43 +02:00
|
|
|
}
|
|
|
|
|
2018-02-22 20:58:15 +02:00
|
|
|
static fieldToLabel(field) {
|
|
|
|
const fieldsToLabels = {
|
2018-05-30 19:22:07 +02:00
|
|
|
title: _('title'),
|
|
|
|
user_updated_time: _('updated date'),
|
|
|
|
user_created_time: _('created date'),
|
2018-02-22 20:58:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return field in fieldsToLabels ? fieldsToLabels[field] : field;
|
|
|
|
}
|
|
|
|
|
2017-07-02 17:46:03 +02:00
|
|
|
static async serialize(note, type = null, shownKeys = null) {
|
2017-06-29 22:52:52 +02:00
|
|
|
let fieldNames = this.fieldNames();
|
2018-03-09 22:59:12 +02:00
|
|
|
fieldNames.push('type_');
|
|
|
|
return super.serialize(note, 'note', fieldNames);
|
2017-05-12 21:54:06 +02:00
|
|
|
}
|
|
|
|
|
2017-07-04 21:12:30 +02:00
|
|
|
static async serializeForEdit(note) {
|
2018-03-09 22:59:12 +02:00
|
|
|
return super.serialize(note, 'note', ['title', 'body']);
|
2017-07-04 21:12:30 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 20:31:11 +02:00
|
|
|
static async unserializeForEdit(content) {
|
|
|
|
content += "\n\ntype_: " + BaseModel.TYPE_NOTE;
|
2017-07-13 23:26:45 +02:00
|
|
|
let output = await super.unserialize(content);
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!output.title) output.title = '';
|
|
|
|
if (!output.body) output.body = '';
|
2017-07-13 23:26:45 +02:00
|
|
|
return output;
|
2017-07-05 20:31:11 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 20:47:31 +02:00
|
|
|
static async serializeAllProps(note) {
|
|
|
|
let fieldNames = this.fieldNames();
|
2018-03-09 22:59:12 +02:00
|
|
|
fieldNames.push('type_');
|
|
|
|
lodash.pull(fieldNames, 'title', 'body');
|
|
|
|
return super.serialize(note, 'note', fieldNames);
|
2017-07-13 20:47:31 +02:00
|
|
|
}
|
|
|
|
|
2017-10-24 22:35:02 +02:00
|
|
|
static minimalSerializeForDisplay(note) {
|
|
|
|
let n = Object.assign({}, note);
|
|
|
|
|
|
|
|
let fieldNames = this.fieldNames();
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!n.is_conflict) lodash.pull(fieldNames, 'is_conflict');
|
|
|
|
if (!Number(n.latitude)) lodash.pull(fieldNames, 'latitude');
|
|
|
|
if (!Number(n.longitude)) lodash.pull(fieldNames, 'longitude');
|
|
|
|
if (!Number(n.altitude)) lodash.pull(fieldNames, 'altitude');
|
|
|
|
if (!n.author) lodash.pull(fieldNames, 'author');
|
|
|
|
if (!n.source_url) lodash.pull(fieldNames, 'source_url');
|
2017-10-24 22:35:02 +02:00
|
|
|
if (!n.is_todo) {
|
2018-03-09 22:59:12 +02:00
|
|
|
lodash.pull(fieldNames, 'is_todo');
|
|
|
|
lodash.pull(fieldNames, 'todo_due');
|
|
|
|
lodash.pull(fieldNames, 'todo_completed');
|
2017-10-24 22:35:02 +02:00
|
|
|
}
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!n.application_data) lodash.pull(fieldNames, 'application_data');
|
2017-10-24 22:35:02 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
lodash.pull(fieldNames, 'type_');
|
|
|
|
lodash.pull(fieldNames, 'title');
|
|
|
|
lodash.pull(fieldNames, 'body');
|
|
|
|
lodash.pull(fieldNames, 'created_time');
|
|
|
|
lodash.pull(fieldNames, 'updated_time');
|
|
|
|
lodash.pull(fieldNames, 'order');
|
2017-10-24 22:35:02 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
return super.serialize(n, 'note', fieldNames);
|
2017-10-24 22:35:02 +02:00
|
|
|
}
|
|
|
|
|
2017-08-21 22:46:31 +02:00
|
|
|
static defaultTitle(note) {
|
|
|
|
if (note.body && note.body.length) {
|
|
|
|
const lines = note.body.trim().split("\n");
|
2018-03-14 19:37:47 +02:00
|
|
|
let output = lines[0].trim();
|
|
|
|
// Remove the first #, *, etc.
|
|
|
|
while (output.length) {
|
|
|
|
const c = output[0];
|
|
|
|
if (['#', ' ', "\n", "\t", '*', '`', '-'].indexOf(c) >= 0) {
|
|
|
|
output = output.substr(1);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output.substr(0, 80).trim();
|
2017-08-21 22:46:31 +02:00
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
return _('Untitled');
|
2017-08-21 22:46:31 +02:00
|
|
|
}
|
|
|
|
|
2017-07-18 21:27:10 +02:00
|
|
|
static geolocationUrl(note) {
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!('latitude' in note) || !('longitude' in note)) throw new Error('Latitude or longitude is missing');
|
|
|
|
if (!Number(note.latitude) && !Number(note.longitude)) throw new Error(_('This note does not have geolocation information.'));
|
2018-09-16 20:37:31 +02:00
|
|
|
return this.geoLocationUrlFromLatLong(note.latitude, note.longitude);
|
|
|
|
}
|
|
|
|
|
|
|
|
static geoLocationUrlFromLatLong(lat, long) {
|
|
|
|
return sprintf('https://www.openstreetmap.org/?lat=%s&lon=%s&zoom=20', lat, long)
|
2017-07-18 21:27:10 +02:00
|
|
|
}
|
|
|
|
|
2017-07-03 21:50:45 +02:00
|
|
|
static modelType() {
|
|
|
|
return BaseModel.TYPE_NOTE;
|
2017-05-18 21:58:01 +02:00
|
|
|
}
|
|
|
|
|
2018-05-03 14:11:45 +02:00
|
|
|
static linkedItemIds(body) {
|
2017-08-20 16:29:18 +02:00
|
|
|
// For example: ![](:/fcca2938a96a22570e8eae2565bc6b0b)
|
|
|
|
if (!body || body.length <= 32) return [];
|
|
|
|
const matches = body.match(/\(:\/.{32}\)/g);
|
|
|
|
if (!matches) return [];
|
2018-03-09 22:59:12 +02:00
|
|
|
return matches.map((m) => m.substr(3, 32));
|
2017-08-20 16:29:18 +02:00
|
|
|
}
|
|
|
|
|
2018-05-03 14:11:45 +02:00
|
|
|
static async linkedItems(body) {
|
|
|
|
const itemIds = this.linkedItemIds(body);
|
|
|
|
const output = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < itemIds.length; i++) {
|
|
|
|
const item = await BaseItem.loadItemById(itemIds[i]);
|
|
|
|
if (!item) continue;
|
|
|
|
output.push(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async linkedItemIdsByType(type, body) {
|
|
|
|
const items = await this.linkedItems(body);
|
|
|
|
const output = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
const item = items[i];
|
|
|
|
if (item.type_ === type) output.push(item.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async linkedResourceIds(body) {
|
|
|
|
return await this.linkedItemIdsByType(BaseModel.TYPE_RESOURCE, body);
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
static new(parentId = '') {
|
2017-05-20 00:16:50 +02:00
|
|
|
let output = super.new();
|
|
|
|
output.parent_id = parentId;
|
|
|
|
return output;
|
2017-05-10 21:51:43 +02:00
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
static newTodo(parentId = '') {
|
2017-05-24 22:51:50 +02:00
|
|
|
let output = this.new(parentId);
|
|
|
|
output.is_todo = true;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-10-20 00:02:13 +02:00
|
|
|
// Note: sort logic must be duplicated in previews();
|
2017-07-27 19:25:42 +02:00
|
|
|
static sortNotes(notes, orders, uncompletedTodosOnTop) {
|
2018-03-09 22:59:12 +02:00
|
|
|
const noteOnTop = (note) => {
|
2017-07-27 19:25:42 +02:00
|
|
|
return uncompletedTodosOnTop && note.is_todo && !note.todo_completed;
|
2018-03-09 22:59:12 +02:00
|
|
|
}
|
2017-07-27 19:25:42 +02:00
|
|
|
|
2017-10-20 00:02:13 +02:00
|
|
|
const noteFieldComp = (f1, f2) => {
|
|
|
|
if (f1 === f2) return 0;
|
|
|
|
return f1 < f2 ? -1 : +1;
|
2018-03-09 22:59:12 +02:00
|
|
|
}
|
2017-10-20 00:02:13 +02:00
|
|
|
|
|
|
|
// Makes the sort deterministic, so that if, for example, a and b have the
|
|
|
|
// same updated_time, they aren't swapped every time a list is refreshed.
|
|
|
|
const sortIdenticalNotes = (a, b) => {
|
|
|
|
let r = null;
|
2018-03-09 22:59:12 +02:00
|
|
|
r = noteFieldComp(a.user_updated_time, b.user_updated_time); if (r) return r;
|
|
|
|
r = noteFieldComp(a.user_created_time, b.user_created_time); if (r) return r;
|
2017-12-05 01:38:09 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
const titleA = a.title ? a.title.toLowerCase() : '';
|
|
|
|
const titleB = b.title ? b.title.toLowerCase() : '';
|
|
|
|
r = noteFieldComp(titleA, titleB); if (r) return r;
|
2018-05-30 19:22:07 +02:00
|
|
|
|
2017-10-20 00:02:13 +02:00
|
|
|
return noteFieldComp(a.id, b.id);
|
2018-03-09 22:59:12 +02:00
|
|
|
}
|
2017-10-20 00:02:13 +02:00
|
|
|
|
2017-07-27 19:25:42 +02:00
|
|
|
return notes.sort((a, b) => {
|
|
|
|
if (noteOnTop(a) && !noteOnTop(b)) return -1;
|
|
|
|
if (!noteOnTop(a) && noteOnTop(b)) return +1;
|
|
|
|
|
|
|
|
let r = 0;
|
|
|
|
|
|
|
|
for (let i = 0; i < orders.length; i++) {
|
|
|
|
const order = orders[i];
|
|
|
|
if (a[order.by] < b[order.by]) r = +1;
|
|
|
|
if (a[order.by] > b[order.by]) r = -1;
|
2018-03-09 22:59:12 +02:00
|
|
|
if (order.dir == 'ASC') r = -r;
|
2017-10-20 00:02:13 +02:00
|
|
|
if (r !== 0) return r;
|
2017-07-27 19:25:42 +02:00
|
|
|
}
|
|
|
|
|
2017-10-20 00:02:13 +02:00
|
|
|
return sortIdenticalNotes(a, b);
|
2017-07-27 19:25:42 +02:00
|
|
|
});
|
|
|
|
}
|
2017-07-15 20:13:31 +02:00
|
|
|
|
2017-06-25 14:49:46 +02:00
|
|
|
static previewFields() {
|
2018-03-10 17:34:55 +02:00
|
|
|
return ['id', 'title', 'body', 'is_todo', 'todo_completed', 'parent_id', 'updated_time', 'user_updated_time', 'user_created_time', 'encryption_applied'];
|
2017-06-25 14:49:46 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 23:09:58 +02:00
|
|
|
static previewFieldsSql() {
|
2018-03-09 22:59:12 +02:00
|
|
|
return this.db().escapeFields(this.previewFields()).join(',');
|
2017-05-24 23:09:58 +02:00
|
|
|
}
|
|
|
|
|
2017-07-15 17:35:40 +02:00
|
|
|
static async loadFolderNoteByField(folderId, field, value) {
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!folderId) throw new Error('folderId is undefined');
|
2017-07-15 17:35:40 +02:00
|
|
|
|
|
|
|
let options = {
|
2018-03-09 22:59:12 +02:00
|
|
|
conditions: ['`' + field + '` = ?'],
|
2017-07-15 17:35:40 +02:00
|
|
|
conditionsParams: [value],
|
2018-03-09 22:59:12 +02:00
|
|
|
fields: '*',
|
|
|
|
}
|
2017-07-15 17:35:40 +02:00
|
|
|
|
|
|
|
// TODO: add support for limits on .search()
|
|
|
|
|
|
|
|
let results = await this.previews(folderId, options);
|
|
|
|
return results.length ? results[0] : null;
|
2017-06-27 21:48:01 +02:00
|
|
|
}
|
|
|
|
|
2017-07-26 20:36:16 +02:00
|
|
|
static async previews(parentId, options = null) {
|
2017-10-20 00:02:13 +02:00
|
|
|
// Note: ordering logic must be duplicated in sortNotes(), which
|
2017-07-27 19:25:42 +02:00
|
|
|
// is used to sort already loaded notes.
|
|
|
|
|
2017-06-25 11:00:54 +02:00
|
|
|
if (!options) options = {};
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!options.order) options.order = [
|
|
|
|
{ by: 'user_updated_time', dir: 'DESC' },
|
|
|
|
{ by: 'user_created_time', dir: 'DESC' },
|
|
|
|
{ by: 'title', dir: 'DESC' },
|
|
|
|
{ by: 'id', dir: 'DESC' },
|
|
|
|
];
|
2017-07-03 20:58:01 +02:00
|
|
|
if (!options.conditions) options.conditions = [];
|
|
|
|
if (!options.conditionsParams) options.conditionsParams = [];
|
|
|
|
if (!options.fields) options.fields = this.previewFields();
|
2017-07-26 20:36:16 +02:00
|
|
|
if (!options.uncompletedTodosOnTop) options.uncompletedTodosOnTop = false;
|
2018-05-09 22:00:05 +02:00
|
|
|
if (!('showCompletedTodos' in options)) options.showCompletedTodos = true;
|
2017-07-03 20:58:01 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (parentId == BaseItem.getClass('Folder').conflictFolderId()) {
|
|
|
|
options.conditions.push('is_conflict = 1');
|
2017-07-15 17:35:40 +02:00
|
|
|
} else {
|
2018-03-09 22:59:12 +02:00
|
|
|
options.conditions.push('is_conflict = 0');
|
2017-07-17 21:56:14 +02:00
|
|
|
if (parentId) {
|
2018-03-09 22:59:12 +02:00
|
|
|
options.conditions.push('parent_id = ?');
|
2017-07-17 21:56:14 +02:00
|
|
|
options.conditionsParams.push(parentId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.anywherePattern) {
|
2018-03-09 22:59:12 +02:00
|
|
|
let pattern = options.anywherePattern.replace(/\*/g, '%');
|
|
|
|
options.conditions.push('(title LIKE ? OR body LIKE ?)');
|
2017-07-17 21:56:14 +02:00
|
|
|
options.conditionsParams.push(pattern);
|
|
|
|
options.conditionsParams.push(pattern);
|
|
|
|
}
|
2017-06-25 11:00:54 +02:00
|
|
|
|
2017-07-26 20:36:16 +02:00
|
|
|
let hasNotes = true;
|
|
|
|
let hasTodos = true;
|
2017-06-25 11:00:54 +02:00
|
|
|
if (options.itemTypes && options.itemTypes.length) {
|
2018-03-09 22:59:12 +02:00
|
|
|
if (options.itemTypes.indexOf('note') < 0) {
|
2017-07-26 20:36:16 +02:00
|
|
|
hasNotes = false;
|
2018-03-09 22:59:12 +02:00
|
|
|
} else if (options.itemTypes.indexOf('todo') < 0) {
|
2017-07-26 20:36:16 +02:00
|
|
|
hasTodos = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 22:00:05 +02:00
|
|
|
if (!options.showCompletedTodos) {
|
|
|
|
options.conditions.push('todo_completed <= 0');
|
|
|
|
}
|
|
|
|
|
2017-07-26 20:36:16 +02:00
|
|
|
if (options.uncompletedTodosOnTop && hasTodos) {
|
|
|
|
let cond = options.conditions.slice();
|
2018-03-09 22:59:12 +02:00
|
|
|
cond.push('is_todo = 1');
|
|
|
|
cond.push('(todo_completed <= 0 OR todo_completed IS NULL)');
|
2017-07-26 20:36:16 +02:00
|
|
|
let tempOptions = Object.assign({}, options);
|
|
|
|
tempOptions.conditions = cond;
|
|
|
|
|
|
|
|
let uncompletedTodos = await this.search(tempOptions);
|
|
|
|
|
|
|
|
cond = options.conditions.slice();
|
|
|
|
if (hasNotes && hasTodos) {
|
2018-03-09 22:59:12 +02:00
|
|
|
cond.push('(is_todo = 0 OR (is_todo = 1 AND todo_completed > 0))');
|
2017-07-26 20:36:16 +02:00
|
|
|
} else {
|
2018-03-09 22:59:12 +02:00
|
|
|
cond.push('(is_todo = 1 AND todo_completed > 0)');
|
2017-06-25 11:00:54 +02:00
|
|
|
}
|
2017-07-26 20:36:16 +02:00
|
|
|
|
|
|
|
tempOptions = Object.assign({}, options);
|
|
|
|
tempOptions.conditions = cond;
|
2018-03-09 22:59:12 +02:00
|
|
|
if ('limit' in tempOptions) tempOptions.limit -= uncompletedTodos.length;
|
2017-07-26 20:36:16 +02:00
|
|
|
let theRest = await this.search(tempOptions);
|
|
|
|
|
|
|
|
return uncompletedTodos.concat(theRest);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasNotes && hasTodos) {
|
2018-05-30 19:22:07 +02:00
|
|
|
|
2017-07-26 20:36:16 +02:00
|
|
|
} else if (hasNotes) {
|
2018-03-09 22:59:12 +02:00
|
|
|
options.conditions.push('is_todo = 0');
|
2017-07-26 20:36:16 +02:00
|
|
|
} else if (hasTodos) {
|
2018-03-09 22:59:12 +02:00
|
|
|
options.conditions.push('is_todo = 1');
|
2017-06-25 11:00:54 +02:00
|
|
|
}
|
2017-06-25 14:49:46 +02:00
|
|
|
|
2017-07-03 20:58:01 +02:00
|
|
|
return this.search(options);
|
2017-05-11 22:14:01 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 23:09:58 +02:00
|
|
|
static preview(noteId) {
|
2018-03-09 22:59:12 +02:00
|
|
|
return this.modelSelectOne('SELECT ' + this.previewFieldsSql() + ' FROM notes WHERE is_conflict = 0 AND id = ?', [noteId]);
|
2017-06-20 21:18:19 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 21:25:01 +02:00
|
|
|
static conflictedNotes() {
|
2018-03-09 22:59:12 +02:00
|
|
|
return this.modelSelectAll('SELECT * FROM notes WHERE is_conflict = 1');
|
2017-05-24 23:09:58 +02:00
|
|
|
}
|
|
|
|
|
2017-07-15 17:35:40 +02:00
|
|
|
static async conflictedCount() {
|
2018-03-09 22:59:12 +02:00
|
|
|
let r = await this.db().selectOne('SELECT count(*) as total FROM notes WHERE is_conflict = 1');
|
2017-07-15 17:35:40 +02:00
|
|
|
return r && r.total ? r.total : 0;
|
|
|
|
}
|
|
|
|
|
2017-07-02 23:01:37 +02:00
|
|
|
static unconflictedNotes() {
|
2018-03-09 22:59:12 +02:00
|
|
|
return this.modelSelectAll('SELECT * FROM notes WHERE is_conflict = 0');
|
2017-07-02 23:01:37 +02:00
|
|
|
}
|
|
|
|
|
2017-07-11 20:17:23 +02:00
|
|
|
static async updateGeolocation(noteId) {
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!Setting.value('trackLocation')) return;
|
2017-07-10 22:59:58 +02:00
|
|
|
if (!Note.updateGeolocationEnabled_) return;
|
|
|
|
|
2017-07-11 20:17:23 +02:00
|
|
|
let startWait = time.unixMs();
|
|
|
|
while (true) {
|
|
|
|
if (!this.geolocationUpdating_) break;
|
2018-03-09 22:59:12 +02:00
|
|
|
this.logger().info('Waiting for geolocation update...');
|
2017-07-11 20:17:23 +02:00
|
|
|
await time.sleep(1);
|
|
|
|
if (startWait + 1000 * 20 < time.unixMs()) {
|
2018-03-09 22:59:12 +02:00
|
|
|
this.logger().warn('Failed to update geolocation for: timeout: ' + noteId);
|
2017-07-11 20:17:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 21:46:34 +02:00
|
|
|
|
2017-05-23 21:01:37 +02:00
|
|
|
let geoData = null;
|
2017-07-11 20:17:23 +02:00
|
|
|
if (this.geolocationCache_ && this.geolocationCache_.timestamp + 1000 * 60 * 10 > time.unixMs()) {
|
|
|
|
geoData = Object.assign({}, this.geolocationCache_);
|
|
|
|
} else {
|
|
|
|
this.geolocationUpdating_ = true;
|
2017-10-22 14:45:56 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.logger().info('Fetching geolocation...');
|
2017-10-22 14:45:56 +02:00
|
|
|
try {
|
|
|
|
geoData = await shim.Geolocation.currentPosition();
|
|
|
|
} catch (error) {
|
2018-03-09 22:59:12 +02:00
|
|
|
this.logger().error('Could not get lat/long for note ' + noteId + ': ', error);
|
2017-10-22 14:45:56 +02:00
|
|
|
geoData = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.geolocationUpdating_ = false;
|
|
|
|
|
|
|
|
if (!geoData) return;
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.logger().info('Got lat/long');
|
2017-07-11 20:17:23 +02:00
|
|
|
this.geolocationCache_ = geoData;
|
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
this.logger().info('Updating lat/long of note ' + noteId);
|
2017-07-11 20:17:23 +02:00
|
|
|
|
2017-07-11 20:41:18 +02:00
|
|
|
let note = await Note.load(noteId);
|
2017-07-11 20:17:23 +02:00
|
|
|
if (!note) return; // Race condition - note has been deleted in the meantime
|
|
|
|
|
|
|
|
note.longitude = geoData.coords.longitude;
|
|
|
|
note.latitude = geoData.coords.latitude;
|
|
|
|
note.altitude = geoData.coords.altitude;
|
|
|
|
return Note.save(note);
|
2017-05-15 21:46:34 +02:00
|
|
|
}
|
|
|
|
|
2017-06-24 19:40:03 +02:00
|
|
|
static filter(note) {
|
|
|
|
if (!note) return note;
|
|
|
|
|
2017-06-27 01:20:01 +02:00
|
|
|
let output = super.filter(note);
|
2018-03-09 22:59:12 +02:00
|
|
|
if ('longitude' in output) output.longitude = Number(!output.longitude ? 0 : output.longitude).toFixed(8);
|
|
|
|
if ('latitude' in output) output.latitude = Number(!output.latitude ? 0 : output.latitude).toFixed(8);
|
|
|
|
if ('altitude' in output) output.altitude = Number(!output.altitude ? 0 : output.altitude).toFixed(4);
|
2017-06-24 19:40:03 +02:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-07-15 17:35:40 +02:00
|
|
|
static async copyToFolder(noteId, folderId) {
|
2018-03-09 22:59:12 +02:00
|
|
|
if (folderId == this.getClass('Folder').conflictFolderId()) throw new Error(_('Cannot copy note to "%s" notebook', this.getClass('Folder').conflictFolderIdTitle()));
|
2017-07-15 17:35:40 +02:00
|
|
|
|
|
|
|
return Note.duplicate(noteId, {
|
|
|
|
changes: {
|
|
|
|
parent_id: folderId,
|
|
|
|
is_conflict: 0, // Also reset the conflict flag in case we're moving the note out of the conflict folder
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static async moveToFolder(noteId, folderId) {
|
2018-03-09 22:59:12 +02:00
|
|
|
if (folderId == this.getClass('Folder').conflictFolderId()) throw new Error(_('Cannot move note to "%s" notebook', this.getClass('Folder').conflictFolderIdTitle()));
|
2017-07-15 17:35:40 +02:00
|
|
|
|
2017-08-20 22:11:32 +02:00
|
|
|
// When moving a note to a different folder, the user timestamp is not updated.
|
|
|
|
// However updated_time is updated so that the note can be synced later on.
|
|
|
|
|
|
|
|
const modifiedNote = {
|
2017-07-15 17:35:40 +02:00
|
|
|
id: noteId,
|
|
|
|
parent_id: folderId,
|
|
|
|
is_conflict: 0,
|
2017-08-20 22:11:32 +02:00
|
|
|
updated_time: time.unixMs(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return Note.save(modifiedNote, { autoTimestamp: false });
|
2017-07-15 17:35:40 +02:00
|
|
|
}
|
|
|
|
|
2017-07-30 21:51:18 +02:00
|
|
|
static toggleIsTodo(note) {
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!('is_todo' in note)) throw new Error('Missing "is_todo" property');
|
2017-07-30 21:51:18 +02:00
|
|
|
|
|
|
|
let output = Object.assign({}, note);
|
|
|
|
output.is_todo = output.is_todo ? 0 : 1;
|
2017-07-31 20:47:06 +02:00
|
|
|
output.todo_due = 0;
|
|
|
|
output.todo_completed = 0;
|
2017-07-30 21:51:18 +02:00
|
|
|
|
|
|
|
return output;
|
2017-07-17 22:22:05 +02:00
|
|
|
}
|
|
|
|
|
2017-07-11 20:17:23 +02:00
|
|
|
static async duplicate(noteId, options = null) {
|
|
|
|
const changes = options && options.changes;
|
2018-06-27 22:45:31 +02:00
|
|
|
const uniqueTitle = options && options.uniqueTitle;
|
2017-07-11 20:17:23 +02:00
|
|
|
|
|
|
|
const originalNote = await Note.load(noteId);
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!originalNote) throw new Error('Unknown note: ' + noteId);
|
2017-07-11 20:17:23 +02:00
|
|
|
|
|
|
|
let newNote = Object.assign({}, originalNote);
|
|
|
|
delete newNote.id;
|
|
|
|
|
|
|
|
for (let n in changes) {
|
|
|
|
if (!changes.hasOwnProperty(n)) continue;
|
|
|
|
newNote[n] = changes[n];
|
|
|
|
}
|
|
|
|
|
2018-06-27 22:45:31 +02:00
|
|
|
if (uniqueTitle) {
|
|
|
|
const title = await Note.findUniqueItemTitle(uniqueTitle);
|
|
|
|
newNote.title = title;
|
|
|
|
}
|
|
|
|
|
2017-07-11 20:17:23 +02:00
|
|
|
return this.save(newNote);
|
|
|
|
}
|
|
|
|
|
2018-02-07 21:02:07 +02:00
|
|
|
static async save(o, options = null) {
|
2017-06-29 22:52:52 +02:00
|
|
|
let isNew = this.isNew(o, options);
|
2018-03-09 22:59:12 +02:00
|
|
|
if (isNew && !o.source) o.source = Setting.value('appName');
|
|
|
|
if (isNew && !o.source_application) o.source_application = Setting.value('appId');
|
2017-06-29 22:52:52 +02:00
|
|
|
|
2018-02-07 21:02:07 +02:00
|
|
|
const note = await super.save(o, options);
|
2017-11-28 00:50:46 +02:00
|
|
|
|
2018-03-13 01:40:43 +02:00
|
|
|
ItemChange.add(BaseModel.TYPE_NOTE, note.id, isNew ? ItemChange.TYPE_CREATE : ItemChange.TYPE_UPDATE);
|
|
|
|
|
2018-02-07 21:02:07 +02:00
|
|
|
this.dispatch({
|
2018-03-09 22:59:12 +02:00
|
|
|
type: 'NOTE_UPDATE_ONE',
|
2018-02-07 21:02:07 +02:00
|
|
|
note: note,
|
2017-05-22 22:22:50 +02:00
|
|
|
});
|
2018-02-07 21:02:07 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if ('todo_due' in o || 'todo_completed' in o || 'is_todo' in o || 'is_conflict' in o) {
|
2018-02-07 21:02:07 +02:00
|
|
|
this.dispatch({
|
2018-03-09 22:59:12 +02:00
|
|
|
type: 'EVENT_NOTE_ALARM_FIELD_CHANGE',
|
2018-02-07 21:02:07 +02:00
|
|
|
id: note.id,
|
|
|
|
});
|
|
|
|
}
|
2018-05-30 19:22:07 +02:00
|
|
|
|
2018-02-07 21:02:07 +02:00
|
|
|
return note;
|
2017-05-22 22:22:50 +02:00
|
|
|
}
|
|
|
|
|
2018-03-15 19:46:54 +02:00
|
|
|
static async batchDelete(ids, options = null) {
|
|
|
|
const result = await super.batchDelete(ids, options);
|
2017-10-09 21:57:00 +02:00
|
|
|
for (let i = 0; i < ids.length; i++) {
|
2018-03-13 01:40:43 +02:00
|
|
|
ItemChange.add(BaseModel.TYPE_NOTE, ids[i], ItemChange.TYPE_DELETE);
|
|
|
|
|
2017-10-09 21:57:00 +02:00
|
|
|
this.dispatch({
|
2018-03-09 22:59:12 +02:00
|
|
|
type: 'NOTE_DELETE',
|
2017-11-08 23:39:07 +02:00
|
|
|
id: ids[i],
|
2017-10-09 21:57:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:50:46 +02:00
|
|
|
static dueNotes() {
|
2018-03-09 22:59:12 +02:00
|
|
|
return this.modelSelectAll('SELECT id, title, body, is_todo, todo_due, todo_completed, is_conflict FROM notes WHERE is_conflict = 0 AND is_todo = 1 AND todo_completed = 0 AND todo_due > ?', [time.unixMs()]);
|
2017-11-28 00:50:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static needAlarm(note) {
|
|
|
|
return note.is_todo && !note.todo_completed && note.todo_due >= time.unixMs() && !note.is_conflict;
|
|
|
|
}
|
|
|
|
|
2017-11-03 00:48:17 +02:00
|
|
|
// Tells whether the conflict between the local and remote note can be ignored.
|
|
|
|
static mustHandleConflict(localNote, remoteNote) {
|
|
|
|
// That shouldn't happen so throw an exception
|
2018-03-09 22:59:12 +02:00
|
|
|
if (localNote.id !== remoteNote.id) throw new Error('Cannot handle conflict for two different notes');
|
2017-11-03 00:48:17 +02:00
|
|
|
|
2017-12-14 00:53:20 +02:00
|
|
|
// For encrypted notes the conflict must always be handled
|
|
|
|
if (localNote.encryption_cipher_text || remoteNote.encryption_cipher_text) return true;
|
|
|
|
|
|
|
|
// Otherwise only handle the conflict if there's a different on the title or body
|
2017-11-03 00:48:17 +02:00
|
|
|
if (localNote.title !== remoteNote.title) return true;
|
|
|
|
if (localNote.body !== remoteNote.body) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2017-05-10 21:21:09 +02:00
|
|
|
}
|
|
|
|
|
2017-07-10 22:59:58 +02:00
|
|
|
Note.updateGeolocationEnabled_ = true;
|
2017-07-11 20:17:23 +02:00
|
|
|
Note.geolocationUpdating_ = false;
|
2017-07-10 22:59:58 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
module.exports = Note;
|