dkforest

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

commit 6ee132a7b613587325779556283b6d9e095fa917
parent 0b64606904a7f14c2e1760f3a3858ee271a53d78
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Thu, 15 Jun 2023 12:55:15 -0700

can escape @ to avoid actual tagging

Diffstat:
Mpkg/database/utils/processMessage.go | 15+++++++++++----
Mpkg/web/handlers/interceptors/msgInterceptor.go | 2+-
Mpkg/web/handlers/interceptors/snippetInterceptor.go | 3+++
3 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/pkg/database/utils/processMessage.go b/pkg/database/utils/processMessage.go @@ -84,7 +84,7 @@ var roomNameF = `\w{3,50}` var userOr0 = usernameF + `|0` var optAtGUserOr0 = `@?(` + userOr0 + `)` // Optional @, Grouped, Username or 0 var pmRgx = regexp.MustCompile(`^/pm ` + optAtGUserOr0 + `(?:\s(?s:(.*)))?`) -var tagRgx = regexp.MustCompile(`@(` + userOr0 + `)`) +var tagRgx = regexp.MustCompile(`(?:\\?)@(` + userOr0 + `)`) var roomTagRgx = regexp.MustCompile(`#(` + roomNameF + `)`) var noSchemeOnionLinkRgx = regexp.MustCompile(`\s[a-z2-7]{56}\.onion`) @@ -699,9 +699,13 @@ type getUsersByUsernameFn func(usernames []string) ([]database.User, error) // Update the given html to add user style for tags. // Return the new html, and a map[userID]User of tagged users. func colorifyTaggedUsers(html string, getUsersByUsername getUsersByUsernameFn) (string, map[database.UserID]database.User) { - usernameMatches := tagRgx.FindAllStringSubmatch(html, -1) + tagRgxL := tagRgx + usernameMatches := tagRgxL.FindAllStringSubmatch(html, -1) usernames := hashset.New[string]() for _, usernameMatch := range usernameMatches { + if strings.HasPrefix(usernameMatch[0], `\`) { + continue + } usernames.Insert(usernameMatch[1]) } taggedUsers, _ := getUsersByUsername(usernames.ToArray()) @@ -715,8 +719,11 @@ func colorifyTaggedUsers(html string, getUsersByUsername getUsersByUsernameFn) ( } } - if len(usernameMatches) > 0 { - html = tagRgx.ReplaceAllStringFunc(html, func(s string) string { + if usernames.Len() > 0 { + html = tagRgxL.ReplaceAllStringFunc(html, func(s string) string { + if strings.HasPrefix(s, `\`) { + return strings.TrimPrefix(s, `\`) + } lowerS := strings.ToLower(s) if user, ok := taggedUsersMap[lowerS]; ok { return fmt.Sprintf("<span %s>@%s</span>", user.GenerateChatStyle1(), user.Username) diff --git a/pkg/web/handlers/interceptors/msgInterceptor.go b/pkg/web/handlers/interceptors/msgInterceptor.go @@ -117,7 +117,7 @@ var randRgx = regexp.MustCompile(`^/rand (-?\d+) (-?\d+)$`) var tokenRgx = regexp.MustCompile(`^/token (\d{1,2})$`) var snippetRgx = regexp.MustCompile(`!\w{1,20}`) var tagRgx = regexp.MustCompile(`@(` + userOr0 + `)`) -var autoTagRgx = regexp.MustCompile(`@(\w+)\*`) +var autoTagRgx = regexp.MustCompile(`(?:\\?)@(\w+)\*`) var roomTagRgx = regexp.MustCompile(`#(` + roomNameF + `)`) var tzRgx = regexp.MustCompile(`(\d{4}-\d{1,2}-\d{1,2} at \d{1,2}\.\d{1,2}\.\d{1,2} (?i)[A|P]M)`) // Screen Shot 2022-02-04 at 11.58.58 PM var tz1Rgx = regexp.MustCompile(`(\d{4}-\d{1,2}-\d{1,2} \d{1,2}-\d{1,2}-\d{1,2})`) // Screenshot from 2022-02-04 11-58-58.png diff --git a/pkg/web/handlers/interceptors/snippetInterceptor.go b/pkg/web/handlers/interceptors/snippetInterceptor.go @@ -43,6 +43,9 @@ func snippets(db *database.DkfDB, authUserID database.UserID, html string) strin func autocompleteTags(html string) string { activeUsers := managers.ActiveUsers.GetActiveUsers() html = autoTagRgx.ReplaceAllStringFunc(html, func(s string) string { + if strings.HasPrefix(s, `\`) { + return strings.TrimPrefix(s, `\`) + } s1 := strings.TrimPrefix(s, "@") s1 = strings.TrimSuffix(s1, "*") s1 = strings.ToLower(s1)