website/pkg/controller/inbox/inbox.go

154 lines
4.4 KiB
Go
Raw Normal View History

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
}
// What view are we looking at? Threads (default), Inbox, or Outbox?
var box = r.FormValue("box")
if box != "inbox" && box != "sent" {
box = "threads"
}
2022-08-14 00:42:42 +00:00
// Are we reading a specific message?
var (
viewThread []*models.Message
threadPager *models.Pagination
composeToUser *models.User
msgId int
)
2022-08-14 00:42:42 +00:00
if uri := ReadURLRegexp.FindStringSubmatch(r.URL.Path); uri != nil {
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)
}
composeToUser = sender
2022-08-14 00:42:42 +00:00
// 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.
var messages []*models.Message
2022-08-14 00:42:42 +00:00
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)
}
// Viewing the threads, or a specific inbox/sent box?
if box == "threads" {
if result, err := models.GetMessageThreads(currentUser, pager); err != nil {
session.FlashError(w, r, "Couldn't get your messages from DB: %s", err)
} else {
messages = result
}
} else {
if result, err := models.GetMessages(currentUser, box == "sent", pager); err != nil {
session.FlashError(w, r, "Couldn't get your messages from DB: %s", err)
} else {
messages = result
}
2022-08-14 00:42:42 +00:00
}
// How many unreads?
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)
}
for _, m := range viewThread {
userIDs = append(userIDs, m.SourceUserID, m.TargetUserID)
2022-08-14 00:42:42 +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,
"Box": box,
2022-08-14 00:42:42 +00:00
"ViewThread": viewThread, // nil on inbox page
"ThreadPager": threadPager,
"ReplyTo": composeToUser,
"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
}
})
}