From bebb970ef56547c86adb35fcb24e9569c03e496f Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 16 Aug 2018 16:49:54 +0800 Subject: [PATCH] 1. add channel api --- background.js | 4 +- bin/dbVersion.json | 3 +- libs/twitch-bot/index.js | 3 ++ route/api/twitch/index.js | 78 +++++++++++++++++++++++++++++++++++++-- schema/20180816-1.sql | 3 ++ 5 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 schema/20180816-1.sql diff --git a/background.js b/background.js index caf5739..55ceb6d 100644 --- a/background.js +++ b/background.js @@ -289,7 +289,7 @@ const checkDonate = async (loginName = null, opayid = null) => { let ids = result.data.lstDonate.map(t => t.donateid) let inparams = ids.map((t, idx) => `$${(idx + 1)}`) let dbres = await db.query({ - text: `select * from "public"."donate_list" where "donate_id" in (${inparams.join(',')})`, + text: `select * from "public"."opay_donate_list" where "donate_id" in (${inparams.join(',')})`, values: [...ids] }) let list = result.data.lstDonate.filter(t => { @@ -299,7 +299,7 @@ const checkDonate = async (loginName = null, opayid = null) => { return true }) for (let i of list) { - let text = `insert into "public"."donate_list" ("opayid", "donate_id", "price", "text", "name") values + let text = `insert into "public"."opay_donate_list" ("opayid", "donate_id", "price", "text", "name") values ($1, $2, $3, $4, $5)` let values = [opayid, i.donateid, i.amount || 0, i.msg || '', i.name || ''] try { diff --git a/bin/dbVersion.json b/bin/dbVersion.json index aac5194..c7ef116 100644 --- a/bin/dbVersion.json +++ b/bin/dbVersion.json @@ -14,7 +14,8 @@ {"file": "20180721-1.sql", "version": 12}, {"file": "20180807-1.sql", "version": 13}, {"file": "20180811-1.sql", "version": 14}, - {"file": "20180815-1.sql", "version": 15} + {"file": "20180815-1.sql", "version": 15}, + {"file": "20180816-1.sql", "version": 16} ], "test": [] } \ No newline at end of file diff --git a/libs/twitch-bot/index.js b/libs/twitch-bot/index.js index a64ae4d..cf62508 100644 --- a/libs/twitch-bot/index.js +++ b/libs/twitch-bot/index.js @@ -27,6 +27,9 @@ class WS { // this.joinChannel(channel) this.addCmdQueue(this.joinChannel, [channel]) }) + event.on('twitchLeave', (channel) => { + this.addCmdQueue(this.leaveChannel, [channel]) + }) event.on('twitchSend', data => { if (!('msg' in data) || typeof data.msg !== 'string') return if (!('channel' in data) || typeof data.channel !== 'string') return diff --git a/route/api/twitch/index.js b/route/api/twitch/index.js index 54722fc..55c2426 100644 --- a/route/api/twitch/index.js +++ b/route/api/twitch/index.js @@ -2,9 +2,12 @@ const Router = require('koa-router') const { genError, checkSession, - resObject + resObject, + chkObject, + toInt } = require('@libs/route-utils') const r = new Router() +const evt = require('@root/event') const typeTwitch = async (c, n) => { let text = `select * from "public"."twitch_channel" where "id" = $1` @@ -22,7 +25,7 @@ const typeSystem = async (c, n) => { return n() } -r.get('/channels', checkSession, async (c, n) => { +const getChannelList = async (c, n) => { if (c.session.loginType === 'twitch') { return typeTwitch(c, n) } else if (c.session.loginType === 'system') { @@ -30,7 +33,28 @@ r.get('/channels', checkSession, async (c, n) => { } throw genError('Forbidden') -}, async (c, n) => { +} + +const hasChannel = (key = '') => { + if (typeof key !== 'string' || key.trim().length === 0) key = 'chid' + key = key.trim() + return async (c, n) => { + if (!('channelList' in c.state) || !Array.isArray(c.state.channelList)) throw genError('InternalError') + let chid = c.params[key] || '' + c.state.chid = chid + let chk = chkObject.bind({body: {chid}}) + if (!chk('chid', 'string')) throw genError('DataFormat') + + if (c.state.channelList.length === 0) throw genError('NotFound', 'channel not found') + let channel = c.state.channelList.filter(t => t.id === chid) + if (channel.length === 0) throw genError('NotFound', 'channel not found') + + c.state.channel = channel[0] + return n() + } +} + +r.get('/channels', checkSession, getChannelList, async (c, n) => { if (!('channelList' in c.state) || !Array.isArray(c.state.channelList)) throw genError('InternalError') c.obj = resObject('Success', { @@ -38,4 +62,52 @@ r.get('/channels', checkSession, async (c, n) => { }) }) +r.get('/channel/:chid', checkSession, getChannelList, hasChannel('chid'), async (c, n) => { + c.obj = resObject('Success', {channel: c.state.channel}) +}) + +r.put('/channel/:chid/botjoin', checkSession, getChannelList, hasChannel('chid'), async (c, n) => { + if (!c.chkBody('join', 'number')) throw genError('DataFormat') + let join = toInt(c.request.body.join, 0, 0, 1) + let text = `update "public"."twitch_channel" set "join" = $1 where "id" = $2` + let values = [!!join, c.state.channel.id] + await c.db.query({ + text, + values + }) + + evt.emit(join ? 'twitchJoin' : 'twitchLeave', c.state.channel.name) + + c.obj = resObject('Success') +}) + +r.put('/channel/:chid/opay', checkSession, getChannelList, hasChannel('chid'), async (c, n) => { + if (!c.chkBody('opay', 'string', true)) throw genError('DataFormat') + + let text = `update "public"."twitch_channel" set "opayid" = $1 where "id" = $2` + let values = [c.request.body.opay, c.state.channel.id] + await c.db.query({ + text, + values + }) + + c.obj = resObject('Success') +}) + +r.get('/channel/:chid/donation', checkSession, getChannelList, hasChannel('chid'), async (c, n) => { + let text = `select ds.* from "public"."donate_setting" ds + left join "public"."twitch_channel" tw + on tw.id = ds.twitch + where + tw.id = $1` + let values = [c.state.channel.id] + let result = await c.db.query({ + text, + values + }) + c.obj = resObject('Success', { + list: result.rows + }) +}) + module.exports = r diff --git a/schema/20180816-1.sql b/schema/20180816-1.sql new file mode 100644 index 0000000..7691850 --- /dev/null +++ b/schema/20180816-1.sql @@ -0,0 +1,3 @@ +ALTER TABLE public.donate_progres RENAME TO donate_setting; +ALTER TABLE public.donate_list RENAME TO opay_donate_list; +ALTER TABLE public.donate_setting ADD type varchar(100) DEFAULT '' NOT NULL; \ No newline at end of file