From a8dbbec988cf1ed42a051d1a0dfa9987d890d440 Mon Sep 17 00:00:00 2001 From: r Date: Wed, 8 Jan 2020 18:16:06 +0000 Subject: Add fluoride mode --- service/transport.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) (limited to 'service/transport.go') 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) +} -- cgit v1.2.3