From 3ebf9b3793f42647c37e88e2dd3ae97070e54607 Mon Sep 17 00:00:00 2001 From: Ryan Stafford Date: Sun, 23 Jul 2023 18:19:53 -0400 Subject: [PATCH 01/21] show mlmym version --- .gitignore | 1 + Dockerfile | 2 ++ Makefile | 23 ++++++++++++----------- main.go | 4 ++++ public/style.css | 3 +++ routes.go | 7 ++++--- state.go | 1 + templates/settings.html | 2 +- 8 files changed, 28 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index c9a2389..3d7be86 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ mlmym +VERSION *.toml *.txt diff --git a/Dockerfile b/Dockerfile index 1bd6c58..9b9ab73 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,7 @@ WORKDIR /app COPY go.* ./ RUN go mod download COPY . ./ +RUN git describe --tag > VERSION RUN go build -v -o mlmym FROM debian:bullseye-slim @@ -14,4 +15,5 @@ RUN set -x && apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install - COPY --from=builder /app/mlmym /app/mlmym COPY --from=builder /app/templates /app/templates COPY --from=builder /app/public /app/public +COPY --from=builder /app/VERSION /app/VERSION CMD ["./mlmym", "--addr", "0.0.0.0:8080"] diff --git a/Makefile b/Makefile index 0ac9db1..701866f 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,18 @@ -.PHONY: dev reload serve style +.PHONY: dev reload serve VERSION -all: - $(MAKE) -j3 --no-print-directory dev +all: mlmym -dev: reload serve style +mlmym: + go build -v -o mlmym + +dev: + $(MAKE) -j2 --no-print-directory reload serve reload: - #websocketd --port=8080 watchexec -w public echo reload &>/dev/null - websocketd --loglevel=fatal --port=8009 watchexec --no-vcs-ignore -e html,css,js -d 500 -w public 'echo "$$WATCHEXEC_WRITTEN_PATH"' + websocketd --loglevel=fatal --port=8009 watchexec -e html,css,js -d 500 'echo "$$WATCHEXEC_WRITTEN_PATH"' -serve: - #python -m http.server --directory ./public 8081 &>/dev/null - DEBUG=true watchexec -e go -r "go run . --addr 0.0.0.0:8008 -w" +VERSION: + git describe --tag > $@ -style: - npm run watchcss > /dev/null 2>&1 +serve: VERSION + DEBUG=true watchexec --no-vcs-ignore -e go -r "go run . --addr 0.0.0.0:8008 -w" diff --git a/main.go b/main.go index a7aee4f..b38cc10 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "github.com/yuin/goldmark/extension" ) +var version string var watch = flag.Bool("w", false, "watch for file changes") var addr = flag.String("addr", ":80", "http service address") var md goldmark.Markdown @@ -68,6 +69,9 @@ func init() { if os.Getenv("DEBUG") != "" { test() } + if data, err := os.ReadFile("VERSION"); err == nil { + version = string(data) + } } func test() { links := [][]string{ diff --git a/public/style.css b/public/style.css index 4cef8b7..d3f0be1 100644 --- a/public/style.css +++ b/public/style.css @@ -1057,6 +1057,9 @@ form.create input[type=file], form.create select { font-size: 13px; margin: 10px; } +.preferences div:last-child label { + text-align: left; +} .preferences label{ display: inline-block; width: 130px; diff --git a/routes.go b/routes.go index eea125b..fbe20f4 100644 --- a/routes.go +++ b/routes.go @@ -198,9 +198,10 @@ func RegReplace(input string, match string, replace string) string { func Initialize(Host string, r *http.Request) (State, error) { state := State{ - Host: Host, - Page: 1, - Status: http.StatusOK, + Host: Host, + Page: 1, + Status: http.StatusOK, + Version: version, } lemmyDomain := os.Getenv("LEMMY_DOMAIN") if lemmyDomain != "" { diff --git a/state.go b/state.go index 630a9e1..0b8a6bc 100644 --- a/state.go +++ b/state.go @@ -64,6 +64,7 @@ type Session struct { } type State struct { + Version string Client *lemmy.Client HTTPClient *http.Client Session *Session diff --git a/templates/settings.html b/templates/settings.html index 655b61b..151e208 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -129,7 +129,7 @@
- + {{ if .XHR }}{{ end }}
From d5a96181d2183e4781f4ad012a9c7686d574e822 Mon Sep 17 00:00:00 2001 From: Ryan Stafford Date: Sun, 23 Jul 2023 22:12:01 -0400 Subject: [PATCH 02/21] remove excess community api calls --- public/utils.js | 20 +++++++++++++------- state.go | 29 ++++++++++++++++++----------- templates/frontpage.html | 2 +- templates/main.html | 2 +- templates/nav.html | 10 +++++++++- 5 files changed, 42 insertions(+), 21 deletions(-) diff --git a/public/utils.js b/public/utils.js index 04c8a92..1ff1c50 100644 --- a/public/utils.js +++ b/public/utils.js @@ -245,19 +245,25 @@ function toggleMyCommunities(e) { return false } mycommunities.className = "open" - mycommunities.innerHTML = "
loading
" - request(e.target.href + "&xhr=1", "", function(res) { - mycommunities.innerHTML = '
view all »' - mycommunities.innerHTML += res - }, function() { - mycommunities.className = "" - }) + if (mycommunities.innerHTML == "") { + mycommunities.innerHTML = "
loading
" + request(e.target.href + "&xhr=1", "", function(res) { + mycommunities.innerHTML = '
view all »' + mycommunities.innerHTML += res + }, function() { + mycommunities.className = "" + }) + } return false } function openSettings(e) { e.preventDefault() var settings = document.getElementById("settingspopup") + if (settings.className == "open") { + settings.className = "" + return false + } settings.className = "open" request(e.target.href + "?xhr=1", "", function(res) { settings.innerHTML = res diff --git a/state.go b/state.go index 0b8a6bc..474d893 100644 --- a/state.go +++ b/state.go @@ -59,8 +59,9 @@ type Post struct { } type Session struct { - UserName string - UserID int + UserName string + UserID int + Communities []types.CommunityView } type State struct { @@ -267,6 +268,18 @@ func (state *State) GetSite() { return } state.Site = resp + if !state.Site.MyUser.IsValid() { + return + } + for _, c := range state.Site.MyUser.MustValue().Follows { + state.Session.Communities = append(state.Session.Communities, types.CommunityView{ + Community: c.Community, + Subscribed: "Subscribed", + }) + } + sort.Slice(state.Session.Communities, func(a, b int) bool { + return state.Session.Communities[a].Community.Name < state.Session.Communities[b].Community.Name + }) } func (state *State) GetComment(commentid int) { @@ -532,16 +545,10 @@ func (state *State) Search(searchtype string) { if state.Page > 1 { return } - state.GetSite() - for _, c := range state.Site.MyUser.MustValue().Follows { - state.Communities = append(state.Communities, types.CommunityView{ - Community: c.Community, - Subscribed: "Subscribed", - }) + if state.Site == nil { + state.GetSite() } - sort.Slice(state.Communities, func(a, b int) bool { - return state.Communities[a].Community.Name < state.Communities[b].Community.Name - }) + state.Communities = state.Session.Communities return } resp, err := state.Client.Communities(context.Background(), types.ListCommunities{ diff --git a/templates/frontpage.html b/templates/frontpage.html index 14f746f..85a4af5 100644 --- a/templates/frontpage.html +++ b/templates/frontpage.html @@ -59,6 +59,6 @@ {{ template "sidebar.html" . }} {{ end }} - + diff --git a/templates/main.html b/templates/main.html index a110926..893f703 100644 --- a/templates/main.html +++ b/templates/main.html @@ -140,7 +140,7 @@ {{ end }} {{ end }} - + {{ template "sidebar.html" . }} {{ end }} diff --git a/templates/nav.html b/templates/nav.html index d093b83..f455ace 100644 --- a/templates/nav.html +++ b/templates/nav.html @@ -1,3 +1,4 @@ +{{ $state := . }}
{{ if and .Session (ne .Op "edit_post") }} -
-
- -
- - -
+
+ {{ template "create_comment.html" .}} +
{{ end }} {{ end }} {{ end}} @@ -140,7 +124,7 @@ {{ end }} {{ end }} - + {{ if .Watch }} {{ end }} diff --git a/templates/settings.html b/templates/settings.html index c683790..969f969 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -3,16 +3,12 @@ {{ host .Host }}: preferences - +