dkforest

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

commit e7b9eafa071127b47d595a72f7ef7b4ed1c11e11
parent 31e8dc242503cc58b616c9a0cfc8a93a0ce0b86d
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Mon, 29 May 2023 19:35:55 -0700

auto link /l/shorthand

Diffstat:
Mpkg/web/handlers/api/v1/msgInterceptor.go | 2+-
Mpkg/web/handlers/api/v1/topBarHandler.go | 24++++++++++++++++++++++--
Mpkg/web/handlers/api/v1/topBarHandler_test.go | 17++++++++++-------
3 files changed, 33 insertions(+), 10 deletions(-)

diff --git a/pkg/web/handlers/api/v1/msgInterceptor.go b/pkg/web/handlers/api/v1/msgInterceptor.go @@ -97,7 +97,7 @@ func ProcessRawMessage(db *database.DkfDB, in, roomKey string, authUserID databa html = convertMarkdown(html) html = convertBangShortcuts(html) html = convertArchiveLinks(db, html, roomID, authUserID) - html = convertLinks(html, db.GetUserByUsername) + html = convertLinks(html, db.GetUserByUsername, db.GetLinkByShorthand) html = linkDefaultRooms(html) html, taggedUsersIDsMap := colorifyTaggedUsers(html, db.GetUsersByUsername) html = linkRoomTags(db, html) diff --git a/pkg/web/handlers/api/v1/topBarHandler.go b/pkg/web/handlers/api/v1/topBarHandler.go @@ -788,8 +788,10 @@ func convertLinksWithoutScheme(in string) string { var linkRgxStr = `(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&amp;:/~\+#\(\)]*[\w\-\@?^=%&amp;/~\+#\(\)])?` var profileRgxStr = `/u/\w{3,20}` -var linkOrProfileRgx = regexp.MustCompile(`(` + linkRgxStr + `|` + profileRgxStr + `)`) +var linkShorthandRgxStr = `/l/\w{3,20}` +var linkOrProfileRgx = regexp.MustCompile(`(` + linkRgxStr + `|` + profileRgxStr + `|` + linkShorthandRgxStr + `)`) var userProfileLinkRgx = regexp.MustCompile(`^` + profileRgxStr + `$`) +var linkShorthandPageLinkRgx = regexp.MustCompile(`^` + linkShorthandRgxStr + `$`) var youtubeComIDRgx = regexp.MustCompile(`watch\?v=([\w-]+)`) var youtubeComShortsIDRgx = regexp.MustCompile(`/shorts/([\w-]+)`) var youtuBeIDRgx = regexp.MustCompile(`https://youtu\.be/([\w-]+)`) @@ -814,7 +816,9 @@ func splitQuote(in string) (string, string) { return in[:idx], in[idx:] } -func convertLinks(in string, getUserByUsername func(database.Username) (database.User, error)) string { +func convertLinks(in string, + getUserByUsername func(database.Username) (database.User, error), + getLinkByShorthand func(string) (database.Link, error)) string { quote, rest := splitQuote(in) libredditURLs := []string{ @@ -864,6 +868,16 @@ func convertLinks(in string, getUserByUsername func(database.Username) (database return makeHtmlLink(href, href) } + // Convert all occurrences of "/l/shorthand" to a link to link page if the shorthand exists + if linkShorthandPageLinkRgx.MatchString(link) { + l, err := getLinkByShorthand(strings.TrimPrefix(link, "/l/")) + if err != nil { + return link + } + href := "/l/" + *l.Shorthand + return makeHtmlLink(href, href) + } + // Handle reddit links if strings.HasPrefix(link, "https://www.reddit.com/") { old := strings.Replace(link, "https://www.reddit.com/", "https://old.reddit.com/", 1) @@ -1006,6 +1020,12 @@ func convertLinks(in string, getUserByUsername func(database.Username) (database label = "/u/" + string(user.Username) href = "/u/" + string(user.Username) } + } else if linkShorthandPageLinkRgx.MatchString(trimmed) { + // Convert all occurrences of "/l/shorthand" to a link to link page if the shorthand exists + if l, err := getLinkByShorthand(strings.TrimPrefix(trimmed, "/l/")); err == nil { + label = "/l/" + *l.Shorthand + href = "/l/" + *l.Shorthand + } } return makeHtmlLink(label, href) } diff --git a/pkg/web/handlers/api/v1/topBarHandler_test.go b/pkg/web/handlers/api/v1/topBarHandler_test.go @@ -121,35 +121,38 @@ i think it would be better if it shows } func TestConvertLinks(t *testing.T) { - getUserByUsername := func(username string) (database.User, error) { - if strings.ToLower(username) == "username" { + getUserByUsername := func(username database.Username) (database.User, error) { + if strings.ToLower(string(username)) == "username" { return database.User{Username: "username"}, nil } return database.User{}, errors.New("not exists") } + getLinkByShorthand := func(shorthand string) (database.Link, error) { + return database.Link{}, nil + } // Replace /u/username to link when user exists - actual := convertLinks("this is /u/username a test", getUserByUsername) + actual := convertLinks("this is /u/username a test", getUserByUsername, getLinkByShorthand) 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) + actual = convertLinks("this is /u/notExist a test", getUserByUsername, getLinkByShorthand) expected = `this is /u/notExist a test` assert.Equal(t, expected, actual) // Fix case errors - actual = convertLinks("this is /u/uSerNaMe a test", getUserByUsername) + actual = convertLinks("this is /u/uSerNaMe a test", getUserByUsername, getLinkByShorthand) 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) + actual = convertLinks("this is http://dkforestseeaaq2dqz2uflmlsybvnq2irzn4ygyvu53oazyorednviid.onion/u/username a test", getUserByUsername, getLinkByShorthand) 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) + actual = convertLinks("this is http://dkforestseeaaq2dqz2uflmlsybvnq2irzn4ygyvu53oazyorednviid.onion/u/notExist a test", getUserByUsername, getLinkByShorthand) 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) }