add twitch stream notify
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
const api = require('../../api-action')
|
||||
/**
|
||||
* add group to database
|
||||
* @param {string} txt command body format => groupName notifyEnable(0,1)
|
||||
@@ -45,7 +46,7 @@ const addPage = async (txt, source = {}, db) => {
|
||||
if (!('type' in source) || !('groupId' in source) || !('userId' in source)) return null
|
||||
let {groupId, userId} = source
|
||||
let arr = txt.split(' ')
|
||||
if (arr.length < 3) return null
|
||||
if (arr.length < 2) return null
|
||||
let page = arr[0]
|
||||
let tmpl = arr.slice(1).join(' ')
|
||||
let text = `select "id","owner" from "public"."line_group" where "id" = $1`
|
||||
@@ -113,7 +114,167 @@ const addPage = async (txt, source = {}, db) => {
|
||||
return { reply }
|
||||
}
|
||||
|
||||
/**
|
||||
* del facebook page notify from group
|
||||
* @param {string} txt command body format => pageid tmpl
|
||||
* @param {object} source
|
||||
* @param {object} db
|
||||
*/
|
||||
const delPage = async (txt = '', source = {}, db) => {
|
||||
if (!db) return null
|
||||
if (!('type' in source) || !('groupId' in source) || !('userId' in source)) return null
|
||||
let {groupId, userId} = source
|
||||
let arr = txt.split(' ')
|
||||
if (arr.length < 1) return null
|
||||
let page = arr[0]
|
||||
let text = `select "id","owner" from "public"."line_group" where "id" = $1`
|
||||
let values = [groupId]
|
||||
let result = await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
if (result.rowCount === 0) {
|
||||
let reply = 'group not register'
|
||||
return { reply }
|
||||
}
|
||||
if (result.rows[0].owner !== userId) {
|
||||
let reply = 'not owner'
|
||||
return { reply }
|
||||
}
|
||||
|
||||
text = `select count(rt.*) as c from "public"."line_fb_rt" rt
|
||||
left join "public"."facebook_page" fb
|
||||
on fb."id" = rt."facebook"
|
||||
where
|
||||
fb."id" = $1`
|
||||
values = [page]
|
||||
let count = await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
if (count.rowCount === 0 || count.rows[0].c === 0) return null
|
||||
|
||||
text = `select rt.* as c from "public"."line_fb_rt" rt
|
||||
left join "public"."facebook_page" fb
|
||||
on fb."id" = rt."facebook"
|
||||
where
|
||||
fb."id" = $1
|
||||
and rt."line" = $2`
|
||||
values = [page, groupId]
|
||||
let rt = await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
if (rt.rowCount === 0) return null
|
||||
if (rt.rowCount == 1 && count.rows[0].c == 1) { // eslint-disable-line
|
||||
let text = `delete from "public"."facebook_page" where "id" = $1`
|
||||
let values = [page]
|
||||
await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
} else {
|
||||
let text = `delete from "public"."line_fb_rt" where "line" = $1 and "facebook" = $2`
|
||||
let values = [groupId, page]
|
||||
await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
reply: 'delete page notify success'
|
||||
}
|
||||
}
|
||||
|
||||
const addTwitch = async (txt = '', source = {}, db) => {
|
||||
if (!db) return null
|
||||
if (!('type' in source) || !('groupId' in source) || !('userId' in source)) return null
|
||||
let {groupId, userId} = source
|
||||
let arr = txt.split(' ')
|
||||
if (arr.length < 3) return null
|
||||
let name = arr[0]
|
||||
let type = arr[1]
|
||||
let tmpl = arr.slice(2).join(' ')
|
||||
|
||||
if (['live'].indexOf(type) < 0) return null
|
||||
|
||||
let text = `select "id","owner" from "public"."line_group" where "id" = $1`
|
||||
let values = [groupId]
|
||||
let result = await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
if (result.rowCount === 0) {
|
||||
let reply = 'group not register'
|
||||
return { reply }
|
||||
}
|
||||
if (result.rows[0].owner !== userId) {
|
||||
let reply = 'not owner'
|
||||
return { reply }
|
||||
}
|
||||
|
||||
let twitchUser = await api.twitch.getUserIDByName(name)
|
||||
if (twitchUser === null) return null
|
||||
|
||||
// check channel count
|
||||
text = `select "id" from "public"."twitch_channel" where "id" = $1`
|
||||
values = [twitchUser.id]
|
||||
let twch = await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
|
||||
if (twch.rowCount === 0) {
|
||||
let text = `insert into "public"."twitch_channel" ("id", "name", "ctime", "mtime") values
|
||||
($1, $2, now(), now())`
|
||||
let values = [twitchUser.id, twitchUser.login]
|
||||
await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
text = `insert into "public"."line_twitch_rt" ("line", "twitch", "tmpl", "type") values
|
||||
($1, $2, $3, $4)`
|
||||
values = [groupId, twitchUser.id, tmpl, type]
|
||||
await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
} else {
|
||||
let text = `select rt.* from "public"."line_twitch_rt" rt
|
||||
left join "public"."twitch_channel" twitch
|
||||
on twitch."id" = rt."twitch"
|
||||
left join "public"."line_group" line
|
||||
on line."id" = rt."line"
|
||||
where
|
||||
line."id" = $1
|
||||
and twitch."id" = $2
|
||||
and rt."type" = $3`
|
||||
let values = [groupId, twitchUser.id, type]
|
||||
let rt = await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
if (rt.rowCount === 0) {
|
||||
let text = `insert into "public"."line_twitch_rt" ("line", "twitch", "tmpl", "type") values
|
||||
($1, $2, $3, $4)`
|
||||
let values = [groupId, twitchUser.id, tmpl, type]
|
||||
await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// api.twitch.registerWebHook(twitchUser.id, type)
|
||||
return {
|
||||
reply: 'add channel success'
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
addgroup: addGroup,
|
||||
addpage: addPage
|
||||
addpage: addPage,
|
||||
delpage: delPage,
|
||||
addtwitch: addTwitch
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user