From c2f237e9019a4fb31cd7d969bf59670c144592c8 Mon Sep 17 00:00:00 2001 From: r Date: Thu, 10 Feb 2022 16:03:44 +0000 Subject: Don't overwrite global config file on make install --- .gitignore | 1 + INSTALL | 1 + Makefile | 9 +++++---- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 516f77b..79904fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ bloat database +bloat.gen.conf diff --git a/INSTALL b/INSTALL index c73fd78..8f8c6d4 100644 --- a/INSTALL +++ b/INSTALL @@ -20,6 +20,7 @@ bloat looks for a file named bloat.conf in the working directory and /etc/bloat in that order. You can also specify another file by using the -f flag. Comments in the config file describe what each config value does. For most cases, you only need to change the value of "client_website". +# cp bloat.gen.conf /etc/bloat.conf # $EDITOR /etc/bloat.conf 4. Create database directory diff --git a/Makefile b/Makefile index 1b32268..c38de6b 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,10 @@ all: bloat bloat: $(SRC) $(TMPL) $(GO) build $(GOFLAGS) -o bloat main.go + sed -e "s%=database%=/var/bloat%g" \ + -e "s%=templates%=$(SHAREPATH)/templates%g" \ + -e "s%=static%=$(SHAREPATH)/static%g" \ + < bloat.conf > bloat.gen.conf install: bloat mkdir -p $(DESTDIR)$(BINPATH) \ @@ -29,10 +33,6 @@ install: bloat chmod 0644 $(DESTDIR)$(SHAREPATH)/templates/* cp -r static/* $(DESTDIR)$(SHAREPATH)/static chmod 0644 $(DESTDIR)$(SHAREPATH)/static/* - sed -e "s%=database%=/var/bloat%g" \ - -e "s%=templates%=$(SHAREPATH)/templates%g" \ - -e "s%=static%=$(SHAREPATH)/static%g" \ - < bloat.conf > /etc/bloat.conf uninstall: rm -f $(DESTDIR)$(BINPATH)/bloat @@ -40,3 +40,4 @@ uninstall: clean: rm -f bloat + rm -f bloat.gen.conf -- cgit v1.2.3 From c390a0c32720f6afe852bc7a3a3f64c3afe9e401 Mon Sep 17 00:00:00 2001 From: r Date: Fri, 11 Feb 2022 11:18:02 +0000 Subject: Add lists --- mastodon/lists.go | 4 +- mastodon/status.go | 3 +- renderer/model.go | 13 ++++++ renderer/renderer.go | 2 + service/service.go | 106 +++++++++++++++++++++++++++++++++++++++++--- service/transport.go | 76 ++++++++++++++++++++++++++++++- static/style.css | 11 +++-- templates/about.tmpl | 4 +- templates/list.tmpl | 63 ++++++++++++++++++++++++++ templates/lists.tmpl | 35 +++++++++++++++ templates/nav.tmpl | 9 ++-- templates/userlist.tmpl | 14 +----- templates/userlistitem.tmpl | 15 +++++++ 13 files changed, 322 insertions(+), 33 deletions(-) create mode 100644 templates/list.tmpl create mode 100644 templates/lists.tmpl create mode 100644 templates/userlistitem.tmpl diff --git a/mastodon/lists.go b/mastodon/lists.go index d323b79..1b76bdc 100644 --- a/mastodon/lists.go +++ b/mastodon/lists.go @@ -90,7 +90,7 @@ func (c *Client) DeleteList(ctx context.Context, id string) error { func (c *Client) AddToList(ctx context.Context, list string, accounts ...string) error { params := url.Values{} for _, acct := range accounts { - params.Add("account_ids", string(acct)) + params.Add("account_ids[]", string(acct)) } return c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/lists/%s/accounts", url.PathEscape(string(list))), params, nil, nil) @@ -100,7 +100,7 @@ func (c *Client) AddToList(ctx context.Context, list string, accounts ...string) func (c *Client) RemoveFromList(ctx context.Context, list string, accounts ...string) error { params := url.Values{} for _, acct := range accounts { - params.Add("account_ids", string(acct)) + params.Add("account_ids[]", string(acct)) } return c.doAPI(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/lists/%s/accounts", url.PathEscape(string(list))), params, nil, nil) diff --git a/mastodon/status.go b/mastodon/status.go index 8b148b3..2fae6ee 100644 --- a/mastodon/status.go +++ b/mastodon/status.go @@ -301,7 +301,7 @@ func (c *Client) DeleteStatus(ctx context.Context, id string) error { } // Search search content with query. -func (c *Client) Search(ctx context.Context, q string, qType string, limit int, resolve bool, offset int, accountID string) (*Results, error) { +func (c *Client) Search(ctx context.Context, q string, qType string, limit int, resolve bool, offset int, accountID string, following bool) (*Results, error) { var results Results params := url.Values{} params.Set("q", q) @@ -309,6 +309,7 @@ func (c *Client) Search(ctx context.Context, q string, qType string, limit int, params.Set("limit", fmt.Sprint(limit)) params.Set("resolve", fmt.Sprint(resolve)) params.Set("offset", fmt.Sprint(offset)) + params.Set("following", fmt.Sprint(following)) if len(accountID) > 0 { params.Set("account_id", accountID) } diff --git a/renderer/model.go b/renderer/model.go index 4d09338..e7cfbfb 100644 --- a/renderer/model.go +++ b/renderer/model.go @@ -62,6 +62,19 @@ type TimelineData struct { PrevLink string } +type ListsData struct { + *CommonData + Lists []*mastodon.List +} + +type ListData struct { + *CommonData + List *mastodon.List + Accounts []*mastodon.Account + Q string + SearchAccounts []*mastodon.Account +} + type ThreadData struct { *CommonData Statuses []*mastodon.Status diff --git a/renderer/renderer.go b/renderer/renderer.go index 0d6776c..20a3c05 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -19,6 +19,8 @@ const ( NavPage = "nav.tmpl" RootPage = "root.tmpl" TimelinePage = "timeline.tmpl" + ListsPage = "lists.tmpl" + ListPage = "list.tmpl" ThreadPage = "thread.tmpl" QuickReplyPage = "quickreply.tmpl" NotificationPage = "notification.tmpl" diff --git a/service/service.go b/service/service.go index 8297764..64c7bf0 100644 --- a/service/service.go +++ b/service/service.go @@ -163,8 +163,8 @@ func (s *service) NavPage(c *client) (err error) { return s.renderer.Render(c.rctx, c.w, renderer.NavPage, data) } -func (s *service) TimelinePage(c *client, tType string, instance string, - maxID string, minID string) (err error) { +func (s *service) TimelinePage(c *client, tType, instance, listId, maxID, + minID string) (err error) { var nextLink, prevLink, title string var statuses []*mastodon.Status @@ -179,24 +179,46 @@ func (s *service) TimelinePage(c *client, tType string, instance string, return errInvalidArgument case "home": statuses, err = c.GetTimelineHome(c.ctx, &pg) + if err != nil { + return err + } title = "Timeline" case "direct": statuses, err = c.GetTimelineDirect(c.ctx, &pg) + if err != nil { + return err + } title = "Direct Timeline" case "local": statuses, err = c.GetTimelinePublic(c.ctx, true, "", &pg) + if err != nil { + return err + } title = "Local Timeline" case "remote": if len(instance) > 0 { statuses, err = c.GetTimelinePublic(c.ctx, false, instance, &pg) + if err != nil { + return err + } } title = "Remote Timeline" case "twkn": statuses, err = c.GetTimelinePublic(c.ctx, false, "", &pg) + if err != nil { + return err + } title = "The Whole Known Network" - } - if err != nil { - return err + case "list": + statuses, err = c.GetTimelineList(c.ctx, listId, &pg) + if err != nil { + return err + } + list, err := c.GetList(c.ctx, listId) + if err != nil { + return err + } + title = "List Timeline - " + list.Title } for i := range statuses { @@ -211,6 +233,9 @@ func (s *service) TimelinePage(c *client, tType string, instance string, if len(instance) > 0 { v.Set("instance", instance) } + if len(listId) > 0 { + v.Set("list", listId) + } prevLink = "/timeline/" + tType + "?" + v.Encode() } @@ -220,6 +245,9 @@ func (s *service) TimelinePage(c *client, tType string, instance string, if len(instance) > 0 { v.Set("instance", instance) } + if len(listId) > 0 { + v.Set("list", listId) + } nextLink = "/timeline/" + tType + "?" + v.Encode() } @@ -252,6 +280,70 @@ func addToReplyMap(m map[string][]mastodon.ReplyInfo, key interface{}, m[keyStr] = append(m[keyStr], mastodon.ReplyInfo{val, number}) } +func (s *service) ListsPage(c *client) (err error) { + lists, err := c.GetLists(c.ctx) + if err != nil { + return + } + + cdata := s.cdata(c, "Lists", 0, 0, "") + data := renderer.ListsData{ + Lists: lists, + CommonData: cdata, + } + return s.renderer.Render(c.rctx, c.w, renderer.ListsPage, data) +} + +func (s *service) AddList(c *client, title string) (err error) { + _, err = c.CreateList(c.ctx, title) + return err +} + +func (s *service) RemoveList(c *client, id string) (err error) { + return c.DeleteList(c.ctx, id) +} + +func (s *service) RenameList(c *client, id, title string) (err error) { + _, err = c.RenameList(c.ctx, id, title) + return err +} + +func (s *service) ListPage(c *client, id string, q string) (err error) { + list, err := c.GetList(c.ctx, id) + if err != nil { + return + } + accounts, err := c.GetListAccounts(c.ctx, id) + if err != nil { + return + } + var searchAccounts []*mastodon.Account + if len(q) > 0 { + result, err := c.Search(c.ctx, q, "accounts", 20, true, 0, id, true) + if err != nil { + return err + } + searchAccounts = result.Accounts + } + cdata := s.cdata(c, "List "+list.Title, 0, 0, "") + data := renderer.ListData{ + List: list, + Accounts: accounts, + Q: q, + SearchAccounts: searchAccounts, + CommonData: cdata, + } + return s.renderer.Render(c.rctx, c.w, renderer.ListPage, data) +} + +func (s *service) ListAddUser(c *client, id string, uid string) (err error) { + return c.AddToList(c.ctx, id, uid) +} + +func (s *service) ListRemoveUser(c *client, id string, uid string) (err error) { + return c.RemoveFromList(c.ctx, id, uid) +} + func (s *service) ThreadPage(c *client, id string, reply bool) (err error) { var pctx model.PostContext @@ -608,7 +700,7 @@ func (s *service) UserSearchPage(c *client, var results *mastodon.Results if len(q) > 0 { - results, err = c.Search(c.ctx, q, "statuses", 20, true, offset, id) + results, err = c.Search(c.ctx, q, "statuses", 20, true, offset, id, false) if err != nil { return err } @@ -666,7 +758,7 @@ func (s *service) SearchPage(c *client, var results *mastodon.Results if len(q) > 0 { - results, err = c.Search(c.ctx, q, qType, 20, true, offset, "") + results, err = c.Search(c.ctx, q, qType, 20, true, offset, "", false) if err != nil { return err } diff --git a/service/transport.go b/service/transport.go index 615fe2a..4518b1a 100644 --- a/service/transport.go +++ b/service/transport.go @@ -160,9 +160,10 @@ func NewHandler(s *service, logger *log.Logger, staticDir string) http.Handler { tType, _ := mux.Vars(c.r)["type"] q := c.r.URL.Query() instance := q.Get("instance") + list := q.Get("list") maxID := q.Get("max_id") minID := q.Get("min_id") - return s.TimelinePage(c, tType, instance, maxID, minID) + return s.TimelinePage(c, tType, instance, list, maxID, minID) }, SESSION, HTML) defaultTimelinePage := handle(func(c *client) error { @@ -597,6 +598,72 @@ func NewHandler(s *service, logger *log.Logger, staticDir string) http.Handler { return nil }, CSRF, HTML) + listsPage := handle(func(c *client) error { + return s.ListsPage(c) + }, SESSION, HTML) + + addList := handle(func(c *client) error { + title := c.r.FormValue("title") + err := s.AddList(c, title) + if err != nil { + return err + } + redirect(c, c.r.FormValue("referrer")) + return nil + }, CSRF, HTML) + + removeList := handle(func(c *client) error { + id, _ := mux.Vars(c.r)["id"] + err := s.RemoveList(c, id) + if err != nil { + return err + } + redirect(c, c.r.FormValue("referrer")) + return nil + }, CSRF, HTML) + + renameList := handle(func(c *client) error { + id, _ := mux.Vars(c.r)["id"] + title := c.r.FormValue("title") + err := s.RenameList(c, id, title) + if err != nil { + return err + } + redirect(c, c.r.FormValue("referrer")) + return nil + }, CSRF, HTML) + + listPage := handle(func(c *client) error { + id, _ := mux.Vars(c.r)["id"] + q := c.r.URL.Query() + sq := q.Get("q") + return s.ListPage(c, id, sq) + }, SESSION, HTML) + + listAddUser := handle(func(c *client) error { + id, _ := mux.Vars(c.r)["id"] + q := c.r.URL.Query() + uid := q.Get("uid") + err := s.ListAddUser(c, id, uid) + if err != nil { + return err + } + redirect(c, c.r.FormValue("referrer")) + return nil + }, CSRF, HTML) + + listRemoveUser := handle(func(c *client) error { + id, _ := mux.Vars(c.r)["id"] + q := c.r.URL.Query() + uid := q.Get("uid") + err := s.ListRemoveUser(c, id, uid) + if err != nil { + return err + } + redirect(c, c.r.FormValue("referrer")) + return nil + }, CSRF, HTML) + signout := handle(func(c *client) error { s.Signout(c) setSessionCookie(c.w, "", 0) @@ -685,6 +752,13 @@ func NewHandler(s *service, logger *log.Logger, staticDir string) http.Handler { r.HandleFunc("/unbookmark/{id}", unBookmark).Methods(http.MethodPost) r.HandleFunc("/filter", filter).Methods(http.MethodPost) r.HandleFunc("/unfilter/{id}", unFilter).Methods(http.MethodPost) + r.HandleFunc("/lists", listsPage).Methods(http.MethodGet) + r.HandleFunc("/list", addList).Methods(http.MethodPost) + r.HandleFunc("/list/{id}", listPage).Methods(http.MethodGet) + r.HandleFunc("/list/{id}/remove", removeList).Methods(http.MethodPost) + r.HandleFunc("/list/{id}/rename", renameList).Methods(http.MethodPost) + r.HandleFunc("/list/{id}/adduser", listAddUser).Methods(http.MethodPost) + r.HandleFunc("/list/{id}/removeuser", listRemoveUser).Methods(http.MethodPost) r.HandleFunc("/signout", signout).Methods(http.MethodPost) r.HandleFunc("/fluoride/like/{id}", fLike).Methods(http.MethodPost) r.HandleFunc("/fluoride/unlike/{id}", fUnlike).Methods(http.MethodPost) diff --git a/static/style.css b/static/style.css index 4e2a196..10aff68 100644 --- a/static/style.css +++ b/static/style.css @@ -290,6 +290,10 @@ textarea { display: inline; } +.p-0 { + padding: 0; +} + .btn-link { border: none; outline: none; @@ -422,9 +426,6 @@ img.emoji { margin-right: 2px; } -.user-list-container { -} - .user-list-item { overflow: auto; margin: 0 0 12px 0; @@ -441,6 +442,10 @@ img.emoji { overflow: auto; } +.user-list-action { + margin: 0 12px; +} + #settings-form { margin: 8px 0; } diff --git a/templates/about.tmpl b/templates/about.tmpl index 54316cf..0e4d001 100644 --- a/templates/about.tmpl +++ b/templates/about.tmpl @@ -46,11 +46,11 @@ 6 - Settings + Lists 7 - Signout + Settings 8 diff --git a/templates/list.tmpl b/templates/list.tmpl new file mode 100644 index 0000000..1b15278 --- /dev/null +++ b/templates/list.tmpl @@ -0,0 +1,63 @@ +{{with .Data}} +{{template "header.tmpl" (WithContext .CommonData $.Ctx)}} +
List {{.List.Title}}
+ +
+ + + + +
+ +
Users
+{{if .Accounts}} + +{{range .Accounts}} + + + + +{{end}} +
{{template "userlistitem.tmpl" (WithContext . $.Ctx)}} +
+ + + +
+
+{{else}} +
No data found
+{{end}} + +
Add user
+
+ + + + + +
+ +{{if .Q}} +{{if .SearchAccounts}} + +{{range .SearchAccounts}} + + + + +{{end}} +
{{template "userlistitem.tmpl" (WithContext . $.Ctx)}} +
+ + + +
+
+{{else}} +
No data found
+{{end}} +{{end}} + +{{template "footer.tmpl"}} +{{end}} diff --git a/templates/lists.tmpl b/templates/lists.tmpl new file mode 100644 index 0000000..27979cb --- /dev/null +++ b/templates/lists.tmpl @@ -0,0 +1,35 @@ +{{with .Data}} +{{template "header.tmpl" (WithContext .CommonData $.Ctx)}} +
Lists
+ +{{range .Lists}} +
+ {{.Title}} timeline + - +
+ +
+ - +
+ + + +
+
+{{else}} +
No data found
+{{end}} + +
Add list
+
+ + + + + + + +
+ +{{template "footer.tmpl"}} +{{end}} diff --git a/templates/nav.tmpl b/templates/nav.tmpl index ea18a5f..e7a7981 100644 --- a/templates/nav.tmpl +++ b/templates/nav.tmpl @@ -17,16 +17,17 @@ home direct local - twkn - remote + twkn + remote search
- settings + lists + settings
- +
about
diff --git a/templates/userlist.tmpl b/templates/userlist.tmpl index b8e0e5d..f206397 100644 --- a/templates/userlist.tmpl +++ b/templates/userlist.tmpl @@ -1,19 +1,7 @@ {{with .Data}}
{{range .}} -
-
- - avatar - -
-
-
{{EmojiFilter (html .DisplayName) .Emojis}}
- -
@{{.Acct}}
-
-
-
+ {{template "userlistitem.tmpl" (WithContext . $.Ctx)}} {{else}}
No data found
{{end}} diff --git a/templates/userlistitem.tmpl b/templates/userlistitem.tmpl new file mode 100644 index 0000000..51261c8 --- /dev/null +++ b/templates/userlistitem.tmpl @@ -0,0 +1,15 @@ +{{with .Data}} +
+
+ + avatar + +
+
+
{{EmojiFilter (html .DisplayName) .Emojis}}
+ +
@{{.Acct}}
+
+
+
+{{end}} -- cgit v1.2.3 From 2d49ff9fb4ea9b0e88df1621e954246bc1851de6 Mon Sep 17 00:00:00 2001 From: r Date: Wed, 30 Mar 2022 15:52:30 +0000 Subject: Switch to html/template --- renderer/renderer.go | 13 ++++++++----- service/service.go | 4 ++-- templates/header.tmpl | 2 +- templates/list.tmpl | 2 +- templates/nav.tmpl | 2 +- templates/notification.tmpl | 4 ++-- templates/requestlist.tmpl | 2 +- templates/search.tmpl | 2 +- templates/status.tmpl | 13 ++++++++----- templates/user.tmpl | 6 +++--- templates/userlistitem.tmpl | 2 +- templates/usersearch.tmpl | 4 ++-- 12 files changed, 31 insertions(+), 25 deletions(-) diff --git a/renderer/renderer.go b/renderer/renderer.go index 20a3c05..6e50270 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -1,11 +1,11 @@ package renderer import ( + "html/template" "io" "regexp" "strconv" "strings" - "text/template" "time" "bloat/mastodon" @@ -54,10 +54,7 @@ func emojiFilter(content string, emojis []mastodon.Emoji) string { var quoteRE = regexp.MustCompile("(?mU)(^|> *|\n)(>.*)( 0 { - content = spoiler + "
" + content - } +func statusContentFilter(content string, emojis []mastodon.Emoji, mentions []mastodon.Mention) string { content = quoteRE.ReplaceAllString(content, `$1$2$3`) var replacements []string for _, e := range emojis { @@ -129,6 +126,10 @@ func withContext(data interface{}, ctx *Context) TemplateData { return TemplateData{data, ctx} } +func raw(s string) template.HTML { + return template.HTML(s) +} + type Renderer interface { Render(ctx *Context, writer io.Writer, page string, data interface{}) (err error) } @@ -148,6 +149,8 @@ func NewRenderer(templateGlobPattern string) (r *renderer, err error) { "FormatTimeRFC3339": formatTimeRFC3339, "FormatTimeRFC822": formatTimeRFC822, "WithContext": withContext, + "HTML": template.HTMLEscapeString, + "Raw": raw, }).ParseGlob(templateGlobPattern) if err != nil { return diff --git a/service/service.go b/service/service.go index 64c7bf0..cda42f8 100644 --- a/service/service.go +++ b/service/service.go @@ -711,7 +711,7 @@ func (s *service) UserSearchPage(c *client, if len(results.Statuses) == 20 { offset += 20 nextLink = fmt.Sprintf("/usersearch/%s?q=%s&offset=%d", id, - url.QueryEscape(q), offset) + q, offset) } if len(q) > 0 { @@ -770,7 +770,7 @@ func (s *service) SearchPage(c *client, (qType == "statuses" && len(results.Statuses) == 20) { offset += 20 nextLink = fmt.Sprintf("/search?q=%s&type=%s&offset=%d", - url.QueryEscape(q), qType, offset) + q, qType, offset) } if len(q) > 0 { diff --git a/templates/header.tmpl b/templates/header.tmpl index 8eb53f6..1abb6dd 100644 --- a/templates/header.tmpl +++ b/templates/header.tmpl @@ -17,7 +17,7 @@ {{if .RefreshInterval}} {{end}} - {{if gt .Count 0}}({{.Count}}){{end}} {{.Title | html}} + {{if gt .Count 0}}({{.Count}}){{end}} {{.Title}} {{if .CustomCSS}} diff --git a/templates/list.tmpl b/templates/list.tmpl index 1b15278..dcc6ee8 100644 --- a/templates/list.tmpl +++ b/templates/list.tmpl @@ -33,7 +33,7 @@
- +
diff --git a/templates/nav.tmpl b/templates/nav.tmpl index e7a7981..db88aa0 100644 --- a/templates/nav.tmpl +++ b/templates/nav.tmpl @@ -8,7 +8,7 @@