2025-01-09 18:07:02 +02:00
|
|
|
import server from "./server.js";
|
|
|
|
|
import froca from "./froca.js";
|
|
|
|
|
import FNote from "../entities/fnote.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import type { AttributeRow } from "./load_results.js";
|
2020-10-23 00:11:44 +02:00
|
|
|
|
2024-12-19 20:51:47 +02:00
|
|
|
async function addLabel(noteId: string, name: string, value: string = "") {
|
2020-10-23 00:11:44 +02:00
|
|
|
await server.put(`notes/${noteId}/attribute`, {
|
2025-01-09 18:07:02 +02:00
|
|
|
type: "label",
|
2020-10-23 00:11:44 +02:00
|
|
|
name: name,
|
|
|
|
|
value: value
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-19 20:51:47 +02:00
|
|
|
async function setLabel(noteId: string, name: string, value: string = "") {
|
2020-10-24 23:50:32 +02:00
|
|
|
await server.put(`notes/${noteId}/set-attribute`, {
|
2025-01-09 18:07:02 +02:00
|
|
|
type: "label",
|
2020-10-24 23:50:32 +02:00
|
|
|
name: name,
|
|
|
|
|
value: value
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-19 20:51:47 +02:00
|
|
|
async function removeAttributeById(noteId: string, attributeId: string) {
|
2020-10-23 00:11:44 +02:00
|
|
|
await server.remove(`notes/${noteId}/attributes/${attributeId}`);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 22:49:24 +02:00
|
|
|
/**
|
2024-12-22 21:59:08 +02:00
|
|
|
* @returns - returns true if this attribute has the potential to influence the note in the argument.
|
2021-08-25 22:49:24 +02:00
|
|
|
* That can happen in multiple ways:
|
|
|
|
|
* 1. attribute is owned by the note
|
|
|
|
|
* 2. attribute is owned by the template of the note
|
|
|
|
|
* 3. attribute is owned by some note's ancestor and is inheritable
|
|
|
|
|
*/
|
2025-01-07 12:34:10 +02:00
|
|
|
function isAffecting(attrRow: AttributeRow, affectedNote: FNote | null | undefined) {
|
2021-08-25 22:49:24 +02:00
|
|
|
if (!affectedNote || !attrRow) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-22 21:59:08 +02:00
|
|
|
const attrNote = attrRow.noteId && froca.notes[attrRow.noteId];
|
2021-08-25 22:49:24 +02:00
|
|
|
|
|
|
|
|
if (!attrNote) {
|
2023-01-15 21:04:17 +01:00
|
|
|
// the note (owner of the attribute) is not even loaded into the cache, so it should not affect anything else
|
2021-08-25 22:49:24 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 20:31:55 +01:00
|
|
|
const owningNotes = [affectedNote, ...affectedNote.getNotesToInheritAttributesFrom()];
|
2021-08-25 22:49:24 +02:00
|
|
|
|
|
|
|
|
for (const owningNote of owningNotes) {
|
|
|
|
|
if (owningNote.noteId === attrNote.noteId) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-19 20:51:47 +02:00
|
|
|
// TODO: This doesn't seem right.
|
2024-12-22 21:59:08 +02:00
|
|
|
//@ts-ignore
|
2021-08-25 22:49:24 +02:00
|
|
|
if (this.isInheritable) {
|
|
|
|
|
for (const owningNote of owningNotes) {
|
2023-02-01 22:55:31 +01:00
|
|
|
if (owningNote.hasAncestor(attrNote.noteId, true)) {
|
2021-08-25 22:49:24 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 00:11:44 +02:00
|
|
|
export default {
|
|
|
|
|
addLabel,
|
2020-10-24 23:50:32 +02:00
|
|
|
setLabel,
|
2021-08-25 22:49:24 +02:00
|
|
|
removeAttributeById,
|
|
|
|
|
isAffecting
|
2025-01-09 18:07:02 +02:00
|
|
|
};
|