Browse Source

get bots, create bot, delete bot routes

push-params
Evan Feenstra 5 years ago
parent
commit
171f0ec7f1
  1. 45
      api/controllers/actions.ts
  2. 3
      api/controllers/index.ts
  3. 3
      api/utils/json.ts
  4. 45
      dist/api/controllers/actions.js
  5. 2
      dist/api/controllers/actions.js.map
  6. 3
      dist/api/controllers/index.js
  7. 2
      dist/api/controllers/index.js.map
  8. 2
      dist/api/utils/json.js
  9. 2
      dist/api/utils/json.js.map

45
api/controllers/actions.ts

@ -4,6 +4,8 @@ import * as network from '../network'
import { models } from '../models' import { models } from '../models'
import * as short from 'short-uuid' import * as short from 'short-uuid'
import * as rsa from '../crypto/rsa' import * as rsa from '../crypto/rsa'
import * as crypto from 'crypto'
import * as jsonUtils from '../utils/json'
/* /*
hexdump -n 8 -e '4/4 "%08X" 1 "\n"' /dev/random hexdump -n 8 -e '4/4 "%08X" 1 "\n"' /dev/random
@ -12,8 +14,47 @@ hexdump -n 16 -e '4/4 "%08X" 1 "\n"' /dev/random
const constants = require(path.join(__dirname, '../../config/constants.json')) const constants = require(path.join(__dirname, '../../config/constants.json'))
export const getBots = async (req, res) => {
try {
const bots = await models.Bot.findAll()
success(res, {
bots: bots.map(b=> jsonUtils.botToJson(b))
})
} catch(e) {
failure(res,'no bots')
}
}
export const createBot = async (req, res) => {
const { chat_id, name, } = req.body
const newBot = {
id: crypto.randomBytes(8).toString('hex').toUpperCase(),
chatId: chat_id,
name: name,
secret: crypto.randomBytes(16).toString('hex').toUpperCase()
}
try {
const theBot = await models.Bot.create(newBot)
success(res, jsonUtils.botToJson(theBot))
} catch (e) {
failure(res, 'bot creation failed')
}
}
export const deleteBot = async (req, res) => {
const id = req.params.id
if (!id) return
try {
models.Bot.destroy({ where: { id } })
success(res, true)
} catch (e) {
console.log('ERROR deleteBot', e)
failure(res, e)
}
}
export async function processAction(req, res) { export async function processAction(req, res) {
console.log('=> process action')
let body = req.body let body = req.body
if (body.data && typeof body.data === 'string' && body.data[1] === "'") { if (body.data && typeof body.data === 'string' && body.data[1] === "'") {
try { // parse out body from "data" for github webhook action try { // parse out body from "data" for github webhook action
@ -25,9 +66,7 @@ export async function processAction(req, res) {
} }
} }
const { action, bot_id, bot_secret, pubkey, amount, text } = body const { action, bot_id, bot_secret, pubkey, amount, text } = body
console.log('=> process action',JSON.stringify(body,null,2))
const bot = await models.Bot.findOne({ where: { id: bot_id } }) const bot = await models.Bot.findOne({ where: { id: bot_id } })
console.log('=> bot:',bot.dataValues)
if (!bot) return failure(res, 'no bot') if (!bot) return failure(res, 'no bot')
if (!(bot.secret && bot.secret === bot_secret)) { if (!(bot.secret && bot.secret === bot_secret)) {

3
api/controllers/index.ts

@ -94,6 +94,9 @@ export async function set(app) {
app.get('/info', details.getNodeInfo) app.get('/info', details.getNodeInfo)
app.post('/action', actions.processAction) app.post('/action', actions.processAction)
app.get('/bots', actions.getBots)
app.post('/bot', actions.createBot)
app.delete('/bot/:id', actions.deleteBot)
app.get('/version', async function(req,res) { app.get('/version', async function(req,res) {
const version = await checkTag() const version = await checkTag()

3
api/utils/json.ts

@ -38,6 +38,8 @@ function contactToJson(contact){
const inviteToJson = (invite) => toSnake(invite.dataValues||invite) const inviteToJson = (invite) => toSnake(invite.dataValues||invite)
const botToJson = (bot) => toSnake(bot.dataValues||bot)
const jsonToContact = (json) => toCamel(json) const jsonToContact = (json) => toCamel(json)
function subscriptionToJson(subscription, chat) { function subscriptionToJson(subscription, chat) {
@ -58,4 +60,5 @@ export {
jsonToContact, jsonToContact,
chatToJson, chatToJson,
subscriptionToJson, subscriptionToJson,
botToJson,
} }

45
dist/api/controllers/actions.js

@ -15,14 +15,55 @@ const network = require("../network");
const models_1 = require("../models"); const models_1 = require("../models");
const short = require("short-uuid"); const short = require("short-uuid");
const rsa = require("../crypto/rsa"); const rsa = require("../crypto/rsa");
const crypto = require("crypto");
const jsonUtils = require("../utils/json");
/* /*
hexdump -n 8 -e '4/4 "%08X" 1 "\n"' /dev/random hexdump -n 8 -e '4/4 "%08X" 1 "\n"' /dev/random
hexdump -n 16 -e '4/4 "%08X" 1 "\n"' /dev/random hexdump -n 16 -e '4/4 "%08X" 1 "\n"' /dev/random
*/ */
const constants = require(path.join(__dirname, '../../config/constants.json')); const constants = require(path.join(__dirname, '../../config/constants.json'));
exports.getBots = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const bots = yield models_1.models.Bot.findAll();
res_1.success(res, {
bots: bots.map(b => jsonUtils.botToJson(b))
});
}
catch (e) {
res_1.failure(res, 'no bots');
}
});
exports.createBot = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { chat_id, name, } = req.body;
const newBot = {
id: crypto.randomBytes(8).toString('hex').toUpperCase(),
chatId: chat_id,
name: name,
secret: crypto.randomBytes(16).toString('hex').toUpperCase()
};
try {
const theBot = yield models_1.models.Bot.create(newBot);
res_1.success(res, jsonUtils.botToJson(theBot));
}
catch (e) {
res_1.failure(res, 'bot creation failed');
}
});
exports.deleteBot = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const id = req.params.id;
if (!id)
return;
try {
models_1.models.Bot.destroy({ where: { id } });
res_1.success(res, true);
}
catch (e) {
console.log('ERROR deleteBot', e);
res_1.failure(res, e);
}
});
function processAction(req, res) { function processAction(req, res) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
console.log('=> process action');
let body = req.body; let body = req.body;
if (body.data && typeof body.data === 'string' && body.data[1] === "'") { if (body.data && typeof body.data === 'string' && body.data[1] === "'") {
try { // parse out body from "data" for github webhook action try { // parse out body from "data" for github webhook action
@ -36,9 +77,7 @@ function processAction(req, res) {
} }
} }
const { action, bot_id, bot_secret, pubkey, amount, text } = body; const { action, bot_id, bot_secret, pubkey, amount, text } = body;
console.log('=> process action', JSON.stringify(body, null, 2));
const bot = yield models_1.models.Bot.findOne({ where: { id: bot_id } }); const bot = yield models_1.models.Bot.findOne({ where: { id: bot_id } });
console.log('=> bot:', bot.dataValues);
if (!bot) if (!bot)
return res_1.failure(res, 'no bot'); return res_1.failure(res, 'no bot');
if (!(bot.secret && bot.secret === bot_secret)) { if (!(bot.secret && bot.secret === bot_secret)) {

2
dist/api/controllers/actions.js.map

File diff suppressed because one or more lines are too long

3
dist/api/controllers/index.js

@ -91,6 +91,9 @@ function set(app) {
app.get('/logs', details.getLogsSince); app.get('/logs', details.getLogsSince);
app.get('/info', details.getNodeInfo); app.get('/info', details.getNodeInfo);
app.post('/action', actions.processAction); app.post('/action', actions.processAction);
app.get('/bots', actions.getBots);
app.post('/bot', actions.createBot);
app.delete('/bot/:id', actions.deleteBot);
app.get('/version', function (req, res) { app.get('/version', function (req, res) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const version = yield gitinfo_1.checkTag(); const version = yield gitinfo_1.checkTag();

2
dist/api/controllers/index.js.map

File diff suppressed because one or more lines are too long

2
dist/api/utils/json.js

@ -34,6 +34,8 @@ function contactToJson(contact) {
exports.contactToJson = contactToJson; exports.contactToJson = contactToJson;
const inviteToJson = (invite) => case_1.toSnake(invite.dataValues || invite); const inviteToJson = (invite) => case_1.toSnake(invite.dataValues || invite);
exports.inviteToJson = inviteToJson; exports.inviteToJson = inviteToJson;
const botToJson = (bot) => case_1.toSnake(bot.dataValues || bot);
exports.botToJson = botToJson;
const jsonToContact = (json) => case_1.toCamel(json); const jsonToContact = (json) => case_1.toCamel(json);
exports.jsonToContact = jsonToContact; exports.jsonToContact = jsonToContact;
function subscriptionToJson(subscription, chat) { function subscriptionToJson(subscription, chat) {

2
dist/api/utils/json.js.map

@ -1 +1 @@
{"version":3,"file":"json.js","sourceRoot":"","sources":["../../../api/utils/json.ts"],"names":[],"mappings":";;AAAA,wCAA6C;AAC7C,oCAAmC;AAEnC,SAAS,UAAU,CAAC,CAAC;IACnB,IAAG,CAAC,CAAC;QAAE,OAAO,EAAE,CAAA;IAChB,MAAM,EAAE,GAAG,CAAC,CAAC,UAAU,IAAE,CAAC,CAAA;IAC1B,MAAM,IAAI,GAAG,IAAI,C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