mtfosbot/router/api/line.go

292 lines
5.3 KiB
Go
Raw Permalink Normal View History

2018-10-14 13:16:58 +00:00
package api
import (
2018-10-15 08:39:06 +00:00
"fmt"
2018-12-11 03:36:52 +00:00
"log"
2018-10-14 13:16:58 +00:00
"strconv"
"git.trj.tw/golang/mtfosbot/model"
2019-03-29 14:19:39 +00:00
"git.trj.tw/golang/mtfosbot/module/apis/line"
2018-10-14 13:16:58 +00:00
"git.trj.tw/golang/mtfosbot/module/context"
"git.trj.tw/golang/mtfosbot/module/utils"
)
2019-03-29 14:19:39 +00:00
// PushMessage -
func PushLineMessage(c *context.Context) {
bodyData := struct {
Group string `json:"group,required"`
Message string `json:"message,required"`
}{}
err := c.BindData(&bodyData)
if err != nil {
c.DataFormat(nil)
return
}
textObj := line.TextMessage{}
textObj.Text = bodyData.Message
2019-06-25 09:02:03 +00:00
group, err := model.GetLineGroup(bodyData.Group)
if err != nil {
c.ServerError(nil)
log.Println("get group :: ", err)
return
}
bot, err := group.GetBot()
if err != nil || bot == nil {
log.Println("get group binding bot fail :: ", err)
c.ServerError(nil)
return
}
line.PushMessage(bot.AccessToken, bodyData.Group, textObj)
2019-03-29 14:19:39 +00:00
c.Success(nil)
}
2018-10-14 13:16:58 +00:00
// GetLineList -
func GetLineList(c *context.Context) {
ls, err := model.GetLineGroupList()
if err != nil {
c.ServerError(nil)
return
}
list := make([]map[string]interface{}, 0)
if ls != nil {
2018-10-14 13:26:18 +00:00
for _, v := range ls {
if v == nil {
continue
}
2018-10-14 13:16:58 +00:00
list = append(list, utils.ToMap(v))
}
}
c.Success(map[string]interface{}{
"list": list,
})
}
// GetLineMessageLog -
func GetLineMessageLog(c *context.Context) {
numP := 1
if p, ok := c.GetQuery("p"); ok {
if i, err := strconv.Atoi(p); err == nil {
numP = i
}
}
numMax := 20
if max, ok := c.GetQuery("max"); ok {
if m, err := strconv.Atoi(max); err == nil {
numMax = m
}
}
g := c.DefaultQuery("group", "")
u := c.DefaultQuery("user", "")
2018-12-05 12:35:24 +00:00
order := c.DefaultQuery("order", "desc")
if order != "asc" && order != "desc" {
order = "desc"
}
2018-10-14 13:16:58 +00:00
2018-10-15 08:36:06 +00:00
where := make(map[string]string)
if len(g) > 0 {
where["group"] = g
}
if len(u) > 0 {
where["user"] = u
}
count, err := model.GetLineMessageLogCount(where)
2018-10-14 13:16:58 +00:00
if err != nil {
2018-10-15 08:39:06 +00:00
fmt.Println("count error :::: ", err)
2018-10-14 13:16:58 +00:00
c.ServerError(nil)
return
}
page := utils.CalcPage(count, numP, numMax)
2018-12-05 12:35:24 +00:00
if numP == -1 {
numP = page.Total
}
page = utils.CalcPage(count, numP, numMax)
2018-10-14 13:16:58 +00:00
2018-12-05 12:35:24 +00:00
logs, err := model.GetLineMessageLogList(g, u, page.Offset, page.Limit, order)
2018-10-14 13:16:58 +00:00
if err != nil {
c.ServerError(nil)
return
}
2018-12-10 07:13:06 +00:00
resMap := make([]map[string]interface{}, len(logs))
2018-10-14 13:16:58 +00:00
2018-12-13 02:02:05 +00:00
for k, v := range logs {
2018-10-14 13:16:58 +00:00
m := utils.ToMap(v.LineMessageLog)
m["group_name"] = v.GroupName
m["user_name"] = v.UserName
2018-12-13 02:02:05 +00:00
resMap[k] = m
2018-10-14 13:16:58 +00:00
}
c.Success(map[string]interface{}{
"list": resMap,
"page": map[string]interface{}{
"cur": page.Page,
"total": page.Total,
},
})
}
2018-12-06 09:44:37 +00:00
2018-12-10 07:13:06 +00:00
// GetCommandList -
2018-12-06 09:44:37 +00:00
func GetCommandList(c *context.Context) {
numP := 1
if p, ok := c.GetQuery("p"); ok {
if i, err := strconv.Atoi(p); err == nil {
numP = i
}
}
numMax := 20
if max, ok := c.GetQuery("max"); ok {
if m, err := strconv.Atoi(max); err == nil {
numMax = m
}
}
g := c.DefaultQuery("group", "")
cmd := c.DefaultQuery("cmd", "")
2018-12-10 07:13:06 +00:00
whereMap := make(map[string]string)
if len(g) > 0 {
whereMap["group"] = g
}
if len(cmd) > 0 {
whereMap["cmd"] = cmd
}
count, err := model.GetCommandCount(whereMap)
if err != nil {
2018-12-11 03:36:52 +00:00
log.Println("error :::: ", err)
2018-12-10 07:13:06 +00:00
c.ServerError(nil)
return
}
page := utils.CalcPage(count, numP, numMax)
cmds, err := model.GetCommands(whereMap, page.Offset, page.Limit, nil)
if err != nil {
2018-12-11 03:36:52 +00:00
log.Println("list error :::: ", err)
2018-12-10 07:13:06 +00:00
c.ServerError(nil)
return
}
cmdList := make([]map[string]interface{}, len(cmds))
2018-12-11 03:53:27 +00:00
for k, v := range cmds {
2018-12-11 04:02:57 +00:00
tmp := utils.ToMap(v.Commands)
2018-12-10 07:13:06 +00:00
tmp["group_name"] = v.GroupName
2018-12-11 03:53:27 +00:00
cmdList[k] = tmp
2018-12-10 07:13:06 +00:00
tmp = nil
}
c.Success(map[string]interface{}{
"list": cmdList,
"page": map[string]interface{}{
"cur": page.Page,
"total": page.Total,
},
})
2018-12-06 09:44:37 +00:00
}
2018-12-10 08:11:13 +00:00
// AddLineGroupCommand -
func AddLineGroupCommand(c *context.Context) {
bodyStruct := struct {
Cmd string `json:"cmd"`
Message string `json:"message"`
Group string `json:"group"`
}{}
err := c.BindData(&bodyStruct)
if err != nil {
c.DataFormat(nil)
return
}
if len(bodyStruct.Cmd) == 0 || len(bodyStruct.Message) == 0 {
c.DataFormat(nil)
return
}
cmd, err := model.AddCommand(bodyStruct.Cmd, bodyStruct.Message, bodyStruct.Group)
if err != nil {
c.ServerError(nil)
return
}
c.Success(map[string]interface{}{
"cmd": utils.ToMap(cmd),
})
}
2018-12-10 08:48:22 +00:00
// DeleteLineGroupCommand -
func DeleteLineGroupCommand(c *context.Context) {
cmd, ok := c.Params.Get("cmd")
if !ok {
c.DataFormat(nil)
return
}
g := c.DefaultQuery("group", "")
exist, err := model.CheckCommand(cmd, g)
if err != nil {
c.ServerError(nil)
return
}
if !exist {
2018-12-11 02:17:47 +00:00
c.NotFound("command not found")
2018-12-10 08:48:22 +00:00
return
}
err = model.DeleteCommand(cmd, g)
if err != nil {
c.ServerError(nil)
return
}
c.Success(nil)
}
2018-12-11 02:17:47 +00:00
// EditLineGroupCommand -
func EditLineGroupCommand(c *context.Context) {
bodyStruct := struct {
Message string `json:"message"`
}{}
cmd, ok := c.Params.Get("cmd")
if !ok {
c.DataFormat(nil)
return
}
g := c.DefaultQuery("group", "")
err := c.BindData(&bodyStruct)
if err != nil {
c.DataFormat(nil)
return
}
exist, err := model.CheckCommand(cmd, g)
if err != nil {
c.ServerError(nil)
return
}
if !exist {
c.NotFound("command not found")
return
}
err = model.UpdateCommand(cmd, g, bodyStruct.Message)
if err != nil {
c.ServerError(nil)
return
}
c.Success(nil)
}