mtfosbot/libs/line-message/commands/actions/group.js

494 lines
13 KiB
JavaScript
Raw Permalink Normal View History

2018-07-13 05:30:54 +00:00
const api = require('@libs/api-action')
const DB = require('@libs/database')
2018-07-12 16:29:41 +00:00
const axios = require('axios')
2018-06-29 09:59:48 +00:00
async function run (fn = null, txt, source) {
if (!fn || typeof fn !== 'function') return null
let res = null
let db = null
try {
db = await DB.connect()
res = await fn(txt, source, db)
} catch (err) {
console.log(err)
}
if (db !== null && 'release' in db) db.release()
return res
}
2018-06-27 09:50:56 +00:00
/**
* add group to database
* @param {string} txt command body format => groupName notifyEnable(0,1)
* @param {object} source
* @param {object} db
*/
const addGroup = 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 < 2 || !isFinite(arr[1])) return null
let name = arr[0]
let notify = arr[1]
if (typeof notify === 'string') notify = parseInt(notify)
if (notify !== 0 && notify !== 1) return null
let text = `select "id" from "public"."line_group" where "id" = $1`
let values = [groupId]
let result = await db.query({
text,
values
})
if (result.rowCount > 0) {
let reply = 'group exists'
return { reply }
}
text = `insert into "public"."line_group" ("id", "name", "notify", "owner", "ctime", "mtime") values ($1, $2, $3, $4, now(), now())`
values = [groupId, name, (notify === 1), userId]
await db.query({
text,
values
})
let reply = 'add success'
return { reply }
}
/**
* add facebook page notify to group
2018-06-27 15:38:56 +00:00
* @param {string} txt command body format => pageid tmpl
2018-06-27 09:50:56 +00:00
* @param {object} source
* @param {object} db
*/
const addPage = 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(' ')
2018-06-28 09:37:33 +00:00
if (arr.length < 2) return null
2018-06-27 09:50:56 +00:00
let page = arr[0]
2018-06-27 15:38:56 +00:00
let tmpl = arr.slice(1).join(' ')
2018-06-27 09:50:56 +00:00
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 }
}
2018-06-27 15:38:56 +00:00
// check page exists
text = `select "id" from "public"."facebook_page" where "id"=$1`
2018-06-27 09:50:56 +00:00
values = [page]
2018-06-27 15:38:56 +00:00
let fb = await db.query({
2018-06-27 09:50:56 +00:00
text,
values
})
2018-06-27 15:38:56 +00:00
// if no page data insert page data and insert rt
if (fb.rowCount === 0) {
text = `insert into "public"."facebook_page" ("id", "lastpost", "ctime", "mtime") values
($1, '', now(), now())`
values = [page]
await db.query({
text,
values
})
text = `insert into "public"."line_fb_rt" ("line", "facebook", "tmpl") values ($1, $2, $3)`
values = [groupId, page, tmpl]
await db.query({
text,
values
})
} else {
// check rt exists
text = `select rt.* from "public"."line_fb_rt" rt
left join "public"."line_group" line
on line."id" = rt."line"
left join "public"."facebook_page" fb
on fb."id" = rt."facebook"
where
fb."id" = $1
and line."id" = $2`
values = [page, groupId]
let rt = await db.query({
text,
values
})
if (rt.rowCount === 0) {
text = `insert into "public"."line_fb_rt" ("line", "facebook", "tmpl") values ($1, $2, $3)`
values = [groupId, page, tmpl]
await db.query({
text,
values
})
}
2018-06-27 09:50:56 +00:00
}
2018-06-27 15:38:56 +00:00
2018-06-27 09:50:56 +00:00
let reply = 'add page success'
return { reply }
}
2018-06-28 09:37:33 +00:00
/**
* 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
2018-06-28 10:27:40 +00:00
text = `select rt.* from "public"."line_fb_rt" rt
2018-06-28 09:37:33 +00:00
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'
}
}
2018-06-28 09:41:49 +00:00
/**
* add twitch channel notify to group
* @param {string} txt command body format => twitch_user type tmpl
* @param {object} source
* @param {object} db
*/
2018-06-28 09:37:33 +00:00
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 }
}
2018-08-12 15:31:09 +00:00
let twitchUser = await api.twitchPublic.getUserIDByName(name)
2018-06-28 09:37:33 +00:00
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'
}
}
2018-06-28 10:27:40 +00:00
/**
* del twitch channel notify from group
* @param {string} txt command body format => twitch_user type
* @param {object} source
* @param {object} db
*/
const delTwitch = 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 < 2) return null
let twitch = arr[0]
let type = arr[1]
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_twitch_rt" rt
left join "public"."twitch_channel" twitch
on twitch."id" = rt."twitch"
where
twitch."name" = $1`
values = [twitch]
let count = await db.query({
text,
values
})
if (count.rowCount === 0 || count.rows[0].c === 0) return null
text = `select rt.* from "public"."line_twitch_rt" rt
left join "public"."twitch_channel" twitch
on twitch."id" = rt."twitch"
where
twitch."name" = $1
and rt."line" = $2`
values = [twitch, 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"."twitch_channel" where "name" = $1`
let values = [twitch]
await db.query({
text,
values
})
} else {
let text = `select "id" from "public"."twitch_channel" where "name" = $1`
let values = [twitch]
let tc = await db.query({
text,
values
})
if (tc.rowCount > 0) {
let text = `delete from "public"."line_twitch_rt" where "line" = $1 and "type" = $2 and "twitch" = $3`
let values = [groupId, type, tc.rows[0].id]
await db.query({
text,
values
})
}
}
return {
reply: 'delete twitch notify success'
}
}
2018-07-06 09:57:49 +00:00
/**
* add youtube channel notify to group
* @param {string} txt command body format => youtube tmpl
* @param {object} source
* @param {object} db
*/
const addYoutube = 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 < 2) return null
let id = arr[0]
let tmpl = arr.slice(1).join(' ')
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 }
}
2018-07-10 15:48:26 +00:00
console.log('youtube id ::: ', id)
2018-07-06 09:57:49 +00:00
let youtubeName = await api.google.queryYoutubeName(id)
2018-07-10 15:48:26 +00:00
console.log('youtubr name :::: ', youtubeName)
2018-07-06 09:57:49 +00:00
if (youtubeName === null) return null
text = `select "id" from "public"."youtube_channel" where "id" = $1`
values = [id]
let ytch = await db.query({
text,
values
})
if (ytch.rowCount === 0) {
let text = `insert into "public"."youtube_channel" ("id", "name", "lastvideo", "expire", "ctime", "mtime") values
($1, $2, '', -1, now(), now())`
let values = [id, youtubeName]
await db.query({
text, values
})
text = `insert into "public"."line_youtube_rt" ("line", "youtube", "tmpl") values ($1, $2, $3)`
values = [groupId, id, tmpl]
await db.query({
text, values
})
} else {
let text = `insert into "public"."line_youtube_rt" ("line", "youtube", "tmpl") values ($1, $2, $3)`
let values = [groupId, id, tmpl]
await db.query({
text, values
})
}
try {
await api.google.subYoutube(id)
2018-07-10 15:48:26 +00:00
} catch (err) {
console.log(err)
}
2018-07-06 09:57:49 +00:00
return {
reply: 'add youtube channel success'
}
}
2018-07-12 16:29:41 +00:00
const image = async (txt, source, db) => {
if (typeof txt !== 'string' || txt.trim().length === 0) return null
let imgs = txt.split(';')
if (imgs.length !== 2) return null
try {
await axios({
url: imgs[0],
method: 'head'
})
await axios({
url: imgs[1],
method: 'head'
})
} catch (err) {
console.log(err)
return null
}
return {
reply: `$image$${txt}`
}
}
2018-07-02 15:02:48 +00:00
const hello = async () => {
return {reply: 'Hello World'}
2018-07-02 15:02:48 +00:00
}
2018-08-07 05:42:55 +00:00
const roll = async () => {
let r = Math.floor(Math.random() * 10000)
return {reply: r}
}
2018-06-27 09:50:56 +00:00
module.exports = {
2018-06-29 09:59:48 +00:00
addgroup: run.bind(null, addGroup),
addpage: run.bind(null, addPage),
delpage: run.bind(null, delPage),
addtwitch: run.bind(null, addTwitch),
2018-07-02 15:02:48 +00:00
deltwitch: run.bind(null, delTwitch),
2018-07-06 09:57:49 +00:00
addyoutube: run.bind(null, addYoutube),
2018-07-12 16:29:41 +00:00
hello: run.bind(null, hello),
2018-08-07 05:42:55 +00:00
image: run.bind(null, image),
roll: run.bind(null, roll)
2018-06-27 09:50:56 +00:00
}