From 17e55d2a9bc7d0f5ed922370b8ac3ad4db9f583b Mon Sep 17 00:00:00 2001 From: r Date: Thu, 30 Jan 2020 15:32:37 +0000 Subject: Add user search page --- mastodon/status.go | 7 +++++-- renderer/model.go | 8 ++++++++ renderer/renderer.go | 6 ++++++ service/auth.go | 9 +++++++++ service/logging.go | 9 +++++++++ service/service.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- service/transport.go | 25 +++++++++++++++++++++++++ static/style.css | 6 +++++- templates/search.tmpl | 30 ++++++++++++++---------------- templates/user.tmpl | 3 +++ templates/usersearch.tmpl | 25 +++++++++++++++++++++++++ 11 files changed, 153 insertions(+), 20 deletions(-) create mode 100644 templates/usersearch.tmpl diff --git a/mastodon/status.go b/mastodon/status.go index 5be4f49..22f042c 100644 --- a/mastodon/status.go +++ b/mastodon/status.go @@ -284,14 +284,17 @@ 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) (*Results, error) { +func (c *Client) Search(ctx context.Context, q string, qType string, limit int, resolve bool, offset int, accountID string) (*Results, error) { + var results Results params := url.Values{} params.Set("q", q) params.Set("type", qType) params.Set("limit", fmt.Sprint(limit)) params.Set("resolve", fmt.Sprint(resolve)) params.Set("offset", fmt.Sprint(offset)) - var results Results + if len(accountID) > 0 { + params.Set("account_id", accountID) + } err := c.doAPI(ctx, http.MethodGet, "/api/v2/search", params, &results, nil) if err != nil { return nil, err diff --git a/renderer/model.go b/renderer/model.go index 8df64ab..77e6704 100644 --- a/renderer/model.go +++ b/renderer/model.go @@ -74,6 +74,14 @@ type UserData struct { DarkMode bool } +type UserSearchData struct { + *CommonData + User *mastodon.Account + Q string + Statuses []*mastodon.Status + NextLink string +} + type AboutData struct { *CommonData } diff --git a/renderer/renderer.go b/renderer/renderer.go index 2ad5ddf..cbb6c73 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -23,6 +23,7 @@ type Renderer interface { RenderThreadPage(ctx *Context, writer io.Writer, data *ThreadData) (err error) RenderNotificationPage(ctx *Context, writer io.Writer, data *NotificationData) (err error) RenderUserPage(ctx *Context, writer io.Writer, data *UserData) (err error) + RenderUserSearchPage(ctx *Context, writer io.Writer, data *UserSearchData) (err error) RenderAboutPage(ctx *Context, writer io.Writer, data *AboutData) (err error) RenderEmojiPage(ctx *Context, writer io.Writer, data *EmojiData) (err error) RenderLikedByPage(ctx *Context, writer io.Writer, data *LikedByData) (err error) @@ -87,6 +88,11 @@ func (r *renderer) RenderUserPage(ctx *Context, writer io.Writer, return r.template.ExecuteTemplate(writer, "user.tmpl", WithContext(data, ctx)) } +func (r *renderer) RenderUserSearchPage(ctx *Context, writer io.Writer, + data *UserSearchData) (err error) { + return r.template.ExecuteTemplate(writer, "usersearch.tmpl", WithContext(data, ctx)) +} + func (r *renderer) RenderAboutPage(ctx *Context, writer io.Writer, data *AboutData) (err error) { return r.template.ExecuteTemplate(writer, "about.tmpl", WithContext(data, ctx)) diff --git a/service/auth.go b/service/auth.go index 78934fd..f127f5c 100644 --- a/service/auth.go +++ b/service/auth.go @@ -162,6 +162,15 @@ func (s *as) ServeSearchPage(ctx context.Context, c *model.Client, q string, return s.Service.ServeSearchPage(ctx, c, q, qType, offset) } +func (s *as) ServeUserSearchPage(ctx context.Context, c *model.Client, + id string, q string, offset int) (err error) { + err = s.authenticateClient(ctx, c) + if err != nil { + return + } + return s.Service.ServeUserSearchPage(ctx, c, id, q, offset) +} + func (s *as) ServeSettingsPage(ctx context.Context, c *model.Client) (err error) { err = s.authenticateClient(ctx, c) if err != nil { diff --git a/service/logging.go b/service/logging.go index e4f8985..edd1035 100644 --- a/service/logging.go +++ b/service/logging.go @@ -129,6 +129,15 @@ func (s *ls) ServeSearchPage(ctx context.Context, c *model.Client, q string, return s.Service.ServeSearchPage(ctx, c, q, qType, offset) } +func (s *ls) ServeUserSearchPage(ctx context.Context, c *model.Client, + id string, q string, offset int) (err error) { + defer func(begin time.Time) { + s.logger.Printf("method=%v, took=%v, err=%v\n", + "ServeUserSearchPage", time.Since(begin), err) + }(time.Now()) + return s.Service.ServeUserSearchPage(ctx, c, id, q, offset) +} + func (s *ls) ServeSettingsPage(ctx context.Context, c *model.Client) (err error) { defer func(begin time.Time) { s.logger.Printf("method=%v, took=%v, err=%v\n", diff --git a/service/service.go b/service/service.go index fbf617a..fe025b8 100644 --- a/service/service.go +++ b/service/service.go @@ -32,6 +32,7 @@ type Service interface { ServeAboutPage(ctx context.Context, c *model.Client) (err error) ServeEmojiPage(ctx context.Context, c *model.Client) (err error) ServeSearchPage(ctx context.Context, c *model.Client, q string, qType string, offset int) (err error) + ServeUserSearchPage(ctx context.Context, c *model.Client, id string, q string, offset int) (err error) ServeSettingsPage(ctx context.Context, c *model.Client) (err error) NewSession(ctx context.Context, instance string) (redirectUrl string, sessionID string, err error) Signin(ctx context.Context, c *model.Client, sessionID string, code string) (token string, err error) @@ -560,6 +561,48 @@ func (svc *service) ServeUserPage(ctx context.Context, c *model.Client, return svc.renderer.RenderUserPage(rCtx, c.Writer, data) } +func (svc *service) ServeUserSearchPage(ctx context.Context, c *model.Client, + id string, q string, offset int) (err error) { + + var nextLink string + var title = "search" + + user, err := c.GetAccount(ctx, id) + if err != nil { + return + } + + results, err := c.Search(ctx, q, "statuses", 20, true, offset, id) + if err != nil { + return + } + + if len(results.Statuses) == 20 { + offset += 20 + nextLink = fmt.Sprintf("/usersearch/%s?q=%s&offset=%d", id, q, offset) + } + + if len(q) > 0 { + title += " \"" + q + "\"" + } + + commonData, err := svc.getCommonData(ctx, c, title) + if err != nil { + return + } + + data := &renderer.UserSearchData{ + CommonData: commonData, + User: user, + Q: q, + Statuses: results.Statuses, + NextLink: nextLink, + } + + rCtx := getRendererContext(c) + return svc.renderer.RenderUserSearchPage(rCtx, c.Writer, data) +} + func (svc *service) ServeAboutPage(ctx context.Context, c *model.Client) (err error) { commonData, err := svc.getCommonData(ctx, c, "about") if err != nil { @@ -600,7 +643,7 @@ func (svc *service) ServeSearchPage(ctx context.Context, c *model.Client, var nextLink string var title = "search" - results, err := c.Search(ctx, q, qType, 20, true, offset) + results, err := c.Search(ctx, q, qType, 20, true, offset, "") if err != nil { return } diff --git a/service/transport.go b/service/transport.go index fbab2e5..8f6c547 100644 --- a/service/transport.go +++ b/service/transport.go @@ -188,6 +188,30 @@ func NewHandler(s Service, staticDir string) http.Handler { } } + userSearchPage := func(w http.ResponseWriter, req *http.Request) { + c := newClient(w) + ctx := newCtxWithSesion(req) + id, _ := mux.Vars(req)["id"] + q := req.URL.Query().Get("q") + offsetStr := req.URL.Query().Get("offset") + + var offset int + var err error + if len(offsetStr) > 1 { + offset, err = strconv.Atoi(offsetStr) + if err != nil { + s.ServeErrorPage(ctx, c, err) + return + } + } + + err = s.ServeUserSearchPage(ctx, c, id, q, offset) + if err != nil { + s.ServeErrorPage(ctx, c, err) + return + } + } + aboutPage := func(w http.ResponseWriter, req *http.Request) { c := newClient(w) ctx := newCtxWithSesion(req) @@ -545,6 +569,7 @@ func NewHandler(s Service, staticDir string) http.Handler { r.HandleFunc("/followers/{id}", followersPage).Methods(http.MethodGet) r.HandleFunc("/notifications", notificationsPage).Methods(http.MethodGet) r.HandleFunc("/user/{id}", userPage).Methods(http.MethodGet) + r.HandleFunc("/usersearch/{id}", userSearchPage).Methods(http.MethodGet) r.HandleFunc("/about", aboutPage).Methods(http.MethodGet) r.HandleFunc("/emojis", emojisPage).Methods(http.MethodGet) r.HandleFunc("/search", searchPage).Methods(http.MethodGet) diff --git a/static/style.css b/static/style.css index 888fb5d..67dbd77 100644 --- a/static/style.css +++ b/static/style.css @@ -105,7 +105,7 @@ } .page-title { - font-size: 23pt; + font-size: 18pt; } .post-form { @@ -426,6 +426,10 @@ a:hover, margin: 0 8px 0 8px; } +.search-form { + margin: 16px 0 0 0; +} + .dark { background-color: #222222; background-image: none; diff --git a/templates/search.tmpl b/templates/search.tmpl index acbfbdd..96548b5 100644 --- a/templates/search.tmpl +++ b/templates/search.tmpl @@ -3,22 +3,20 @@ {{template "navigation.tmpl" (WithContext .NavbarData $.Ctx)}}
Search
-
-
- Query - - - Type - - - -
-
+
+ Query + + + Type + + + +
{{if eq .Type "statuses"}} {{range .Statuses}} diff --git a/templates/user.tmpl b/templates/user.tmpl index ca8a1ad..e3cb7e8 100644 --- a/templates/user.tmpl +++ b/templates/user.tmpl @@ -44,6 +44,9 @@ {{.User.FollowingCount}} following - {{.User.FollowersCount}} followers +
+ search statuses +
{{.User.Note}} diff --git a/templates/usersearch.tmpl b/templates/usersearch.tmpl new file mode 100644 index 0000000..8e19fd1 --- /dev/null +++ b/templates/usersearch.tmpl @@ -0,0 +1,25 @@ +{{with .Data}} +{{template "header.tmpl" (WithContext .HeaderData $.Ctx)}} +{{template "navigation.tmpl" (WithContext .NavbarData $.Ctx)}} +
Search {{EmojiFilter .User.DisplayName .User.Emojis}}'s statuses
+ +
+ Query + + + +
+ +{{range .Statuses}} +{{template "status.tmpl" (WithContext . $.Ctx)}} +{{end}} + + + +{{template "footer.tmpl"}} +{{end}} -- cgit v1.2.3