From d61d68d9e4b7095ceeb71524fe1728d090a4bccc Mon Sep 17 00:00:00 2001 From: Ryan Stafford Date: Sun, 2 Jul 2023 17:29:35 -0400 Subject: [PATCH] removed gorilla cookies, single host mode, settings page, default filter, mobile viewport style, voting xhr, docker workflow --- main.go | 36 +----- routes.go | 219 +++++++++++++++++++++++++------- state.go | 42 +----- templates/comment.html | 6 +- templates/create_community.html | 4 +- templates/frontpage.html | 13 +- templates/login.html | 5 +- templates/main.html | 5 +- templates/menu.html | 15 ++- templates/nav.html | 28 ++-- templates/post.html | 14 +- templates/root.html | 1 + templates/sidebar.html | 2 +- 13 files changed, 234 insertions(+), 156 deletions(-) diff --git a/main.go b/main.go index a11fc21..cdd4cdc 100644 --- a/main.go +++ b/main.go @@ -7,9 +7,7 @@ import ( "log" "net" "net/http" - "os" - "github.com/gorilla/sessions" "github.com/julienschmidt/httprouter" "github.com/yuin/goldmark" "github.com/yuin/goldmark/extension" @@ -19,7 +17,6 @@ var watch = flag.Bool("w", false, "watch for file changes") var addr = flag.String("addr", ":80", "http service address") var md goldmark.Markdown var templates map[string]*template.Template -var store = sessions.NewCookieStore([]byte(os.Getenv("SESSIONSECRET"))) type AddHeaderTransport struct { T http.RoundTripper @@ -54,7 +51,7 @@ func init() { md = goldmark.New(goldmark.WithExtensions(extension.Linkify)) templates = make(map[string]*template.Template) if !*watch { - for _, name := range []string{"index.html", "login.html", "frontpage.html", "root.html"} { + for _, name := range []string{"index.html", "login.html", "frontpage.html", "root.html", "settings.html"} { t := template.New(name).Funcs(funcMap) glob, err := t.ParseGlob("templates/*") if err != nil { @@ -81,36 +78,7 @@ func middleware(n httprouter.Handle) httprouter.Handle { func main() { flag.Parse() log.Println("serve", *addr) - router := httprouter.New() - router.ServeFiles("/:host/static/*filepath", http.Dir("public")) - router.GET("/", GetRoot) - router.POST("/", PostRoot) - router.GET("/:host/", middleware(GetFrontpage)) - router.GET("/:host/search", middleware(Search)) - router.POST("/:host/search", middleware(UserOp)) - router.GET("/:host/inbox", middleware(Inbox)) - router.GET("/:host/login", middleware(GetLogin)) - router.POST("/:host/login", middleware(SignUpOrLogin)) - router.POST("/:host/", middleware(UserOp)) - router.GET("/:host/icon.jpg", middleware(GetIcon)) - router.GET("/:host/c/:community", middleware(GetFrontpage)) - router.POST("/:host/c/:community", middleware(UserOp)) - router.GET("/:host/c/:community/search", middleware(Search)) - router.GET("/:host/post/:postid", middleware(GetPost)) - router.POST("/:host/post/:postid", middleware(UserOp)) - router.GET("/:host/comment/:commentid", middleware(GetComment)) - router.GET("/:host/comment/:commentid/:op", middleware(GetComment)) - router.POST("/:host/comment/:commentid", middleware(UserOp)) - router.GET("/:host/u/:username", middleware(GetUser)) - router.GET("/:host/u/:username/message", middleware(GetMessageForm)) - router.POST("/:host/u/:username/message", middleware(SendMessage)) - router.POST("/:host/u/:username", middleware(UserOp)) - router.GET("/:host/u/:username/search", middleware(Search)) - router.GET("/:host/create_post", middleware(GetCreatePost)) - router.POST("/:host/create_post", middleware(UserOp)) - router.GET("/:host/create_community", middleware(GetCreateCommunity)) - router.POST("/:host/create_community", middleware(UserOp)) - + router := GetRouter() err := http.ListenAndServe(*addr, router) if err != nil { log.Fatal("ListenAndServe: ", err) diff --git a/routes.go b/routes.go index 3198353..7d397aa 100644 --- a/routes.go +++ b/routes.go @@ -11,6 +11,7 @@ import ( "io" "net/http" "net/url" + "os" "regexp" "strconv" "strings" @@ -24,6 +25,12 @@ import ( ) var funcMap = template.FuncMap{ + "host": func(host string) string { + if l := os.Getenv("LEMMY_DOMAIN"); l != "" { + return l + } + return host + }, "proxy": func(s string) string { u, err := url.Parse(s) if err != nil { @@ -68,7 +75,7 @@ var funcMap = template.FuncMap{ } return false }, - "host": func(p Post) string { + "domain": func(p Post) string { if p.Post.URL.IsValid() { l, err := url.Parse(p.Post.URL.String()) if err != nil { @@ -111,8 +118,10 @@ var funcMap = template.FuncMap{ } converted := buf.String() converted = strings.Replace(converted, ` 0 { -// state.GetComment() -// state.GetPost() -// state.GetCommunity() -// return -// } -// -// if state.UserName != "" { -// state.GetUser() -// return -// } -// -// if state.PostID == 0 { -// state.GetPosts() -// } else { -// state.GetPost() -// state.GetComments() -// } -// -// if state.CommunityName != "" { -// state.GetCommunity() -// } -// -//} - func (state *State) LemmyError(domain string) error { var nodeInfo NodeInfo res, err := state.HTTPClient.Get("https://" + domain + "/nodeinfo/2.0.json") @@ -255,6 +220,9 @@ func (state *State) GetSite() { } func (state *State) GetComment(commentid int) { + if state.Sort != "Hot" && state.Sort != "Top" && state.Sort != "Old" && state.Sort != "New" { + state.Sort = "Hot" + } state.CommentID = commentid cresp, err := state.Client.Comments(context.Background(), types.GetComments{ ParentID: types.NewOptional(state.CommentID), @@ -284,6 +252,9 @@ func (state *State) GetComment(commentid int) { } } func (state *State) GetComments() { + if state.Sort != "Hot" && state.Sort != "Top" && state.Sort != "Old" && state.Sort != "New" { + state.Sort = "Hot" + } cresp, err := state.Client.Comments(context.Background(), types.GetComments{ PostID: types.NewOptional(state.PostID), Sort: types.NewOptional(types.CommentSortType(state.Sort)), @@ -293,6 +264,7 @@ func (state *State) GetComments() { }) if err != nil { state.Status = http.StatusInternalServerError + fmt.Println(err) return } state.CommentCount = len(cresp.Comments) diff --git a/templates/comment.html b/templates/comment.html index f646bb4..604d4db 100644 --- a/templates/comment.html +++ b/templates/comment.html @@ -1,8 +1,8 @@
{{ if .State.Session }} -
- {{ end }} diff --git a/templates/create_community.html b/templates/create_community.html index f24c009..9a63f1a 100644 --- a/templates/create_community.html +++ b/templates/create_community.html @@ -1,8 +1,8 @@ {{ $c := .Community }} -
+
- +
diff --git a/templates/frontpage.html b/templates/frontpage.html index 775a680..fa1611a 100644 --- a/templates/frontpage.html +++ b/templates/frontpage.html @@ -1,8 +1,9 @@ - {{ if and .Community (ne .Community.CommunityView.Community.Title "")}}{{.Community.CommunityView.Community.Title}}{{else if ne .CommunityName ""}}/c/{{.CommunityName}}{{ else if .User}}overview for {{.User.PersonView.Person.Name}}{{else}}{{ .Host }}{{end}} + {{ if and .Community (ne .Community.CommunityView.Community.Title "")}}{{.Community.CommunityView.Community.Title}}{{else if ne .CommunityName ""}}/c/{{.CommunityName}}{{ else if .User}}overview for {{.User.PersonView.Person.Name}}{{else}}{{ host .Host }}{{end}} + {{ template "nav.html" . -}} - {{ template "sidebar.html" . }} {{ if or (contains .Sort "Top") (and (not .PostID) (not .User) (not .Community) (not .Activities) (eq .Op ""))}} {{ template "menu.html" . }} {{ end}} @@ -31,11 +31,7 @@ {{ template "post.html" . }} {{ end }} -{{ if and .Session (not .Posts) (not .Community) (and .Listing "Subscribed") }} -

This is your home

-

When you find a community that you like, click the join button

-

Click here to find communities or check out what's popular

-{{ else if or (and (not .Op) (not .Activities) (not .Comments) (not .Posts) (not .Communities)) (and (not .Comments) .PostID) (and (not .Activities) (not .Query) .User) }} +{{ if or (and (not .Op) (not .Activities) (not .Comments) (not .Posts) (not .Communities)) (and (not .Comments) .PostID) (and (not .Activities) (not .Query) .User) }}
there doesn't seem to be anything here
{{ end }} @@ -46,7 +42,8 @@
{{ end }} - + {{ template "sidebar.html" . }} + diff --git a/templates/login.html b/templates/login.html index 9b7fd28..0fdb736 100644 --- a/templates/login.html +++ b/templates/login.html @@ -1,8 +1,9 @@ - {{ .Host }}: sign up or log in + {{ host .Host }}: sign up or log in +
- {{.Host}} - sign up or login + {{ host .Host}} - sign up or login {{ if .Alert }}
diff --git a/templates/main.html b/templates/main.html index cb5714a..c6bfbb6 100644 --- a/templates/main.html +++ b/templates/main.html @@ -1,6 +1,7 @@ - {{if and .Posts .PostID }}{{ (index .Posts 0).Post.Name}} : {{.CommunityName}}{{else if and .Community (ne .Community.CommunityView.Community.Title "")}}{{.Community.CommunityView.Community.Title}}{{else if ne .CommunityName ""}}/c/{{.CommunityName}}{{ else if .User}}overview for {{.User.PersonView.Person.Name}}{{else}}{{ .Host }}{{end}} + {{if and .Posts .PostID }}{{ (index .Posts 0).Post.Name}} : {{.CommunityName}}{{else if and .Community (ne .Community.CommunityView.Community.Title "")}}{{.Community.CommunityView.Community.Title}}{{else if ne .CommunityName ""}}/c/{{.CommunityName}}{{ else if .User}}overview for {{.User.PersonView.Person.Name}}{{else}}{{ host .Host }}{{end}} + @@ -17,7 +18,6 @@ {{ template "nav.html" . -}} - {{ template "sidebar.html" . }} {{ if or (contains .Sort "Top") (and (not .PostID) (not .User) (not .Community) (not .Activities) (eq .Op ""))}} {{ template "menu.html" . }} {{ end}} @@ -118,6 +118,7 @@ {{ end }} + {{ template "sidebar.html" . }} diff --git a/templates/menu.html b/templates/menu.html index 0043659..a58f031 100644 --- a/templates/menu.html +++ b/templates/menu.html @@ -1,4 +1,12 @@ diff --git a/templates/nav.html b/templates/nav.html index 8b237cd..e856413 100644 --- a/templates/nav.html +++ b/templates/nav.html @@ -11,6 +11,21 @@ {{ end }} more »
+
+{{ if .Session }} + {{ .Session.UserName }} + | + + | + settings + | + +{{else}} + Want to join? Log in or sign up in seconds + | + settings +{{end}} +
@@ -21,7 +36,7 @@ {{ else if .User }} {{fullname .User.PersonView.Person}} {{ else }} - {{.Host}} + {{ host .Host}} {{- end -}} {{- if eq .Op "create_post" "create_community" -}} : submit @@ -49,15 +64,4 @@ {{ end }} {{ end }} -
- {{ if .Session }} - {{ .Session.UserName }} - | - - | -
- {{else}} - Want to join? Log in or sign up in seconds - {{end}} -
diff --git a/templates/post.html b/templates/post.html index 542dcb6..f9d0a7c 100644 --- a/templates/post.html +++ b/templates/post.html @@ -1,10 +1,12 @@ +{{ if not .State.XHR }}
-{{ if gt .Rank 0 }} + {{ if gt .Rank 0 }}
{{ .Rank }}
+ {{ end }} +
{{ end }} -
{{ if .State.Session }} - {{ else }}
{{ .Counts.Score }}
{{ end }} +{{ if not .State.XHR}}
{{ .Post.Name }} - ({{ host . }}) + ({{ domain . }})
{{ if or (and .Post.Body.IsValid (ne .Post.Body.String "")) (isImage .Post.URL.String) }}
@@ -66,3 +69,4 @@
+{{ end }} diff --git a/templates/root.html b/templates/root.html index 749b411..cee9782 100644 --- a/templates/root.html +++ b/templates/root.html @@ -3,6 +3,7 @@ mlmym +