1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-03-03 15:32:30 +02:00

169 lines
4.7 KiB
JavaScript
Raw Normal View History

2018-03-09 17:49:35 +00:00
const BaseModel = require("lib/BaseModel.js");
const { Log } = require("lib/log.js");
const { promiseChain } = require("lib/promise-utils.js");
const { time } = require("lib/time-utils.js");
const Note = require("lib/models/Note.js");
const Setting = require("lib/models/Setting.js");
const { Database } = require("lib/database.js");
const { _ } = require("lib/locale.js");
const moment = require("moment");
const BaseItem = require("lib/models/BaseItem.js");
const lodash = require("lodash");
2017-05-15 19:10:00 +00:00
2017-06-15 19:18:48 +01:00
class Folder extends BaseItem {
2017-05-15 19:10:00 +00:00
static tableName() {
2018-03-09 17:49:35 +00:00
return "folders";
2017-05-15 19:10:00 +00:00
}
2017-07-02 16:46:03 +01:00
static async serialize(folder) {
2017-06-29 21:52:52 +01:00
let fieldNames = this.fieldNames();
2018-03-09 17:49:35 +00:00
fieldNames.push("type_");
lodash.pull(fieldNames, "parent_id");
return super.serialize(folder, "folder", fieldNames);
2017-05-15 19:10:00 +00:00
}
2017-07-03 20:50:45 +01:00
static modelType() {
return BaseModel.TYPE_FOLDER;
2017-05-18 19:58:01 +00:00
}
2018-03-09 17:49:35 +00:00
2017-05-15 19:10:00 +00:00
static newFolder() {
return {
id: null,
2018-03-09 17:49:35 +00:00
title: "",
};
2017-05-15 19:10:00 +00:00
}
2017-11-11 17:36:47 +00:00
static async findUniqueFolderTitle(title) {
let counter = 1;
let titleToTry = title;
while (true) {
2018-03-09 17:49:35 +00:00
const folder = await this.loadByField("title", titleToTry);
2017-11-11 17:36:47 +00:00
if (!folder) return titleToTry;
2018-03-09 17:49:35 +00:00
titleToTry = title + " (" + counter + ")";
2017-11-11 17:36:47 +00:00
counter++;
2018-03-09 17:49:35 +00:00
if (counter >= 100) titleToTry = title + " (" + new Date().getTime() + ")";
if (counter >= 1000) throw new Error("Cannot find unique title");
2017-11-11 17:36:47 +00:00
}
}
2017-06-19 23:18:24 +01:00
static noteIds(parentId) {
2018-03-09 17:49:35 +00:00
return this.db()
.selectAll("SELECT id FROM notes WHERE is_conflict = 0 AND parent_id = ?", [parentId])
.then(rows => {
let output = [];
for (let i = 0; i < rows.length; i++) {
let row = rows[i];
output.push(row.id);
}
return output;
});
2017-05-18 22:31:40 +02:00
}
2017-07-12 21:39:47 +01:00
static async noteCount(parentId) {
2018-03-09 17:49:35 +00:00
let r = await this.db().selectOne("SELECT count(*) as total FROM notes WHERE is_conflict = 0 AND parent_id = ?", [parentId]);
2017-07-12 21:39:47 +01:00
return r ? r.total : 0;
}
static markNotesAsConflict(parentId) {
2018-03-09 17:49:35 +00:00
let query = Database.updateQuery("notes", { is_conflict: 1 }, { parent_id: parentId });
return this.db().exec(query);
}
2017-06-25 08:52:25 +01:00
static async delete(folderId, options = null) {
if (!options) options = {};
2018-03-09 17:49:35 +00:00
if (!("deleteChildren" in options)) options.deleteChildren = true;
2017-06-25 08:52:25 +01:00
let folder = await Folder.load(folderId);
if (!folder) return; // noop
2018-03-09 17:49:35 +00:00
if (options.deleteChildren) {
let noteIds = await Folder.noteIds(folderId);
for (let i = 0; i < noteIds.length; i++) {
await Note.delete(noteIds[i]);
}
2017-06-25 08:52:25 +01:00
}
2017-06-25 16:17:40 +01:00
await super.delete(folderId, options);
2017-06-25 08:52:25 +01:00
this.dispatch({
2018-03-09 17:49:35 +00:00
type: "FOLDER_DELETE",
2017-11-08 21:39:07 +00:00
id: folderId,
});
}
2017-07-15 16:35:40 +01:00
static conflictFolderTitle() {
2018-03-09 17:49:35 +00:00
return _("Conflicts");
2017-07-15 16:35:40 +01:00
}
2017-06-18 00:49:52 +01:00
2017-07-15 16:35:40 +01:00
static conflictFolderId() {
2018-03-09 17:49:35 +00:00
return "c04f1c7c04f1c7c04f1c7c04f1c7c04f";
2017-07-15 16:35:40 +01:00
}
static conflictFolder() {
return {
type_: this.TYPE_FOLDER,
id: this.conflictFolderId(),
title: this.conflictFolderTitle(),
updated_time: time.unixMs(),
2017-08-20 22:11:32 +02:00
user_updated_time: time.unixMs(),
2017-07-15 16:35:40 +01:00
};
}
2017-06-25 10:00:54 +01:00
2017-07-15 16:35:40 +01:00
static async all(options = null) {
let output = await super.all(options);
if (options && options.includeConflictFolder) {
let conflictCount = await Note.conflictedCount();
if (conflictCount) output.push(this.conflictFolder());
}
return output;
}
2017-06-25 10:00:54 +01:00
2017-07-15 16:35:40 +01:00
static load(id) {
if (id == this.conflictFolderId()) return this.conflictFolder();
return super.load(id);
2017-06-19 18:58:49 +00:00
}
2017-06-27 20:16:03 +00:00
static defaultFolder() {
2018-03-09 17:49:35 +00:00
return this.modelSelectOne("SELECT * FROM folders ORDER BY created_time DESC LIMIT 1");
2017-06-25 00:19:11 +01:00
}
2017-07-15 16:35:40 +01:00
// These "duplicateCheck" and "reservedTitleCheck" should only be done when a user is
// manually creating a folder. They shouldn't be done for example when the folders
2018-03-09 17:49:35 +00:00
// are being synced to avoid any strange side-effects. Technically it's possible to
2017-07-17 19:19:01 +00:00
// have folders and notes with duplicate titles (or no title), or with reserved words.
2017-07-03 18:58:01 +00:00
static async save(o, options = null) {
2017-07-17 19:19:01 +00:00
if (!options) options = {};
if (options.userSideValidation === true) {
2018-03-09 17:49:35 +00:00
if (!("duplicateCheck" in options)) options.duplicateCheck = true;
if (!("reservedTitleCheck" in options)) options.reservedTitleCheck = true;
if (!("stripLeftSlashes" in options)) options.stripLeftSlashes = true;
2017-07-17 19:19:01 +00:00
}
if (options.stripLeftSlashes === true && o.title) {
2018-03-09 17:49:35 +00:00
while (o.title.length && (o.title[0] == "/" || o.title[0] == "\\")) {
2017-07-17 19:19:01 +00:00
o.title = o.title.substr(1);
}
}
if (options.duplicateCheck === true && o.title) {
2017-07-03 18:58:01 +00:00
let existingFolder = await Folder.loadByTitle(o.title);
2017-07-15 17:25:33 +01:00
if (existingFolder && existingFolder.id != o.id) throw new Error(_('A notebook with this title already exists: "%s"', o.title));
2017-07-03 18:58:01 +00:00
}
2017-07-17 19:19:01 +00:00
if (options.reservedTitleCheck === true && o.title) {
2017-07-15 16:35:40 +01:00
if (o.title == Folder.conflictFolderTitle()) throw new Error(_('Notebooks cannot be named "%s", which is a reserved title.', o.title));
}
2018-03-09 17:49:35 +00:00
return super.save(o, options).then(folder => {
this.dispatch({
2018-03-09 17:49:35 +00:00
type: "FOLDER_UPDATE_ONE",
item: folder,
2017-05-18 22:31:40 +02:00
});
return folder;
2017-05-18 22:31:40 +02:00
});
}
2017-05-15 19:10:00 +00:00
}
2018-03-09 17:49:35 +00:00
module.exports = Folder;