dkforest

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

commit 50d7fa477bbc0a75c5941741e2549f63041e20cf
parent 92871d2b76da4741e681158be7c69f1bd5a37465
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Thu, 19 Jan 2023 02:39:06 -0800

remove side effects & add some tests

Diffstat:
Mpkg/web/handlers/api/v1/msgInterceptor.go | 2+-
Mpkg/web/handlers/api/v1/topBarHandler.go | 6+++---
Mpkg/web/handlers/api/v1/topBarHandler_test.go | 35+++++++++++++++++++++++++++++++++++
3 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/pkg/web/handlers/api/v1/msgInterceptor.go b/pkg/web/handlers/api/v1/msgInterceptor.go @@ -74,7 +74,7 @@ func ProcessRawMessage(in, roomKey string, authUserID database.UserID, roomID da html = convertMarkdown(html) html = convertBangShortcuts(html) html = convertArchiveLinks(html, roomID, authUserID) - html = convertLinks(html) + html = convertLinks(html, database.GetUserByUsername) html = linkDefaultRooms(html) html, taggedUsersIDsMap := colorifyTaggedUsers(html, database.GetUsersByUsername) html = linkRoomTags(html) diff --git a/pkg/web/handlers/api/v1/topBarHandler.go b/pkg/web/handlers/api/v1/topBarHandler.go @@ -800,7 +800,7 @@ func splitQuote(in string) (string, string) { return in[:idx], in[idx:] } -func convertLinks(in string) string { +func convertLinks(in string, getUserByUsername func(string) (database.User, error)) string { quote, rest := splitQuote(in) libredditURLs := []string{ @@ -845,7 +845,7 @@ func convertLinks(in string) string { // Convert all occurrences of "/u/username" to a link to user profile page if the user exists if userProfileLinkRgx.MatchString(link) { - user, err := database.GetUserByUsername(strings.TrimPrefix(link, "/u/")) + user, err := getUserByUsername(strings.TrimPrefix(link, "/u/")) if err != nil { return link } @@ -991,7 +991,7 @@ func convertLinks(in string) string { } // Allows to have messages such as: "my profile is /u/username :)" if userProfileLinkRgx.MatchString(trimmed) { - if user, err := database.GetUserByUsername(strings.TrimPrefix(trimmed, "/u/")); err == nil { + if user, err := getUserByUsername(strings.TrimPrefix(trimmed, "/u/")); err == nil { label = "/u/" + user.Username href = "/u/" + user.Username } diff --git a/pkg/web/handlers/api/v1/topBarHandler_test.go b/pkg/web/handlers/api/v1/topBarHandler_test.go @@ -2,6 +2,7 @@ package v1 import ( "dkforest/pkg/database" + "errors" "github.com/stretchr/testify/assert" "strings" "testing" @@ -118,3 +119,37 @@ i think it would be better if it shows expected = "“[00:00:02] user1 - instead of showing \"[00:00:01] user2 - an article about HHVM...…”" assert.Equal(t, expected, txt) } + +func TestConvertLinks(t *testing.T) { + getUserByUsername := func(username string) (database.User, error) { + if strings.ToLower(username) == "username" { + return database.User{Username: "username"}, nil + } + return database.User{}, errors.New("not exists") + } + + // Replace /u/username to link when user exists + actual := convertLinks("this is /u/username a test", getUserByUsername) + expected := `this is <a href="/u/username" rel="noopener noreferrer" target="_blank">/u/username</a> a test` + assert.Equal(t, expected, actual) + + // Does not replace /u/notExist to link when user does not exist + actual = convertLinks("this is /u/notExist a test", getUserByUsername) + expected = `this is /u/notExist a test` + assert.Equal(t, expected, actual) + + // Fix case errors + actual = convertLinks("this is /u/uSerNaMe a test", getUserByUsername) + expected = `this is <a href="/u/username" rel="noopener noreferrer" target="_blank">/u/username</a> a test` + assert.Equal(t, expected, actual) + + // Convert long dkf url + actual = convertLinks("this is http://dkforestseeaaq2dqz2uflmlsybvnq2irzn4ygyvu53oazyorednviid.onion/u/username a test", getUserByUsername) + expected = `this is <a href="/u/username" rel="noopener noreferrer" target="_blank">/u/username</a> a test` + assert.Equal(t, expected, actual) + + // Shorten dkf url but keep the short form since the user does not exist + actual = convertLinks("this is http://dkforestseeaaq2dqz2uflmlsybvnq2irzn4ygyvu53oazyorednviid.onion/u/notExist a test", getUserByUsername) + expected = `this is <a href="/u/notExist" rel="noopener noreferrer" target="_blank">http://dkf.onion/u/notExist</a> a test` + assert.Equal(t, expected, actual) +}