2022-08-14 00:42:42 +00:00
|
|
|
package inbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/config"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/models"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/session"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/templates"
|
2022-08-14 00:42:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var ReadURLRegexp = regexp.MustCompile(`^/messages/read/(\d+)$`)
|
|
|
|
|
|
|
|
// Inbox is where users receive direct messages.
|
|
|
|
func Inbox() http.HandlerFunc {
|
|
|
|
tmpl := templates.Must("inbox/inbox.html")
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
currentUser, err := session.CurrentUser(r)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Unexpected error: could not get currentUser.")
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default is inbox, what about sentbox?
|
|
|
|
var showSent = r.FormValue("box") == "sent"
|
|
|
|
|
|
|
|
// Are we reading a specific message?
|
2022-08-21 21:05:08 +00:00
|
|
|
var (
|
|
|
|
viewThread []*models.Message
|
|
|
|
threadPager *models.Pagination
|
|
|
|
composeToUsername string
|
|
|
|
msgId int
|
|
|
|
)
|
2022-08-14 00:42:42 +00:00
|
|
|
if uri := ReadURLRegexp.FindStringSubmatch(r.URL.Path); uri != nil {
|
2022-08-21 21:05:08 +00:00
|
|
|
msgId, _ = strconv.Atoi(uri[1])
|
2022-08-14 00:42:42 +00:00
|
|
|
if msg, err := models.GetMessage(uint64(msgId)); err != nil {
|
|
|
|
session.FlashError(w, r, "Message not found.")
|
|
|
|
templates.Redirect(w, "/messages")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// We must be a party to this thread.
|
|
|
|
if msg.SourceUserID != currentUser.ID && msg.TargetUserID != currentUser.ID {
|
|
|
|
templates.ForbiddenPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the other party in this thread.
|
|
|
|
var senderUserID = msg.SourceUserID
|
|
|
|
if senderUserID == currentUser.ID {
|
|
|
|
senderUserID = msg.TargetUserID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the sender's username to compose a response to them.
|
|
|
|
sender, err := models.GetUser(senderUserID)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't get sender of that message: %s", err)
|
|
|
|
}
|
|
|
|
composeToUsername = sender.Username
|
|
|
|
|
|
|
|
// Get the full chat thread (paginated).
|
|
|
|
threadPager = &models.Pagination{
|
2022-08-14 05:44:57 +00:00
|
|
|
PerPage: config.PageSizeInboxThread,
|
2022-08-14 00:42:42 +00:00
|
|
|
Sort: "created_at desc",
|
|
|
|
}
|
|
|
|
threadPager.ParsePage(r)
|
|
|
|
thread, err := models.GetMessageThread(msg.SourceUserID, msg.TargetUserID, threadPager)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't get chat history: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
viewThread = thread
|
|
|
|
|
|
|
|
// Mark all these messages as read if the recipient sees them.
|
|
|
|
for _, m := range viewThread {
|
|
|
|
if m.TargetUserID == currentUser.ID && !m.Read {
|
|
|
|
m.Read = true
|
|
|
|
if err := m.Save(); err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't mark message as read: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the inbox list of messages.
|
|
|
|
pager := &models.Pagination{
|
|
|
|
Page: 1,
|
2022-08-14 05:44:57 +00:00
|
|
|
PerPage: config.PageSizeInboxList,
|
2022-08-14 00:42:42 +00:00
|
|
|
Sort: "created_at desc",
|
|
|
|
}
|
|
|
|
if viewThread == nil {
|
|
|
|
// On the main inbox view, ?page= params page thru the message list, not a thread.
|
|
|
|
pager.ParsePage(r)
|
|
|
|
}
|
2023-10-22 22:02:24 +00:00
|
|
|
messages, err := models.GetMessages(currentUser, showSent, pager)
|
2022-08-14 00:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't get your messages from DB: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// How many unreads?
|
2023-10-22 22:02:24 +00:00
|
|
|
unread, err := models.CountUnreadMessages(currentUser)
|
2022-08-14 00:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't get your unread message count from DB: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map sender data on these messages.
|
|
|
|
var userIDs = []uint64{}
|
|
|
|
for _, m := range messages {
|
|
|
|
userIDs = append(userIDs, m.SourceUserID, m.TargetUserID)
|
|
|
|
}
|
|
|
|
if viewThread != nil {
|
|
|
|
for _, m := range viewThread {
|
|
|
|
userIDs = append(userIDs, m.SourceUserID, m.TargetUserID)
|
|
|
|
}
|
|
|
|
}
|
2022-09-09 04:42:20 +00:00
|
|
|
userMap, err := models.MapUsers(currentUser, userIDs)
|
2022-08-14 00:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't map users: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var vars = map[string]interface{}{
|
|
|
|
"Messages": messages,
|
|
|
|
"UserMap": userMap,
|
|
|
|
"Unread": unread,
|
|
|
|
"Pager": pager,
|
|
|
|
"IsSentBox": showSent,
|
|
|
|
"ViewThread": viewThread, // nil on inbox page
|
|
|
|
"ThreadPager": threadPager,
|
|
|
|
"ReplyTo": composeToUsername,
|
2022-08-21 21:05:08 +00:00
|
|
|
"MessageID": msgId,
|
2022-08-14 00:42:42 +00:00
|
|
|
}
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|