Add support for scopes

- Add scope selection for for new post
- Save new post scope in db
- Copy scope on reply
- Show scope icon on posts
This commit is contained in:
r 2019-12-21 13:26:31 +00:00
parent 3af4361927
commit 2678f33157
13 changed files with 126 additions and 70 deletions

View File

@ -1,5 +1,10 @@
package model package model
type PostContext struct {
DefaultVisibility string
ReplyContext *ReplyContext
}
type ReplyContext struct { type ReplyContext struct {
InReplyToID string InReplyToID string
InReplyToName string InReplyToName string

View File

@ -17,7 +17,6 @@ type Session struct {
type SessionRepository interface { type SessionRepository interface {
Add(session Session) (err error) Add(session Session) (err error)
Update(sessionID string, accessToken string) (err error)
Get(sessionID string) (session Session, err error) Get(sessionID string) (session Session, err error)
} }

View File

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

View File

@ -16,39 +16,41 @@ func NewNavbarTemplateData(notificationCount int) *NavbarTemplateData {
} }
type TimelinePageTemplateData struct { type TimelinePageTemplateData struct {
Statuses []*mastodon.Status Statuses []*mastodon.Status
HasNext bool HasNext bool
NextLink string NextLink string
HasPrev bool HasPrev bool
PrevLink string PrevLink string
NavbarData *NavbarTemplateData PostContext model.PostContext
NavbarData *NavbarTemplateData
} }
func NewTimelinePageTemplateData(statuses []*mastodon.Status, hasNext bool, nextLink string, hasPrev bool, func NewTimelinePageTemplateData(statuses []*mastodon.Status, hasNext bool, nextLink string, hasPrev bool,
prevLink string, navbarData *NavbarTemplateData) *TimelinePageTemplateData { prevLink string, postContext model.PostContext, navbarData *NavbarTemplateData) *TimelinePageTemplateData {
return &TimelinePageTemplateData{ return &TimelinePageTemplateData{
Statuses: statuses, Statuses: statuses,
HasNext: hasNext, HasNext: hasNext,
NextLink: nextLink, NextLink: nextLink,
HasPrev: hasPrev, HasPrev: hasPrev,
PrevLink: prevLink, PrevLink: prevLink,
NavbarData: navbarData, PostContext: postContext,
NavbarData: navbarData,
} }
} }
type ThreadPageTemplateData struct { type ThreadPageTemplateData struct {
Statuses []*mastodon.Status Statuses []*mastodon.Status
ReplyContext *model.ReplyContext PostContext model.PostContext
ReplyMap map[string][]mastodon.ReplyInfo ReplyMap map[string][]mastodon.ReplyInfo
NavbarData *NavbarTemplateData NavbarData *NavbarTemplateData
} }
func NewThreadPageTemplateData(statuses []*mastodon.Status, replyContext *model.ReplyContext, replyMap map[string][]mastodon.ReplyInfo, navbarData *NavbarTemplateData) *ThreadPageTemplateData { func NewThreadPageTemplateData(statuses []*mastodon.Status, postContext model.PostContext, replyMap map[string][]mastodon.ReplyInfo, navbarData *NavbarTemplateData) *ThreadPageTemplateData {
return &ThreadPageTemplateData{ return &ThreadPageTemplateData{
Statuses: statuses, Statuses: statuses,
ReplyContext: replyContext, PostContext: postContext,
ReplyMap: replyMap, ReplyMap: replyMap,
NavbarData: navbarData, NavbarData: navbarData,
} }
} }

View File

@ -23,17 +23,9 @@ func NewAuthService(sessionRepo model.SessionRepository, appRepo model.AppReposi
return &authService{sessionRepo, appRepo, s} return &authService{sessionRepo, appRepo, s}
} }
func getSessionID(ctx context.Context) (sessionID string, err error) { func (s *authService) getClient(ctx context.Context) (c *model.Client, err error) {
sessionID, ok := ctx.Value("session_id").(string) sessionID, ok := ctx.Value("session_id").(string)
if !ok || len(sessionID) < 1 { if !ok || len(sessionID) < 1 {
return "", ErrInvalidSession
}
return sessionID, nil
}
func (s *authService) getClient(ctx context.Context) (c *model.Client, err error) {
sessionID, err := getSessionID(ctx)
if err != nil {
return nil, ErrInvalidSession return nil, ErrInvalidSession
} }
session, err := s.sessionRepo.Get(sessionID) session, err := s.sessionRepo.Get(sessionID)
@ -50,7 +42,7 @@ func (s *authService) getClient(ctx context.Context) (c *model.Client, err error
ClientSecret: client.ClientSecret, ClientSecret: client.ClientSecret,
AccessToken: session.AccessToken, AccessToken: session.AccessToken,
}) })
c = &model.Client{Client: mc} c = &model.Client{Client: mc, Session: session}
return c, nil return c, nil
} }
@ -61,21 +53,18 @@ func (s *authService) GetAuthUrl(ctx context.Context, instance string) (
func (s *authService) GetUserToken(ctx context.Context, sessionID string, c *model.Client, func (s *authService) GetUserToken(ctx context.Context, sessionID string, c *model.Client,
code string) (token string, err error) { code string) (token string, err error) {
sessionID, err = getSessionID(ctx)
if err != nil {
return
}
c, err = s.getClient(ctx) c, err = s.getClient(ctx)
if err != nil { if err != nil {
return return
} }
token, err = s.Service.GetUserToken(ctx, sessionID, c, code) token, err = s.Service.GetUserToken(ctx, c.Session.ID, c, code)
if err != nil { if err != nil {
return return
} }
err = s.sessionRepo.Update(sessionID, token) c.Session.AccessToken = token
err = s.sessionRepo.Add(c.Session)
if err != nil { if err != nil {
return return
} }
@ -168,12 +157,12 @@ func (s *authService) UnRetweet(ctx context.Context, client io.Writer, c *model.
return s.Service.UnRetweet(ctx, client, c, id) return s.Service.UnRetweet(ctx, client, c, id)
} }
func (s *authService) PostTweet(ctx context.Context, client io.Writer, c *model.Client, content string, replyToID string, files []*multipart.FileHeader) (id string, err error) { func (s *authService) PostTweet(ctx context.Context, client io.Writer, c *model.Client, content string, replyToID string, visibility string, files []*multipart.FileHeader) (id string, err error) {
c, err = s.getClient(ctx) c, err = s.getClient(ctx)
if err != nil { if err != nil {
return return
} }
return s.Service.PostTweet(ctx, client, c, content, replyToID, files) return s.Service.PostTweet(ctx, client, c, content, replyToID, visibility, files)
} }
func (s *authService) Follow(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) { func (s *authService) Follow(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {

View File

@ -133,12 +133,12 @@ func (s *loggingService) UnRetweet(ctx context.Context, client io.Writer, c *mod
return s.Service.UnRetweet(ctx, client, c, id) return s.Service.UnRetweet(ctx, client, c, id)
} }
func (s *loggingService) PostTweet(ctx context.Context, client io.Writer, c *model.Client, content string, replyToID string, files []*multipart.FileHeader) (id string, err error) { func (s *loggingService) PostTweet(ctx context.Context, client io.Writer, c *model.Client, content string, replyToID string, visibility string, files []*multipart.FileHeader) (id string, err error) {
defer func(begin time.Time) { defer func(begin time.Time) {
s.logger.Printf("method=%v, content=%v, reply_to_id=%v, took=%v, err=%v\n", s.logger.Printf("method=%v, content=%v, reply_to_id=%v, visibility=%v, took=%v, err=%v\n",
"PostTweet", content, replyToID, time.Since(begin), err) "PostTweet", content, replyToID, visibility, time.Since(begin), err)
}(time.Now()) }(time.Now())
return s.Service.PostTweet(ctx, client, c, content, replyToID, files) return s.Service.PostTweet(ctx, client, c, content, replyToID, visibility, files)
} }
func (s *loggingService) Follow(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) { func (s *loggingService) Follow(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {

View File

@ -38,7 +38,7 @@ type Service interface {
UnLike(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) 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) UnRetweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error)
PostTweet(ctx context.Context, client io.Writer, c *model.Client, content string, replyToID string, files []*multipart.FileHeader) (id string, err error) PostTweet(ctx context.Context, client io.Writer, c *model.Client, content string, replyToID string, visibility string, files []*multipart.FileHeader) (id string, err error)
Follow(ctx context.Context, client io.Writer, c *model.Client, 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) UnFollow(ctx context.Context, client io.Writer, c *model.Client, id string) (err error)
} }
@ -251,12 +251,16 @@ func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer,
nextLink = "/timeline?max_id=" + pg.MaxID nextLink = "/timeline?max_id=" + pg.MaxID
} }
postContext := model.PostContext{
DefaultVisibility: c.Session.Settings.DefaultVisibility,
}
navbarData, err := svc.getNavbarTemplateData(ctx, client, c) navbarData, err := svc.getNavbarTemplateData(ctx, client, c)
if err != nil { if err != nil {
return return
} }
data := renderer.NewTimelinePageTemplateData(statuses, hasNext, nextLink, hasPrev, prevLink, navbarData) data := renderer.NewTimelinePageTemplateData(statuses, hasNext, nextLink, hasPrev, prevLink, postContext, navbarData)
err = svc.renderer.RenderTimelinePage(ctx, client, data) err = svc.renderer.RenderTimelinePage(ctx, client, data)
if err != nil { if err != nil {
return return
@ -276,7 +280,7 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *mo
return return
} }
var replyContext *model.ReplyContext var postContext model.PostContext
if reply { if reply {
var content string var content string
if u.ID != status.Account.ID { if u.ID != status.Account.ID {
@ -287,10 +291,19 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *mo
content += "@" + status.Mentions[i].Acct + " " content += "@" + status.Mentions[i].Acct + " "
} }
} }
replyContext = &model.ReplyContext{
InReplyToID: id, s, err := c.GetStatus(ctx, id)
InReplyToName: status.Account.Acct, if err != nil {
ReplyContent: content, return err
}
postContext = model.PostContext{
DefaultVisibility: s.Visibility,
ReplyContext: &model.ReplyContext{
InReplyToID: id,
InReplyToName: status.Account.Acct,
ReplyContent: content,
},
} }
} }
@ -314,7 +327,7 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *mo
return return
} }
data := renderer.NewThreadPageTemplateData(statuses, replyContext, replyMap, navbarData) data := renderer.NewThreadPageTemplateData(statuses, postContext, replyMap, navbarData)
err = svc.renderer.RenderThreadPage(ctx, client, data) err = svc.renderer.RenderThreadPage(ctx, client, data)
if err != nil { if err != nil {
return return
@ -469,7 +482,7 @@ func (svc *service) UnRetweet(ctx context.Context, client io.Writer, c *model.Cl
return return
} }
func (svc *service) PostTweet(ctx context.Context, client io.Writer, c *model.Client, content string, replyToID string, files []*multipart.FileHeader) (id string, err error) { func (svc *service) PostTweet(ctx context.Context, client io.Writer, c *model.Client, content string, replyToID string, visibility string, files []*multipart.FileHeader) (id string, err error) {
var mediaIds []string var mediaIds []string
for _, f := range files { for _, f := range files {
a, err := c.UploadMediaFromMultipartFileHeader(ctx, f) a, err := c.UploadMediaFromMultipartFileHeader(ctx, f)
@ -479,10 +492,17 @@ func (svc *service) PostTweet(ctx context.Context, client io.Writer, c *model.Cl
mediaIds = append(mediaIds, a.ID) 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{ tweet := &mastodon.Toot{
Status: content, Status: content,
InReplyToID: replyToID, InReplyToID: replyToID,
MediaIDs: mediaIds, MediaIDs: mediaIds,
Visibility: visibility,
} }
s, err := c.PostStatus(ctx, tweet) s, err := c.PostStatus(ctx, tweet)

View File

@ -155,9 +155,10 @@ func NewHandler(s Service, staticDir string) http.Handler {
content := getMultipartFormValue(req.MultipartForm, "content") content := getMultipartFormValue(req.MultipartForm, "content")
replyToID := getMultipartFormValue(req.MultipartForm, "reply_to_id") replyToID := getMultipartFormValue(req.MultipartForm, "reply_to_id")
visibility := getMultipartFormValue(req.MultipartForm, "visibility")
files := req.MultipartForm.File["attachments"] files := req.MultipartForm.File["attachments"]
id, err := s.PostTweet(ctx, w, nil, content, replyToID, files) id, err := s.PostTweet(ctx, w, nil, content, replyToID, visibility, files)
if err != nil { if err != nil {
s.ServeErrorPage(ctx, w, err) s.ServeErrorPage(ctx, w, err)
return return

View File

@ -246,3 +246,14 @@
font-family: inherit; font-family: inherit;
font-size: inherit; font-size: inherit;
} }
.status-visibility {
margin: 0 4px;
display: inline-block;
color: #444444;
}
.status-visibility span {
font-size: 9pt;
vertical-align: bottom;
}

View File

@ -1,15 +1,25 @@
<form class="post-form" action="/post" method="POST" enctype="multipart/form-data"> <form class="post-form" action="/post" method="POST" enctype="multipart/form-data">
{{if .}} {{if .ReplyContext}}
<input type="hidden" name="reply_to_id" value="{{.InReplyToID}}" /> <input type="hidden" name="reply_to_id" value="{{.ReplyContext.InReplyToID}}" />
<label for="post-content"> Reply to {{.InReplyToName}} </label> <label for="post-content"> Reply to {{.ReplyContext.InReplyToName}} </label>
{{else}} {{else}}
<label for="post-content"> New post </label> <label for="post-content"> New post </label>
{{end}} {{end}}
<div class="post-form-content-container"> <div class="post-form-content-container">
<textarea id="post-content" name="content" class="post-content" cols="50" rows="5">{{if .}}{{.ReplyContent}}{{end}}</textarea> <textarea id="post-content" name="content" class="post-content" cols="50" rows="5">{{if .ReplyContext}}{{.ReplyContext.ReplyContent}}{{end}}</textarea>
</div> </div>
<div> <div>
Attachments <input id="post-file-picker" type="file" name="attachments" multiple> <label for ="post-visilibity"> Visibility </label>
<select id="post-visilibity" type="file" name="visibility">
<option value="public" {{if eq .DefaultVisibility "public"}}selected{{end}}>Public</option>
<option value="unlisted" {{if eq .DefaultVisibility "unlisted"}}selected{{end}}>Unlisted</option>
<option value="private" {{if eq .DefaultVisibility "private"}}selected{{end}}>Private</option>
<option value="direct" {{if eq .DefaultVisibility "direct"}}selected{{end}}>Direct</option>
</select>
</div>
<div>
<label for ="post-file-picker"> Attachments </label>
<input id="post-file-picker" type="file" name="attachments" multiple>
</div> </div>
<button type="submit"> Post </button> <button type="submit"> Post </button>
</form> </form>

View File

@ -26,6 +26,17 @@
<a href="/user/{{.Account.ID}}" > <a href="/user/{{.Account.ID}}" >
<span class="status-uname"> {{.Account.Acct}} </span> <span class="status-uname"> {{.Account.Acct}} </span>
</a> </a>
<a class="status-visibility" href="{{.URL}}" target="_blank">
{{if eq .Visibility "public"}}
<span class="icon dripicons-web" title="Public"></span>
{{else if eq .Visibility "unlisted"}}
<span class="icon dripicons-lock-open" title="Unlisted"></span>
{{else if eq .Visibility "private"}}
<span class="icon dripicons-lock" title="Private"></span>
{{else if eq .Visibility "direct"}}
<span class="icon dripicons-mail" title="Direct"></span>
{{end}}
</a>
</div> </div>
{{end}} {{end}}
<div class="status-reply-container"> <div class="status-reply-container">
@ -68,16 +79,23 @@
<span class="icon dripicons-reply"></span> <span class="icon dripicons-reply"></span>
<span> {{DisplayInteractionCount .RepliesCount}} </span> <span> {{DisplayInteractionCount .RepliesCount}} </span>
</a> </a>
{{if .Reblogged}} {{if or (eq .Visibility "private") (eq .Visibility "direct")}}
<a class="status-retweet" href="/unretweet/{{.ID}}" title="undo repost"> <a class="status-retweet" title="this status cannot be retweeted">
<span class="icon dripicons-retweet retweeted"></span>
<span> {{DisplayInteractionCount .ReblogsCount}} </span>
</a>
{{else}}
<a class="status-retweet" href="/retweet/{{.ID}}" title="repost">
<span class="icon dripicons-retweet"></span> <span class="icon dripicons-retweet"></span>
<span> {{DisplayInteractionCount .ReblogsCount}} </span> <span> {{DisplayInteractionCount .ReblogsCount}} </span>
</a> </a>
{{else}}
{{if .Reblogged}}
<a class="status-retweet" href="/unretweet/{{.ID}}" title="undo retweet">
<span class="icon dripicons-retweet retweeted"></span>
<span> {{DisplayInteractionCount .ReblogsCount}} </span>
</a>
{{else}}
<a class="status-retweet" href="/retweet/{{.ID}}" title="retweet">
<span class="icon dripicons-retweet"></span>
<span> {{DisplayInteractionCount .ReblogsCount}} </span>
</a>
{{end}}
{{end}} {{end}}
{{if .Favourited}} {{if .Favourited}}
<a class="status-like" href="/unlike/{{.ID}}" title="unlike"> <a class="status-like" href="/unlike/{{.ID}}" title="unlike">

View File

@ -5,8 +5,8 @@
{{range .Statuses}} {{range .Statuses}}
{{template "status.tmpl" .}} {{template "status.tmpl" .}}
{{if $.ReplyContext}}{{if eq .ID $.ReplyContext.InReplyToID}} {{if $.PostContext.ReplyContext}}{{if eq .ID $.PostContext.ReplyContext.InReplyToID}}
{{template "postform.tmpl" $.ReplyContext}} {{template "postform.tmpl" $.PostContext}}
{{end}}{{end}} {{end}}{{end}}
{{end}} {{end}}

View File

@ -3,7 +3,7 @@
<div class="page-title"> Timeline </div> <div class="page-title"> Timeline </div>
{{template "postform.tmpl" }} {{template "postform.tmpl" .PostContext}}
{{range .Statuses}} {{range .Statuses}}
{{template "status.tmpl" .}} {{template "status.tmpl" .}}