Skip to content

Commit

Permalink
Dedupe invites to rooms. Fixes #80
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Apr 16, 2017
1 parent b1d98a1 commit 66a379b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/VoyagerBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class VoyagerBot {
}

_processMembership(event, state, member) {
if (member.userId != this._client.credentials.userId)
if (member.userId != this._client.credentials.userId || event.getType() !== 'm.room.member')
return Promise.resolve(); // not applicable for us

var newState = member.membership;
Expand Down Expand Up @@ -164,17 +164,17 @@ class VoyagerBot {
_onInvite(event) {
var sourceNode;
var targetNode;
var inviteLink;

return this.getNode(event.getSender(), 'user').then(node=> {
sourceNode = node;
return this.getNode(event.getRoomId(), 'room');
}).then(node => {
targetNode = node;
return this._store.createLink(sourceNode, targetNode, 'invite', event.getTs());
}).then(link=> {
inviteLink = link;
return this._store.createTimelineEvent(inviteLink, event.getTs(), event.getId());
return this._store.findLinkByTimeline(sourceNode, targetNode, 'invite', event.getId());
}).then(existingLink => {
if (existingLink) return Promise.resolve();
else return this._store.createLink(sourceNode, targetNode, 'invite', event.getTs())
.then(link => this._store.createTimelineEvent(link, event.getTs(), event.getId()));
}).then(() => {
return this._client.joinRoom(event.getRoomId());
}).then(room => {
Expand Down
36 changes: 34 additions & 2 deletions src/storage/VoyagerStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class VoyagerStore {
this.__Nodes.hasMany(this.__StateEvents, {foreignKey: 'id', targetKey: 'nodeId'});
this.__NodeVersions.hasMany(this.__StateEvents, {foreignKey: 'id', targetKey: 'nodeVersionId'});

this.__TimelineEvents.belongsTo(this.__Links, {foreignKey: 'linkId'});
this.__Links.hasMany(this.__TimelineEvents, {foreignKey: 'id', targetKey: 'linkId'});
this.__TimelineEvents.belongsTo(this.__Links, {foreignKey: 'linkId', as: 'link'});
this.__Links.hasMany(this.__TimelineEvents, {foreignKey: 'id', targetKey: 'linkId', as: 'link'});

this.__Nodes.belongsTo(this.__NodeMeta, {as: 'nodeMeta', foreignKey: 'nodeMetaId'});
this.__NodeMeta.belongsTo(this.__Nodes, {as: 'nodeMeta', foreignKey: 'nodeId'});
Expand Down Expand Up @@ -305,6 +305,38 @@ class VoyagerStore {
return this.__NodeVersions.findById(id).then(nv => nv ? new NodeVersion(nv) : null);
}

/**
* Attempts to find a link where the given source node, target node, type, and timeline
* event exist.
* @param {Node} sourceNode the source Node
* @param {Node} targetNode the target Node
* @param {'invite'|'message'|'self_link'|'kick'|'ban'} type the link type
* @param {string} matrixEventId the timeline event ID
* @returns {Promise<Link>} resovles to the first found link, or null if not found
*/
findLinkByTimeline(sourceNode, targetNode, type, matrixEventId) {
return this.__TimelineEvents.findAll({
where: {matrixEventId: matrixEventId},
include: [{
model: this.__Links,
where: {
type: type,
sourceNodeId: sourceNode.id,
targetNodeId: targetNode.id
},
as: 'link'
}]
}).then(events => {
if (!events)return Promise.resolve(null);

for (var event of events) {
if (event.link) return Promise.resolve(event.link);
}

return Promise.resolve(null);
});
}

/**
* Creates a new Link
* @param {Node} sourceNode the source Node
Expand Down

0 comments on commit 66a379b

Please sign in to comment.