mirror relation has to target source note

This commit is contained in:
azivner 2018-11-19 00:06:04 +01:00
parent 2e76de5f34
commit ad6cb6ba34

View File

@ -79,7 +79,11 @@ async function processMirrorRelations(entityName, entity, handler) {
eventService.subscribe(eventService.ENTITY_CHANGED, async ({ entityName, entity }) => { eventService.subscribe(eventService.ENTITY_CHANGED, async ({ entityName, entity }) => {
await processMirrorRelations(entityName, entity, async (definition, note, targetNote) => { await processMirrorRelations(entityName, entity, async (definition, note, targetNote) => {
// we need to make sure that also target's mirror attribute exists and if note, then create it // we need to make sure that also target's mirror attribute exists and if note, then create it
if (!await targetNote.hasRelation(definition.mirrorRelation)) { // mirror attribute has to target our note as well
const hasMirrorAttribute = (await targetNote.getRelations(definition.mirrorRelation))
.some(attr => attr.value === note.noteId);
if (!hasMirrorAttribute) {
await new Attribute({ await new Attribute({
noteId: targetNote.noteId, noteId: targetNote.noteId,
type: 'relation', type: 'relation',
@ -97,13 +101,18 @@ eventService.subscribe(eventService.ENTITY_DELETED, async ({ entityName, entity
await processMirrorRelations(entityName, entity, async (definition, note, targetNote) => { await processMirrorRelations(entityName, entity, async (definition, note, targetNote) => {
// if one mirror attribute is deleted then the other should be deleted as well // if one mirror attribute is deleted then the other should be deleted as well
const relations = await targetNote.getRelations(definition.mirrorRelation); const relations = await targetNote.getRelations(definition.mirrorRelation);
let deletedSomething = false;
for (const relation of relations) { for (const relation of relations) {
relation.isDeleted = true; if (relation.value === note.noteId) {
await relation.save(); relation.isDeleted = true;
await relation.save();
deletedSomething = true;
}
} }
if (relations.length > 0) { if (deletedSomething) {
targetNote.invalidateAttributeCache(); targetNote.invalidateAttributeCache();
} }
}); });