This commit is contained in:
Ryan Stafford 2024-05-14 23:46:33 +00:00
parent 9fc7c1380b
commit 616ee76275
3 changed files with 40 additions and 45 deletions

26
main.go
View file

@ -75,19 +75,19 @@ func init() {
} }
func test() { func test() {
links := [][]string{ links := [][]string{
[]string{"https://lemmy.local/u/dude", "/lemmy.local/u/dude", "/u/dude"}, {"https://lemmy.local/u/dude", "/lemmy.local/u/dude", "/u/dude"},
[]string{"https://lemmy.local/u/dude@lemmy.local", "/lemmy.local/u/dude", "/u/dude"}, {"https://lemmy.local/u/dude@lemmy.local", "/lemmy.local/u/dude", "/u/dude"},
[]string{"/u/dude", "/lemmy.local/u/dude", "/u/dude"}, {"/u/dude", "/lemmy.local/u/dude", "/u/dude"},
[]string{"/u/dude@lemmy.world", "/lemmy.local/u/dude@lemmy.world", "/u/dude@lemmy.world"}, {"/u/dude@lemmy.world", "/lemmy.local/u/dude@lemmy.world", "/u/dude@lemmy.world"},
[]string{"/u/dude@lemmy.local", "/lemmy.local/u/dude", "/u/dude"}, {"/u/dude@lemmy.local", "/lemmy.local/u/dude", "/u/dude"},
[]string{"https://lemmy.world/c/dude", "/lemmy.local/c/dude@lemmy.world", "/c/dude@lemmy.world"}, {"https://lemmy.world/c/dude", "/lemmy.local/c/dude@lemmy.world", "/c/dude@lemmy.world"},
[]string{"https://lemmy.world/u/dude", "/lemmy.local/u/dude@lemmy.world", "/u/dude@lemmy.world"}, {"https://lemmy.world/u/dude", "/lemmy.local/u/dude@lemmy.world", "/u/dude@lemmy.world"},
[]string{"https://lemmy.world/u/dude@lemmy.world", "/lemmy.local/u/dude@lemmy.world", "/u/dude@lemmy.world"}, {"https://lemmy.world/u/dude@lemmy.world", "/lemmy.local/u/dude@lemmy.world", "/u/dude@lemmy.world"},
[]string{"https://lemmy.world/post/123", "/lemmy.local/post/123@lemmy.world", "/post/123@lemmy.world"}, {"https://lemmy.world/post/123", "/lemmy.local/post/123@lemmy.world", "/post/123@lemmy.world"},
[]string{"https://lemmy.world/post/123#123", "https://lemmy.world/post/123#123", "https://lemmy.world/post/123#123"}, {"https://lemmy.world/post/123#123", "https://lemmy.world/post/123#123", "https://lemmy.world/post/123#123"},
[]string{"/post/123", "/lemmy.local/post/123", "/post/123"}, {"/post/123", "/lemmy.local/post/123", "/post/123"},
[]string{"/comment/123", "/lemmy.local/comment/123", "/comment/123"}, {"/comment/123", "/lemmy.local/comment/123", "/comment/123"},
[]string{"https://lemmy.local/comment/123", "/lemmy.local/comment/123", "/comment/123"}, {"https://lemmy.local/comment/123", "/lemmy.local/comment/123", "/comment/123"},
} }
for _, url := range links { for _, url := range links {
output := LemmyLinkRewrite(`href="`+url[0]+`"`, "lemmy.local", "") output := LemmyLinkRewrite(`href="`+url[0]+`"`, "lemmy.local", "")

View file

@ -126,10 +126,7 @@ var funcMap = template.FuncMap{
}, },
"isYoutube": func(u string) bool { "isYoutube": func(u string) bool {
re := regexp.MustCompile(`^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube(-nocookie)?\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|live\/|v\/)?)([\w\-]+)(\S+)?$`) re := regexp.MustCompile(`^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube(-nocookie)?\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|live\/|v\/)?)([\w\-]+)(\S+)?$`)
if re.MatchString(u) { return re.MatchString(u)
return true
}
return false
}, },
"thumbnail": func(p lemmy.Post) string { "thumbnail": func(p lemmy.Post) string {
if p.ThumbnailURL.IsValid() { if p.ThumbnailURL.IsValid() {
@ -422,7 +419,7 @@ func PostRoot(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if re.MatchString(input) { if re.MatchString(input) {
input = "https://" + input input = "https://" + input
} }
dest, err := url.Parse(input) dest, _ := url.Parse(input)
if dest.Host != "" { if dest.Host != "" {
state, _ := Initialize(dest.Host, r) state, _ := Initialize(dest.Host, r)
if err := state.LemmyError(dest.Host); err != nil { if err := state.LemmyError(dest.Host); err != nil {
@ -432,7 +429,7 @@ func PostRoot(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if dest.RawQuery != "" { if dest.RawQuery != "" {
redirectUrl = redirectUrl + "?" + dest.RawQuery redirectUrl = redirectUrl + "?" + dest.RawQuery
} }
http.Redirect(w, r, redirectUrl, 302) http.Redirect(w, r, redirectUrl, http.StatusFound)
return return
} }
} else { } else {
@ -445,7 +442,7 @@ func GetIcon(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 - Not Found")) w.Write([]byte("404 - Not Found"))
} }
state, err := Initialize(ps.ByName("host"), r) state, _ := Initialize(ps.ByName("host"), r)
state.Client.Token = "" state.Client.Token = ""
resp, err := state.Client.Site(context.Background()) resp, err := state.Client.Site(context.Background())
if err != nil { if err != nil {
@ -469,8 +466,6 @@ func GetIcon(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "image/jpeg") w.Header().Set("Content-Type", "image/jpeg")
w.Header().Set("Cache-Control", "max-age=2592000") w.Header().Set("Cache-Control", "max-age=2592000")
io.Copy(w, iresp.Body) io.Copy(w, iresp.Body)
return
} }
func GetFrontpage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func GetFrontpage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
state, err := Initialize(ps.ByName("host"), r) state, err := Initialize(ps.ByName("host"), r)
@ -502,7 +497,7 @@ func GetCommunities(w http.ResponseWriter, r *http.Request, ps httprouter.Params
r.URL.Path = "/" + ps.ByName("host") + "/search" r.URL.Path = "/" + ps.ByName("host") + "/search"
} }
r.URL.RawQuery = "searchtype=Communities&sort=TopMonth" r.URL.RawQuery = "searchtype=Communities&sort=TopMonth"
http.Redirect(w, r, r.URL.String(), 301) http.Redirect(w, r, r.URL.String(), http.StatusFound)
} }
func ResolveId(r *http.Request, class string, id string, host string) string { func ResolveId(r *http.Request, class string, id string, host string) string {
@ -552,17 +547,17 @@ func GetPost(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if os.Getenv("LEMMY_DOMAIN") == "" { if os.Getenv("LEMMY_DOMAIN") == "" {
dest = RegReplace(dest, `https:\/\/([a-zA-Z0-9\.\-]+\/post\/\d+)`, `/$1`) dest = RegReplace(dest, `https:\/\/([a-zA-Z0-9\.\-]+\/post\/\d+)`, `/$1`)
} }
http.Redirect(w, r, dest, 302) http.Redirect(w, r, dest, http.StatusFound)
return return
} }
post, _ := resp.Post.Value() post, _ := resp.Post.Value()
if post.Post.ID > 0 { if post.Post.ID > 0 {
dest := RegReplace(r.URL.String(), `(([a-zA-Z0-9\.\-]+)?/post/)([a-zA-Z0-9\-\.@]+)`, `$1`) dest := RegReplace(r.URL.String(), `(([a-zA-Z0-9\.\-]+)?/post/)([a-zA-Z0-9\-\.@]+)`, `$1`)
dest += strconv.FormatInt(post.Post.ID, 10) dest += strconv.FormatInt(post.Post.ID, 10)
http.Redirect(w, r, dest, 302) http.Redirect(w, r, dest, http.StatusFound)
return return
} else { } else {
http.Redirect(w, r, apid, 302) http.Redirect(w, r, apid, http.StatusFound)
return return
} }
} }
@ -602,17 +597,17 @@ func GetComment(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if os.Getenv("LEMMY_DOMAIN") == "" { if os.Getenv("LEMMY_DOMAIN") == "" {
dest = RegReplace(dest, `https:\/\/([a-zA-Z0-9\.\-]+\/comment\/\d+)`, `/$1`) dest = RegReplace(dest, `https:\/\/([a-zA-Z0-9\.\-]+\/comment\/\d+)`, `/$1`)
} }
http.Redirect(w, r, dest, 302) http.Redirect(w, r, dest, http.StatusFound)
return return
} }
comment, _ := resp.Comment.Value() comment, _ := resp.Comment.Value()
if comment.Comment.ID > 0 { if comment.Comment.ID > 0 {
dest := RegReplace(r.URL.String(), `(([a-zA-Z0-9\.\-]+)?/comment/)([a-zA-Z0-9\-\.@]+)`, `$1`) dest := RegReplace(r.URL.String(), `(([a-zA-Z0-9\.\-]+)?/comment/)([a-zA-Z0-9\-\.@]+)`, `$1`)
dest += strconv.FormatInt(comment.Comment.ID, 10) dest += strconv.FormatInt(comment.Comment.ID, 10)
http.Redirect(w, r, dest, 302) http.Redirect(w, r, dest, http.StatusFound)
return return
} else { } else {
http.Redirect(w, r, apid, 302) http.Redirect(w, r, apid, http.StatusFound)
return return
} }
} }
@ -687,7 +682,7 @@ func SendMessage(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
return return
} }
r.URL.Path = "/" + state.Host + "/inbox" r.URL.Path = "/" + state.Host + "/inbox"
http.Redirect(w, r, r.URL.String(), 301) http.Redirect(w, r, r.URL.String(), http.StatusFound)
} }
func GetCreatePost(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func GetCreatePost(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
state, err := Initialize(ps.ByName("host"), r) state, err := Initialize(ps.ByName("host"), r)
@ -826,10 +821,10 @@ func Settings(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
state.Sort = r.FormValue("DefaultSortType") state.Sort = r.FormValue("DefaultSortType")
state.CommentSort = r.FormValue("DefaultCommentSortType") state.CommentSort = r.FormValue("DefaultCommentSortType")
// TODO save user settings // TODO save user settings
case "GET": // TODO fetch user settings
if state.Session != nil { //case "GET":
// TODO fetch user settings // if state.Session != nil {
} // }
} }
Render(w, "settings.html", state) Render(w, "settings.html", state)
} }
@ -910,7 +905,7 @@ func SignUpOrLogin(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
q := r.URL.Query() q := r.URL.Query()
q.Add("alert", alert) q.Add("alert", alert)
r.URL.RawQuery = q.Encode() r.URL.RawQuery = q.Encode()
http.Redirect(w, r, r.URL.String(), 301) http.Redirect(w, r, r.URL.String(), http.StatusFound)
return return
} }
} }
@ -924,7 +919,7 @@ func SignUpOrLogin(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
setCookie(w, state.Host, "user", state.User.PersonView.Person.Name+":"+userid) setCookie(w, state.Host, "user", state.User.PersonView.Person.Name+":"+userid)
setCookie(w, state.Host, "jwt", token) setCookie(w, state.Host, "jwt", token)
r.URL.Path = "/" + state.Host r.URL.Path = "/" + state.Host
http.Redirect(w, r, r.URL.String(), 301) http.Redirect(w, r, r.URL.String(), http.StatusFound)
return return
} }
} }
@ -1519,7 +1514,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
r.URL.Path = "/" + state.Host r.URL.Path = "/" + state.Host
} }
} }
http.Redirect(w, r, r.URL.String(), 301) http.Redirect(w, r, r.URL.String(), http.StatusFound)
} }
func GetLink(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func GetLink(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
ref, _ := url.Parse(r.Referer()) ref, _ := url.Parse(r.Referer())
@ -1534,7 +1529,7 @@ func GetLink(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
dest, _ = url.Parse(m["url"][0]) dest, _ = url.Parse(m["url"][0])
} }
if dest.Host == r.Host || !IsLemmy(dest.Host, RemoteAddr(r)) { if dest.Host == r.Host || !IsLemmy(dest.Host, RemoteAddr(r)) {
http.Redirect(w, r, dest.String(), 302) http.Redirect(w, r, dest.String(), http.StatusFound)
return return
} }
if host := ps.ByName("host"); host != "" { if host := ps.ByName("host"); host != "" {
@ -1542,7 +1537,7 @@ func GetLink(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if host != dest.Host && !strings.Contains(redirect, "@") { if host != dest.Host && !strings.Contains(redirect, "@") {
redirect += ("@" + dest.Host) redirect += ("@" + dest.Host)
} }
http.Redirect(w, r, redirect, 302) http.Redirect(w, r, redirect, http.StatusFound)
return return
} }
if host := os.Getenv("LEMMY_DOMAIN"); host != "" { if host := os.Getenv("LEMMY_DOMAIN"); host != "" {
@ -1550,7 +1545,7 @@ func GetLink(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if host != dest.Host && !strings.Contains(redirect, "@") { if host != dest.Host && !strings.Contains(redirect, "@") {
redirect += ("@" + dest.Host) redirect += ("@" + dest.Host)
} }
http.Redirect(w, r, redirect, 302) http.Redirect(w, r, redirect, http.StatusFound)
return return
} }
} }

View file

@ -125,7 +125,7 @@ func (s State) UserBlocked() bool {
} }
func (s State) Unknown() string { func (s State) Unknown() string {
fmt.Println(fmt.Sprintf("%v", s.Error)) fmt.Printf("%v\n", s.Error)
re := regexp.MustCompile(`(.*?)@(.*?)@`) re := regexp.MustCompile(`(.*?)@(.*?)@`)
if strings.Contains(fmt.Sprintf("%v", s.Error), "couldnt_find_community") { if strings.Contains(fmt.Sprintf("%v", s.Error), "couldnt_find_community") {
matches := re.FindAllStringSubmatch(s.CommunityName+"@", -1) matches := re.FindAllStringSubmatch(s.CommunityName+"@", -1)
@ -252,7 +252,7 @@ func (state *State) LemmyError(domain string) error {
return err return err
} }
if res.StatusCode != http.StatusOK { if res.StatusCode != http.StatusOK {
return fmt.Errorf("Status Code: %v", res.StatusCode) return fmt.Errorf("status code: %v", res.StatusCode)
} }
err = json.NewDecoder(res.Body).Decode(&nodeInfo) err = json.NewDecoder(res.Body).Decode(&nodeInfo)
if err != nil { if err != nil {
@ -261,7 +261,7 @@ func (state *State) LemmyError(domain string) error {
if nodeInfo.Software.Name == "lemmy" { if nodeInfo.Software.Name == "lemmy" {
return nil return nil
} }
return errors.New("Not a lemmy instance") return errors.New("not a lemmy instance")
} }
func (state *State) GetCaptcha() { func (state *State) GetCaptcha() {
@ -312,7 +312,7 @@ func (state *State) GetSingleComment(commentid int64) {
state.Status = http.StatusInternalServerError state.Status = http.StatusInternalServerError
return return
} }
state.Comments = []Comment{Comment{ state.Comments = []Comment{{
P: cresp.CommentView, P: cresp.CommentView,
State: state, State: state,
Op: state.Op, Op: state.Op,
@ -777,7 +777,7 @@ func getChildren(parent *Comment, pool []lemmy.CommentView, postCreatorID int64)
} }
} }
for i, _ := range children { for i := range children {
getChildren(&children[i], pool, postCreatorID) getChildren(&children[i], pool, postCreatorID)
parent.ChildCount += 1 parent.ChildCount += 1
} }