commit 0f1c80b73b3a5489648e6ea33a27bf15f503ca44
parent 2e5a22179f9ddbcdf1d057cd5df71e8aee059d12
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 30 Nov 2023 04:15:47 -0500
use hashmaps to matches slash commands, and avoid hundreds of useless conditions
Diffstat:
1 file changed, 166 insertions(+), 145 deletions(-)
diff --git a/pkg/web/handlers/interceptors/slashInterceptor.go b/pkg/web/handlers/interceptors/slashInterceptor.go
@@ -40,6 +40,104 @@ import (
// and a red message will appear beside the text box.
type SlashInterceptor struct{}
+type CmdHandler func(c *command.Command) (handled bool)
+
+var userCmdsMap = map[string]CmdHandler{
+ "/i": handleIgnoreCmd,
+ "/ignore": handleIgnoreCmd,
+ "/ui": handleUnIgnoreCmd,
+ "/unignore": handleUnIgnoreCmd,
+ "/toggle-autocomplete": handleToggleAutocomplete,
+ "/tuto": handleTutorialCmd,
+ "/d": handleDeleteMsgCmd,
+ "/hide": handleHideMsgCmd,
+ "/unhide": handleUnHideMsgCmd,
+ "/pmwhitelist": handleListPmWhitelistCmd,
+ "/setpmmode": handleSetPmModeCmd,
+ "/pmb": handleTogglePmBlacklistedUser,
+ "/pmw": handleTogglePmWhitelistedUser,
+ "/g": handleGroupChatCmd,
+ "/me": handleMeCmd,
+ "/e": handleEditCmd,
+ "/pm": handlePMCmd,
+ "/subscribe": handleSubscribeCmd,
+ "/unsubscribe": handleUnsubscribeCmd,
+ "/p": handleProfileCmd,
+ "/inbox": handleInboxCmd,
+ "/chess": handleChessCmd,
+ "/hbm": handleHbmCmd,
+ "/hbmt": handleHbmtCmd,
+ "/token": handleTokenCmd,
+ "/md5": handleMd5Cmd,
+ "/sha1": handleSha1Cmd,
+ "/sha256": handleSha256Cmd,
+ "/sha512": handleSha512Cmd,
+ "/dice": handleDiceCmd,
+ "/rand": handleRandCmd,
+ "/choice": handleChoiceCmd,
+ "/memes": handleListMemes,
+ "/success": handleSuccessCmd,
+ "/afk": handleAfkCmd,
+ "/date": handleDateCmd,
+ "/r": handleUpdateReadMarkerCmd,
+ "/code": handleCodeCmd,
+ "/locate": handleLocateCmd,
+ "/error": handleErrorCmd,
+}
+
+var privateRoomCmdsMap = map[string]CmdHandler{
+ "/mode": handleGetModeCmd,
+ "/wl": handleWhitelistCmd,
+ "/whitelist": handleWhitelistCmd,
+}
+
+var privateRoomOwnerCmdsMap = map[string]CmdHandler{
+ "/addgroup": handleAddGroupCmd,
+ "/rmgroup": handleRmGroupCmd,
+ "/glock": handleLockGroupCmd,
+ "/gunlock": handleUnlockGroupCmd,
+ "/gusers": handleGroupUsersCmd,
+ "/groups": handleListGroupsCmd,
+ "/gadduser": handleGroupAddUserCmd,
+ "/grmuser": handleGroupRmUserCmd,
+ "/mode": handleSetModeCmd,
+ "/ro": handleToggleReadOnlyCmd,
+ "/wl": handleGetRoomWhitelistCmd,
+ "/whitelist": handleGetRoomWhitelistCmd,
+}
+
+var moderatorCmdsMap = map[string]CmdHandler{
+ "/m": handleModeratorGroupCmd,
+ "/n": handleModeratorGroupCmd,
+ "/moderators": handleListModeratorsCmd,
+ "/mods": handleListModeratorsCmd,
+ "/k": handleKickCmd,
+ "/kick": handleKickCmd,
+ "/kk": handleKickKeepCmd,
+ "/ks": handleKickSilentCmd,
+ "/kks": handleKickKeepSilentCmd,
+ "/uk": handleUnkickCmd,
+ "/unkick": handleUnkickCmd,
+ "/logout": handleLogoutCmd,
+ "/captcha": handleForceCaptchaCmd,
+ "/rtuto": handleResetTutorialCmd,
+ "/hb": handleHellbanCmd,
+ "/hellban": handleHellbanCmd,
+ "/unhellban": handleUnhellbanCmd,
+ "/uhb": handleUnhellbanCmd,
+}
+
+var adminCmdsMap = map[string]CmdHandler{
+ "/sys": handleSystemCmd,
+ "/system": handleSystemCmd,
+ "/seturl": handleSetChatRoomExternalLink,
+ "/purge": handlePurge,
+ "/rename": handleRename,
+ "/meme": handleNewMeme,
+ "/memerm": handleRemoveMeme,
+ "/refresh": handleRefreshCmd,
+}
+
func (i SlashInterceptor) InterceptMsg(c *command.Command) {
if !strings.HasPrefix(c.Message, "/") {
return
@@ -55,101 +153,47 @@ func (i SlashInterceptor) InterceptMsg(c *command.Command) {
}
func handleUserCmd(c *command.Command) (handled bool) {
- return handleIgnoreCmd(c) ||
- handleUnIgnoreCmd(c) ||
- handleToggleAutocomplete(c) ||
- handleTutorialCmd(c) ||
- handleDeleteMsgCmd(c) ||
- handleHideMsgCmd(c) ||
- handleUnHideMsgCmd(c) ||
- handleListIgnoredCmd(c) ||
- handleListPmWhitelistCmd(c) ||
- handleSetPmModeWhitelistCmd(c) ||
- handleSetPmModeStandardCmd(c) ||
- handleTogglePmBlacklistedUser(c) ||
- handleTogglePmWhitelistedUser(c) ||
- handleGroupChatCmd(c) ||
- handleMeCmd(c) ||
- handleEditCmd(c) ||
- handlePMCmd(c) ||
- handleEditLastCmd(c) ||
- handleSubscribeCmd(c) ||
- handleUnsubscribeCmd(c) ||
- handleProfileCmd(c) ||
- handleInboxCmd(c) ||
- handleChessCmd(c) ||
- handleHbmCmd(c) ||
- handleHbmtCmd(c) ||
- handleTokenCmd(c) ||
- handleMd5Cmd(c) ||
- handleSha1Cmd(c) ||
- handleSha256Cmd(c) ||
- handleSha512Cmd(c) ||
- handleDiceCmd(c) ||
- handleRandCmd(c) ||
- handleChoiceCmd(c) ||
- handleListMemes(c) ||
- handleSuccessCmd(c) ||
- handleAfkCmd(c) ||
- handleDateCmd(c) ||
- handleUpdateReadMarkerCmd(c) ||
- handleCodeCmd(c) ||
- handleLocateCmd(c) ||
- handleErrorCmd(c)
+ cmd := strings.Fields(c.Message)[0]
+ if cmdFn, found := userCmdsMap[cmd]; found {
+ return cmdFn(c)
+ }
+ return
}
func handlePrivateRoomCmd(c *command.Command) (handled bool) {
- return handleGetModeCmd(c) ||
- handleWhitelistCmd(c)
+ cmd := strings.Fields(c.Message)[0]
+ if cmdFn, found := privateRoomCmdsMap[cmd]; found {
+ return cmdFn(c)
+ }
+ return
}
func handlePrivateRoomOwnerCmd(c *command.Command) (handled bool) {
if c.Room.IsRoomOwner(*c.AuthUser) || c.AuthUser.IsAdmin {
- return handleAddGroupCmd(c) ||
- handleRmGroupCmd(c) ||
- handleLockGroupCmd(c) ||
- handleUnlockGroupCmd(c) ||
- handleGroupUsersCmd(c) ||
- handleListGroupsCmd(c) ||
- handleGroupAddUserCmd(c) ||
- handleGroupRmUserCmd(c) ||
- handleSetModeWhitelistCmd(c) ||
- handleSetModeStandardCmd(c) ||
- handleGetRoomWhitelistCmd(c) ||
- handleToggleReadOnlyCmd(c)
+ cmd := strings.Fields(c.Message)[0]
+ if cmdFn, found := privateRoomOwnerCmdsMap[cmd]; found {
+ return cmdFn(c)
+ }
}
return false
}
func handleModeratorCmd(c *command.Command) (handled bool) {
if c.AuthUser.IsModerator() {
- return handleModeratorGroupCmd(c) ||
- handleListModeratorsCmd(c) ||
- handleKickCmd(c) ||
- handleKickKeepCmd(c) ||
- handleKickSilentCmd(c) ||
- handleKickKeepSilentCmd(c) ||
- handleUnkickCmd(c) ||
- handleLogoutCmd(c) ||
- handleForceCaptchaCmd(c) ||
- handleResetTutorialCmd(c) ||
- handleHellbanCmd(c) ||
- handleUnhellbanCmd(c)
+ cmd := strings.Fields(c.Message)[0]
+ if cmdFn, found := moderatorCmdsMap[cmd]; found {
+ return cmdFn(c)
+ }
}
return false
}
func handleAdminCmd(c *command.Command) (handled bool) {
if c.AuthUser.IsAdmin {
- return handleSystemCmd(c) ||
- handleSetChatRoomExternalLink(c) ||
- handlePurge(c) ||
- handlePurgeCmd(c) ||
- handleRename(c) ||
- handleNewMeme(c) ||
- handleRenameMeme(c) ||
- handleRemoveMeme(c) ||
- handleRefreshCmd(c)
+ cmd := strings.Fields(c.Message)[0]
+ if cmdFn, found := adminCmdsMap[cmd]; found {
+ return cmdFn(c)
+ }
}
return false
}
@@ -702,7 +746,7 @@ func handleGroupRmUserCmd(c *command.Command) (handled bool) {
return false
}
-func handleSetModeWhitelistCmd(c *command.Command) (handled bool) {
+func handleSetModeCmd(c *command.Command) (handled bool) {
if c.Message == "/mode user-whitelist" {
c.Room.Mode = database.UserWhitelistRoomMode
c.Room.DoSave(c.DB)
@@ -710,12 +754,8 @@ func handleSetModeWhitelistCmd(c *command.Command) (handled bool) {
c.ZeroProcMsg(msg)
c.Err = command.ErrRedirect
return true
- }
- return false
-}
-func handleSetModeStandardCmd(c *command.Command) (handled bool) {
- if c.Message == "/mode standard" {
+ } else if c.Message == "/mode standard" {
c.Room.Mode = database.NormalRoomMode
c.Room.DoSave(c.DB)
msg := `room mode set to "standard"`
@@ -872,12 +912,8 @@ func handleEditCmd(c *command.Command) (handled bool) {
handleSystemCmd(c)
}
return true
- }
- return
-}
-func handleEditLastCmd(c *command.Command) (handled bool) {
- if c.Message == "/e" {
+ } else if c.Message == "/e" {
msg, err := c.DB.GetUserLastChatMessageInRoom(c.AuthUser.ID, c.Room.ID)
if err != nil {
return true
@@ -1093,28 +1129,6 @@ func handleGroupChatCmd(c *command.Command) (handled bool) {
return false
}
-func handleListIgnoredCmd(c *command.Command) (handled bool) {
- if c.Message == "/i" || c.Message == "/ignore" {
- ignoredUsers, _ := c.DB.GetIgnoredUsers(c.AuthUser.ID)
- sort.Slice(ignoredUsers, func(i, j int) bool {
- return ignoredUsers[i].IgnoredUser.Username < ignoredUsers[j].IgnoredUser.Username
- })
- msg := ""
- if len(ignoredUsers) > 0 {
- msg += "\n"
- for _, ignoredUser := range ignoredUsers {
- msg += ignoredUser.IgnoredUser.Username.AtStr() + "\n"
- }
- } else {
- msg += "no ignored users"
- }
- c.ZeroProcMsg(msg)
- c.Err = command.ErrRedirect
- return true
- }
- return false
-}
-
func handleListPmWhitelistCmd(c *command.Command) (handled bool) {
if c.Message == "/pmwhitelist" {
pmWhitelistUsers, _ := c.DB.GetPmWhitelistedUsers(c.AuthUser.ID)
@@ -1137,7 +1151,7 @@ func handleListPmWhitelistCmd(c *command.Command) (handled bool) {
return false
}
-func handleSetPmModeWhitelistCmd(c *command.Command) (handled bool) {
+func handleSetPmModeCmd(c *command.Command) (handled bool) {
if c.Message == "/setpmmode whitelist" {
c.AuthUser.PmMode = database.PmModeWhitelist
c.AuthUser.DoSave(c.DB)
@@ -1145,12 +1159,8 @@ func handleSetPmModeWhitelistCmd(c *command.Command) (handled bool) {
c.ZeroProcMsg(msg)
c.Err = command.ErrRedirect
return true
- }
- return false
-}
-func handleSetPmModeStandardCmd(c *command.Command) (handled bool) {
- if c.Message == "/setpmmode standard" {
+ } else if c.Message == "/setpmmode standard" {
c.AuthUser.PmMode = database.PmModeStandard
c.AuthUser.DoSave(c.DB)
msg := `pm mode set to "standard"`
@@ -1448,6 +1458,25 @@ func handleIgnoreCmd(c *command.Command) (handled bool) {
database.MsgPubSub.Pub("refresh_"+string(c.AuthUser.Username), database.ChatMessageType{Typ: database.ForceRefresh})
c.Err = command.ErrRedirect
return true
+
+ } else if c.Message == "/i" || c.Message == "/ignore" {
+ ignoredUsers, _ := c.DB.GetIgnoredUsers(c.AuthUser.ID)
+ sort.Slice(ignoredUsers, func(i, j int) bool {
+ return ignoredUsers[i].IgnoredUser.Username < ignoredUsers[j].IgnoredUser.Username
+ })
+ msg := ""
+ if len(ignoredUsers) > 0 {
+ msg += "\n"
+ for _, ignoredUser := range ignoredUsers {
+ msg += ignoredUser.IgnoredUser.Username.AtStr() + "\n"
+ }
+ } else {
+ msg += "no ignored users"
+ }
+ c.ZeroProcMsg(msg)
+ c.Err = command.ErrRedirect
+ return true
+
} else if strings.HasPrefix(c.Message, "/ignore ") || strings.HasPrefix(c.Message, "/i ") {
c.Err = errors.New("invalid ignore command")
return true
@@ -1550,20 +1579,6 @@ func handleSetChatRoomExternalLink(c *command.Command) (handled bool) {
return
}
-func handlePurgeCmd(c *command.Command) (handled bool) {
- if c.Message == "/purge" {
- c.Err = command.ErrRedirect
- if !c.AuthUser.UseStream {
- c.Err = errors.New("only work on stream version of this chat")
- return true
- }
- payload := database.ChatMessageType{}
- streamModals.PurgeModal{}.Show(c.AuthUser.ID, c.Room.ID, payload)
- return true
- }
- return
-}
-
func handlePurge(c *command.Command) (handled bool) {
if m := purgeRgx.FindStringSubmatch(c.Message); len(m) == 3 {
isHB := m[1] == " -hb"
@@ -1582,6 +1597,16 @@ func handlePurge(c *command.Command) (handled bool) {
database.MsgPubSub.Pub(database.RefreshTopic, database.ChatMessageType{Typ: database.ForceRefresh})
c.Err = command.ErrRedirect
return true
+
+ } else if c.Message == "/purge" {
+ c.Err = command.ErrRedirect
+ if !c.AuthUser.UseStream {
+ c.Err = errors.New("only work on stream version of this chat")
+ return true
+ }
+ payload := database.ChatMessageType{}
+ streamModals.PurgeModal{}.Show(c.AuthUser.ID, c.Room.ID, payload)
+ return true
}
return
}
@@ -1640,40 +1665,36 @@ func handleNewMeme(c *command.Command) (handled bool) {
c.Err = command.ErrRedirect
return true
- }
- return
-}
-func handleRemoveMeme(c *command.Command) (handled bool) {
- if m := memeRemoveRgx.FindStringSubmatch(c.Message); len(m) == 2 {
+ } else if m := memeRenameRgx.FindStringSubmatch(c.Message); len(m) == 3 {
slug := m[1]
+ newSlug := m[2]
meme, err := c.DB.GetMemeBySlug(slug)
if err != nil {
c.Err = errors.New("meme not found")
return true
}
- if err := meme.Delete(c.DB); err != nil {
- c.Err = err
- return true
- }
- c.Err = command.NewErrSuccess("meme removed")
+ meme.Slug = newSlug
+ meme.DoSave(c.DB)
+ c.Err = command.NewErrSuccess("meme renamed")
return true
}
return
}
-func handleRenameMeme(c *command.Command) (handled bool) {
- if m := memeRenameRgx.FindStringSubmatch(c.Message); len(m) == 3 {
+func handleRemoveMeme(c *command.Command) (handled bool) {
+ if m := memeRemoveRgx.FindStringSubmatch(c.Message); len(m) == 2 {
slug := m[1]
- newSlug := m[2]
meme, err := c.DB.GetMemeBySlug(slug)
if err != nil {
c.Err = errors.New("meme not found")
return true
}
- meme.Slug = newSlug
- meme.DoSave(c.DB)
- c.Err = command.NewErrSuccess("meme renamed")
+ if err := meme.Delete(c.DB); err != nil {
+ c.Err = err
+ return true
+ }
+ c.Err = command.NewErrSuccess("meme removed")
return true
}
return