/
notification.go
146 lines (129 loc) · 3.17 KB
/
notification.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package notification
import (
"encoding/json"
"strconv"
"github.com/Qihoo360/wayne/src/backend/controllers/base"
"github.com/Qihoo360/wayne/src/backend/models"
"github.com/Qihoo360/wayne/src/backend/util/logs"
)
type NotificationController struct {
base.APIController
}
func (c *NotificationController) URLMapping() {
c.Mapping("List", c.List)
c.Mapping("Create", c.Create)
c.Mapping("Publish", c.Publish)
c.Mapping("Subscribe", c.Subscribe)
c.Mapping("Read", c.Read)
}
func (c *NotificationController) Prepare() {
c.APIController.Prepare()
_, method := c.GetControllerAndAction()
switch method {
case "List", "Create", "Publish":
if !c.User.Admin {
// 只有管理员才能查看、创建和广播消息
c.AbortForbidden("operation need admin permission.")
}
default:
}
}
// 列出
// @router / [get]
func (c *NotificationController) List() {
param := c.BuildQueryParam()
notifications := []models.Notification{}
total, err := models.GetTotal(new(models.Notification), param)
if err != nil {
logs.Error("get total count by param (%s) error. %v", param, err)
c.HandleError(err)
return
}
param.Relate = "all"
err = models.GetAll(new(models.Notification), ¬ifications, param)
if err != nil {
logs.Error("list by param (%s) error. %v", param, err)
c.HandleError(err)
return
}
c.Success(param.NewPage(total, notifications))
}
// 创建
// @router / [post]
func (c *NotificationController) Create() {
var notify models.Notification
err := json.Unmarshal(c.Ctx.Input.RequestBody, ¬ify)
if err != nil {
logs.Error("get body error. %v", err)
c.AbortBadRequestFormat("Notification")
}
notify.FromUser = c.User
_, err = models.NotificationModel.Add(¬ify)
if err != nil {
logs.Error("create error.%v", err.Error())
c.HandleError(err)
return
}
c.Success(notify)
}
// 发布
// @router / [put]
func (c *NotificationController) Publish() {
id := c.Input().Get("id")
id64, err := strconv.ParseInt(id, 10, 64)
if err != nil {
c.HandleError(err)
return
}
err = models.NotificationLogModel.AddToAllUser(id64)
if err != nil {
c.HandleError(err)
return
}
_ = models.NotificationModel.UpdateById(&models.Notification{Id: id64, IsPublished: true})
c.Success(nil)
}
// 用户订阅
// @router /subscribe [get]
func (c *NotificationController) Subscribe() {
param := c.BuildQueryParam()
param.Query["user_id"] = c.User.Id
IsReaded := c.Input().Get("is_readed")
if IsReaded != "" {
r, err := strconv.ParseBool(IsReaded)
if err != nil {
c.HandleError(err)
return
}
param.Query["is_readed"] = r
}
param.Relate = "all"
notificationlogs := []models.NotificationLog{}
err := models.GetAll(new(models.NotificationLog), ¬ificationlogs, param)
if err != nil {
c.HandleError(err)
return
}
c.Success(notificationlogs)
}
// 用户已读
// @router /subscribe [put]
func (c *NotificationController) Read() {
id := c.Input().Get("id")
id64, err := strconv.ParseInt(id, 10, 64)
if err != nil {
c.HandleError(err)
return
}
l := models.NotificationLog{
UserId: c.User.Id,
Id: id64,
IsReaded: true,
}
err = models.NotificationLogModel.UpdateByUserId(&l)
if err != nil {
c.HandleError(err)
return
}
c.Success(nil)
}