diff --git a/state.go b/state.go index b271855..ed3ad28 100644 --- a/state.go +++ b/state.go @@ -11,6 +11,8 @@ import ( "mime/multipart" "net/http" "net/url" + "os" + "regexp" "sort" "strconv" "strings" @@ -100,6 +102,37 @@ type State struct { HideInstanceNames bool } +func (s State) Unknown() string { + fmt.Println(fmt.Sprintf("%v", s.Error)) + re := regexp.MustCompile(`(.*?)@(.*?)@`) + if strings.Contains(fmt.Sprintf("%v", s.Error), "couldnt_find_community") { + matches := re.FindAllStringSubmatch(s.CommunityName+"@", -1) + if len(matches) < 1 || len(matches[0]) < 3 { + return "" + } + if matches[0][2] != s.Host { + remote := "/" + matches[0][2] + "/c/" + matches[0][1] + if os.Getenv("LEMMY_DOMAIN") != "" { + remote = "https:/" + remote + } + return remote + } + } + if strings.Contains(fmt.Sprintf("%v", s.Error), "couldnt_find_that_username_or_email") { + matches := re.FindAllStringSubmatch(s.UserName+"@", -1) + if len(matches) < 1 || len(matches[0]) < 3 { + return "" + } + if matches[0][2] != s.Host { + remote := "/" + matches[0][2] + "/u/" + matches[0][1] + if os.Getenv("LEMMY_DOMAIN") != "" { + remote = "https:/" + remote + } + return remote + } + } + return "" +} func (p State) SortBy(v string) string { var q string if p.Query != "" || p.SearchType == "Communities" { @@ -411,6 +444,7 @@ func (state *State) GetUser(username string) { }) if err != nil { fmt.Println(err) + state.Error = err state.Status = http.StatusInternalServerError return } diff --git a/templates/frontpage.html b/templates/frontpage.html index b812252..63637d1 100644 --- a/templates/frontpage.html +++ b/templates/frontpage.html @@ -34,7 +34,12 @@ {{ end}} {{ if .Error }} -
{{.Error}}
+
+ {{.Error}}. + {{ if .Unknown }} + try remote instance: {{ .Unknown }} + {{ end }} +
{{ end }} {{ range .Posts }} diff --git a/templates/main.html b/templates/main.html index ed08e98..e2d6390 100644 --- a/templates/main.html +++ b/templates/main.html @@ -56,7 +56,12 @@ {{ end}} {{ if .Error }} -
{{.Error}}
+
+ {{.Error}}. + {{ if .Unknown }} + try remote instance: {{ .Unknown }} + {{ end }} +
{{ end }} {{ range .Communities }}