dkforest

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

commit 593bc4ef4b961ffc5e886281db1c47d8cdad1dcc
parent 2deda54d412f8d2b9e170901542cceda7781725b
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Sat, 10 Jun 2023 23:07:37 -0700

add /locate command

Diffstat:
Mpkg/database/tableChatRooms.go | 5+++++
Mpkg/managers/managers.go | 14++++++++++++++
Mpkg/web/handlers/interceptors/msgInterceptor.go | 1+
Mpkg/web/handlers/interceptors/slashInterceptor.go | 18++++++++++++++++++
Mpkg/web/public/views/pages/chat-help.gohtml | 7+++++++
5 files changed, 45 insertions(+), 0 deletions(-)

diff --git a/pkg/database/tableChatRooms.go b/pkg/database/tableChatRooms.go @@ -132,6 +132,11 @@ func (r *ChatRoom) HasAccess(c echo.Context) (bool, string) { return true, cookie.Value } +func (d *DkfDB) GetChatRoomsByID(roomIDs []RoomID) (out []ChatRoom, err error) { + err = d.db.Where("id IN (?)", roomIDs).Find(&out).Error + return +} + func (d *DkfDB) GetChatRoomByID(roomID RoomID) (out ChatRoom, err error) { err = d.db.Where("id = ?", roomID).First(&out).Error return diff --git a/pkg/managers/managers.go b/pkg/managers/managers.go @@ -169,6 +169,20 @@ func (m *ActiveUsersManager) getRoomUsersMap(roomKey RoomKey) UsersMap { return usersMap } +func (m *ActiveUsersManager) LocateUser(target database.Username) (out []database.RoomID) { + m.Lock() + for roomKey, usersMap := range m.activeUsers { + for username := range usersMap { + if username == target { + roomID := database.RoomID(utils.DoParseInt64(string(roomKey))) + out = append(out, roomID) + } + } + } + m.Unlock() + return +} + // GetActiveUsers gets a list of all users that are in public rooms. // We use this to display online users on the home (login) page if the feature is enabled. func (m *ActiveUsersManager) GetActiveUsers() []UserInfo { diff --git a/pkg/web/handlers/interceptors/msgInterceptor.go b/pkg/web/handlers/interceptors/msgInterceptor.go @@ -139,6 +139,7 @@ var memeRgx = regexp.MustCompile(`^/meme ([a-zA-Z0-9_-]{3,50})$`) var memeRenameRgx = regexp.MustCompile(`^/meme ([a-zA-Z0-9_-]{3,50}) ([a-zA-Z0-9_-]{3,50})$`) var memeRemoveRgx = regexp.MustCompile(`^/memerm ([a-zA-Z0-9_-]{3,50})$`) var memesRgx = regexp.MustCompile(`^/memes$`) +var locateRgx = regexp.MustCompile(`^/locate ` + optAtGUser) type MsgInterceptor struct{} diff --git a/pkg/web/handlers/interceptors/slashInterceptor.go b/pkg/web/handlers/interceptors/slashInterceptor.go @@ -94,6 +94,7 @@ func handleUserCmd(c *command.Command) (handled bool) { handleDateCmd(c) || handleUpdateReadMarkerCmd(c) || handleCodeCmd(c) || + handleLocateCmd(c) || handleErrorCmd(c) } @@ -1705,6 +1706,23 @@ func handleRefreshCmd(c *command.Command) (handled bool) { return } +func handleLocateCmd(c *command.Command) (handled bool) { + if m := locateRgx.FindStringSubmatch(c.Message); len(m) == 2 { + username := database.Username(m[1]) + roomIDs := managers.ActiveUsers.LocateUser(username) + rooms, _ := c.DB.GetChatRoomsByID(roomIDs) + roomLinks := make([]string, len(rooms)) + for idx, room := range rooms { + roomLinks[idx] = "#" + room.Name + } + msg := "@" + string(username) + " is in " + strings.Join(roomLinks, " ") + c.ZeroProcMsg(msg) + c.Err = command.ErrRedirect + return true + } + return +} + func handleCodeCmd(c *command.Command) (handled bool) { if c.Message == "/code" { c.Err = command.ErrRedirect diff --git a/pkg/web/public/views/pages/chat-help.gohtml b/pkg/web/public/views/pages/chat-help.gohtml @@ -30,6 +30,13 @@ <div> <div> + <code>/locate username</code> + </div> + <p>Find in which (public) room(s) a user currently is.</p> + </div> + + <div> + <div> <code>/ignore username</code> (or: <code>/i username</code>) </div> <p>Hides all messages from that user</p>