Remove integrations by type instead of by user ID

This is because the user ID might not exist for the integration (such as the case for RSS).
This commit is contained in:
turt2live 2017-05-28 17:39:02 -06:00
parent 69a0ea8118
commit 3aa60b66a6
20 changed files with 86 additions and 59 deletions

View File

@ -1,4 +1,5 @@
type: "bot" type: "bot"
integrationType: "demo"
enabled: false enabled: false
userId: "@dimension:t2bot.io" userId: "@dimension:t2bot.io"
name: "Demo Bot" name: "Demo Bot"

View File

@ -1,4 +1,5 @@
type: "bot" type: "bot"
integrationType: "giphy"
enabled: true enabled: true
userId: "@neb_giphy:matrix.org" userId: "@neb_giphy:matrix.org"
name: "Giphy" name: "Giphy"

View File

@ -1,4 +1,5 @@
type: "bot" type: "bot"
integrationType: "google"
enabled: true enabled: true
userId: "@_neb_google:matrix.org" userId: "@_neb_google:matrix.org"
name: "Google" name: "Google"

View File

@ -1,4 +1,5 @@
type: "bot" type: "bot"
integrationType: "guggy"
enabled: true enabled: true
userId: "@_neb_guggy:matrix.org" userId: "@_neb_guggy:matrix.org"
name: "Guggy" name: "Guggy"

View File

@ -1,4 +1,5 @@
type: "bot" type: "bot"
integrationType: "imgur"
enabled: true enabled: true
userId: "@_neb_imgur:matrix.org" userId: "@_neb_imgur:matrix.org"
name: "Imgur" name: "Imgur"

View File

@ -1,4 +1,5 @@
type: "bot" type: "bot"
integrationType: "wikipedia"
enabled: true enabled: true
userId: "@_neb_wikipedia:matrix.org" userId: "@_neb_wikipedia:matrix.org"
name: "Wikipedia" name: "Wikipedia"

View File

@ -23,7 +23,7 @@ class DimensionApi {
this._db = db; this._db = db;
app.get("/api/v1/dimension/integrations/:roomId", this._getIntegrations.bind(this)); app.get("/api/v1/dimension/integrations/:roomId", this._getIntegrations.bind(this));
app.post("/api/v1/dimension/removeIntegration", this._removeIntegration.bind(this)); app.delete("/api/v1/dimension/integrations/:roomId/:type/:integrationType", this._removeIntegration.bind(this));
} }
_getIntegration(integrationConfig, roomId, scalarToken) { _getIntegration(integrationConfig, roomId, scalarToken) {
@ -75,22 +75,23 @@ class DimensionApi {
} }
_removeIntegration(req, res) { _removeIntegration(req, res) {
var roomId = req.body.roomId; var roomId = req.params.roomId;
var userId = req.body.userId; var scalarToken = req.query.scalar_token;
var scalarToken = req.body.scalarToken; var type = req.params.type;
var integrationType = req.params.integrationType;
if (!roomId || !userId || !scalarToken) { if (!roomId || !scalarToken || !type || !integrationType) {
res.status(400).send({error: "Missing room, user, or token"}); res.status(400).send({error: "Missing room, integration type, type, or token"});
return; return;
} }
var integrationConfig = Integrations.byUserId[userId]; var integrationConfig = Integrations.byType[type][integrationType];
if (!integrationConfig) { if (!integrationConfig) {
res.status(400).send({error: "Unknown integration"}); res.status(400).send({error: "Unknown integration"});
return; return;
} }
log.info("DimensionApi", "Remove requested for " + userId + " in room " + roomId); log.info("DimensionApi", "Remove requested for " + type + " (" + integrationType + ") in room " + roomId);
this._db.checkToken(scalarToken).then(() => { this._db.checkToken(scalarToken).then(() => {
return this._getIntegration(integrationConfig, roomId, scalarToken); return this._getIntegration(integrationConfig, roomId, scalarToken);

View File

@ -1,4 +1,4 @@
var IntegrationStub = require("../type/IntegrationStub"); var IntegrationStub = require("../generic_types/IntegrationStub");
/** /**
* Creates an integration using the given * Creates an integration using the given

View File

@ -1,4 +1,4 @@
var ComplexBot = require("../../type/ComplexBot"); var ComplexBot = require("../../generic_types/ComplexBot");
/** /**
* Represents an RSS bot * Represents an RSS bot

View File

@ -1,11 +1,11 @@
var sdk = require("matrix-js-sdk"); var sdk = require("matrix-js-sdk");
var log = require("../../../util/LogService"); var log = require("../../../util/LogService");
var IntegrationStub = require("../../type/IntegrationStub"); var StubbedSimpleBackbone = require("./StubbedSimpleBackbone");
/** /**
* Standalone (matrix) backbone for simple bots * Standalone (matrix) backbone for simple bots
*/ */
class HostedSimpleBackbone extends IntegrationStub { class HostedSimpleBackbone extends StubbedSimpleBackbone {
/** /**
* Creates a new standalone bot backbone * Creates a new standalone bot backbone
@ -21,14 +21,9 @@ class HostedSimpleBackbone extends IntegrationStub {
}); });
} }
/**
* Leaves a given Matrix room
* @param {string} roomId the room to leave
* @returns {Promise<>} resolves when completed
*/
/*override*/ /*override*/
removeFromRoom(roomId) { removeFromRoom(roomId) {
log.info("HostedSimpleBackbone", "Removing " + this._settings.userId + " from " + roomId); log.info("HostedSimpleBackbone", "Removing " + this._config.userId + " from " + roomId);
return this._client.leave(roomId); return this._client.leave(roomId);
} }
} }

View File

@ -1,9 +1,9 @@
var ComplexBot = require("../../type/ComplexBot"); var IntegrationStub = require("../../generic_types/IntegrationStub");
/** /**
* Represents an RSS bot * Represents an RSS bot
*/ */
class RSSBot extends ComplexBot { class SimpleBot extends IntegrationStub {
/** /**
* Creates a new RSS bot * Creates a new RSS bot
@ -16,20 +16,9 @@ class RSSBot extends ComplexBot {
} }
/*override*/ /*override*/
getUserId() { removeFromRoom(roomId) {
return this._backbone.getUserId(); return this._backbone.removeFromRoom(roomId);
}
getFeeds() {
return this._backbone.getFeeds();
}
/*override*/
getState() {
return this.getFeeds().then(feeds => {
return {feeds: feeds};
});
} }
} }
module.exports = RSSBot; module.exports = SimpleBot;

View File

@ -1,12 +1,16 @@
var RSSBot = require("./RSSBot"); var SimpleBot = require("./SimpleBot");
var VectorRssBackbone = require("./VectorRssBackbone"); var VectorSimpleBackbone = require("./VectorSimpleBackbone");
var HostedSimpleBackbone = require("./HostedSimpleBackbone");
module.exports = (db, integrationConfig, roomId, scalarToken) => { module.exports = (db, integrationConfig, roomId, scalarToken) => {
if (integrationConfig.upstream) { if (integrationConfig.upstream) {
if (integrationConfig.upstream.type !== "vector") throw new Error("Unsupported upstream"); if (integrationConfig.upstream.type !== "vector") throw new Error("Unsupported upstream");
return db.getUpstreamToken(scalarToken).then(upstreamToken => { return db.getUpstreamToken(scalarToken).then(upstreamToken => {
var backbone = new VectorRssBackbone(roomId, upstreamToken); var backbone = new VectorSimpleBackbone(roomId, upstreamToken);
return new RSSBot(integrationConfig, backbone); return new SimpleBot(integrationConfig, backbone);
}); });
} else if (integrationConfig.hosted) {
var backbone = new HostedSimpleBackbone(integrationConfig);
return Promise.resolve(new SimpleBot(integrationConfig, backbone));
} else throw new Error("Unsupported config"); } else throw new Error("Unsupported config");
}; };

View File

@ -1,3 +1,24 @@
/** /**
* Created by Travis on 5/28/2017. * Stubbed backbone for simple bots
*/ */
class StubbedSimpleBackbone {
/**
* Creates a new stubbed bot backbone
* @param {*} botConfig the configuration for the bot
*/
constructor(botConfig) {
this._config = botConfig;
}
/**
* Leaves a given Matrix room
* @param {string} roomId the room to leave
* @returns {Promise<>} resolves when completed
*/
removeFromRoom(roomId) {
throw new Error("Not implemented");
}
}
module.exports = StubbedSimpleBackbone;

View File

@ -1,22 +1,28 @@
/** var VectorScalarClient = require("../../../scalar/VectorScalarClient");
* Stubbed/placeholder simple bot backbone var log = require("../../../util/LogService");
*/ var StubbedSimpleBackbone = require("./StubbedSimpleBackbone");
class StubbedSimpleBackbone {
/** /**
* Creates a new stubbed RSS backbone * Vector backbone for simple bots
*/ */
constructor() { class VectorSimpleBackbone extends StubbedSimpleBackbone {
/**
* Creates a new vector bot backbone
* @param {*} botConfig the configuration for the bot
* @param {string} upstreamScalarToken the upstream scalar token
*/
constructor(botConfig, upstreamScalarToken) {
super(botConfig);
this._config = botConfig;
this._upstreamToken = upstreamScalarToken;
} }
/** /*override*/
* Leaves a given Matrix room removeFromRoom(roomId) {
* @param {string} roomId the room to leave log.info("VectorSimpleBackbone", "Removing " + this._config.userId + " from " + roomId);
* @returns {Promise<>} resolves when completed return VectorScalarClient.removeIntegration(this._config.upstream.id, roomId, this._upstreamToken);
*/
leaveRoom(roomId) {
throw new Error("Not implemented");
} }
} }
module.exports = StubbedRssBackbone; module.exports = VectorSimpleBackbone;

View File

@ -33,6 +33,7 @@ log.info("Integrations", "Discovered " + keys.length + " integrations. Parsing d
var linear = []; var linear = [];
var byUserId = {}; var byUserId = {};
var byType = {};
for (var key of keys) { for (var key of keys) {
log.info("Integrations", "Preparing " + key); log.info("Integrations", "Preparing " + key);
@ -45,11 +46,18 @@ for (var key of keys) {
linear.push(merged); linear.push(merged);
if (merged['userId']) if (merged['userId'])
byUserId[merged['userId']] = merged; byUserId[merged['userId']] = merged;
if (!byType[merged['type']])
byType[merged['type']] = {};
if (byType[merged['type']][merged['integrationType']])
throw new Error("Duplicate type " + merged['type'] + " (" + merged['integrationType'] + ") at key " + key);
byType[merged['type']][merged['integrationType']] = merged;
} }
log.info("Integrations", "Loaded " + linear.length + " integrations"); log.info("Integrations", "Loaded " + linear.length + " integrations");
module.exports = { module.exports = {
all: linear, all: linear,
byUserId: byUserId byUserId: byUserId,
byType: byType
}; };

View File

@ -71,7 +71,7 @@ export class RiotComponent {
let promise = null; let promise = null;
if (!integration.isEnabled) { if (!integration.isEnabled) {
promise = this.api.removeIntegration(this.roomId, integration.userId, this.scalarToken); promise = this.api.removeIntegration(this.roomId, integration.type, integration.integrationType, this.scalarToken);
} else promise = this.scalar.inviteUser(this.roomId, integration.userId); } else promise = this.scalar.inviteUser(this.roomId, integration.userId);
promise promise

View File

@ -17,12 +17,8 @@ export class ApiService {
.map(res => res.json()).toPromise(); .map(res => res.json()).toPromise();
} }
removeIntegration(roomId: string, userId: string, scalarToken: string): Promise<any> { removeIntegration(roomId: string, type: string, integrationType: string, scalarToken: string): Promise<any> {
return this.http.post("/api/v1/dimension/removeIntegration", { return this.http.delete("/api/v1/dimension/integrations/" + roomId + "/" + type + "/" + integrationType, {params: {scalar_token: scalarToken}})
roomId: roomId,
userId: userId,
scalarToken: scalarToken
})
.map(res => res.json()).toPromise(); .map(res => res.json()).toPromise();
} }
} }

View File

@ -1,5 +1,6 @@
export interface Integration { export interface Integration {
type: string; type: string;
integrationType: string;
userId: string; userId: string;
name: string; name: string;
avatar: string; avatar: string;