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"
integrationType: "demo"
enabled: false
userId: "@dimension:t2bot.io"
name: "Demo Bot"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -23,7 +23,7 @@ class DimensionApi {
this._db = db;
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) {
@ -75,22 +75,23 @@ class DimensionApi {
}
_removeIntegration(req, res) {
var roomId = req.body.roomId;
var userId = req.body.userId;
var scalarToken = req.body.scalarToken;
var roomId = req.params.roomId;
var scalarToken = req.query.scalar_token;
var type = req.params.type;
var integrationType = req.params.integrationType;
if (!roomId || !userId || !scalarToken) {
res.status(400).send({error: "Missing room, user, or token"});
if (!roomId || !scalarToken || !type || !integrationType) {
res.status(400).send({error: "Missing room, integration type, type, or token"});
return;
}
var integrationConfig = Integrations.byUserId[userId];
var integrationConfig = Integrations.byType[type][integrationType];
if (!integrationConfig) {
res.status(400).send({error: "Unknown integration"});
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(() => {
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

View File

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

View File

@ -1,11 +1,11 @@
var sdk = require("matrix-js-sdk");
var log = require("../../../util/LogService");
var IntegrationStub = require("../../type/IntegrationStub");
var StubbedSimpleBackbone = require("./StubbedSimpleBackbone");
/**
* Standalone (matrix) backbone for simple bots
*/
class HostedSimpleBackbone extends IntegrationStub {
class HostedSimpleBackbone extends StubbedSimpleBackbone {
/**
* 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*/
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);
}
}

View File

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

View File

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

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 @@
/**
* Stubbed/placeholder simple bot backbone
*/
class StubbedSimpleBackbone {
var VectorScalarClient = require("../../../scalar/VectorScalarClient");
var log = require("../../../util/LogService");
var StubbedSimpleBackbone = require("./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;
}
/**
* Leaves a given Matrix room
* @param {string} roomId the room to leave
* @returns {Promise<>} resolves when completed
*/
leaveRoom(roomId) {
throw new Error("Not implemented");
/*override*/
removeFromRoom(roomId) {
log.info("VectorSimpleBackbone", "Removing " + this._config.userId + " from " + roomId);
return VectorScalarClient.removeIntegration(this._config.upstream.id, roomId, this._upstreamToken);
}
}
module.exports = StubbedRssBackbone;
module.exports = VectorSimpleBackbone;

View File

@ -33,6 +33,7 @@ log.info("Integrations", "Discovered " + keys.length + " integrations. Parsing d
var linear = [];
var byUserId = {};
var byType = {};
for (var key of keys) {
log.info("Integrations", "Preparing " + key);
@ -45,11 +46,18 @@ for (var key of keys) {
linear.push(merged);
if (merged['userId'])
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");
module.exports = {
all: linear,
byUserId: byUserId
byUserId: byUserId,
byType: byType
};

View File

@ -71,7 +71,7 @@ export class RiotComponent {
let promise = null;
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);
promise

View File

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

View File

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