Add poll support

Currenlty only voting is possible.
This commit is contained in:
r 2020-02-09 13:42:16 +00:00
parent a68a09a83e
commit cfec7879e3
9 changed files with 182 additions and 39 deletions

View file

@ -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 {

View file

@ -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",

View file

@ -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

View file

@ -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)