-
Notifications
You must be signed in to change notification settings - Fork 3
/
feed-messages.go
126 lines (108 loc) · 5.13 KB
/
feed-messages.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
// These functions deal with the 'feed' table, which I have no idea if it's something standard or not.
// But I'm using it nevertheless, since the code is the same as FeedMessages, and at least I'll give some use to the
// notification area. (gwyneth 20200815)
package main
import (
"database/sql"
"encoding/gob"
// "fmt"
"github.com/dustin/go-humanize"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"html/template"
"log"
"strconv"
)
type FeedMessage struct {
PostParentID string `json:"PostParentID"`
PosterID string `json:"PosterID"` // UUID of poster. Feed messages are seen by everyone.
PostID string `json:"PostID"` // primary key
Username string `json:"Username"` // will be constructed by getting it from the UserAccounts table
Libravatar string `json:"Libravatar"`
PostMarkup template.HTML `json:"PostMarkup"` // actual message. May contain HTML.
Chronostamp string `json:"Chronostamp"`
Visibility int `json:"Visibility"` // Ignored on this implementation
Comment int `json:"Comment"` // Ignored on this implementation
Commentlock string `json:"Commentlock"` // possibly the UUID of the avatar locking this thread for commenting
Editlock string `json:"Editlock"` // possibly the UUID of the avatar locking this message for editing
Feedgroup string `json:"Feedgroup"`
}
type FeedMessageList []FeedMessage
const MaxNumberFeedMessages int = 5 // maximum number of feed 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("listOfFeedMessages", FeedMessageList{})
}
// GetTopFeedMessages will retrieve the top first 5 feed messages and put it on the session, to avoid constant reloading
func GetTopFeedMessages(c *gin.Context) {
session := sessions.Default(c)
username := session.Get("Username")
uuid := session.Get("UUID")
if uuid == "" {
log.Println("[WARN]: GetTopFeedMessages(): No UUID stored; messages for this user cannot get retrieved")
}
if *config["dsn"] == "" {
log.Fatal("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 numberFeedMessages int
err = db.QueryRow("SELECT COUNT(*) FROM feeds").Scan(&numberFeedMessages)
checkErr(err)
if numberFeedMessages > 0 {
rows, err := db.Query("SELECT PostParentID, PosterID, PostID, PostMarkup, Chronostamp, Visibility, Comment, Commentlock, Editlock, Feedgroup, FirstName, LastName, Email FROM feeds, UserAccounts WHERE UserAccounts.PrincipalID = PosterID ORDER BY Chronostamp ASC LIMIT ?", strconv.Itoa(MaxNumberFeedMessages))
checkErr(err)
defer rows.Close()
var (
oneMessage FeedMessage
messages FeedMessageList
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.PostParentID,
&oneMessage.PosterID,
&oneMessage.PostID,
&unsafeMessage,
&messageTimeStamp,
&oneMessage.Visibility,
&oneMessage.Comment,
&oneMessage.Commentlock,
&oneMessage.Editlock,
&oneMessage.Feedgroup,
&firstName,
&lastName,
&email,
)
oneMessage.PostMarkup = 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.Chronostamp = humanize.Time(messageTimeStamp.Time)
} else {
oneMessage.Chronostamp = ""
}
// if *config["ginMode"] == "debug" {
// log.Printf("[DEBUG]: message # %d from user %q <%s> to %q is: %q\n", i, oneMessage.Username, email, username, oneMessage.Message)
// }
messages = append(messages, oneMessage)
}
checkErr(err)
// if *config["ginMode"] == "debug" {
// log.Printf("[DEBUG]: GetTopFeedMessages(): All messages for user %q: %+v\n", username, messages)
// }
session.Set("FeedMessages", messages)
session.Set("numberFeedMessages", numberFeedMessages)
} else { // no messages for this user
session.Set("FeedMessages", nil)
session.Set("numberFeedMessages", numberFeedMessages)
}
if err := session.Save(); err != nil {
log.Printf("[WARN]: GetTopFeedMessages(): Could not save messages to user %q on the session, error was: %q\n", username, err)
}
}