Add settings page

This commit is contained in:
r 2019-12-27 08:06:43 +00:00
parent 14bb18fbc7
commit b9d7eb05be
11 changed files with 165 additions and 16 deletions

View File

@ -2,4 +2,5 @@ package model
type Settings struct {
DefaultVisibility string `json:"default_visibility"`
CopyScope bool `json:"copy_scope"`
}

View File

@ -92,10 +92,15 @@ type RetweetedByData struct {
type SearchData struct {
*CommonData
Q string
Type string
Users []*mastodon.Account
Statuses []*mastodon.Status
HasNext bool
Q string
Type string
Users []*mastodon.Account
Statuses []*mastodon.Status
HasNext bool
NextLink string
}
type SettingsData struct {
*CommonData
Settings *model.Settings
}

View File

@ -24,6 +24,7 @@ type Renderer interface {
RenderLikedByPage(ctx context.Context, writer io.Writer, data *LikedByData) (err error)
RenderRetweetedByPage(ctx context.Context, writer io.Writer, data *RetweetedByData) (err error)
RenderSearchPage(ctx context.Context, writer io.Writer, data *SearchData) (err error)
RenderSettingsPage(ctx context.Context, writer io.Writer, data *SettingsData) (err error)
}
type renderer struct {
@ -96,6 +97,10 @@ func (r *renderer) RenderSearchPage(ctx context.Context, writer io.Writer, data
return r.template.ExecuteTemplate(writer, "search.tmpl", data)
}
func (r *renderer) RenderSettingsPage(ctx context.Context, writer io.Writer, data *SettingsData) (err error) {
return r.template.ExecuteTemplate(writer, "settings.tmpl", data)
}
func EmojiFilter(content string, emojis []mastodon.Emoji) string {
var replacements []string
for _, e := range emojis {

View File

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

View File

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

View File

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

View File

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

View File

@ -386,3 +386,19 @@
vertical-align: top;
margin: 0 4px 8px 0;
}
#settings-form {
margin: 8px 0;
}
.settings-form-field {
margin: 4px 0;
}
.settings-form-field * {
vertical-align: middle;
}
#settings-form button[type=submit] {
margin-top: 8px;
}

View File

@ -20,6 +20,7 @@
<a class="nav-link" href="/about">about</a>
</div>
<div>
<a class="nav-link" href="/settings">settings</a>
<a class="nav-link" href="/signout">sign out</a>
</div>
</div>

View File

@ -21,7 +21,7 @@
</span>
{{end}}
<span class="post-form-field">
<label for="post-visilibity"> Visibility </label>
<label for="post-visilibity"> Scope </label>
<select id="post-visilibity" name="visibility">
<option value="public" {{if eq .DefaultVisibility "public"}}selected{{end}}>Public</option>
<option value="unlisted" {{if eq .DefaultVisibility "unlisted"}}selected{{end}}>Unlisted</option>

22
templates/settings.tmpl Normal file
View File

@ -0,0 +1,22 @@
{{template "header.tmpl" .HeaderData}}
{{template "navigation.tmpl" .NavbarData}}
<div class="page-title"> Settings </div>
<form id="settings-form" action="/settings" method="POST">
<div class="settings-form-field">
<label for="visibility"> Default scope </label>
<select id="visibility" name="visibility">
<option value="public" {{if eq .Settings.DefaultVisibility "public"}}selected{{end}}>Public</option>
<option value="unlisted" {{if eq .Settings.DefaultVisibility "unlisted"}}selected{{end}}>Unlisted</option>
<option value="private" {{if eq .Settings.DefaultVisibility "private"}}selected{{end}}>Private</option>
<option value="direct" {{if eq .Settings.DefaultVisibility "direct"}}selected{{end}}>Direct</option>
</select>
</div>
<div class="settings-form-field">
<input id="copy-scope" name="copy_scope" type="checkbox" value="true" {{if .Settings.CopyScope}}checked{{end}}>
<label for="copy-scope"> Copy scope when replying </label>
</div>
<button type="submit"> Save </button>
</form>
{{template "footer.tmpl"}}