forked from Alex13658/SchoolServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forum.go
123 lines (120 loc) · 4.98 KB
/
forum.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
// forum
package restapi
import (
"encoding/json"
"net/http"
"strconv"
"strings"
)
// getForumRequest используется в GetForumHandler
type getForumRequest struct {
Page int `json:"page"`
}
// GetForumHandler обрабатывает запросы на получение тем форума
func (rest *RestAPI) GetForumHandler(respwr http.ResponseWriter, req *http.Request) {
rest.logger.Info("REST: GetForumHandler called", "IP", req.RemoteAddr)
// Проверка метода запроса
if req.Method != "POST" {
rest.logger.Info("REST: Wrong method", "Method", req.Method, "IP", req.RemoteAddr)
respwr.WriteHeader(http.StatusMethodNotAllowed)
return
}
// Получить существующие имя и объект локальной сессии
sessionName, session := rest.getLocalSession(respwr, req)
if session == nil {
return
}
// Чтение запроса от клиента
var rReq getForumRequest
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(&rReq)
if err != nil {
rest.logger.Info("REST: Malformed request data", "Error", err.Error(), "IP", req.RemoteAddr)
respwr.WriteHeader(http.StatusBadRequest)
status, err := respwr.Write(rest.Errors.MalformedData)
if err != nil {
rest.logger.Error("REST: Error occured when sending response", "Error", err, "Status", status, "IP", req.RemoteAddr)
} else {
rest.logger.Info("REST: Successfully sent response", "IP", req.RemoteAddr)
}
return
}
// Распечатаем запрос от клиента
rest.logger.Info("REST: Request data", "Data", rReq, "IP", req.RemoteAddr)
// Получим удаленную сессию
remoteSession, ok := rest.sessionsMap[sessionName]
if !ok {
// Если нет удаленной сессии
rest.logger.Info("REST: No remote session", "IP", req.RemoteAddr)
// Создать новую
remoteSession = rest.remoteRelogin(respwr, req, session)
if remoteSession == nil {
return
}
}
// Сходить по удаленной сессии
forumThemes, err := remoteSession.GetForumThemesList(strconv.Itoa(rReq.Page))
if err != nil {
if strings.Contains(err.Error(), "You was logged out from server") {
// Если удаленная сессия есть, но не активна
rest.logger.Info("REST: Remote connection timed out", "IP", req.RemoteAddr)
// Создать новую
remoteSession = rest.remoteRelogin(respwr, req, session)
if remoteSession == nil {
return
}
// Повторно получить с сайта школы
forumThemes, err = remoteSession.GetForumThemesList(strconv.Itoa(rReq.Page))
if err != nil {
// Ошибка
rest.logger.Error("REST: Error occured when getting data from site", "Error", err, "IP", req.RemoteAddr)
respwr.WriteHeader(http.StatusBadGateway)
return
}
} else if strings.Contains(err.Error(), "current page number is more than max page number") {
// Запрашиваемая страница не существует
rest.logger.Info("REST: Invalid page number", "Error", err.Error(), "Page", rReq.Page, "IP", req.RemoteAddr)
respwr.WriteHeader(http.StatusBadRequest)
status, err := respwr.Write(rest.Errors.InvalidPage)
if err != nil {
rest.logger.Error("REST: Error occured when sending response", "Error", err, "Status", status, "IP", req.RemoteAddr)
} else {
rest.logger.Info("REST: Successfully sent response", "IP", req.RemoteAddr)
}
return
} else {
// Другая ошибка
rest.logger.Error("REST: Error occured when getting data from site", "Error", err, "IP", req.RemoteAddr)
respwr.WriteHeader(http.StatusBadGateway)
return
}
}
// Сравнить с БД
userName := session.Values["userName"].(string)
schoolID := session.Values["schoolID"].(int)
err = rest.Db.UpdateTopicsStatuses(userName, schoolID, forumThemes)
if err != nil {
rest.logger.Error("REST: Error occured when updating statuses for forum themes", "Error", err, "IP", req.RemoteAddr)
respwr.WriteHeader(http.StatusInternalServerError)
return
}
// Закодировать ответ в JSON
bytes, err := json.Marshal(forumThemes)
if err != nil {
rest.logger.Error("REST: Error occured when marshalling response", "Error", err, "Response", forumThemes, "IP", req.RemoteAddr)
respwr.WriteHeader(http.StatusInternalServerError)
return
}
// Отправить ответ клиенту
status, err := respwr.Write(bytes)
if err != nil {
rest.logger.Error("REST: Error occured when sending response", "Error", err, "Response", forumThemes, "Status", status, "IP", req.RemoteAddr)
} else {
rest.logger.Info("REST: Successfully sent response", "Response", forumThemes, "IP", req.RemoteAddr)
}
// Отправить пуш на удаление пушей с сообщениями форума
err = rest.pushDelete(userName, schoolID, "forum_new_message")
if err != nil {
rest.logger.Error("REST: Error occured when sending deleting push", "Error", err, "Category", "forum_new_message", "IP", req.RemoteAddr)
}
}