2018-08-02 22:48:21 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-07-18 21:35:17 +03:00
|
|
|
import searchService from "./search/services/search.js";
|
|
|
|
import sql from "./sql.js";
|
|
|
|
import becca from "../becca/becca.js";
|
|
|
|
import BAttribute from "../becca/entities/battribute.js";
|
|
|
|
import attributeFormatter from "./attribute_formatter.js";
|
|
|
|
import BUILTIN_ATTRIBUTES from "./builtin_attributes.js";
|
|
|
|
import BNote from "../becca/entities/bnote.js";
|
2024-07-24 20:19:27 +03:00
|
|
|
import { AttributeRow } from '../becca/entities/rows.js';
|
2018-08-02 22:48:21 +02:00
|
|
|
|
2023-04-08 19:54:59 +08:00
|
|
|
const ATTRIBUTE_TYPES = ['label', 'relation'];
|
2019-02-11 23:45:58 +01:00
|
|
|
|
2024-02-18 11:26:05 +02:00
|
|
|
function getNotesWithLabel(name: string, value?: string): BNote[] {
|
|
|
|
const query = attributeFormatter.formatAttrForSearch({type: 'label', name, value}, value !== undefined);
|
2021-07-04 21:05:47 +02:00
|
|
|
return searchService.searchNotes(query, {
|
|
|
|
includeArchivedNotes: true,
|
|
|
|
ignoreHoistedNote: true
|
|
|
|
});
|
2018-08-02 22:48:21 +02:00
|
|
|
}
|
|
|
|
|
2021-06-06 11:01:10 +02:00
|
|
|
// TODO: should be in search service
|
2024-02-18 11:26:05 +02:00
|
|
|
function getNoteWithLabel(name: string, value?: string): BNote | null {
|
2023-06-30 11:18:34 +02:00
|
|
|
// optimized version (~20 times faster) without using normal search, useful for e.g., finding date notes
|
2021-10-07 07:46:13 +02:00
|
|
|
const attrs = becca.findAttributes('label', name);
|
2018-08-02 22:48:21 +02:00
|
|
|
|
2021-10-08 22:13:39 +02:00
|
|
|
if (value === undefined) {
|
|
|
|
return attrs[0]?.getNote();
|
|
|
|
}
|
|
|
|
|
|
|
|
value = value?.toLowerCase();
|
|
|
|
|
2021-10-07 07:46:13 +02:00
|
|
|
for (const attr of attrs) {
|
2021-10-08 22:13:39 +02:00
|
|
|
if (attr.value.toLowerCase() === value) {
|
2021-10-07 07:46:13 +02:00
|
|
|
return attr.getNote();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2018-08-02 22:48:21 +02:00
|
|
|
}
|
|
|
|
|
2024-02-18 11:26:05 +02:00
|
|
|
function createLabel(noteId: string, name: string, value: string = "") {
|
2020-06-20 12:31:38 +02:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-18 11:26:05 +02:00
|
|
|
function createRelation(noteId: string, name: string, targetNoteId: string) {
|
2020-06-20 12:31:38 +02:00
|
|
|
return createAttribute({
|
2019-09-07 21:40:18 +02:00
|
|
|
noteId: noteId,
|
|
|
|
type: 'relation',
|
|
|
|
name: name,
|
|
|
|
value: targetNoteId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-18 11:26:05 +02:00
|
|
|
function createAttribute(attribute: AttributeRow) {
|
2023-01-03 13:52:37 +01:00
|
|
|
return new BAttribute(attribute).save();
|
2018-08-02 22:48:21 +02:00
|
|
|
}
|
|
|
|
|
2024-02-18 11:26:05 +02:00
|
|
|
function getAttributeNames(type: string, nameLike: string) {
|
2018-08-15 22:06:49 +02:00
|
|
|
nameLike = nameLike.toLowerCase();
|
2018-08-15 18:22:02 +02:00
|
|
|
|
2024-02-18 11:26:05 +02:00
|
|
|
let names = sql.getColumn<string>(
|
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 = ?
|
2022-12-21 15:19:05 +01: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
|
|
|
}
|
|
|
|
|
2022-01-15 22:09:51 +01:00
|
|
|
names = names.filter(name => ![
|
|
|
|
'internalLink',
|
|
|
|
'imageLink',
|
|
|
|
'includeNoteLink',
|
|
|
|
'relationMapLink'
|
|
|
|
].includes(name));
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-02-18 11:26:05 +02:00
|
|
|
function isAttributeType(type: string): boolean {
|
2019-02-11 23:45:58 +01:00
|
|
|
return ATTRIBUTE_TYPES.includes(type);
|
|
|
|
}
|
|
|
|
|
2024-02-18 11:26:05 +02:00
|
|
|
function isAttributeDangerous(type: string, name: string): boolean {
|
2020-06-07 10:20:48 +02:00
|
|
|
return BUILTIN_ATTRIBUTES.some(attr =>
|
2023-04-08 19:54:59 +08:00
|
|
|
attr.type === type &&
|
2019-02-11 23:45:58 +01:00
|
|
|
attr.name.toLowerCase() === name.trim().toLowerCase() &&
|
|
|
|
attr.isDangerous
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-18 21:47:30 +03:00
|
|
|
export default {
|
2018-08-07 12:48:11 +02:00
|
|
|
getNotesWithLabel,
|
|
|
|
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,
|
2022-12-23 14:18:40 +01:00
|
|
|
isAttributeDangerous
|
2020-06-07 10:20:48 +02:00
|
|
|
};
|