dkforest

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

commit d833f176d0cb3cea64b64038feecd39ea4a345ba
parent f58d0cd45ff25c6879b13d6c1b1ef38878751bfd
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Wed, 18 Jan 2023 18:34:01 -0800

simplify code

Diffstat:
Mpkg/database/utils/utils.go | 22++++++++++++++++++++++
Mpkg/web/handlers/api/v1/topBarHandler.go | 16++--------------
2 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/pkg/database/utils/utils.go b/pkg/database/utils/utils.go @@ -5,9 +5,12 @@ import ( "dkforest/pkg/database" "dkforest/pkg/managers" "dkforest/pkg/utils" + hutils "dkforest/pkg/web/handlers/utils" "errors" "fmt" + "github.com/labstack/echo" "github.com/sirupsen/logrus" + "net/http" ) func GetZeroUser() database.User { @@ -136,3 +139,22 @@ func kick(kicked, kickedBy database.User, silent, purge bool) error { return nil } + +func GetRoomAndKey(c echo.Context, roomName string) (database.ChatRoom, string, error) { + room, err := database.GetChatRoomByName(roomName) + if err != nil { + return room, "", c.NoContent(http.StatusNotFound) + } + if !room.HasAccess(c) { + return room, "", c.NoContent(http.StatusForbidden) + } + roomKey := "" + if room.IsProtected() { + key, err := hutils.GetRoomKeyCookie(c, int64(room.ID)) + if err != nil { + return room, "", c.NoContent(http.StatusForbidden) + } + roomKey = key.Value + } + return room, roomKey, nil +} diff --git a/pkg/web/handlers/api/v1/topBarHandler.go b/pkg/web/handlers/api/v1/topBarHandler.go @@ -7,7 +7,6 @@ import ( dutils "dkforest/pkg/database/utils" "dkforest/pkg/hashset" "dkforest/pkg/utils" - hutils "dkforest/pkg/web/handlers/utils" "errors" "fmt" "github.com/Depado/bfchroma" @@ -234,20 +233,9 @@ func ChatTopBarHandler(c echo.Context) error { } } - room, err := database.GetChatRoomByName(data.RoomName) + room, roomKey, err := dutils.GetRoomAndKey(c, data.RoomName) if err != nil { - return c.NoContent(http.StatusNotFound) - } - if !room.HasAccess(c) { - return c.NoContent(http.StatusForbidden) - } - roomKey := "" - if room.IsProtected() { - key, err := hutils.GetRoomKeyCookie(c, int64(room.ID)) - if err != nil { - return c.NoContent(http.StatusForbidden) - } - roomKey = key.Value + return err } // If the tutorial is not completed, just render the chat top-bar, no matter what.