Skip to content

Commit

Permalink
Support (un)linking yourself to rooms. Adds #13
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Mar 23, 2017
1 parent 1be87b6 commit 5b0651e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ d3.json(source, function (error, graph) {

link.append("title")
.text(function (d) {
return d.value + " " + d.type + (d.value !== 1 ? "s" : "") + " from " + (display[d.source]);
return d.value + " " + d.type.replace(/_/g, ' ') + (d.value !== 1 ? "s" : "") + " from " + (display[d.source]);
});

simulation
Expand Down
4 changes: 4 additions & 0 deletions app/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ body {
stroke: #10b748;
}

.links line.self_link {
stroke: #694bcc;
}

.nodes circle {
stroke: #fff;
stroke-width: 1px;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

var dbm;
var type;
var seed;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function (options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
};

exports.up = function (db) {
return db.addColumn('membership_events', 'unlisted', {type: 'boolean', defaultValue: 0});
};

exports.down = function (db) {
return db.removeColumn('membership_events', 'unlisted');
};

exports._meta = {
"version": 1
};
20 changes: 19 additions & 1 deletion src/DataStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DataStore {

getMembershipEvents() {
return new Promise((resolve, reject)=> {
this._db.all("SELECT * FROM membership_events", function (error, rows) {
this._db.all("SELECT * FROM membership_events WHERE unlisted = 0", function (error, rows) {
if (error) reject(error);
else resolve(rows);
});
Expand Down Expand Up @@ -73,6 +73,24 @@ class DataStore {
});
}

hasSimilarState(type, roomId, sender) {
return new Promise((resolve, reject) => {
this._db.get("SELECT * FROM membership_events WHERE type = ? AND room_id = ? AND sender = ? AND unlisted = 0", type, roomId, sender, function (error, row) {
if (error)reject(error);
else resolve(row);
});
});
}

deleteSimilarState(type, roomId, sender) {
return new Promise((resolve, reject) => {
this._db.run("UPDATE membership_events SET unlisted = 1 WHERE type = ? AND room_id = ? AND sender = ?", type, roomId, sender, function (_, error) {
if (error)reject(error);
else resolve();
});
});
}

recordState(eventId, type, roomId, sender, timestamp, message, error = null) {
return new Promise((resolve, reject)=> {
this._db.get("SELECT * FROM membership_events WHERE event_id = ?", eventId, function (error, row) {
Expand Down
34 changes: 30 additions & 4 deletions src/MatrixHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class MatrixHandler {
if (!body) return Promise.resolve(); // Probably redacted

if (body.startsWith('!voyager')) {
this._processCommand(event, body.substring('!voyager'.length).trim().split(' '));
this._processCommand(event, body.substring('!voyager'.length).trim().split(' '), wrapperEvent);
// don't need a promise from _processCommand - it should respond appropriately for us
return Promise.resolve();
}
Expand All @@ -97,7 +97,7 @@ class MatrixHandler {
return Promise.all(dbPromises).then(() => this._client.sendReadReceipt(wrapperEvent));
}

_processCommand(event, args) {
_processCommand(event, args, wrapperEvent) {
var sender = this._client.getUser(event.sender);

if (args.length == 0) {
Expand All @@ -110,6 +110,8 @@ class MatrixHandler {
this._client.sendNotice(event.room_id,
"!voyager showme - Sets your name and avatar to be visible on the graph\n" +
"!voyager hideme - Hides your name and avatar from the graph\n" +
"!voyager linkme - Links your user account to this current room on the graph\n" +
"!voyager unlinkme - Removes your self-links from the current room on the graph\n" +
"!voyager help - This menu"
);
break;
Expand All @@ -118,7 +120,7 @@ class MatrixHandler {
this._db.setEnrolledState(event.sender, true).then(() => {
this._client.sendNotice(event.room_id, sender.displayName + ": Your name and avatar will now appear on the graph.");
}, err => {
log.error("MatrixHandler", "Error setting enrolled state for " + event.sender);
log.error("MatrixHandler", "Error setting enrolled state to visible for " + event.sender);
log.error("MatrixHandler", err);
this._client.sendNotice(event.room_id, sender.displayName + ": There was an error processing your command.");
});
Expand All @@ -128,7 +130,31 @@ class MatrixHandler {
this._db.setEnrolledState(event.sender, false).then(() => {
this._client.sendNotice(event.room_id, sender.displayName + ": Your name and avatar will no longer appear on the graph.");
}, err => {
log.error("MatrixHandler", "Error setting enrolled state for " + event.sender);
log.error("MatrixHandler", "Error setting enrolled state to hidden for " + event.sender);
log.error("MatrixHandler", err);
this._client.sendNotice(event.room_id, sender.displayName + ": There was an error processing your command.");
});
break;
case 'linkme':
this._db.hasSimilarState('self_link', event.room_id, event.sender).then(hasState => {
if (hasState) {
this._client.sendNotice(event.room_id, sender.displayName + ": You're already linked to this room!");
} else this._db.recordState(event.event_id, 'self_link', event.room_id, event.sender, event.origin_server_ts, event.content.body)
.then(() => this._client.sendReadReceipt(wrapperEvent))
}, err=> {
log.error("MatrixHandler", "Error linking " + event.sender + " to room " + event.room_id);
log.error("MatrixHandler", err);
this._client.sendNotice(event.room_id, sender.displayName + ": There was an error processing your command.");
});
break;
case 'unlinkme':
this._db.deleteSimilarState('self_link', event.room_id, event.sender).then(hasState => {
if (hasState) {
this._client.sendNotice(event.room_id, sender.displayName + ": Your links to this room have been removed.");
} else this._db.recordState(event.event_id, 'self_link', event.room_id, event.sender, event.origin_server_ts, event.content.body)
.then(() => this._client.sendReadReceipt(wrapperEvent))
}, err=> {
log.error("MatrixHandler", "Error unlinking " + event.sender + " to room " + event.room_id);
log.error("MatrixHandler", err);
this._client.sendNotice(event.room_id, sender.displayName + ": There was an error processing your command.");
});
Expand Down

0 comments on commit 5b0651e

Please sign in to comment.