-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathoffline-messages.go
More file actions
208 lines (173 loc) · 7.79 KB
/
Copy pathoffline-messages.go
File metadata and controls
208 lines (173 loc) · 7.79 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"database/sql"
"encoding/gob"
// "fmt"
"html/template"
"net/http"
"strconv"
"github.com/dustin/go-humanize"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
type OfflineIM struct {
ID string `json:"ID"`
PrincipalID string `json:"PrincipalID"`
Username string `json:"Username"` // will be constructed by getting it from the UserAccounts table
Libravatar string `json:"Libravatar"`
FromID string `json:"FromID"`
Message template.HTML `json:"Message"` // may contain HTML, so it will be sanitised later on (gwyneth 20200815)
TMStamp string `json:"TMStamp"`
}
type OfflineIMList []OfflineIM
const MaxNumberMessages int = 5 // maximum number of messages to retrieve
// For some very, very, very stupid reason, we need to register our message type (and probably others) when starting...
func init() {
gob.RegisterName("listOfOfflineIMs", OfflineIMList{})
}
// GetTopOfflineMessages will retrieve the top first 5 messages and put it on the session, to avoid constant reloading
func GetTopOfflineMessages(c *gin.Context) {
session := sessions.Default(c)
username := session.Get("Username")
uuid := session.Get("UUID")
if uuid == "" {
config.LogWarn("GetTopOfflineMessages(): No UUID stored; messages for this user cannot get retrieved")
}
if *config["dsn"] == "" {
config.LogFatal("Please configure the DSN for accessing your OpenSimulator database; this application won't work without that")
}
db, err := sql.Open("mysql", *config["dsn"]+"?parseTime=true") // this will allow parsing MySQL timestamps into Time vars; see https://stackoverflow.com/a/46613451/1035977
checkErrFatal(err)
defer db.Close()
// first count how many messages we have, we will need this later.
// According to the Internet, current versions of MariaDB/MySQL are actually much faster doing _two_ queries, one just for counting rows, since it's allegedly optimised; in this case, we can simplify the whole query as well.
var numberMessages int
err = db.QueryRow("SELECT COUNT(*) FROM im_offline WHERE im_offline.PrincipalID = ?", uuid).Scan(&numberMessages)
checkErr(err)
if numberMessages > 0 {
rows, err := db.Query("SELECT ID, im_offline.PrincipalID, FromID, Message, TMStamp, FirstName, LastName, Email FROM im_offline, UserAccounts WHERE im_offline.PrincipalID = ? AND UserAccounts.PrincipalID = im_offline.FromID ORDER BY TMStamp ASC LIMIT ?", uuid, strconv.Itoa(MaxNumberMessages))
checkErr(err)
defer rows.Close()
var (
oneMessage OfflineIM
messages OfflineIMList
firstName, lastName, email, unsafeMessage string
messageTimeStamp sql.NullTime // sql.NullTime will match timestamps with NULLs without crashing; see https://stackoverflow.com/a/60293251/1035977
)
for /* i := 1; */ rows.Next() /* ; i++ */ { // uncomment for special
err = rows.Scan(
&oneMessage.ID,
&oneMessage.PrincipalID,
&oneMessage.FromID,
&unsafeMessage,
&messageTimeStamp,
&firstName,
&lastName,
&email,
)
oneMessage.Message = template.HTML(bluemondaySafeHTML.Sanitize(unsafeMessage))
oneMessage.Username = firstName + " " + lastName
oneMessage.Libravatar = getLibravatar(email, oneMessage.Username, 60)
// do something to the time
if messageTimeStamp.Valid {
oneMessage.TMStamp = humanize.Time(messageTimeStamp.Time)
} else {
oneMessage.TMStamp = ""
}
config.LogTracef("message from user %q <%s> to %q is: %q\n",
oneMessage.Username, email, username, oneMessage.Message)
messages = append(messages, oneMessage)
}
checkErr(err)
config.LogTracef("GetTopOfflineMessages(): All messages for user %q: %+v\n", username, messages)
session.Set("Messages", messages)
session.Set("numberMessages", numberMessages)
} else { // no messages for this user
session.Set("Messages", nil)
session.Set("numberMessages", numberMessages)
}
config.LogDebugf(": GetTopOfflineMessages(): user %q(%s) has %d message(s).\n",
username, uuid, numberMessages)
if err := session.Save(); err != nil {
config.LogWarnf("GetTopOfflineMessages(): Could not save messages to user %q on the session, error was: %q\n", username, err)
}
}
// getOfflineMessages opens the template for offline messages and fills it with all data.
// It's conceptually similar to the above code, only using DataTables and its wn template instead.
func getOfflineMessages(c *gin.Context) {
session := sessions.Default(c)
username := session.Get("Username").(string)
uuid := session.Get("UUID").(string)
if uuid == "" {
config.LogWarn("getOfflineMessages(): No UUID stored; messages for this user cannot get retrieved")
}
if *config["dsn"] == "" {
config.LogFatal("Please configure the DSN for accessing your OpenSimulator database; this application won't work without that")
}
db, err := sql.Open("mysql", *config["dsn"]+"?parseTime=true") // this will allow parsing MySQL timestamps into Time vars; see https://stackoverflow.com/a/46613451/1035977
checkErrFatal(err)
defer db.Close()
// first count how many messages we have, we will need this later.
// According to the Internet, current versions of MariaDB/MySQL are actually much faster doing _two_ queries, one just for counting rows, since it's allegedly optimised; in this case, we can simplify the whole query as well.
var numberMessages int
err = db.QueryRow("SELECT COUNT(*) FROM im_offline WHERE im_offline.PrincipalID = ?", uuid).Scan(&numberMessages)
checkErr(err)
if numberMessages > 0 {
rows, err := db.Query("SELECT ID, im_offline.PrincipalID, FromID, Message, TMStamp, FirstName, LastName, Email FROM im_offline, UserAccounts WHERE im_offline.PrincipalID = ? AND UserAccounts.PrincipalID = im_offline.FromID ORDER BY TMStamp ASC", uuid)
checkErr(err)
defer rows.Close()
var (
oneMessage OfflineIM
messages OfflineIMList
firstName, lastName, email, unsafeMessage string
messageTimeStamp sql.NullTime // sql.NullTime will match timestamps with NULLs without crashing; see https://stackoverflow.com/a/60293251/1035977
)
for rows.Next() { // uncomment for special
err = rows.Scan(
&oneMessage.ID,
&oneMessage.PrincipalID,
&oneMessage.FromID,
&unsafeMessage,
&messageTimeStamp,
&firstName,
&lastName,
&email,
)
oneMessage.Message = template.HTML(bluemondaySafeHTML.Sanitize(unsafeMessage))
oneMessage.Username = firstName + " " + lastName
oneMessage.Libravatar = getLibravatar(email, oneMessage.Username, 60)
// do something to the time
if messageTimeStamp.Valid {
oneMessage.TMStamp = humanize.Time(messageTimeStamp.Time)
} else {
oneMessage.TMStamp = ""
}
config.LogTracef("message from user %q <%s> to %q is: %q\n",
oneMessage.Username, email, username, oneMessage.Message)
messages = append(messages, oneMessage)
}
checkErr(err)
config.LogTracef("getOfflineMessages(): All messages for user %q: %+v\n", username, messages)
config.LogDebugf("getOfflineMessages(): user %q(%s) has %d message(s).\n",
username, uuid, numberMessages)
c.HTML(http.StatusOK, "tables.tpl", environment(c, gin.H{
"needsTables": false,
"needsMap": false,
"moreValidation": true,
"Debug": false,
"titleCommon": *config["titleCommon"] + "Offline Messages for: " + username,
"offlineMessages": messages,
}))
return
}
c.HTML(http.StatusOK, "generic.tpl", environment(c,
gin.H{
"needsTables": false,
"needsMap": false,
"moreValidation": true,
"Debug": false,
"titleCommon": *config["titleCommon"] + "Offline Messages for: " + username,
"title": "Offline Messages",
"content": "Good news! You have no pending offline messages to read!",
}))
}