diff --git a/mastodon/status.go b/mastodon/status.go index 48180c4..d69a895 100644 --- a/mastodon/status.go +++ b/mastodon/status.go @@ -12,6 +12,14 @@ import ( type StatusPleroma struct { InReplyToAccountAcct string `json:"in_reply_to_account_acct"` + Reactions []*ReactionsPleroma +} + +type ReactionsPleroma struct { + Accounts []Account `json:"accounts"` + Count int `json:"count"` + Me bool `json:"me"` + Name string `json:"name"` } type ReplyInfo struct { @@ -158,14 +166,24 @@ func (c *Client) GetFavouritedBy(ctx context.Context, id string, pg *Pagination) return accounts, nil } -// GetReactionBy returns the account list of the user who reacted the toot of id. (Pleroma) -func (c *Client) GetReactedBy(ctx context.Context, id string, pg *Pagination) ([]*Account, error) { - var accounts []*Account - err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/pleroma/statuses/%s/reactions", id), nil, &accounts, pg) +// GetReactionBy returns the reactions list of the user who reacted the toot of id. (Pleroma) +func (c *Client) GetReactedBy(ctx context.Context, id string) ([]*ReactionsPleroma, error) { + var reactions []*ReactionsPleroma + err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/pleroma/statuses/%s/reactions", id), nil, &reactions, nil) if err != nil { return nil, err } - return accounts, nil + return reactions, nil +} + +// PutReaction is reaction on status with unicode emoji (Pleroma) +func (c *Client) PutReaction(ctx context.Context, id string, emoji string) (*Status, error) { + var status Status + err := c.doAPI(ctx, http.MethodPut, fmt.Sprintf("/api/v1/pleroma/statuses/%s/reactions/%s", id, emoji), nil, &status, nil) + if err != nil { + return nil, err + } + return &status, nil } diff --git a/service/service.go b/service/service.go index bffd3c6..5fb57b4 100644 --- a/service/service.go +++ b/service/service.go @@ -167,7 +167,8 @@ func (s *service) TimelinePage(c *client, tType, instance, listId, maxID, minID string, tag string) (err error) { var nextLink, prevLink, title string - var statuses []*mastodon.Status + var statuses []*mastodon.Status + var reactions []*mastodon.ReactionsPleroma var pg = mastodon.Pagination{ MaxID: maxID, MinID: minID, @@ -232,6 +233,17 @@ func (s *service) TimelinePage(c *client, tType, instance, listId, maxID, if statuses[i].Reblog != nil { statuses[i].Reblog.RetweetedByID = statuses[i].ID } + if statuses[i].Pleroma.Reactions == nil && statuses[i].Account.Pleroma != nil { + if statuses[i].Reblog != nil { + reactions, err = c.GetReactedBy(c.ctx, statuses[i].Reblog.ID) + } else { + reactions, err = c.GetReactedBy(c.ctx, statuses[i].ID) + } + if err != nil { + return err + } + statuses[i].Pleroma.Reactions = reactions + } } if (len(maxID) > 0 || len(minID) > 0) && len(statuses) > 0 { @@ -991,6 +1003,15 @@ func (s *service) UnLike(c *client, id string) (count int64, err error) { return } +func (s *service) PutReact(c *client, id string, emoji string) (count int64, err error) { + st, err := c.PutReaction(c.ctx, id, emoji) + if err != nil { + return + } + count = st.FavouritesCount + return +} + func (s *service) Retweet(c *client, id string) (count int64, err error) { st, err := c.Reblog(c.ctx, id) if err != nil { diff --git a/service/transport.go b/service/transport.go index f5f4a4f..7f57c83 100644 --- a/service/transport.go +++ b/service/transport.go @@ -325,6 +325,19 @@ func NewHandler(s *service, logger *log.Logger, staticDir string) http.Handler { return nil }, CSRF, HTML) + putreact := handle(func(c *client) error { + q := c.r.URL.Query() + emoji := q.Get("emoji") + id, _ := mux.Vars(c.r)["id"] + _, err := s.PutReact(c, id, emoji) + if err != nil { + return err + } + redirect(c, c.r.FormValue("referrer")+"#status-"+id) + return nil + }, CSRF, HTML) + + retweet := handle(func(c *client) error { id, _ := mux.Vars(c.r)["id"] rid := c.r.FormValue("retweeted_by_id") @@ -737,6 +750,7 @@ func NewHandler(s *service, logger *log.Logger, staticDir string) http.Handler { r.HandleFunc("/post", post).Methods(http.MethodPost) r.HandleFunc("/like/{id}", like).Methods(http.MethodPost) r.HandleFunc("/unlike/{id}", unlike).Methods(http.MethodPost) + r.HandleFunc("/react-with/{id}", putreact).Methods(http.MethodPost) r.HandleFunc("/retweet/{id}", retweet).Methods(http.MethodPost) r.HandleFunc("/unretweet/{id}", unretweet).Methods(http.MethodPost) r.HandleFunc("/vote/{id}", vote).Methods(http.MethodPost) diff --git a/static/style.css b/static/style.css index da5d258..86cb61c 100644 --- a/static/style.css +++ b/static/style.css @@ -107,6 +107,10 @@ body { display: inline-block; } +.pleroma-reactions form { + display: inline-block; +} + .status-action a { display: inline-block; } diff --git a/templates/status.tmpl b/templates/status.tmpl index 2551e80..7a4d7a5 100644 --- a/templates/status.tmpl +++ b/templates/status.tmpl @@ -97,6 +97,18 @@ {{StatusContentFilter .Content .Emojis .Mentions | Raw}} {{end}} + {{$st_id := .ID}} + {{if .Pleroma.Reactions}} +
+ {{range .Pleroma.Reactions}} +
+ + + +
+ {{end}} +
+ {{end}} {{if .MediaAttachments}}
{{range .MediaAttachments}}