diff options
Diffstat (limited to 'service')
-rw-r--r-- | service/auth.go | 13 | ||||
-rw-r--r-- | service/logging.go | 12 | ||||
-rw-r--r-- | service/service.go | 10 | ||||
-rw-r--r-- | service/transport.go | 19 |
4 files changed, 52 insertions, 2 deletions
diff --git a/service/auth.go b/service/auth.go index 6c71439..4c5b38b 100644 --- a/service/auth.go +++ b/service/auth.go @@ -250,6 +250,19 @@ func (s *as) UnRetweet(ctx context.Context, c *model.Client, id string) (count i return s.Service.UnRetweet(ctx, c, id) } +func (s *as) Vote(ctx context.Context, c *model.Client, id string, + choices []string) (err error) { + err = s.authenticateClient(ctx, c) + if err != nil { + return + } + err = checkCSRF(ctx, c) + if err != nil { + return + } + return s.Service.Vote(ctx, c, id, choices) +} + func (s *as) Follow(ctx context.Context, c *model.Client, id string) (err error) { err = s.authenticateClient(ctx, c) if err != nil { diff --git a/service/logging.go b/service/logging.go index e429fac..055dadd 100644 --- a/service/logging.go +++ b/service/logging.go @@ -77,7 +77,7 @@ func (s *ls) ServeNotificationPage(ctx context.Context, c *model.Client, return s.Service.ServeNotificationPage(ctx, c, maxID, minID) } -func (s *ls) ServeUserPage(ctx context.Context, c *model.Client, id string, +func (s *ls) ServeUserPage(ctx context.Context, c *model.Client, id string, pageType string, maxID string, minID string) (err error) { defer func(begin time.Time) { s.logger.Printf("method=%v, id=%v, type=%v, took=%v, err=%v\n", @@ -111,7 +111,7 @@ 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, +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", @@ -189,6 +189,14 @@ func (s *ls) UnRetweet(ctx context.Context, c *model.Client, id string) (count i return s.Service.UnRetweet(ctx, c, id) } +func (s *ls) Vote(ctx context.Context, c *model.Client, id string, choices []string) (err error) { + defer func(begin time.Time) { + s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n", + "Vote", id, time.Since(begin), err) + }(time.Now()) + return s.Service.Vote(ctx, c, id, choices) +} + func (s *ls) Follow(ctx context.Context, c *model.Client, id string) (err error) { defer func(begin time.Time) { s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n", diff --git a/service/service.go b/service/service.go index d762842..ecd0d3f 100644 --- a/service/service.go +++ b/service/service.go @@ -42,6 +42,7 @@ type Service interface { UnLike(ctx context.Context, c *model.Client, id string) (count int64, err error) Retweet(ctx context.Context, c *model.Client, id string) (count int64, err error) UnRetweet(ctx context.Context, c *model.Client, id string) (count int64, err error) + Vote(ctx context.Context, c *model.Client, id string, choices []string) (err error) Follow(ctx context.Context, c *model.Client, id string) (err error) UnFollow(ctx context.Context, c *model.Client, id string) (err error) Mute(ctx context.Context, c *model.Client, id string) (err error) @@ -843,6 +844,15 @@ func (svc *service) UnRetweet(ctx context.Context, c *model.Client, id string) ( return } +func (svc *service) Vote(ctx context.Context, c *model.Client, id string, + choices []string) (err error) { + _, err = c.Vote(ctx, id, choices) + if err != nil { + return + } + return +} + func (svc *service) Follow(ctx context.Context, c *model.Client, id string) (err error) { _, err = c.AccountFollow(ctx, id) return diff --git a/service/transport.go b/service/transport.go index 81af4fa..5ce0e56 100644 --- a/service/transport.go +++ b/service/transport.go @@ -419,6 +419,24 @@ func NewHandler(s Service, staticDir string) http.Handler { w.WriteHeader(http.StatusFound) } + vote := func(w http.ResponseWriter, req *http.Request) { + c := newClient(w) + ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token")) + id, _ := mux.Vars(req)["id"] + statusID := req.FormValue("status_id") + choices, _ := req.PostForm["choices"] + + err := s.Vote(ctx, c, id, choices) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + s.ServeErrorPage(ctx, c, err) + return + } + + w.Header().Add("Location", req.Header.Get("Referer")+"#status-"+statusID) + w.WriteHeader(http.StatusFound) + } + follow := func(w http.ResponseWriter, req *http.Request) { c := newClient(w) ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token")) @@ -697,6 +715,7 @@ func NewHandler(s Service, staticDir string) http.Handler { r.HandleFunc("/unlike/{id}", unlike).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) r.HandleFunc("/follow/{id}", follow).Methods(http.MethodPost) r.HandleFunc("/unfollow/{id}", unfollow).Methods(http.MethodPost) r.HandleFunc("/mute/{id}", mute).Methods(http.MethodPost) |