Browse Source

get bots, create bot, delete bot routes

push-params
Evan Feenstra 5 years ago
parent
commit
171f0ec7f1
  1. 101
      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

101
api/controllers/actions.ts

@ -4,42 +4,81 @@ 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
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'))
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
const dataBody = JSON.parse(body.data.replace(/'/g,'"')) const dataBody = JSON.parse(body.data.replace(/'/g, '"'))
if(dataBody) body=dataBody if (dataBody) body = dataBody
} catch(e) { } catch (e) {
console.log(e) console.log(e)
return failure(res, 'failed to parse webhook body json') return failure(res, 'failed to parse webhook body json')
} }
} }
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}}) if (!bot) return failure(res, 'no bot')
console.log('=> bot:',bot.dataValues)
if(!bot) return failure(res,'no bot')
if(!(bot.secret&&bot.secret===bot_secret)) { if (!(bot.secret && bot.secret === bot_secret)) {
return failure(res, 'wrong secret') return failure(res, 'wrong secret')
} }
if(!action){ if (!action) {
return failure(res, 'no action') return failure(res, 'no action')
} }
if(action==='keysend') { if (action === 'keysend') {
console.log('=> BOT KEYSEND') console.log('=> BOT KEYSEND')
if(!(pubkey&&pubkey.length===66&&amount)) { if (!(pubkey && pubkey.length === 66 && amount)) {
return failure(res, 'wrong params') return failure(res, 'wrong params')
} }
const MIN_SATS = 3 const MIN_SATS = 3
@ -47,34 +86,34 @@ export async function processAction(req, res) {
const opts = { const opts = {
dest: destkey, dest: destkey,
data: {}, data: {},
amt: Math.max((amount||0), MIN_SATS) amt: Math.max((amount || 0), MIN_SATS)
} }
try { try {
await network.signAndSend(opts) await network.signAndSend(opts)
return success(res, {success:true}) return success(res, { success: true })
} catch(e) { } catch (e) {
return failure(res, e) return failure(res, e)
} }
} else if (action==='broadcast') { } else if (action === 'broadcast') {
console.log('=> BOT BROADCAST') console.log('=> BOT BROADCAST')
if(!bot.chatId || !text) return failure(res,'no uuid or text') if (!bot.chatId || !text) return failure(res, 'no uuid or text')
const owner = await models.Contact.findOne({ where: { isOwner: true } }) const owner = await models.Contact.findOne({ where: { isOwner: true } })
const theChat = await models.Chat.findOne({where:{id: bot.chatId}}) const theChat = await models.Chat.findOne({ where: { id: bot.chatId } })
if(!theChat || !owner) return failure(res,'no chat') if (!theChat || !owner) return failure(res, 'no chat')
if(!theChat.type===constants.chat_types.tribe) return failure(res,'not a tribe') if (!theChat.type === constants.chat_types.tribe) return failure(res, 'not a tribe')
const encryptedForMeText = rsa.encrypt(owner.contactKey, text) const encryptedForMeText = rsa.encrypt(owner.contactKey, text)
const encryptedText = rsa.encrypt(theChat.groupKey, text) const encryptedText = rsa.encrypt(theChat.groupKey, text)
const textMap = {'chat': encryptedText} const textMap = { 'chat': encryptedText }
var date = new Date(); var date = new Date();
date.setMilliseconds(0) date.setMilliseconds(0)
const alias = bot.name || 'Bot' const alias = bot.name || 'Bot'
const msg:{[k:string]:any}={ const msg: { [k: string]: any } = {
chatId: theChat.id, chatId: theChat.id,
uuid: short.generate(), uuid: short.generate(),
type: constants.message_types.message, type: constants.message_types.message,
sender: owner.id, sender: owner.id,
amount: amount||0, amount: amount || 0,
date: date, date: date,
messageContent: encryptedForMeText, messageContent: encryptedForMeText,
remoteMessageContent: JSON.stringify(textMap), remoteMessageContent: JSON.stringify(textMap),
@ -86,11 +125,11 @@ export async function processAction(req, res) {
const message = await models.Message.create(msg) const message = await models.Message.create(msg)
await network.sendMessage({ await network.sendMessage({
chat: theChat, chat: theChat,
sender: {...owner.dataValues, alias}, sender: { ...owner.dataValues, alias },
message: { content:textMap, id:message.id, uuid: message.uuid }, message: { content: textMap, id: message.id, uuid: message.uuid },
type: constants.message_types.message, type: constants.message_types.message,
success: ()=> success(res, {success:true}), success: () => success(res, { success: true }),
failure: ()=> failure(res, 'failed'), failure: () => failure(res, 'failed'),
}) })
} else { } else {
return failure(res, 'no action') return failure(res, 'no action')

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,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAA;IAC3C,IAAI,UAAU,GAAG,IAAI,CAAC,UAAU,IAAI,IAAI,CAAA;IACxC,IAAG,IAAI,CAAC,UAAU,IAAI,OAAO,IAAI,CAAC,UAAU,KAAG,QAAQ,EAAC;QACtD,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;KACzC;IACD,OAAO,IAAI,CAAC,eAAe,CAAA;IAC3B,OAAO,cAAO,iCACT,IAAI,KACP,UAAU,IACV,CAAA;AACJ,CAAC;AA0CC,gCAAU;AAxCZ,SAAS,aAAa,CAAC,GAAG,EAAE,IAAI,EAAE,OAAQ;IACxC,IAA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