diff options
Diffstat (limited to 'service')
-rw-r--r-- | service/auth.go | 16 | ||||
-rw-r--r-- | service/logging.go | 16 | ||||
-rw-r--r-- | service/service.go | 56 | ||||
-rw-r--r-- | service/transport.go | 31 |
4 files changed, 109 insertions, 10 deletions
diff --git a/service/auth.go b/service/auth.go index 431e093..4d1b5af 100644 --- a/service/auth.go +++ b/service/auth.go @@ -157,6 +157,22 @@ func (s *authService) ServeSearchPage(ctx context.Context, client io.Writer, c * return s.Service.ServeSearchPage(ctx, client, c, q, qType, offset) } +func (s *authService) ServeSettingsPage(ctx context.Context, client io.Writer, c *model.Client) (err error) { + c, err = s.getClient(ctx) + if err != nil { + return + } + return s.Service.ServeSettingsPage(ctx, client, c) +} + +func (s *authService) SaveSettings(ctx context.Context, client io.Writer, c *model.Client, settings *model.Settings) (err error) { + c, err = s.getClient(ctx) + if err != nil { + return + } + 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) { c, err = s.getClient(ctx) if err != nil { diff --git a/service/logging.go b/service/logging.go index a5ffd57..cfd3654 100644 --- a/service/logging.go +++ b/service/logging.go @@ -133,6 +133,22 @@ func (s *loggingService) ServeSearchPage(ctx context.Context, client io.Writer, return s.Service.ServeSearchPage(ctx, client, c, q, qType, offset) } +func (s *loggingService) ServeSettingsPage(ctx context.Context, client io.Writer, c *model.Client) (err error) { + defer func(begin time.Time) { + s.logger.Printf("method=%v, took=%v, err=%v\n", + "ServeSettingsPage", time.Since(begin), err) + }(time.Now()) + return s.Service.ServeSettingsPage(ctx, client, c) +} + +func (s *loggingService) SaveSettings(ctx context.Context, client io.Writer, c *model.Client, settings *model.Settings) (err error) { + defer func(begin time.Time) { + s.logger.Printf("method=%v, took=%v, err=%v\n", + "SaveSettings", time.Since(begin), err) + }(time.Now()) + 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) { 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 157ddf8..bf7967d 100644 --- a/service/service.go +++ b/service/service.go @@ -40,6 +40,8 @@ type Service interface { ServeLikedByPage(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) ServeRetweetedByPage(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) 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) @@ -350,13 +352,19 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *mo } } - s, err := c.GetStatus(ctx, id) - if err != nil { - return err + var visibility string + if c.Session.Settings.CopyScope { + s, err := c.GetStatus(ctx, id) + if err != nil { + return err + } + visibility = s.Visibility + } else { + visibility = c.Session.Settings.DefaultVisibility } postContext = model.PostContext{ - DefaultVisibility: s.Visibility, + DefaultVisibility: visibility, Formats: svc.postFormats, ReplyContext: &model.ReplyContext{ InReplyToID: id, @@ -639,6 +647,40 @@ func (svc *service) ServeSearchPage(ctx context.Context, client io.Writer, c *mo return } +func (svc *service) ServeSettingsPage(ctx context.Context, client io.Writer, c *model.Client) (err error) { + commonData, err := svc.getCommonData(ctx, client, c) + if err != nil { + return + } + + data := &renderer.SettingsData{ + CommonData: commonData, + Settings: &c.Session.Settings, + } + + err = svc.renderer.RenderSettingsPage(ctx, client, data) + if err != nil { + return + } + + return +} + +func (svc *service) SaveSettings(ctx context.Context, client io.Writer, c *model.Client, settings *model.Settings) (err error) { + session, err := svc.sessionRepo.Get(c.Session.ID) + if err != nil { + return + } + + session.Settings = *settings + err = svc.sessionRepo.Add(session) + if err != nil { + return + } + + return +} + func (svc *service) getCommonData(ctx context.Context, client io.Writer, c *model.Client) (data *renderer.CommonData, err error) { data = new(renderer.CommonData) @@ -707,12 +749,6 @@ func (svc *service) PostTweet(ctx context.Context, client io.Writer, c *model.Cl mediaIds = append(mediaIds, a.ID) } - // save visibility if it's a non-reply post - if len(replyToID) < 1 && visibility != c.Session.Settings.DefaultVisibility { - c.Session.Settings.DefaultVisibility = visibility - svc.sessionRepo.Add(c.Session) - } - tweet := &mastodon.Toot{ Status: content, InReplyToID: replyToID, diff --git a/service/transport.go b/service/transport.go index c42462f..3ab2546 100644 --- a/service/transport.go +++ b/service/transport.go @@ -7,6 +7,7 @@ import ( "net/http" "path" "strconv" + "web/model" "github.com/gorilla/mux" ) @@ -305,6 +306,36 @@ func NewHandler(s Service, staticDir string) http.Handler { } }).Methods(http.MethodGet) + r.HandleFunc("/settings", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + + err := s.ServeSettingsPage(ctx, w, nil) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + }).Methods(http.MethodGet) + + r.HandleFunc("/settings", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + + visibility := req.FormValue("visibility") + copyScope := req.FormValue("copy_scope") == "true" + settings := &model.Settings{ + DefaultVisibility: visibility, + CopyScope: copyScope, + } + + err := s.SaveSettings(ctx, w, nil, settings) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + + w.Header().Add("Location", req.Header.Get("Referer")) + w.WriteHeader(http.StatusFound) + }).Methods(http.MethodPost) + r.HandleFunc("/signout", func(w http.ResponseWriter, req *http.Request) { // TODO remove session from database w.Header().Add("Set-Cookie", fmt.Sprintf("session_id=;max-age=0")) |