dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 732dcaf0b69cb73610f71fc1a0b0864c94b5ae5a
parent a9540c39808e33590d88e98e02ccb3114a4d517c
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Sun,  5 Feb 2023 14:21:28 -0800

remove duplicated code

Diffstat:
Mpkg/web/handlers/api/v1/handlers.go | 50++++++++++++--------------------------------------
1 file changed, 12 insertions(+), 38 deletions(-)

diff --git a/pkg/web/handlers/api/v1/handlers.go b/pkg/web/handlers/api/v1/handlers.go @@ -188,33 +188,8 @@ func ChatMessagesHandler(c echo.Context) error { if lastKnownDateCookie, err := hutils.GetLastMsgCookie(c, roomName); err == nil { lastKnownDate = lastKnownDateCookie.Value } - newMessageSound := false - pmSound := false - taggedSound := false - if len(msgs) > 0 { - if lastKnownMsgDate, err := time.Parse(time.RFC3339Nano, lastKnownDate); err == nil { - for _, msg := range msgs { - lastKnownDateTrunc := lastKnownMsgDate.Truncate(time.Second) - createdAtTrunc := msg.CreatedAt.Truncate(time.Second) - if createdAtTrunc.After(lastKnownDateTrunc) { - if msg.User.ID != authUser.ID { - newMessageSound = true - if strings.Contains(msg.Message, "@"+authUser.Username) { - taggedSound = true - } - if msg.ToUserID != nil && *msg.ToUserID == authUser.ID { - pmSound = true - } - break - } - } else if createdAtTrunc.Before(lastKnownMsgDate) { - break - } - } - } - lastMsg := msgs[0] - hutils.CreateLastMsgCookie(c, roomName, lastMsg.CreatedAt.Format(time.RFC3339)) - } + newMessageSound, pmSound, taggedSound, lastMessageCreatedAt := shouldPlaySound(authUser, lastKnownDate, msgs) + hutils.CreateLastMsgCookie(c, roomName, lastMessageCreatedAt) if authUser.NotifyNewMessage { data.NewMessageSound = newMessageSound } @@ -263,9 +238,14 @@ func RoomNotifierHandler(c echo.Context) error { var data testData - newMessageSound := false - pmSound := false - taggedSound := false + data.NewMessageSound, data.PmSound, data.TaggedSound, data.LastMessageCreatedAt = shouldPlaySound(authUser, lastKnownDate, msgs) + data.InboxCount = database.GetUserInboxMessagesCount(authUser.ID) + + return c.JSON(http.StatusOK, data) +} + +// Given a "lastKnownDate" and a list of messages, returns what sound notification should be played. +func shouldPlaySound(authUser *database.User, lastKnownDate string, msgs []database.ChatMessage) (newMessageSound, pmSound, taggedSound bool, lastMsgCreatedAt string) { if len(msgs) > 0 { if lastKnownMsgDate, err := time.Parse(time.RFC3339Nano, lastKnownDate); err == nil { for _, msg := range msgs { @@ -288,15 +268,9 @@ func RoomNotifierHandler(c echo.Context) error { } } lastMsg := msgs[0] - data.LastMessageCreatedAt = lastMsg.CreatedAt.Format(time.RFC3339) + lastMsgCreatedAt = lastMsg.CreatedAt.Format(time.RFC3339) } - - data.NewMessageSound = newMessageSound - data.PmSound = pmSound - data.TaggedSound = taggedSound - data.InboxCount = database.GetUserInboxMessagesCount(authUser.ID) - - return c.JSON(http.StatusOK, data) + return newMessageSound, pmSound, taggedSound, lastMsgCreatedAt } func UserHellbanHandler(c echo.Context) error {