Notes/src/services/attributes.js

157 lines
3.9 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");
2022-01-15 22:09:51 +01:00
const BUILTIN_ATTRIBUTES = require("./builtin_attributes");
2020-06-27 00:40:35 +02:00
const ATTRIBUTE_TYPES = [ 'label', 'relation' ];
2019-02-11 23:45:58 +01:00
/** @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} */
2022-02-20 12:33:50 +01:00
function getNoteWithLabel(name, value = undefined) {
// 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;
}
2021-12-19 22:08:52 +01:00
/**
* Does not take into account templates and inheritance
*/
function getNotesWithLabelFast(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.map(attr => attr.getNote());
}
value = value?.toLowerCase();
return attrs
.filter(attr => attr.value.toLowerCase() === value)
.map(attr => attr.getNote());
}
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();
2022-01-15 22:09:51 +01:00
let 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);
}
}
2022-01-15 22:09:51 +01:00
names = names.filter(name => ![
'internalLink',
'imageLink',
'includeNoteLink',
'relationMapLink'
].includes(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-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,
2021-12-19 22:08:52 +01:00
getNotesWithLabelFast,
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
sanitizeAttributeName
2020-06-07 10:20:48 +02:00
};