commit e2f754b3c56690139b603d1b13d05b286b66c45a
parent a4996e93c906d5b6d10407dedf65748e8a867ace
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 19 Jan 2023 02:23:01 -0800
fix profile link
Diffstat:
1 file changed, 20 insertions(+), 16 deletions(-)
diff --git a/pkg/web/handlers/api/v1/topBarHandler.go b/pkg/web/handlers/api/v1/topBarHandler.go
@@ -30,8 +30,6 @@ const (
pgpPKeySuffix = "-----END PGP PUBLIC KEY BLOCK-----"
)
-var linkRgx = regexp.MustCompile(`(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?`)
-
var emojiReplacer = strings.NewReplacer(
":):", `<span class="emoji" title=":):">☺</span>`,
":smile:", `<span class="emoji" title=":smile:">☺</span>`,
@@ -778,8 +776,10 @@ func convertLinksWithoutScheme(in string) string {
return html
}
-var userProfileLinkRgx = regexp.MustCompile(`^/u/\w{3,20}$`)
-var userProfileLinkRgx1 = regexp.MustCompile(`/u/\w{3,20}`)
+var linkRgxStr = `(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?`
+var profileRgxStr = `/u/\w{3,20}`
+var linkOrProfileRgx = regexp.MustCompile(`(` + linkRgxStr + `)|(` + profileRgxStr + `))`)
+var userProfileLinkRgx = regexp.MustCompile(`^` + profileRgxStr + `$`)
var youtubeComIDRgx = regexp.MustCompile(`watch\?v=([\w-]+)`)
var youtubeComShosrtsIDRgx = regexp.MustCompile(`/shorts/([\w-]+)`)
var youtuBeIDRgx = regexp.MustCompile(`https://youtu\.be/([\w-]+)`)
@@ -841,7 +841,18 @@ func convertLinks(in string) string {
{"http://whonix.onion", config.WhonixOnion},
}
- newRest := linkRgx.ReplaceAllStringFunc(rest, func(link string) string {
+ newRest := linkOrProfileRgx.ReplaceAllStringFunc(rest, func(link 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/"))
+ if err != nil {
+ return link
+ }
+ href := "/u/" + user.Username
+ 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)
@@ -980,7 +991,10 @@ func convertLinks(in string) string {
}
// Allows to have messages such as: "my profile is /u/username :)"
if userProfileLinkRgx.MatchString(trimmed) {
- label = trimmed
+ if user, err := database.GetUserByUsername(strings.TrimPrefix(trimmed, "/u/")); err == nil {
+ label = "/u/" + user.Username
+ href = "/u/" + user.Username
+ }
}
return makeHtmlLink(label, href)
}
@@ -998,16 +1012,6 @@ 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
}