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(' ')
|
|
|
|
if (arr.length < 3) return null
|
|
|
|
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 }
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
addgroup: addGroup,
|
|
|
|
addpage: addPage
|
|
|
|
}
|