forked from Ptt-official-app/Ptt-backend
/
route_users.go
163 lines (134 loc) · 4 KB
/
route_users.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
package main
import (
"github.com/PichuChen/go-bbs"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
)
func routeUsers(w http.ResponseWriter, r *http.Request) {
// TODO: Check IP Flowspeed
if r.Method == "GET" {
getUsers(w, r)
return
}
}
func getUsers(w http.ResponseWriter, r *http.Request) {
userId, item, err := parseUserPath(r.URL.Path)
if item == "information" {
getUserInformation(w, r, userId)
return
} else if item == "favorites" {
getUserFavorites(w, r, userId)
return
}
// else
logger.Noticef("user id: %v not exist but be queried, info: %v err: %v", userId, item, err)
w.WriteHeader(http.StatusNotFound)
}
func getUserInformation(w http.ResponseWriter, r *http.Request, userId string) {
token := getTokenFromRequest(r)
err := checkTokenPermission(token,
[]permission{PermissionReadUserInformation},
map[string]string{
"user_id": userId,
})
if err != nil {
// TODO: record unauthorized access
w.WriteHeader(http.StatusUnauthorized)
return
}
userrec, err := findUserecById(userId)
if err != nil {
// TODO: record error
w.WriteHeader(http.StatusInternalServerError)
m := map[string]string{
"error": "find_userrec_error",
"error_description": "get userrec for " + userId + " failed",
}
b, _ := json.MarshalIndent(m, "", " ")
w.Write(b)
return
}
// TODO: Check Etag or Not-Modified for cache
dataMap := map[string]interface{}{
"user_id": userrec.UserId(),
"nickname": userrec.Nickname(),
"realname": userrec.RealName(),
"number_of_login_days": fmt.Sprintf("%d", userrec.NumLoginDays()),
"number_of_posts": fmt.Sprintf("%d", userrec.NumPosts()),
// "number_of_badposts": fmt.Sprintf("%d", userrec.NumLoginDays),
"money": fmt.Sprintf("%d", userrec.Money()),
"last_login_time": userrec.LastLogin().Format(time.RFC3339),
"last_login_ipv4": userrec.LastHost(),
"last_login_ip": userrec.LastHost(),
// "last_login_country": fmt.Sprintf("%d", userrec.NumLoginDays),
"chess_status": map[string]interface{}{},
"plan": map[string]interface{}{},
}
responseMap := map[string]interface{}{
"data": dataMap,
}
responseByte, _ := json.MarshalIndent(responseMap, "", " ")
w.Write(responseByte)
}
func getUserFavorites(w http.ResponseWriter, r *http.Request, userId string) {
token := getTokenFromRequest(r)
err := checkTokenPermission(token,
[]permission{PermissionReadUserInformation},
map[string]string{
"user_id": userId,
})
if err != nil {
// TODO: record unauthorized access
w.WriteHeader(http.StatusUnauthorized)
return
}
recs, err := db.ReadUserFavoriteRecords(userId)
logger.Debugf("file items length: %v", len(recs))
// dataMap := map[string]interface{}{}
dataItems := parseFavoriteFolderItem(recs)
responseMap := map[string]interface{}{
"data": map[string]interface{}{
"items": dataItems,
},
}
responseByte, _ := json.MarshalIndent(responseMap, "", " ")
w.Write(responseByte)
}
func parseFavoriteFolderItem(recs []bbs.FavoriteRecord) []interface{} {
dataItems := []interface{}{}
for _, item := range recs {
logger.Debugf("fav type: %v", item.Type())
switch item.Type() {
case bbs.FavoriteTypeBoard:
dataItems = append(dataItems, map[string]interface{}{
"type": "board",
"board_id": item.BoardId(),
})
case bbs.FavoriteTypeFolder:
dataItems = append(dataItems, map[string]interface{}{
"type": "folder",
"title": item.Title(),
"items": parseFavoriteFolderItem(item.Records()),
})
case bbs.FavoriteTypeLine:
dataItems = append(dataItems, map[string]interface{}{
"type": "line",
})
default:
logger.Warningf("parseFavoriteFolderItem unknown favItem type")
}
}
return dataItems
}
func parseUserPath(path string) (userId string, item string, err error) {
pathSegment := strings.Split(path, "/")
// /{{version}}/users/{{user_id}}/{{item}}
if len(pathSegment) == 4 {
// /{{version}}/users/{{user_id}}
return pathSegment[3], "", nil
}
return pathSegment[3], pathSegment[4], nil
}