diff options
Diffstat (limited to 'service')
| -rw-r--r-- | service/auth.go | 61 | ||||
| -rw-r--r-- | service/logging.go | 44 | ||||
| -rw-r--r-- | service/service.go | 37 | ||||
| -rw-r--r-- | service/transport.go | 87 | 
4 files changed, 226 insertions, 3 deletions
| diff --git a/service/auth.go b/service/auth.go index dac0338..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 { @@ -274,6 +287,54 @@ func (s *as) UnFollow(ctx context.Context, c *model.Client, id string) (err erro  	return s.Service.UnFollow(ctx, c, id)  } +func (s *as) Mute(ctx context.Context, c *model.Client, id string) (err error) { +	err = s.authenticateClient(ctx, c) +	if err != nil { +		return +	} +	err = checkCSRF(ctx, c) +	if err != nil { +		return +	} +	return s.Service.Mute(ctx, c, id) +} + +func (s *as) UnMute(ctx context.Context, c *model.Client, id string) (err error) { +	err = s.authenticateClient(ctx, c) +	if err != nil { +		return +	} +	err = checkCSRF(ctx, c) +	if err != nil { +		return +	} +	return s.Service.UnMute(ctx, c, id) +} + +func (s *as) Block(ctx context.Context, c *model.Client, id string) (err error) { +	err = s.authenticateClient(ctx, c) +	if err != nil { +		return +	} +	err = checkCSRF(ctx, c) +	if err != nil { +		return +	} +	return s.Service.Block(ctx, c, id) +} + +func (s *as) UnBlock(ctx context.Context, c *model.Client, id string) (err error) { +	err = s.authenticateClient(ctx, c) +	if err != nil { +		return +	} +	err = checkCSRF(ctx, c) +	if err != nil { +		return +	} +	return s.Service.UnBlock(ctx, c, id) +} +  func (s *as) SaveSettings(ctx context.Context, c *model.Client, settings *model.Settings) (err error) {  	err = s.authenticateClient(ctx, c)  	if err != nil { diff --git a/service/logging.go b/service/logging.go index 86aa1cb..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", @@ -205,6 +213,38 @@ func (s *ls) UnFollow(ctx context.Context, c *model.Client, id string) (err erro  	return s.Service.UnFollow(ctx, c, id)  } +func (s *ls) Mute(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", +			"Mute", id, time.Since(begin), err) +	}(time.Now()) +	return s.Service.Mute(ctx, c, id) +} + +func (s *ls) UnMute(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", +			"UnMute", id, time.Since(begin), err) +	}(time.Now()) +	return s.Service.UnMute(ctx, c, id) +} + +func (s *ls) Block(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", +			"Block", id, time.Since(begin), err) +	}(time.Now()) +	return s.Service.Block(ctx, c, id) +} + +func (s *ls) UnBlock(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", +			"UnBlock", id, time.Since(begin), err) +	}(time.Now()) +	return s.Service.UnBlock(ctx, c, id) +} +  func (s *ls) SaveSettings(ctx context.Context, c *model.Client, settings *model.Settings) (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 c9511f9..ecd0d3f 100644 --- a/service/service.go +++ b/service/service.go @@ -34,7 +34,7 @@ type Service interface {  	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,  +	Signin(ctx context.Context, c *model.Client, sessionID string,  		code string) (token string, userID string, err error)  	Post(ctx context.Context, c *model.Client, content string, replyToID string, format string,  		visibility string, isNSFW bool, files []*multipart.FileHeader) (id string, err error) @@ -42,8 +42,13 @@ 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) +	UnMute(ctx context.Context, c *model.Client, id string) (err error) +	Block(ctx context.Context, c *model.Client, id string) (err error) +	UnBlock(ctx context.Context, c *model.Client, id string) (err error)  	SaveSettings(ctx context.Context, c *model.Client, settings *model.Settings) (err error)  	MuteConversation(ctx context.Context, c *model.Client, id string) (err error)  	UnMuteConversation(ctx context.Context, c *model.Client, id string) (err error) @@ -523,6 +528,7 @@ func (svc *service) ServeUserPage(ctx context.Context, c *model.Client,  	data := &renderer.UserData{  		User:       user, +		IsCurrent:  commonData.IsCurrentUser(user.ID),  		Type:       pageType,  		Users:      users,  		Statuses:   statuses, @@ -838,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 @@ -848,6 +863,26 @@ func (svc *service) UnFollow(ctx context.Context, c *model.Client, id string) (e  	return  } +func (svc *service) Mute(ctx context.Context, c *model.Client, id string) (err error) { +	_, err = c.AccountMute(ctx, id) +	return +} + +func (svc *service) UnMute(ctx context.Context, c *model.Client, id string) (err error) { +	_, err = c.AccountUnmute(ctx, id) +	return +} + +func (svc *service) Block(ctx context.Context, c *model.Client, id string) (err error) { +	_, err = c.AccountBlock(ctx, id) +	return +} + +func (svc *service) UnBlock(ctx context.Context, c *model.Client, id string) (err error) { +	_, err = c.AccountUnblock(ctx, id) +	return +} +  func (svc *service) SaveSettings(ctx context.Context, c *model.Client,  	settings *model.Settings) (err error) { diff --git a/service/transport.go b/service/transport.go index 6316748..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")) @@ -451,6 +469,70 @@ func NewHandler(s Service, staticDir string) http.Handler {  		w.WriteHeader(http.StatusFound)  	} +	mute := func(w http.ResponseWriter, req *http.Request) { +		c := newClient(w) +		ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token")) +		id, _ := mux.Vars(req)["id"] + +		err := s.Mute(ctx, c, id) +		if err != nil { +			w.WriteHeader(http.StatusInternalServerError) +			s.ServeErrorPage(ctx, c, err) +			return +		} + +		w.Header().Add("Location", req.Header.Get("Referer")) +		w.WriteHeader(http.StatusFound) +	} + +	unMute := func(w http.ResponseWriter, req *http.Request) { +		c := newClient(w) +		ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token")) +		id, _ := mux.Vars(req)["id"] + +		err := s.UnMute(ctx, c, id) +		if err != nil { +			w.WriteHeader(http.StatusInternalServerError) +			s.ServeErrorPage(ctx, c, err) +			return +		} + +		w.Header().Add("Location", req.Header.Get("Referer")) +		w.WriteHeader(http.StatusFound) +	} + +	block := func(w http.ResponseWriter, req *http.Request) { +		c := newClient(w) +		ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token")) +		id, _ := mux.Vars(req)["id"] + +		err := s.Block(ctx, c, id) +		if err != nil { +			w.WriteHeader(http.StatusInternalServerError) +			s.ServeErrorPage(ctx, c, err) +			return +		} + +		w.Header().Add("Location", req.Header.Get("Referer")) +		w.WriteHeader(http.StatusFound) +	} + +	unBlock := func(w http.ResponseWriter, req *http.Request) { +		c := newClient(w) +		ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token")) +		id, _ := mux.Vars(req)["id"] + +		err := s.UnBlock(ctx, c, id) +		if err != nil { +			w.WriteHeader(http.StatusInternalServerError) +			s.ServeErrorPage(ctx, c, err) +			return +		} + +		w.Header().Add("Location", req.Header.Get("Referer")) +		w.WriteHeader(http.StatusFound) +	} +  	settings := func(w http.ResponseWriter, req *http.Request) {  		c := newClient(w)  		ctx := newCtxWithSesionCSRF(req, req.FormValue("csrf_token")) @@ -633,8 +715,13 @@ 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) +	r.HandleFunc("/unmute/{id}", unMute).Methods(http.MethodPost) +	r.HandleFunc("/block/{id}", block).Methods(http.MethodPost) +	r.HandleFunc("/unblock/{id}", unBlock).Methods(http.MethodPost)  	r.HandleFunc("/settings", settings).Methods(http.MethodPost)  	r.HandleFunc("/muteconv/{id}", muteConversation).Methods(http.MethodPost)  	r.HandleFunc("/unmuteconv/{id}", unMuteConversation).Methods(http.MethodPost) | 
