Skip to content

Commit

Permalink
try remote instance link
Browse files Browse the repository at this point in the history
  • Loading branch information
rystaf committed Jul 16, 2023
1 parent 2d8a3d2 commit 23d08a7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
34 changes: 34 additions & 0 deletions state.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"mime/multipart"
"net/http"
"net/url"
"os"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -411,6 +444,7 @@ func (state *State) GetUser(username string) {
})
if err != nil {
fmt.Println(err)
state.Error = err
state.Status = http.StatusInternalServerError
return
}
Expand Down
7 changes: 6 additions & 1 deletion templates/frontpage.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@
{{ end}}

{{ if .Error }}
<div class="error">{{.Error}}</div>
<div class="error">
{{.Error}}.
{{ if .Unknown }}
try remote instance: <a href="{{ .Unknown }}">{{ .Unknown }}</a>
{{ end }}
</div>
{{ end }}

{{ range .Posts }}
Expand Down
7 changes: 6 additions & 1 deletion templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@
{{ end}}

{{ if .Error }}
<div class="error">{{.Error}}</div>
<div class="error">
{{.Error}}.
{{ if .Unknown }}
try remote instance: <a href="{{ .Unknown }}">{{ .Unknown }}</a>
{{ end }}
</div>
{{ end }}

{{ range .Communities }}
Expand Down

0 comments on commit 23d08a7

Please sign in to comment.