mtfosbot/route/api/twitch/index.js
2018-08-23 00:10:17 +08:00

146 lines
4.3 KiB
JavaScript

const Router = require('koa-router')
const {
genError,
checkSession,
resObject,
chkObject,
toInt
} = require('@libs/route-utils')
const r = new Router()
const evt = require('@root/event')
const koaBody = require('koa-body')
const typeTwitch = async (c, n) => {
let text = `select * from "public"."twitch_channel" where "id" = $1`
let values = [c.session.user.id]
let result = await c.db.query({text, values})
c.state.channelList = result.rows
return n()
}
const typeSystem = async (c, n) => {
let text = `select * from "public"."twitch_channel" order by "name" asc`
let values = []
let result = await c.db.query({text, values})
c.state.channelList = result.rows
return n()
}
const getChannelList = async (c, n) => {
if (c.session.loginType === 'twitch') {
return typeTwitch(c, n)
} else if (c.session.loginType === 'system') {
return typeSystem(c, n)
}
throw genError('Forbidden')
}
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', {
list: c.state.channelList
})
})
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'), koaBody(), 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'), koaBody(), 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/opay/setting', 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
and tw.opayid != ''
limit 1`
let values = [c.state.channel.id]
let result = await c.db.query({
text,
values
})
c.obj = resObject('Success', {
setting: result.rowCount === 0 ? {} : result.rows[0]
})
})
r.put('/channel/:chid/opay/setting', checkSession, getChannelList, hasChannel('chid'), koaBody(), async (c, n) => {
if (!c.chkBody('start', 'number') || !c.chkBody('end', 'number') || !c.chkBody('title', 'string') || !c.chkBody('amount', 'number')) throw genError('DataFormat')
let sts = toInt(c.request.body.start, 0, 0)
let ets = toInt(c.request.body.end, 0, 0)
let amount = toInt(c.request.body.amount, 0, 0)
let sd = new Date(sts)
let ed = new Date(ets)
let text = `insert into "public"."donate_setting" ("twitch", "start_date", "end_date", "target_amount", "title") values
($1, $2, $3, $4, $5)
on CONFLICT ("twitch") DO UPDATE
set
"start_date" = $2,
"end_date" = $3,
"target_amount" = $4,
"title" = $5`
let values = [c.state.channel.id, sd, ed, amount, c.request.body.title]
let result = await c.db.query({
text,
values
})
console.log(result)
c.obj = resObject('Success')
})
module.exports = r