2017-06-24 20:06:28 +02:00
|
|
|
import { BaseModel } from 'lib/base-model.js';
|
|
|
|
import { Log } from 'lib/log.js';
|
2017-07-18 21:27:10 +02:00
|
|
|
import { sprintf } from 'sprintf-js';
|
2017-06-24 20:06:28 +02:00
|
|
|
import { Folder } from 'lib/models/folder.js';
|
|
|
|
import { BaseItem } from 'lib/models/base-item.js';
|
2017-06-29 22:52:52 +02:00
|
|
|
import { Setting } from 'lib/models/setting.js';
|
2017-07-10 20:09:58 +02:00
|
|
|
import { shim } from 'lib/shim.js';
|
2017-07-11 20:17:23 +02:00
|
|
|
import { time } from 'lib/time-utils.js';
|
2017-07-15 17:35:40 +02:00
|
|
|
import { _ } from 'lib/locale.js';
|
2017-06-11 23:11:14 +02:00
|
|
|
import moment from 'moment';
|
2017-06-29 22:52:52 +02:00
|
|
|
import lodash from 'lodash';
|
2017-05-10 21:21:09 +02:00
|
|
|
|
2017-06-15 20:18:48 +02:00
|
|
|
class Note extends BaseItem {
|
2017-05-10 21:21:09 +02:00
|
|
|
|
2017-05-10 21:51:43 +02:00
|
|
|
static tableName() {
|
|
|
|
return 'notes';
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
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) {
|
2017-07-13 20:47:31 +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);
|
|
|
|
if (!output.title) output.title = '';
|
|
|
|
if (!output.body) output.body = '';
|
|
|
|
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();
|
|
|
|
fieldNames.push('type_');
|
|
|
|
lodash.pull(fieldNames, 'title', 'body');
|
|
|
|
return super.serialize(note, 'note', fieldNames);
|
|
|
|
}
|
|
|
|
|
2017-07-18 21:27:10 +02:00
|
|
|
static geolocationUrl(note) {
|
2017-07-22 20:16:16 +02:00
|
|
|
if (!('latitude' in note) || !('longitude' in note)) throw new Error('Latitude or longitude is missing');
|
2017-07-18 21:27:10 +02:00
|
|
|
if (!note.latitude && !note.longitude) throw new Error(_('This note does not have geolocation information.'));
|
|
|
|
return sprintf('https://www.openstreetmap.org/?lat=%s&lon=%s&zoom=20', note.latitude, note.longitude)
|
|
|
|
}
|
|
|
|
|
2017-07-03 21:50:45 +02:00
|
|
|
static modelType() {
|
|
|
|
return BaseModel.TYPE_NOTE;
|
2017-05-18 21:58:01 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 00:16:50 +02:00
|
|
|
static new(parentId = '') {
|
|
|
|
let output = super.new();
|
|
|
|
output.parent_id = parentId;
|
|
|
|
return output;
|
2017-05-10 21:51:43 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 22:51:50 +02:00
|
|
|
static newTodo(parentId = '') {
|
|
|
|
let output = this.new(parentId);
|
|
|
|
output.is_todo = true;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-07-15 20:13:31 +02:00
|
|
|
static sortNotes(notes, order) {
|
|
|
|
return notes.sort((a, b) => {
|
|
|
|
let r = -1;
|
|
|
|
if (a[order.orderBy] < b[order.orderBy]) r = +1;
|
|
|
|
if (order.orderByDir == 'ASC') r = -r;
|
|
|
|
return r;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-25 14:49:46 +02:00
|
|
|
static previewFields() {
|
2017-07-16 14:53:59 +02:00
|
|
|
return ['id', 'title', 'body', 'is_todo', 'todo_completed', 'parent_id', 'updated_time'];
|
2017-06-25 14:49:46 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 23:09:58 +02:00
|
|
|
static previewFieldsSql() {
|
2017-06-25 14:49:46 +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) {
|
2017-07-11 20:41:18 +02:00
|
|
|
if (!folderId) throw new Error('folderId is undefined');
|
2017-07-15 17:35:40 +02:00
|
|
|
|
|
|
|
let options = {
|
|
|
|
conditions: ['`' + field + '` = ?'],
|
|
|
|
conditionsParams: [value],
|
|
|
|
fields: '*',
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-06-25 11:00:54 +02:00
|
|
|
static previews(parentId, options = null) {
|
|
|
|
if (!options) options = {};
|
|
|
|
if (!options.orderBy) options.orderBy = 'updated_time';
|
|
|
|
if (!options.orderByDir) options.orderByDir = '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-15 17:35:40 +02:00
|
|
|
if (parentId == Folder.conflictFolderId()) {
|
|
|
|
options.conditions.push('is_conflict = 1');
|
|
|
|
} else {
|
|
|
|
options.conditions.push('is_conflict = 0');
|
2017-07-17 21:56:14 +02:00
|
|
|
if (parentId) {
|
|
|
|
options.conditions.push('parent_id = ?');
|
|
|
|
options.conditionsParams.push(parentId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.anywherePattern) {
|
|
|
|
let pattern = options.anywherePattern.replace(/\*/g, '%');
|
|
|
|
options.conditions.push('(title LIKE ? OR body LIKE ?)');
|
|
|
|
options.conditionsParams.push(pattern);
|
|
|
|
options.conditionsParams.push(pattern);
|
|
|
|
}
|
2017-06-25 11:00:54 +02:00
|
|
|
|
|
|
|
if (options.itemTypes && options.itemTypes.length) {
|
|
|
|
if (options.itemTypes.indexOf('note') >= 0 && options.itemTypes.indexOf('todo') >= 0) {
|
|
|
|
// Fetch everything
|
|
|
|
} else if (options.itemTypes.indexOf('note') >= 0) {
|
2017-07-03 20:58:01 +02:00
|
|
|
options.conditions.push('is_todo = 0');
|
2017-06-25 11:00:54 +02:00
|
|
|
} else if (options.itemTypes.indexOf('todo') >= 0) {
|
2017-07-03 20:58:01 +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) {
|
2017-06-23 23:32:24 +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() {
|
2017-06-20 21:18:19 +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() {
|
|
|
|
let r = await this.db().selectOne('SELECT count(*) as total FROM notes WHERE is_conflict = 1');
|
|
|
|
return r && r.total ? r.total : 0;
|
|
|
|
}
|
|
|
|
|
2017-07-02 23:01:37 +02:00
|
|
|
static unconflictedNotes() {
|
|
|
|
return this.modelSelectAll('SELECT * FROM notes WHERE is_conflict = 0');
|
|
|
|
}
|
|
|
|
|
2017-07-11 20:17:23 +02:00
|
|
|
static async updateGeolocation(noteId) {
|
2017-07-25 23:55:26 +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;
|
|
|
|
this.logger().info('Waiting for geolocation update...');
|
|
|
|
await time.sleep(1);
|
|
|
|
if (startWait + 1000 * 20 < time.unixMs()) {
|
|
|
|
this.logger().warn('Failed to update geolocation for: timeout: ' + noteId);
|
|
|
|
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;
|
|
|
|
this.logger().info('Fetching geolocation...');
|
|
|
|
geoData = await shim.Geolocation.currentPosition();
|
2017-07-10 20:09:58 +02:00
|
|
|
this.logger().info('Got lat/long');
|
2017-07-11 20:17:23 +02:00
|
|
|
this.geolocationCache_ = geoData;
|
|
|
|
this.geolocationUpdating_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.logger().info('Updating lat/long of note ' + noteId);
|
|
|
|
|
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);
|
2017-06-24 19:40:03 +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);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-07-15 17:35:40 +02:00
|
|
|
static async copyToFolder(noteId, folderId) {
|
|
|
|
if (folderId == Folder.conflictFolderId()) throw new Error(_('Cannot copy note to "%s" notebook', Folder.conflictFolderIdTitle()));
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (folderId == Folder.conflictFolderId()) throw new Error(_('Cannot move note to "%s" notebook', Folder.conflictFolderIdTitle()));
|
|
|
|
|
|
|
|
return Note.save({
|
|
|
|
id: noteId,
|
|
|
|
parent_id: folderId,
|
|
|
|
is_conflict: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-17 22:22:05 +02:00
|
|
|
static async toggleIsTodo(noteId) {
|
|
|
|
let note = await Note.load(noteId);
|
|
|
|
const isTodo = !note.is_todo ? 1 : 0;
|
|
|
|
note.is_todo = isTodo;
|
|
|
|
if (!note.is_todo) {
|
|
|
|
note.todo_due = 0;
|
|
|
|
note.todo_completed = 0;
|
|
|
|
}
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
|
2017-07-11 20:17:23 +02:00
|
|
|
static async duplicate(noteId, options = null) {
|
|
|
|
const changes = options && options.changes;
|
|
|
|
|
|
|
|
const originalNote = await Note.load(noteId);
|
|
|
|
if (!originalNote) throw new Error('Unknown note: ' + noteId);
|
|
|
|
|
|
|
|
let newNote = Object.assign({}, originalNote);
|
|
|
|
delete newNote.id;
|
|
|
|
|
|
|
|
for (let n in changes) {
|
|
|
|
if (!changes.hasOwnProperty(n)) continue;
|
|
|
|
newNote[n] = changes[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.save(newNote);
|
|
|
|
}
|
|
|
|
|
2017-05-22 22:22:50 +02:00
|
|
|
static save(o, options = null) {
|
2017-06-29 22:52:52 +02:00
|
|
|
let isNew = this.isNew(o, options);
|
|
|
|
if (isNew && !o.source) o.source = Setting.value('appName');
|
|
|
|
if (isNew && !o.source_application) o.source_application = Setting.value('appId');
|
|
|
|
|
2017-07-16 01:09:04 +02:00
|
|
|
return super.save(o, options).then((note) => {
|
2017-05-22 22:22:50 +02:00
|
|
|
this.dispatch({
|
|
|
|
type: 'NOTES_UPDATE_ONE',
|
|
|
|
note: note,
|
|
|
|
});
|
2017-07-16 01:09:04 +02:00
|
|
|
|
2017-05-22 22:22:50 +02:00
|
|
|
return note;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-15 01:12:32 +02:00
|
|
|
static async delete(id, options = null) {
|
|
|
|
let r = await super.delete(id, options);
|
|
|
|
|
|
|
|
this.dispatch({
|
|
|
|
type: 'NOTES_DELETE',
|
|
|
|
noteId: id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-05-10 21:21:09 +02:00
|
|
|
export { Note };
|