mtfosbot/route/api/twitch/index.js

42 lines
1.0 KiB
JavaScript

const Router = require('koa-router')
const {
genError,
checkSession,
resObject
} = require('@libs/route-utils')
const r = new Router()
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 * form "public"."twitch_channel"`
let values = [c.session.user.id]
let result = await c.db.query({text, values})
c.state.channelList = result.rows
return n()
}
r.get('/channels', checkSession, 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')
}, async (c, n) => {
if (!('channelList' in c.state) || !Array.isArray(c.state.channelList)) throw genError('InternalError')
c.obj = resObject('Success', {
list: c.state.channelList
})
})
module.exports = r