dkforest

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

commit 3b8e0616ff6c3fbfe57180d68e83913b593bc8ce
parent a2b13800beb3d28d3107ee703174736ef14f36bf
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Wed, 11 Jan 2023 12:08:23 -0800

cleanup

Diffstat:
Mpkg/web/handlers/chat.go | 54+++++++++++++++++++++++++++++-------------------------
1 file changed, 29 insertions(+), 25 deletions(-)

diff --git a/pkg/web/handlers/chat.go b/pkg/web/handlers/chat.go @@ -47,6 +47,7 @@ func chatHandler(c echo.Context, redRoom bool) error { if err != nil { return c.Redirect(http.StatusFound, "/") } + data.Room = room if authUser != nil { // We display tutorial on official or public rooms @@ -63,26 +64,7 @@ func chatHandler(c echo.Context, redRoom bool) error { } if c.Request().Method == http.MethodPost { - formName := c.Request().PostFormValue("formName") - switch formName { - case "logout": - return handleLogoutPost(c, room) - case "toggle-hb": - return handleToggleHBPost(c, authUser) - case "toggle-m": - return handleToggleMPost(c, authUser) - case "toggle-ignored": - return handleToggleIgnoredPost(c, authUser) - case "afk": - return handleAfkPost(c, authUser) - case "update-read-marker": - return handleUpdateReadMarkerPost(c, room, authUser) - case "tutorialP1", "tutorialP2", "tutorialP3": - return handleTutorialPost(c, data, authUser) - case "chat-password": - return handleChatPasswordPost(c, data, authUser, room) - } - return c.Redirect(http.StatusFound, c.Request().Referer()) + return handlePost(c, data, authUser) } // If you don't have access to the room (room is protected and user is nil or no cookie with the password) @@ -92,7 +74,6 @@ func chatHandler(c echo.Context, redRoom bool) error { } data.IsSubscribed = database.IsUserSubscribedToRoom(authUser.ID, room.ID) - data.Room = room data.IsOfficialRoom = room.IsOfficialRoom() return c.Render(http.StatusOK, "chat", data) } @@ -105,6 +86,29 @@ func getRoomName(c echo.Context) string { return roomName } +func handlePost(c echo.Context, data chatData, authUser *database.User) error { + formName := c.Request().PostFormValue("formName") + switch formName { + case "logout": + return handleLogoutPost(c, data.Room) + case "toggle-hb": + return handleToggleHBPost(c, authUser) + case "toggle-m": + return handleToggleMPost(c, authUser) + case "toggle-ignored": + return handleToggleIgnoredPost(c, authUser) + case "afk": + return handleAfkPost(c, authUser) + case "update-read-marker": + return handleUpdateReadMarkerPost(c, data.Room, authUser) + case "tutorialP1", "tutorialP2", "tutorialP3": + return handleTutorialPost(c, data, authUser) + case "chat-password": + return handleChatPasswordPost(c, data, authUser) + } + return c.Redirect(http.StatusFound, c.Request().Referer()) +} + func handleLogoutPost(c echo.Context, room database.ChatRoom) error { hutils.DeleteRoomCookie(c, int64(room.ID)) return c.Redirect(http.StatusFound, "/chat") @@ -152,7 +156,7 @@ func handleTutorialPost(c echo.Context, data chatData, authUser *database.User) } // Handle POST requests for chat-password, when someone tries to authenticate in a protected room providing a password. -func handleChatPasswordPost(c echo.Context, data chatData, authUser *database.User, room database.ChatRoom) error { +func handleChatPasswordPost(c echo.Context, data chatData, authUser *database.User) error { data.RoomPassword = c.Request().PostFormValue("password") // If no user set, we verify the captcha and username for the guest account @@ -174,7 +178,7 @@ func handleChatPasswordPost(c echo.Context, data chatData, authUser *database.Us // Verify room password is correct key := database.GetRoomDecryptionKey(data.RoomPassword) hashedPassword := database.GetRoomPasswordHash(data.RoomPassword) - if !room.VerifyPasswordHash(hashedPassword) { + if !data.Room.VerifyPasswordHash(hashedPassword) { data.Error = "Invalid room password" return c.Render(http.StatusOK, "chat-password", data) } @@ -193,6 +197,6 @@ func handleChatPasswordPost(c echo.Context, data chatData, authUser *database.Us c.SetCookie(createSessionCookie(session.Token)) } - hutils.CreateRoomCookie(c, int64(room.ID), hashedPassword, key) - return c.Redirect(http.StatusFound, "/chat/"+room.Name) + hutils.CreateRoomCookie(c, int64(data.Room.ID), hashedPassword, key) + return c.Redirect(http.StatusFound, "/chat/"+data.Room.Name) }