Skip to content

Commit

Permalink
- #97 added option to fetch state event from local storage and fallba…
Browse files Browse the repository at this point in the history
…ck to server if necessary (allows for faster lookups and gives the full event object with information about when/who created it, etc)

- #97 remove num, bool, bin, and data from being options you can set to a state event (currently only objects and sometimes strings are allowed)
- Updated Leave Room node so it deletes the room from local storage
- Updated server config node so it deletes the matrix client from storage during shutdown (possibly solution to #94)
  • Loading branch information
skylord123 committed Oct 22, 2023
1 parent fd60500 commit 1859696
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/matrix-leave-room.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = function(RED) {
try {
node.log("Leaving room " + msg.topic);
node.server.matrixClient.leave(msg.topic);
node.server.matrixClient.store.removeRoom(msg.topic);
node.send([msg, null]);
} catch(e) {
node.error("Failed to leave room " + msg.topic + ": " + e, msg);
Expand Down
30 changes: 25 additions & 5 deletions src/matrix-room-state-events.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h3>Outputs</h3>
<dt class="optional">dynamic
<span class="property-type">string|object</span>
</dt>
<dd> You configure what room state events to output in the node configuration. <code style="white-space: normal;">m.room.name</code>, <code style="white-space: normal;">m.room.avatar</code>, and <code style="white-space: normal;">m.room.guest_access</code> will come back as strings otherwise you will get the full content object of the event (find this by referencing the <a href="https://spec.matrix.org/latest/client-server-api" target="_blank">Matrix Client-Server docs</a>)</dd>
<dd> You configure what room state events to output in the node configuration. <code style="white-space: normal;">m.room.name</code>, <code style="white-space: normal;">m.room.avatar</code>, and <code style="white-space: normal;">m.room.guest_access</code> will come back as strings otherwise you will get the full content object of the event (find this by referencing the <a href="https://spec.matrix.org/latest/client-server-api" target="_blank">Matrix Client-Server docs</a>). Additionally there is a setting when configuring a getter called "Fetch from local storage" that if enabled will search the local storage for the room and try to fetch the state event that way and fallback to hitting the server if that isn't possible.</dd>
</li>
</ol>
</script>
Expand Down Expand Up @@ -156,15 +156,17 @@ <h3>Outputs</h3>
.appendTo(row2_1)
.typedInput({
default: defaultType || (type === 'set' ? 'str' : 'msg'),
types: (type === 'set' ? ['msg','flow','global','str','num','bool','json','bin','date','jsonata'] : ['msg', 'flow', 'global'])
types: (type === 'set' ? ['msg','flow','global','str','json','jsonata'] : ['msg', 'flow', 'global'])
});

var dcLabel = $('<label style="padding-left: 130px;"></label>').appendTo(row2_2);
var lsLabel = $('<label style="padding-left: 130px;"></label>').appendTo(row2_2);
var localStorageEl = $('<input type="checkbox" class="node-input-rule-property-localStorage" style="width: auto; margin: 0 6px 0 0">').appendTo(lsLabel);
$('<span>').text("Fetch from local storage").appendTo(lsLabel);

propValInput.on("change", function(evt,type,val) {
row2_2.toggle(type === "msg" || type === "flow" || type === "global" || type === "env");
})
return [propValInput];
return [propValInput, localStorageEl];
}

$('#node-input-rule-container').css('min-height','150px').css('min-width','450px').editableList({
Expand Down Expand Up @@ -259,6 +261,7 @@ <h3>Outputs</h3>
.appendTo(row4);

let propertyValue = null;
let localStorageEl = null;
let fromValue = null;
let toValue = null;

Expand All @@ -277,12 +280,18 @@ <h3>Outputs</h3>
if (!propertyValue) {
var parts = createPropertyValue(row2_1, row2_2, type);
propertyValue = parts[0];
localStorageEl = parts[1];
} else {
propertyValue.typedInput('types', (type === 'set' ? ['msg','flow','global','str','num','bool','json','bin','date','jsonata','env'] : ['msg', 'flow', 'global']));
propertyValue.typedInput('types', (type === 'set' ? ['msg','flow','global','str','json','jsonata'] : ['msg', 'flow', 'global']));
}

propertyValue.typedInput('show');
row2.show();
if(type === 'get') {
localStorageEl.parent().show();
} else {
localStorageEl.parent().hide();
}
row3.hide();
row4.hide();
});
Expand All @@ -292,7 +301,14 @@ <h3>Outputs</h3>
if (rule.t === "set" || rule.t === "get") {
var parts = createPropertyValue(row2_1, row2_2, rule.t, rule.tot);
propertyValue = parts[0];
localStorageEl = parts[1];
propertyValue.typedInput('value',rule.to);
localStorageEl.prop("checked", !!rule.ls);
if(rule.t === 'get') {
localStorageEl.show();
} else {
localStorageEl.hide();
}
}
selectField.change();
container[0].appendChild(fragment);
Expand All @@ -319,6 +335,10 @@ <h3>Outputs</h3>
to:rule.find(".node-input-rule-property-value").typedInput('value'),
tot:rule.find(".node-input-rule-property-value").typedInput('type')
};

if (r.t === "get" && rule.find(".node-input-rule-property-localStorage").prop("checked")) {
r.ls = true;
}
node.rules.push(r);
});
},
Expand Down
44 changes: 27 additions & 17 deletions src/matrix-room-state-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,23 +226,33 @@ module.exports = function(RED) {
value = cachedGetters[rule.p];
} else {
try {
// we may want to fetch from local storage in the future, this is how to do that
// const room = this.getRoom(roomId);
// const ev = room.currentState.getStateEvents(EventType.RoomEncryption, "");
value = await node.server.matrixClient.getStateEvent(msg.topic, rule.p, "");
switch(rule.p) {
case "m.room.name":
value = value?.name
break;
case "m.room.topic":
value = value?.topic
break;
case "m.room.avatar":
value = value?.url
break;
case "m.room.guest_access":
value = value?.guest_access;
break;
if(rule.ls) {
// we opted to lookup from local storage, will fallback to server if necessary
let room = node.server.matrixClient.getRoom(msg.topic);
if(room) {
value = await room.getLiveTimeline().getState("f").getStateEvents(rule.p, "");
}
}
if(!value) {
// fetch the latest state event by type from server
value = await node.server.matrixClient.getStateEvent(msg.topic, rule.p, "");
if(value) {
// normalize some simpler events for easier access
switch(rule.p) {
case "m.room.name":
value = value?.name
break;
case "m.room.topic":
value = value?.topic
break;
case "m.room.avatar":
value = value?.url
break;
case "m.room.guest_access":
value = value?.guest_access;
break;
}
}
}
setToValue(value, rule);
} catch(e) {
Expand Down
7 changes: 7 additions & 0 deletions src/matrix-server-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ module.exports = function(RED) {

node.on('close', function(done) {
stopClient();
if(node.globalAccess) {
try {
node.context().global.delete('matrixClient["'+node.userId+'"]');
} catch(e){
node.error(e.message, {});
}
}
done();
});

Expand Down

0 comments on commit 1859696

Please sign in to comment.