Notes/src/services/search/expressions/relation_where.js

42 lines
1.4 KiB
JavaScript
Raw Normal View History

"use strict";
const Expression = require('./expression');
const NoteSet = require('../note_set');
2021-05-17 22:09:49 +02:00
const becca = require('../../../becca/becca.js');
class RelationWhereExp extends Expression {
constructor(relationName, subExpression) {
super();
this.relationName = relationName;
this.subExpression = subExpression;
}
execute(inputNoteSet, executionContext) {
const candidateNoteSet = new NoteSet();
2021-04-16 23:00:08 +02:00
for (const attr of becca.findAttributes('relation', this.relationName)) {
const note = attr.note;
2021-03-30 21:39:42 +02:00
if (inputNoteSet.hasNoteId(note.noteId) && attr.targetNote) {
const subInputNoteSet = new NoteSet([attr.targetNote]);
const subResNoteSet = this.subExpression.execute(subInputNoteSet, executionContext);
if (subResNoteSet.hasNote(attr.targetNote)) {
if (attr.isInheritable) {
2021-05-17 22:35:36 +02:00
candidateNoteSet.addAll(note.getSubtreeNotesIncludingTemplated());
} else if (note.isTemplate()) {
candidateNoteSet.addAll(note.getTemplatedNotes());
} else {
candidateNoteSet.add(note);
}
}
}
}
return candidateNoteSet.intersection(inputNoteSet);
}
}
module.exports = RelationWhereExp;