Skip to content
This repository has been archived by the owner on Apr 23, 2021. It is now read-only.

Commit

Permalink
Hook up the bridge library's StateLookup storage mechanism to track m…
Browse files Browse the repository at this point in the history
….room.member events
  • Loading branch information
leonerd committed Sep 21, 2016
1 parent 4a48497 commit 19aaa4c
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/MatrixGitterBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var GitterUser = require("./GitterUser");
var bridgeLib = require("matrix-appservice-bridge");
var RemoteUser = bridgeLib.RemoteUser;
var Bridge = bridgeLib.Bridge;
var StateLookup = bridgeLib.StateLookup;

var BridgedRoom = require("./BridgedRoom");

Expand All @@ -19,6 +20,12 @@ var Provisioning = require("./Provisioning");

var MatrixIdTemplate = require("./MatrixIdTemplate");

// TODO(paul): monkeypatch
StateLookup.prototype.untrackRoom = StateLookup.prototype.untrackRoom ||
function(roomId) {
delete this._dict[roomId];
};

function MatrixGitterBridge(config) {
var self = this;

Expand All @@ -30,6 +37,10 @@ function MatrixGitterBridge(config) {
this._gitter = new Gitter(config.gitter_api_key);
this._gitterRealtime = new GitterRealtimeClient.RealtimeClient({token: config.gitter_api_key});

// TODO(paul): ugh. this.getBotIntent() doesn't work before .run time
// So we can't create the StateLookup instance yet
this._stateStorage = null;

var bridge = new Bridge({
homeserverUrl: config.matrix_homeserver,
domain: config.matrix_user_domain,
Expand All @@ -41,9 +52,11 @@ function MatrixGitterBridge(config) {

onAliasQuery: this.onAliasQuery.bind(this),

onEvent: function(req, context) {
onEvent: (req, context) => {
var event = req.getData();

this._stateStorage.onEvent(event);

if (event.type == "m.room.member" &&
event.state_key == bridge.getBot().getUserId()) {
var membership = event.content.membership;
Expand Down Expand Up @@ -342,6 +355,10 @@ MatrixGitterBridge.prototype.getBridgedRoomByMatrixId = function(roomId) {
return this._bridgedRoomsByMatrixId[roomId];
};

MatrixGitterBridge.prototype.getStoredEvent = function(roomId, eventType, stateKey) {
return this._stateStorage.getState(roomId, eventType, stateKey);
};

MatrixGitterBridge.prototype.onBotInvited = function(room_id) {
this.getBotIntent().join(room_id);
};
Expand Down Expand Up @@ -536,6 +553,7 @@ MatrixGitterBridge.prototype.run = function(port) {
room.linkMatrixRoom(matrix_id);
}
this._bridgedRoomsByMatrixId[matrix_id] = room;
this._stateStorage.trackRoom(matrix_id);
}).then(() => {
console.log((data.portal ? "PORTAL " : "LINKED ") +
matrix_id + " to " + remote_id);
Expand All @@ -552,6 +570,12 @@ MatrixGitterBridge.prototype.run = function(port) {
bridge.run(port, this._config);
Provisioning.addAppServicePath(bridge, this);

// TODO(paul): see above; we had to defer this until now
this._stateStorage = new StateLookup({
eventTypes: ["m.room.member"],
client: bridge.getIntent().client,
});

if (this._metrics) {
this._metrics.addAppServicePath(bridge);
}
Expand Down Expand Up @@ -579,6 +603,7 @@ MatrixGitterBridge.prototype.actionLink = function(matrixId, gitterName) {
}).then((room) => {
room.linkMatrixRoom(matrixId);
this._bridgedRoomsByMatrixId[matrixId] = room;
this._stateStorage.trackRoom(matrixId);
}).then(() => {
console.log("LINKED " + matrixId + " to " + gitterName);
});
Expand All @@ -594,6 +619,7 @@ MatrixGitterBridge.prototype.actionUnlink = function(matrixId, gitterName) {
var bridgedRoom = this._bridgedRoomsByMatrixId[matrixId];
if (bridgedRoom) {
delete this._bridgedRoomsByMatrixId[matrixId];
this._stateStorage.untrackRoom(matrixId);
return bridgedRoom.unlinkMatrixRoom(matrixId);
}
}).then(() => {
Expand Down

0 comments on commit 19aaa4c

Please sign in to comment.