Notes/src/services/attributes.js

199 lines
6.1 KiB
JavaScript
Raw Normal View History

"use strict";
2021-05-02 19:59:16 +02:00
const searchService = require('./search/services/search');
const sql = require('./sql');
2021-06-29 22:15:57 +02:00
const becca = require('../becca/becca');
const Attribute = require('../becca/entities/attribute');
const {formatAttrForSearch} = require("./attribute_formatter");
2020-06-27 00:40:35 +02:00
const ATTRIBUTE_TYPES = [ 'label', 'relation' ];
2019-02-11 23:45:58 +01:00
const BUILTIN_ATTRIBUTES = [
// label names
2020-11-27 22:21:13 +01:00
{ type: 'label', name: 'inbox' },
{ 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' },
{ type: 'label', name: 'hidePromotedAttributes' },
{ 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' },
{ type: 'label', name: 'cssClass' },
{ type: 'label', name: 'iconClass' },
{ 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 },
{ type: 'label', name: 'widget', isDangerous: true },
{ 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' },
{ type: 'label', name: 'hoistedSearchHome' },
2020-12-07 09:19:27 +01:00
{ type: 'label', name: 'sqlConsoleHome' },
{ type: 'label', name: 'datePattern' },
{ type: 'label', name: 'pageSize' },
{ type: 'label', name: 'viewType' },
2021-09-20 23:04:41 +02:00
{ type: 'label', name: 'mapRootNoteId' },
2021-10-07 21:57:20 +02:00
{ type: 'label', name: 'bookmarked' },
{ type: 'label', name: 'bookmarkFolder' },
{ type: 'label', name: 'sorted' },
{ type: 'label', name: 'top' },
{ type: 'label', name: 'fullContentWidth' },
// 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 },
{ 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 }
];
/** @returns {Note[]} */
2020-06-20 12:31:38 +02:00
function getNotesWithLabel(name, value) {
2021-07-04 21:05:47 +02:00
const query = formatAttrForSearch({type: 'label', name, value}, true);
return searchService.searchNotes(query, {
includeArchivedNotes: true,
ignoreHoistedNote: true
});
}
2021-06-06 11:01:10 +02:00
// TODO: should be in search service
/** @returns {Note|null} */
2020-06-20 12:31:38 +02:00
function getNoteWithLabel(name, value) {
// optimized version (~20 times faster) without using normal search, useful for e.g. finding date notes
const attrs = becca.findAttributes('label', name);
if (value === undefined) {
return attrs[0]?.getNote();
}
value = value?.toLowerCase();
for (const attr of attrs) {
if (attr.value.toLowerCase() === value) {
return attr.getNote();
}
}
return null;
}
2020-06-20 12:31:38 +02:00
function createLabel(noteId, name, value = "") {
return createAttribute({
noteId: noteId,
type: 'label',
name: name,
value: value
});
}
2020-06-20 12:31:38 +02:00
function createRelation(noteId, name, targetNoteId) {
return createAttribute({
noteId: noteId,
type: 'relation',
name: name,
value: targetNoteId
});
}
2020-06-20 12:31:38 +02:00
function createAttribute(attribute) {
return new Attribute(attribute).save();
}
2020-06-20 12:31:38 +02:00
function getAttributeNames(type, nameLike) {
nameLike = nameLike.toLowerCase();
2020-06-20 12:31:38 +02:00
const names = sql.getColumn(
`SELECT DISTINCT name
FROM attributes
WHERE isDeleted = 0
AND type = ?
AND name LIKE ?`, [type, '%' + nameLike + '%']);
for (const attr of BUILTIN_ATTRIBUTES) {
if (attr.type === type && attr.name.toLowerCase().includes(nameLike) && !names.includes(attr.name)) {
names.push(attr.name);
}
}
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;
});
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;
}
module.exports = {
getNotesWithLabel,
getNoteWithLabel,
createLabel,
createRelation,
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
};