From ce2b664c7778915b0d3b7b3c5ba7ea96cf07a2e1 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 28 Jun 2018 18:27:40 +0800 Subject: [PATCH] add del twitch notify command --- libs/line-message/commands/group.js | 87 ++++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 2 deletions(-) diff --git a/libs/line-message/commands/group.js b/libs/line-message/commands/group.js index 633aab2..ab6e0bc 100644 --- a/libs/line-message/commands/group.js +++ b/libs/line-message/commands/group.js @@ -154,7 +154,7 @@ const delPage = async (txt = '', source = {}, db) => { }) if (count.rowCount === 0 || count.rows[0].c === 0) return null - text = `select rt.* as c from "public"."line_fb_rt" rt + text = `select rt.* from "public"."line_fb_rt" rt left join "public"."facebook_page" fb on fb."id" = rt."facebook" where @@ -278,9 +278,92 @@ const addTwitch = async (txt = '', source = {}, db) => { } } +/** + * 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' + } +} + module.exports = { addgroup: addGroup, addpage: addPage, delpage: delPage, - addtwitch: addTwitch + addtwitch: addTwitch, + deltwitch: delTwitch }