dkforest

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

commit 5c403e60ebeeae2c60bc9fc7c018b9f5f9c9a766
parent 285221a9d8e005939c56adaaadd257002fd23a51
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Wed, 16 Nov 2022 14:45:59 -0500

autocomplete tags

Diffstat:
Mpkg/web/handlers/api/v1/handlers.go | 1+
Mpkg/web/handlers/api/v1/snippetInterceptor.go | 20++++++++++++++++++++
2 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/pkg/web/handlers/api/v1/handlers.go b/pkg/web/handlers/api/v1/handlers.go @@ -52,6 +52,7 @@ var hellbanRgx = regexp.MustCompile(`^/(?:hellban|hb) ` + optAtGUser) var unhellbanRgx = regexp.MustCompile(`^/(?:unhellban|uhb) ` + optAtGUser) var tokenRgx = regexp.MustCompile(`^/token (\d{1,2})$`) var tagRgx = regexp.MustCompile(`@(` + userOr0 + `)`) +var autoTagRgx = regexp.MustCompile(`@(\w+)\*`) var roomTagRgx = regexp.MustCompile(`#(` + roomName + `)`) var tzRgx = regexp.MustCompile(`(\d{4}-\d{1,2}-\d{1,2} at \d{1,2}\.\d{1,2}\.\d{1,2} [A|P]M)`) // Screen Shot 2022-02-04 at 11.58.58 PM var addGroupRgx = regexp.MustCompile(`^/addgroup (` + groupName + `)$`) diff --git a/pkg/web/handlers/api/v1/snippetInterceptor.go b/pkg/web/handlers/api/v1/snippetInterceptor.go @@ -2,7 +2,9 @@ package v1 import ( "dkforest/pkg/database" + "dkforest/pkg/managers" "regexp" + "strings" ) type SnippetInterceptor struct{} @@ -11,6 +13,9 @@ func (i SnippetInterceptor) InterceptMsg(cmd *Command) { // Snippets actually mutate the original message, // to simulate that the user actually typed the text cmd.origMessage = snippets(cmd.authUser.ID, cmd.origMessage) + + cmd.origMessage = autocompleteTags(cmd.origMessage) + cmd.message = cmd.origMessage } @@ -36,3 +41,18 @@ func snippets(authUserID int64, html string) string { } return html } + +func autocompleteTags(html string) string { + activeUsers := managers.ActiveUsers.GetActiveUsers() + html = autoTagRgx.ReplaceAllStringFunc(html, func(s string) string { + s1 := strings.TrimPrefix(s, "@") + s1 = strings.TrimSuffix(s1, "*") + for _, au := range activeUsers { + if strings.HasPrefix(au.Username, s1) { + return "@" + au.Username + } + } + return s + }) + return html +}