2018-08-02 22:48:21 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const repository = require('./repository');
|
2018-08-03 11:11:57 +02:00
|
|
|
const sql = require('./sql');
|
2020-12-14 22:12:26 +01:00
|
|
|
const noteCache = require('./note_cache/note_cache');
|
2018-08-02 22:48:21 +02:00
|
|
|
const Attribute = require('../entities/attribute');
|
|
|
|
|
2020-06-27 00:40:35 +02:00
|
|
|
const ATTRIBUTE_TYPES = [ 'label', 'relation' ];
|
2019-02-11 23:45:58 +01:00
|
|
|
|
2018-08-02 22:48:21 +02:00
|
|
|
const BUILTIN_ATTRIBUTES = [
|
2018-08-03 11:11:57 +02:00
|
|
|
// label names
|
2020-11-27 22:21:13 +01:00
|
|
|
{ type: 'label', name: 'inbox' },
|
2018-08-03 11:11:57 +02:00
|
|
|
{ type: 'label', name: 'disableVersioning' },
|
|
|
|
{ type: 'label', name: 'calendarRoot' },
|
|
|
|
{ type: 'label', name: 'archived' },
|
|
|
|
{ type: 'label', name: 'excludeFromExport' },
|
|
|
|
{ type: 'label', name: 'disableInclusion' },
|
|
|
|
{ type: 'label', name: 'appCss' },
|
2019-01-27 21:18:11 +01:00
|
|
|
{ type: 'label', name: 'appTheme' },
|
2018-11-13 22:14:41 +01:00
|
|
|
{ type: 'label', name: 'hidePromotedAttributes' },
|
2019-01-27 13:10:03 +01:00
|
|
|
{ type: 'label', name: 'readOnly' },
|
2020-05-06 23:11:34 +02:00
|
|
|
{ type: 'label', name: 'autoReadOnlyDisabled' },
|
2020-11-24 23:24:05 +01:00
|
|
|
{ type: 'label', name: 'hoistedCssClass' },
|
2019-11-16 09:57:19 +01:00
|
|
|
{ type: 'label', name: 'cssClass' },
|
|
|
|
{ type: 'label', name: 'iconClass' },
|
2019-11-24 18:32:18 +01:00
|
|
|
{ type: 'label', name: 'keyboardShortcut' },
|
2019-02-11 23:45:58 +01:00
|
|
|
{ type: 'label', name: 'run', isDangerous: true },
|
2021-04-08 21:22:47 +02:00
|
|
|
{ type: 'label', name: 'runOnInstance', isDangerous: false },
|
2021-04-10 23:23:43 +02:00
|
|
|
{ type: 'label', name: 'runAtHour', isDangerous: false },
|
2019-02-11 23:45:58 +01:00
|
|
|
{ type: 'label', name: 'customRequestHandler', isDangerous: true },
|
|
|
|
{ type: 'label', name: 'customResourceProvider', isDangerous: true },
|
2020-06-14 16:29:29 +02:00
|
|
|
{ type: 'label', name: 'widget', isDangerous: true },
|
2020-09-13 22:47:23 +02:00
|
|
|
{ type: 'label', name: 'noteInfoWidgetDisabled' },
|
|
|
|
{ type: 'label', name: 'linkMapWidgetDisabled' },
|
|
|
|
{ type: 'label', name: 'noteRevisionsWidgetDisabled' },
|
|
|
|
{ type: 'label', name: 'whatLinksHereWidgetDisabled' },
|
|
|
|
{ type: 'label', name: 'similarNotesWidgetDisabled' },
|
2020-11-29 22:32:31 +01:00
|
|
|
{ type: 'label', name: 'workspace' },
|
|
|
|
{ type: 'label', name: 'workspaceIconClass' },
|
|
|
|
{ type: 'label', name: 'workspaceTabBackgroundColor' },
|
2020-12-04 22:57:54 +01:00
|
|
|
{ type: 'label', name: 'searchHome' },
|
2021-03-12 21:29:50 +01:00
|
|
|
{ type: 'label', name: 'hoistedInbox' },
|
2021-02-27 21:18:10 +01:00
|
|
|
{ type: 'label', name: 'hoistedSearchHome' },
|
2020-12-07 09:19:27 +01:00
|
|
|
{ type: 'label', name: 'sqlConsoleHome' },
|
2020-12-20 22:49:42 +01:00
|
|
|
{ type: 'label', name: 'datePattern' },
|
2021-03-12 20:39:42 +01:00
|
|
|
{ type: 'label', name: 'pageSize' },
|
|
|
|
{ type: 'label', name: 'viewType' },
|
2018-08-03 11:11:57 +02:00
|
|
|
|
|
|
|
// relation names
|
2019-02-11 23:45:58 +01:00
|
|
|
{ type: 'relation', name: 'runOnNoteCreation', isDangerous: true },
|
|
|
|
{ type: 'relation', name: 'runOnNoteTitleChange', isDangerous: true },
|
|
|
|
{ type: 'relation', name: 'runOnNoteChange', isDangerous: true },
|
|
|
|
{ type: 'relation', name: 'runOnChildNoteCreation', isDangerous: true },
|
|
|
|
{ type: 'relation', name: 'runOnAttributeCreation', isDangerous: true },
|
|
|
|
{ type: 'relation', name: 'runOnAttributeChange', isDangerous: true },
|
2018-08-28 20:55:21 +02:00
|
|
|
{ type: 'relation', name: 'template' },
|
2019-09-06 23:36:08 +02:00
|
|
|
{ type: 'relation', name: 'widget', isDangerous: true },
|
2019-02-11 23:45:58 +01:00
|
|
|
{ type: 'relation', name: 'renderNote', isDangerous: true }
|
2018-08-02 22:48:21 +02:00
|
|
|
];
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getNotesWithLabel(name, value) {
|
2018-08-23 10:10:04 +02:00
|
|
|
let valueCondition = "";
|
|
|
|
let params = [name];
|
2018-08-02 22:48:21 +02:00
|
|
|
|
|
|
|
if (value !== undefined) {
|
2018-08-23 10:10:04 +02:00
|
|
|
valueCondition = " AND attributes.value = ?";
|
|
|
|
params.push(value);
|
2018-08-02 22:48:21 +02:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
return repository.getEntities(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
|
2018-08-23 10:10:04 +02:00
|
|
|
WHERE notes.isDeleted = 0 AND attributes.isDeleted = 0 AND attributes.name = ? ${valueCondition} ORDER BY position`, params);
|
2018-08-02 22:48:21 +02:00
|
|
|
}
|
|
|
|
|
2020-12-14 22:12:26 +01:00
|
|
|
function getNoteIdsWithLabels(names) {
|
|
|
|
const noteIds = new Set();
|
2019-01-27 17:01:37 +01:00
|
|
|
|
2020-12-14 22:12:26 +01:00
|
|
|
for (const name of names) {
|
|
|
|
for (const attr of noteCache.findAttributes('label', name)) {
|
|
|
|
noteIds.add(attr.noteId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Array.from(noteIds);
|
2019-01-27 17:01:37 +01:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getNoteWithLabel(name, value) {
|
|
|
|
const notes = getNotesWithLabel(name, value);
|
2018-08-02 22:48:21 +02:00
|
|
|
|
|
|
|
return notes.length > 0 ? notes[0] : null;
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function createLabel(noteId, name, value = "") {
|
|
|
|
return createAttribute({
|
2018-08-02 22:48:21 +02:00
|
|
|
noteId: noteId,
|
2018-08-07 13:33:10 +02:00
|
|
|
type: 'label',
|
2018-08-02 22:48:21 +02:00
|
|
|
name: name,
|
|
|
|
value: value
|
2018-08-07 13:33:10 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function createRelation(noteId, name, targetNoteId) {
|
|
|
|
return createAttribute({
|
2019-09-07 21:40:18 +02:00
|
|
|
noteId: noteId,
|
|
|
|
type: 'relation',
|
|
|
|
name: name,
|
|
|
|
value: targetNoteId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function createAttribute(attribute) {
|
|
|
|
return new Attribute(attribute).save();
|
2018-08-02 22:48:21 +02:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getAttributeNames(type, nameLike) {
|
2018-08-15 22:06:49 +02:00
|
|
|
nameLike = nameLike.toLowerCase();
|
2018-08-15 18:22:02 +02:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const names = sql.getColumn(
|
2018-08-15 22:06:49 +02:00
|
|
|
`SELECT DISTINCT name
|
2018-08-15 18:22:02 +02:00
|
|
|
FROM attributes
|
|
|
|
WHERE isDeleted = 0
|
|
|
|
AND type = ?
|
2020-06-20 23:24:34 +02:00
|
|
|
AND name LIKE ?`, [type, '%' + nameLike + '%']);
|
2018-08-15 22:06:49 +02:00
|
|
|
|
|
|
|
for (const attr of BUILTIN_ATTRIBUTES) {
|
|
|
|
if (attr.type === type && attr.name.toLowerCase().includes(nameLike) && !names.includes(attr.name)) {
|
|
|
|
names.push(attr.name);
|
|
|
|
}
|
2018-08-03 11:11:57 +02:00
|
|
|
}
|
|
|
|
|
2020-10-03 22:00:34 +02:00
|
|
|
names.sort((a, b) => {
|
|
|
|
const aPrefix = a.toLowerCase().startsWith(nameLike);
|
|
|
|
const bPrefix = b.toLowerCase().startsWith(nameLike);
|
|
|
|
|
|
|
|
if (aPrefix !== bPrefix) {
|
|
|
|
return aPrefix ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return a < b ? -1 : 1;
|
|
|
|
});
|
2018-08-03 11:11:57 +02:00
|
|
|
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2019-02-11 23:45:58 +01:00
|
|
|
function isAttributeType(type) {
|
|
|
|
return ATTRIBUTE_TYPES.includes(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isAttributeDangerous(type, name) {
|
2020-06-07 10:20:48 +02:00
|
|
|
return BUILTIN_ATTRIBUTES.some(attr =>
|
|
|
|
attr.type === attr.type &&
|
2019-02-11 23:45:58 +01:00
|
|
|
attr.name.toLowerCase() === name.trim().toLowerCase() &&
|
|
|
|
attr.isDangerous
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-07 10:20:48 +02:00
|
|
|
function getBuiltinAttributeNames() {
|
|
|
|
return BUILTIN_ATTRIBUTES
|
|
|
|
.map(attr => attr.name)
|
|
|
|
.concat([
|
|
|
|
'internalLink',
|
|
|
|
'imageLink',
|
|
|
|
'includeNoteLink',
|
|
|
|
'relationMapLink'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-11-17 22:35:20 +01:00
|
|
|
function sanitizeAttributeName(origName) {
|
|
|
|
let fixedName;
|
|
|
|
|
|
|
|
if (origName === '') {
|
|
|
|
fixedName = "unnamed";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// any not allowed character should be replaced with underscore
|
|
|
|
fixedName = origName.replace(/[^\p{L}\p{N}_:]/ug, "_");
|
|
|
|
}
|
|
|
|
|
|
|
|
return fixedName;
|
|
|
|
}
|
|
|
|
|
2018-08-02 22:48:21 +02:00
|
|
|
module.exports = {
|
2018-08-07 12:48:11 +02:00
|
|
|
getNotesWithLabel,
|
2020-12-14 22:12:26 +01:00
|
|
|
getNoteIdsWithLabels,
|
2018-08-07 12:48:11 +02:00
|
|
|
getNoteWithLabel,
|
2018-08-07 13:33:10 +02:00
|
|
|
createLabel,
|
2019-09-07 21:40:18 +02:00
|
|
|
createRelation,
|
2018-08-02 22:48:21 +02:00
|
|
|
createAttribute,
|
2019-02-11 23:45:58 +01:00
|
|
|
getAttributeNames,
|
|
|
|
isAttributeType,
|
2020-06-07 10:20:48 +02:00
|
|
|
isAttributeDangerous,
|
2020-11-17 22:35:20 +01:00
|
|
|
getBuiltinAttributeNames,
|
|
|
|
sanitizeAttributeName
|
2020-06-07 10:20:48 +02:00
|
|
|
};
|