commit a4996e93c906d5b6d10407dedf65748e8a867ace
parent 26a95f33d78bde5aaf119ca9c4b76c24fa4841cb
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 19 Jan 2023 01:39:06 -0800
auto convert /u/user to link to profile page & make dkf links works on all tor/i2p/dev
Diffstat:
1 file changed, 35 insertions(+), 1 deletion(-)
diff --git a/pkg/web/handlers/api/v1/topBarHandler.go b/pkg/web/handlers/api/v1/topBarHandler.go
@@ -778,6 +778,8 @@ func convertLinksWithoutScheme(in string) string {
return html
}
+var userProfileLinkRgx = regexp.MustCompile(`^/u/\w{3,20}$`)
+var userProfileLinkRgx1 = regexp.MustCompile(`/u/\w{3,20}`)
var youtubeComIDRgx = regexp.MustCompile(`watch\?v=([\w-]+)`)
var youtubeComShosrtsIDRgx = regexp.MustCompile(`/shorts/([\w-]+)`)
var youtuBeIDRgx = regexp.MustCompile(`https://youtu\.be/([\w-]+)`)
@@ -831,7 +833,6 @@ func convertLinks(in string) string {
"http://be7udfhmnzqyt7cxysg6c4pbawarvaofjjywp35nhd5qamewdfxl6sid.onion"}
knownOnions := [][]string{
- {"http://dkf.onion", config.DkfOnion},
{"http://dkfgit.onion", config.DkfGitOnion},
{"http://dread.onion", config.DreadOnion},
{"http://cryptbb.onion", config.CryptbbOnion},
@@ -962,6 +963,29 @@ func convertLinks(in string) string {
return out
}
+ // Special case for dkf links.
+ {
+ dkfShortPrefix := "http://dkf.onion"
+ dkfLongPrefix := config.DkfOnion
+ if strings.HasPrefix(link, dkfLongPrefix) || strings.HasPrefix(link, dkfShortPrefix) {
+ var label, href, trimmed string
+ if strings.HasPrefix(link, dkfLongPrefix) {
+ trimmed = strings.TrimPrefix(link, dkfLongPrefix)
+ label = dkfShortPrefix + trimmed
+ href = trimmed
+ } else if strings.HasPrefix(link, dkfShortPrefix) {
+ trimmed = strings.TrimPrefix(link, dkfShortPrefix)
+ label = link
+ href = trimmed
+ }
+ // Allows to have messages such as: "my profile is /u/username :)"
+ if userProfileLinkRgx.MatchString(trimmed) {
+ label = trimmed
+ }
+ return makeHtmlLink(label, href)
+ }
+ }
+
for _, el := range knownOnions {
shortPrefix := el[0]
longPrefix := el[1]
@@ -974,6 +998,16 @@ func convertLinks(in string) string {
return makeHtmlLink(link, link)
})
+ // Convert all occurrences of "/u/username" to a link to user profile page if the user exists
+ newRest = userProfileLinkRgx1.ReplaceAllStringFunc(newRest, func(link string) string {
+ user, err := database.GetUserByUsername(strings.TrimPrefix(link, "/u/"))
+ if err != nil {
+ return link
+ }
+ href := "/u/" + user.Username
+ return makeHtmlLink(href, href)
+ })
+
return quote + newRest
}