-
Notifications
You must be signed in to change notification settings - Fork 2
/
notification.go
168 lines (163 loc) · 5.39 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package handlers
import (
"encoding/json"
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/jafarlihi/symposium/api/config"
"github.com/jafarlihi/symposium/api/models"
"github.com/jafarlihi/symposium/api/repositories"
"io"
"net/http"
"strconv"
"strings"
)
func GetNotifications(w http.ResponseWriter, r *http.Request) {
queryParams := r.URL.Query()
if len(queryParams["page"]) == 0 || queryParams["page"][0] == "" || len(queryParams["pageSize"]) == 0 || queryParams["pageSize"][0] == "" {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "page and/or pageSize query parameters are missing"}`)
return
}
page, err := strconv.ParseUint(queryParams["page"][0], 10, 32)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "page query parameter couldn't be parsed as an integer"}`)
return
}
pageSize, err := strconv.ParseUint(queryParams["pageSize"][0], 10, 32)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "pageSize query parameter couldn't be parsed as an integer"}`)
return
}
tokenHeader := r.Header.Get("Authorization")
tokenFields := strings.Fields(tokenHeader)
if len(tokenFields) != 2 {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error: "Token is missing"}`)
return
}
tokenString := tokenFields[1]
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return []byte(config.Config.Jwt.SigningSecret), nil
})
if err != nil {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Failed to parse the token"}`)
return
}
var userID float64
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
userID = claims["userID"].(float64)
} else {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Invalid token"}`)
return
}
var notifications []*models.Notification
notifications, err = repositories.GetNotificationsByUserID(uint32(userID), uint32(page), uint32(pageSize))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to get the notifications"}`)
return
}
jsonResult, err := json.Marshal(notifications)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to marshal the result to JSON"}`)
return
}
w.WriteHeader(http.StatusOK)
io.WriteString(w, string(jsonResult))
}
func GetUnseenNotificationCount(w http.ResponseWriter, r *http.Request) {
tokenHeader := r.Header.Get("Authorization")
tokenFields := strings.Fields(tokenHeader)
if len(tokenFields) != 2 {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error: "Token is missing"}`)
return
}
tokenString := tokenFields[1]
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return []byte(config.Config.Jwt.SigningSecret), nil
})
if err != nil {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Failed to parse the token"}`)
return
}
var userID float64
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
userID = claims["userID"].(float64)
} else {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Invalid token"}`)
return
}
count, err := repositories.GetUnseenNotificationCountByUserID(uint32(userID))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to get the notifications"}`)
return
}
w.WriteHeader(http.StatusOK)
io.WriteString(w, "{\"count\": "+strconv.Itoa(count)+"}")
}
type markNotificationsSeenRequest struct {
IDs []int `json:"ids"`
}
func MarkNotificationsSeen(w http.ResponseWriter, r *http.Request) {
tokenHeader := r.Header.Get("Authorization")
tokenFields := strings.Fields(tokenHeader)
if len(tokenFields) != 2 {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error: "Token is missing"}`)
return
}
tokenString := tokenFields[1]
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return []byte(config.Config.Jwt.SigningSecret), nil
})
if err != nil {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Failed to parse the token"}`)
return
}
var userID float64
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
userID = claims["userID"].(float64)
} else {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Invalid token"}`)
return
}
var mnsr markNotificationsSeenRequest
err = json.NewDecoder(r.Body).Decode(&mnsr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Request body couldn't be parsed as JSON"}`)
return
}
if len(mnsr.IDs) == 0 {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "IDs field is missing or is empty"}`)
return
}
err = repositories.MarkNotificationsSeen(uint32(userID), mnsr.IDs)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to mark notifications seen"}`)
return
}
w.WriteHeader(http.StatusOK)
}