dkforest

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

commit 0687a88d907b6b0a2ffb6be0c72b70797e323e3e
parent 1e9a2ea13d212c877ce579e9cab78e96b6fd2970
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Sun, 15 Jan 2023 19:52:58 -0800

move code

Diffstat:
Mpkg/web/handlers/api/v1/handlers.go | 143+++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 71 insertions(+), 72 deletions(-)

diff --git a/pkg/web/handlers/api/v1/handlers.go b/pkg/web/handlers/api/v1/handlers.go @@ -1,26 +1,25 @@ package v1 import ( + "dkforest/pkg/LeChatPHP/captcha" + mycaptcha "dkforest/pkg/captcha" "dkforest/pkg/config" + "dkforest/pkg/database" dutils "dkforest/pkg/database/utils" "dkforest/pkg/global" "dkforest/pkg/hashset" + "dkforest/pkg/managers" + "dkforest/pkg/utils" + hutils "dkforest/pkg/web/handlers/utils" "errors" "fmt" + "github.com/labstack/echo" + "github.com/sirupsen/logrus" "net/http" "net/url" "regexp" "strings" "time" - - "dkforest/pkg/LeChatPHP/captcha" - mycaptcha "dkforest/pkg/captcha" - "dkforest/pkg/database" - "dkforest/pkg/managers" - "dkforest/pkg/utils" - hutils "dkforest/pkg/web/handlers/utils" - "github.com/labstack/echo" - "github.com/sirupsen/logrus" ) var usernameF = `\w{3,20}` // username (regex Fragment) @@ -176,6 +175,69 @@ func ChatMessagesHandler(c echo.Context) error { return c.Render(http.StatusOK, "chat-messages", data) } +func RoomNotifierHandler(c echo.Context) error { + authUser := c.Get("authUser").(*database.User) + roomID := database.RoomID(utils.DoParseInt64(c.Param("roomID"))) + lastKnownDate := c.Request().PostFormValue("last_known_date") + + room, err := database.GetChatRoomByID(roomID) + if err != nil { + return c.NoContent(http.StatusNotFound) + } + if !room.HasAccess(c) { + return c.NoContent(http.StatusForbidden) + } + + managers.ActiveUsers.UpdateUserInRoom(room, managers.NewUserInfo(*authUser, nil)) + + displayHellbanned := authUser.DisplayHellbanned || authUser.IsHellbanned + msgs, _ := database.GetChatMessages(roomID, authUser.Username, authUser.ID, database.PmNoFilter, false, displayHellbanned, authUser.DisplayIgnored, authUser.DisplayModerators) + if room.IsProtected() { + key, err := hutils.GetRoomKeyCookie(c, int64(room.ID)) + if err != nil { + return c.NoContent(http.StatusForbidden) + } + if err := msgs.DecryptAll(key.Value); err != nil { + return c.NoContent(http.StatusInternalServerError) + } + } + + var data testData + + newMessageSound := false + pmSound := false + taggedSound := false + if len(msgs) > 0 { + if lastKnownMsgDate, err := time.Parse(time.RFC3339Nano, lastKnownDate); err == nil { + for _, msg := range msgs { + if msg.CreatedAt.Truncate(time.Second).After(lastKnownMsgDate.Truncate(time.Second)) { + 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 msg.CreatedAt.Truncate(time.Second).Before(lastKnownMsgDate.Truncate(time.Second)) { + break + } + } + } + lastMsg := msgs[0] + data.LastMessageCreatedAt = 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) +} + func UserHellbanHandler(c echo.Context) error { authUser := c.Get("authUser").(*database.User) if authUser.IsModerator() { @@ -431,66 +493,3 @@ func CaptchaSolverHandler(c echo.Context) error { } return c.JSON(http.StatusOK, map[string]any{"answer": answer}) } - -func RoomNotifierHandler(c echo.Context) error { - authUser := c.Get("authUser").(*database.User) - roomID := database.RoomID(utils.DoParseInt64(c.Param("roomID"))) - lastKnownDate := c.Request().PostFormValue("last_known_date") - - room, err := database.GetChatRoomByID(roomID) - if err != nil { - return c.NoContent(http.StatusNotFound) - } - if !room.HasAccess(c) { - return c.NoContent(http.StatusForbidden) - } - - managers.ActiveUsers.UpdateUserInRoom(room, managers.NewUserInfo(*authUser, nil)) - - displayHellbanned := authUser.DisplayHellbanned || authUser.IsHellbanned - msgs, _ := database.GetChatMessages(roomID, authUser.Username, authUser.ID, database.PmNoFilter, false, displayHellbanned, authUser.DisplayIgnored, authUser.DisplayModerators) - if room.IsProtected() { - key, err := hutils.GetRoomKeyCookie(c, int64(room.ID)) - if err != nil { - return c.NoContent(http.StatusForbidden) - } - if err := msgs.DecryptAll(key.Value); err != nil { - return c.NoContent(http.StatusInternalServerError) - } - } - - var data testData - - newMessageSound := false - pmSound := false - taggedSound := false - if len(msgs) > 0 { - if lastKnownMsgDate, err := time.Parse(time.RFC3339Nano, lastKnownDate); err == nil { - for _, msg := range msgs { - if msg.CreatedAt.Truncate(time.Second).After(lastKnownMsgDate.Truncate(time.Second)) { - 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 msg.CreatedAt.Truncate(time.Second).Before(lastKnownMsgDate.Truncate(time.Second)) { - break - } - } - } - lastMsg := msgs[0] - data.LastMessageCreatedAt = 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) -}