diff options
Diffstat (limited to 'service')
-rw-r--r-- | service/auth.go | 8 | ||||
-rw-r--r-- | service/logging.go | 8 | ||||
-rw-r--r-- | service/service.go | 43 | ||||
-rw-r--r-- | service/transport.go | 74 |
4 files changed, 113 insertions, 20 deletions
diff --git a/service/auth.go b/service/auth.go index 2aa71d9..2f63717 100644 --- a/service/auth.go +++ b/service/auth.go @@ -190,7 +190,7 @@ func (s *authService) SaveSettings(ctx context.Context, client io.Writer, c *mod return s.Service.SaveSettings(ctx, client, c, settings) } -func (s *authService) Like(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) { +func (s *authService) Like(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) { c, err = s.getClient(ctx) if err != nil { return @@ -198,7 +198,7 @@ func (s *authService) Like(ctx context.Context, client io.Writer, c *model.Clien return s.Service.Like(ctx, client, c, id) } -func (s *authService) UnLike(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) { +func (s *authService) UnLike(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) { c, err = s.getClient(ctx) if err != nil { return @@ -206,7 +206,7 @@ func (s *authService) UnLike(ctx context.Context, client io.Writer, c *model.Cli return s.Service.UnLike(ctx, client, c, id) } -func (s *authService) Retweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) { +func (s *authService) Retweet(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) { c, err = s.getClient(ctx) if err != nil { return @@ -214,7 +214,7 @@ func (s *authService) Retweet(ctx context.Context, client io.Writer, c *model.Cl return s.Service.Retweet(ctx, client, c, id) } -func (s *authService) UnRetweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) { +func (s *authService) UnRetweet(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) { c, err = s.getClient(ctx) if err != nil { return diff --git a/service/logging.go b/service/logging.go index 7d53f89..27c038d 100644 --- a/service/logging.go +++ b/service/logging.go @@ -166,7 +166,7 @@ func (s *loggingService) SaveSettings(ctx context.Context, client io.Writer, c * return s.Service.SaveSettings(ctx, client, c, settings) } -func (s *loggingService) Like(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) { +func (s *loggingService) Like(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) { defer func(begin time.Time) { s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n", "Like", id, time.Since(begin), err) @@ -174,7 +174,7 @@ func (s *loggingService) Like(ctx context.Context, client io.Writer, c *model.Cl return s.Service.Like(ctx, client, c, id) } -func (s *loggingService) UnLike(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) { +func (s *loggingService) UnLike(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) { defer func(begin time.Time) { s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n", "UnLike", id, time.Since(begin), err) @@ -182,7 +182,7 @@ func (s *loggingService) UnLike(ctx context.Context, client io.Writer, c *model. return s.Service.UnLike(ctx, client, c, id) } -func (s *loggingService) Retweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) { +func (s *loggingService) Retweet(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) { defer func(begin time.Time) { s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n", "Retweet", id, time.Since(begin), err) @@ -190,7 +190,7 @@ func (s *loggingService) Retweet(ctx context.Context, client io.Writer, c *model return s.Service.Retweet(ctx, client, c, id) } -func (s *loggingService) UnRetweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) { +func (s *loggingService) UnRetweet(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) { defer func(begin time.Time) { s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n", "UnRetweet", id, time.Since(begin), err) diff --git a/service/service.go b/service/service.go index 47e3de9..301e33d 100644 --- a/service/service.go +++ b/service/service.go @@ -44,10 +44,10 @@ type Service interface { ServeSearchPage(ctx context.Context, client io.Writer, c *model.Client, q string, qType string, offset int) (err error) ServeSettingsPage(ctx context.Context, client io.Writer, c *model.Client) (err error) SaveSettings(ctx context.Context, client io.Writer, c *model.Client, settings *model.Settings) (err error) - Like(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) - UnLike(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) - Retweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) - UnRetweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) + Like(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) + UnLike(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) + Retweet(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) + UnRetweet(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) PostTweet(ctx context.Context, client io.Writer, c *model.Client, content string, replyToID string, format string, visibility string, isNSFW bool, files []*multipart.FileHeader) (id string, err error) Follow(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) UnFollow(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) @@ -795,6 +795,7 @@ func (svc *service) getCommonData(ctx context.Context, client io.Writer, c *mode Title: "Web", NotificationCount: 0, CustomCSS: svc.customCSS, + FluorideMode: c.Session.Settings.FluorideMode, } if c != nil && c.Session.IsLoggedIn() { @@ -826,23 +827,41 @@ func (svc *service) getCommonData(ctx context.Context, client io.Writer, c *mode return } -func (svc *service) Like(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) { - _, err = c.Favourite(ctx, id) +func (svc *service) Like(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) { + s, err := c.Favourite(ctx, id) + if err != nil { + return + } + count = s.FavouritesCount return } -func (svc *service) UnLike(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) { - _, err = c.Unfavourite(ctx, id) +func (svc *service) UnLike(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) { + s, err := c.Unfavourite(ctx, id) + if err != nil { + return + } + count = s.FavouritesCount return } -func (svc *service) Retweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) { - _, err = c.Reblog(ctx, id) +func (svc *service) Retweet(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) { + s, err := c.Reblog(ctx, id) + if err != nil { + return + } + if s.Reblog != nil { + count = s.Reblog.ReblogsCount + } return } -func (svc *service) UnRetweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) { - _, err = c.Unreblog(ctx, id) +func (svc *service) UnRetweet(ctx context.Context, client io.Writer, c *model.Client, id string) (count int64, err error) { + s, err := c.Unreblog(ctx, id) + if err != nil { + return + } + count = s.ReblogsCount return } diff --git a/service/transport.go b/service/transport.go index 041330d..d89b854 100644 --- a/service/transport.go +++ b/service/transport.go @@ -2,6 +2,8 @@ package service import ( "context" + "encoding/json" + "io" "mime/multipart" "net/http" "path" @@ -232,6 +234,70 @@ func NewHandler(s Service, staticDir string) http.Handler { w.WriteHeader(http.StatusFound) }).Methods(http.MethodPost) + r.HandleFunc("/fluoride/like/{id}", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + id, _ := mux.Vars(req)["id"] + count, err := s.Like(ctx, w, nil, id) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + + err = serveJson(w, count) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + }).Methods(http.MethodPost) + + r.HandleFunc("/fluoride/unlike/{id}", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + id, _ := mux.Vars(req)["id"] + count, err := s.UnLike(ctx, w, nil, id) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + + err = serveJson(w, count) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + }).Methods(http.MethodPost) + + r.HandleFunc("/fluoride/retweet/{id}", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + id, _ := mux.Vars(req)["id"] + count, err := s.Retweet(ctx, w, nil, id) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + + err = serveJson(w, count) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + }).Methods(http.MethodPost) + + r.HandleFunc("/fluoride/unretweet/{id}", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + id, _ := mux.Vars(req)["id"] + count, err := s.UnRetweet(ctx, w, nil, id) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + + err = serveJson(w, count) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + }).Methods(http.MethodPost) + r.HandleFunc("/post", func(w http.ResponseWriter, req *http.Request) { ctx := getContextWithSession(context.Background(), req) @@ -381,11 +447,13 @@ func NewHandler(s Service, staticDir string) http.Handler { copyScope := req.FormValue("copy_scope") == "true" threadInNewTab := req.FormValue("thread_in_new_tab") == "true" maskNSFW := req.FormValue("mask_nsfw") == "true" + fluorideMode := req.FormValue("fluoride_mode") == "true" settings := &model.Settings{ DefaultVisibility: visibility, CopyScope: copyScope, ThreadInNewTab: threadInNewTab, MaskNSFW: maskNSFW, + FluorideMode: fluorideMode, } err := s.SaveSettings(ctx, w, nil, settings) @@ -430,3 +498,9 @@ func getMultipartFormValue(mf *multipart.Form, key string) (val string) { } return vals[0] } + +func serveJson(w io.Writer, data interface{}) (err error) { + var d = make(map[string]interface{}) + d["data"] = data + return json.NewEncoder(w).Encode(d) +} |