This commit is contained in:
Ashley Williams 2016-05-26 17:05:08 -04:00
Родитель 029fcc7653
Коммит f336f178d9
2 изменённых файлов: 40 добавлений и 28 удалений

Просмотреть файл

@ -2,6 +2,7 @@ module.exports = {
parseRequestBody: function (request) { parseRequestBody: function (request) {
var messages = request._body.split('&')[8].split('+'); var messages = request._body.split('&')[8].split('+');
return { return {
command: messages[0],
type: messages[1], type: messages[1],
name: messages[2], name: messages[2],
event: messages[3], event: messages[3],

Просмотреть файл

@ -9,49 +9,60 @@ const Subscription = require("../model");
const helpers = require("../helpers"); const helpers = require("../helpers");
const messenger = require("../messenger"); const messenger = require("../messenger");
var checkSub = function(info) { const opts = {
User.where({ id: 1 }) as_user: true,
.fetch({ withRelated: ['subscriptions'] }) username: "captainhook"
.then(function(user) { };
var subscriptions = user.related('subscriptions');
for (var i=0; i<subscriptions.length;i++) {
Subscription.where(subscriptions[i]).fetch()
.then(function(record) {
if (record.type === info.type && record.name === info.name) {
console.log("Subscription already exists!");
}
})
.catch();
}
});
}
// receive outgoing integration from slack `/captain-hook` var subscribe = function(info) {
module.exports = function(request, response, next) {
var info = helpers.parseRequestBody(request);
var opts = {
as_user: true,
username: "captainhook"
};
slack.chat.postMessage(channelID, messenger.buildRequestMessage(info), opts);
var hook_opts = helpers.buildHookRequestOpts(info); var hook_opts = helpers.buildHookRequestOpts(info);
// checkSub(info);
Request.post(hook_opts, function(err, res, body) { Request.post(hook_opts, function(err, res, body) {
if (err) { if (err) {
console.log(err); slack.chat.postMessage(channelID, err.toString(), opts);
} else { } else {
console.log('just created hook with id=' + body.id); console.log('just created hook with id=' + body.id);
var subscription = helpers.buildSubscription(hook_opts, body); var subscription = helpers.buildSubscription(hook_opts, body);
Subscription.forge(subscription).save() Subscription.forge(subscription).save()
.then(function(record) { .then(function(record) {
if (!record) { if (!record) {
slack.chat.postMessage(channelID, messenger.bookshelfCreateError, opts); slack.chat.postMessage(channelID, messenger.bookshelf, opts);
} else { } else {
slack.chat.postMessage(channelID, messenger.buildSuccessMessage(record), opts); var message = messenger.buildSuccessMessage(record);
slack.chat.postMessage(channelID, message, opts);
} }
}) })
.catch(); .catch();
} }
}); });
};
var help = function() {
var message = "arrrr! i'm captain hook\n" +
"*usage:* \n" +
"`/captain-hook <command> <type> <name> <event>`\n" +
"\n" +
"\t\t*command*: `subscribe` (create a new webhook), `help`\n" +
"\t\t*type*: `package` or `scope`\n" +
"\t\t*name*: the name of the package or scope, e.g. `lodash`\n" +
"\t\t*event*: this doesn't actually work yet :grimacing: :sweat_smile:\n";
slack.chat.postMessage(channelID, message, opts);
};
// receive outgoing integration from slack `/captain-hook`
module.exports = function(request, response, next) {
var info = helpers.parseRequestBody(request);
var command = info.command.slice(5);
var message;
switch(command) {
case "subscribe":
subscribe(info);
break;
case "help":
help();
break;
default:
help();
break;
}
next(); next();
}; };