Add liked by and retweeted by page

This commit is contained in:
r 2019-12-26 09:11:24 +00:00
parent c34bde2d52
commit ac4ff88adb
12 changed files with 276 additions and 53 deletions

View file

@ -21,6 +21,19 @@ type CommonData struct {
NavbarData *NavbarData NavbarData *NavbarData
} }
type ErrorData struct {
*CommonData
Error string
}
type HomePageData struct {
*CommonData
}
type SigninData struct {
*CommonData
}
type TimelineData struct { type TimelineData struct {
*CommonData *CommonData
Title string Title string
@ -59,6 +72,20 @@ type AboutData struct {
} }
type EmojiData struct { type EmojiData struct {
*CommonData
Emojis []*mastodon.Emoji Emojis []*mastodon.Emoji
CommonData *CommonData }
type LikedByData struct {
*CommonData
Users []*mastodon.Account
HasNext bool
NextLink string
}
type RetweetedByData struct {
*CommonData
Users []*mastodon.Account
HasNext bool
NextLink string
} }

View file

@ -12,15 +12,17 @@ import (
) )
type Renderer interface { type Renderer interface {
RenderErrorPage(ctx context.Context, writer io.Writer, err error) RenderErrorPage(ctx context.Context, writer io.Writer, data *ErrorData)
RenderHomePage(ctx context.Context, writer io.Writer) (err error) RenderHomePage(ctx context.Context, writer io.Writer, data *HomePageData) (err error)
RenderSigninPage(ctx context.Context, writer io.Writer) (err error) RenderSigninPage(ctx context.Context, writer io.Writer, data *SigninData) (err error)
RenderTimelinePage(ctx context.Context, writer io.Writer, data *TimelineData) (err error) RenderTimelinePage(ctx context.Context, writer io.Writer, data *TimelineData) (err error)
RenderThreadPage(ctx context.Context, writer io.Writer, data *ThreadData) (err error) RenderThreadPage(ctx context.Context, writer io.Writer, data *ThreadData) (err error)
RenderNotificationPage(ctx context.Context, writer io.Writer, data *NotificationData) (err error) RenderNotificationPage(ctx context.Context, writer io.Writer, data *NotificationData) (err error)
RenderUserPage(ctx context.Context, writer io.Writer, data *UserData) (err error) RenderUserPage(ctx context.Context, writer io.Writer, data *UserData) (err error)
RenderAboutPage(ctx context.Context, writer io.Writer, data *AboutData) (err error) RenderAboutPage(ctx context.Context, writer io.Writer, data *AboutData) (err error)
RenderEmojiPage(ctx context.Context, writer io.Writer, data *EmojiData) (err error) RenderEmojiPage(ctx context.Context, writer io.Writer, data *EmojiData) (err error)
RenderLikedByPage(ctx context.Context, writer io.Writer, data *LikedByData) (err error)
RenderRetweetedByPage(ctx context.Context, writer io.Writer, data *RetweetedByData) (err error)
} }
type renderer struct { type renderer struct {
@ -44,17 +46,17 @@ func NewRenderer(templateGlobPattern string) (r *renderer, err error) {
}, nil }, nil
} }
func (r *renderer) RenderErrorPage(ctx context.Context, writer io.Writer, err error) { func (r *renderer) RenderErrorPage(ctx context.Context, writer io.Writer, errorData *ErrorData) {
r.template.ExecuteTemplate(writer, "error.tmpl", err) r.template.ExecuteTemplate(writer, "error.tmpl", errorData)
return return
} }
func (r *renderer) RenderHomePage(ctx context.Context, writer io.Writer) (err error) { func (r *renderer) RenderHomePage(ctx context.Context, writer io.Writer, homePageData *HomePageData) (err error) {
return r.template.ExecuteTemplate(writer, "homepage.tmpl", nil) return r.template.ExecuteTemplate(writer, "homepage.tmpl", homePageData)
} }
func (r *renderer) RenderSigninPage(ctx context.Context, writer io.Writer) (err error) { func (r *renderer) RenderSigninPage(ctx context.Context, writer io.Writer, signinData *SigninData) (err error) {
return r.template.ExecuteTemplate(writer, "signin.tmpl", nil) return r.template.ExecuteTemplate(writer, "signin.tmpl", signinData)
} }
func (r *renderer) RenderTimelinePage(ctx context.Context, writer io.Writer, data *TimelineData) (err error) { func (r *renderer) RenderTimelinePage(ctx context.Context, writer io.Writer, data *TimelineData) (err error) {
@ -81,6 +83,14 @@ func (r *renderer) RenderEmojiPage(ctx context.Context, writer io.Writer, data *
return r.template.ExecuteTemplate(writer, "emoji.tmpl", data) return r.template.ExecuteTemplate(writer, "emoji.tmpl", data)
} }
func (r *renderer) RenderLikedByPage(ctx context.Context, writer io.Writer, data *LikedByData) (err error) {
return r.template.ExecuteTemplate(writer, "likedby.tmpl", data)
}
func (r *renderer) RenderRetweetedByPage(ctx context.Context, writer io.Writer, data *RetweetedByData) (err error) {
return r.template.ExecuteTemplate(writer, "retweetedby.tmpl", data)
}
func EmojiFilter(content string, emojis []mastodon.Emoji) string { func EmojiFilter(content string, emojis []mastodon.Emoji) string {
var replacements []string var replacements []string
for _, e := range emojis { for _, e := range emojis {

View file

@ -133,6 +133,22 @@ func (s *authService) ServeEmojiPage(ctx context.Context, client io.Writer, c *m
return s.Service.ServeEmojiPage(ctx, client, c) return s.Service.ServeEmojiPage(ctx, client, c)
} }
func (s *authService) ServeLikedByPage(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
}
return s.Service.ServeLikedByPage(ctx, client, c, id)
}
func (s *authService) ServeRetweetedByPage(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
}
return s.Service.ServeRetweetedByPage(ctx, client, c, id)
}
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) (err error) {
c, err = s.getClient(ctx) c, err = s.getClient(ctx)
if err != nil { if err != nil {

View file

@ -109,6 +109,22 @@ func (s *loggingService) ServeEmojiPage(ctx context.Context, client io.Writer, c
return s.Service.ServeEmojiPage(ctx, client, c) return s.Service.ServeEmojiPage(ctx, client, c)
} }
func (s *loggingService) ServeLikedByPage(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",
"ServeLikedByPage", id, time.Since(begin), err)
}(time.Now())
return s.Service.ServeLikedByPage(ctx, client, c, id)
}
func (s *loggingService) ServeRetweetedByPage(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",
"ServeRetweetedByPage", id, time.Since(begin), err)
}(time.Now())
return s.Service.ServeRetweetedByPage(ctx, client, c, id)
}
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) (err error) {
defer func(begin time.Time) { defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n", s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",

View file

@ -37,6 +37,8 @@ type Service interface {
ServeUserPage(ctx context.Context, client io.Writer, c *model.Client, id string, maxID string, minID string) (err error) ServeUserPage(ctx context.Context, client io.Writer, c *model.Client, id string, maxID string, minID string) (err error)
ServeAboutPage(ctx context.Context, client io.Writer, c *model.Client) (err error) ServeAboutPage(ctx context.Context, client io.Writer, c *model.Client) (err error)
ServeEmojiPage(ctx context.Context, client io.Writer, c *model.Client) (err error) ServeEmojiPage(ctx context.Context, client io.Writer, c *model.Client) (err error)
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)
Like(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) (err error)
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)
@ -192,27 +194,50 @@ func (svc *service) GetUserToken(ctx context.Context, sessionID string, c *model
} }
func (svc *service) ServeHomePage(ctx context.Context, client io.Writer) (err error) { func (svc *service) ServeHomePage(ctx context.Context, client io.Writer) (err error) {
err = svc.renderer.RenderHomePage(ctx, client) commonData, err := svc.getCommonData(ctx, client, nil)
if err != nil { if err != nil {
return return
} }
return data := &renderer.HomePageData{
CommonData: commonData,
}
return svc.renderer.RenderHomePage(ctx, client, data)
} }
func (svc *service) ServeErrorPage(ctx context.Context, client io.Writer, err error) { func (svc *service) ServeErrorPage(ctx context.Context, client io.Writer, err error) {
svc.renderer.RenderErrorPage(ctx, client, err) var errStr string
if err != nil {
errStr = err.Error()
} }
func (svc *service) ServeSigninPage(ctx context.Context, client io.Writer) (err error) { commonData, err := svc.getCommonData(ctx, client, nil)
err = svc.renderer.RenderSigninPage(ctx, client)
if err != nil { if err != nil {
return return
} }
data := &renderer.ErrorData{
CommonData: commonData,
Error: errStr,
}
svc.renderer.RenderErrorPage(ctx, client, data)
}
func (svc *service) ServeSigninPage(ctx context.Context, client io.Writer) (err error) {
commonData, err := svc.getCommonData(ctx, client, nil)
if err != nil {
return return
} }
data := &renderer.SigninData{
CommonData: commonData,
}
return svc.renderer.RenderSigninPage(ctx, client, data)
}
func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer, func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer,
c *model.Client, timelineType string, maxID string, sinceID string, minID string) (err error) { c *model.Client, timelineType string, maxID string, sinceID string, minID string) (err error) {
@ -517,6 +542,53 @@ func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *mod
return return
} }
func (svc *service) ServeLikedByPage(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
likers, err := c.GetFavouritedBy(ctx, id, nil)
if err != nil {
return
}
commonData, err := svc.getCommonData(ctx, client, c)
if err != nil {
return
}
data := &renderer.LikedByData{
CommonData: commonData,
Users: likers,
}
err = svc.renderer.RenderLikedByPage(ctx, client, data)
if err != nil {
return
}
return
}
func (svc *service) ServeRetweetedByPage(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
retweeters, err := c.GetRebloggedBy(ctx, id, nil)
if err != nil {
return
}
commonData, err := svc.getCommonData(ctx, client, c)
if err != nil {
return
}
data := &renderer.RetweetedByData{
CommonData: commonData,
Users: retweeters,
}
err = svc.renderer.RenderRetweetedByPage(ctx, client, data)
if err != nil {
return
}
return
}
func (svc *service) getCommonData(ctx context.Context, client io.Writer, c *model.Client) (data *renderer.CommonData, err error) { func (svc *service) getCommonData(ctx context.Context, client io.Writer, c *model.Client) (data *renderer.CommonData, err error) {
data = new(renderer.CommonData) data = new(renderer.CommonData)

View file

@ -98,6 +98,28 @@ func NewHandler(s Service, staticDir string) http.Handler {
} }
}).Methods(http.MethodGet) }).Methods(http.MethodGet)
r.HandleFunc("/likedby/{id}", func(w http.ResponseWriter, req *http.Request) {
ctx := getContextWithSession(context.Background(), req)
id, _ := mux.Vars(req)["id"]
err := s.ServeLikedByPage(ctx, w, nil, id)
if err != nil {
s.ServeErrorPage(ctx, w, err)
return
}
}).Methods(http.MethodGet)
r.HandleFunc("/retweetedby/{id}", func(w http.ResponseWriter, req *http.Request) {
ctx := getContextWithSession(context.Background(), req)
id, _ := mux.Vars(req)["id"]
err := s.ServeRetweetedByPage(ctx, w, nil, id)
if err != nil {
s.ServeErrorPage(ctx, w, err)
return
}
}).Methods(http.MethodGet)
r.HandleFunc("/like/{id}", func(w http.ResponseWriter, req *http.Request) { r.HandleFunc("/like/{id}", func(w http.ResponseWriter, req *http.Request) {
ctx := getContextWithSession(context.Background(), req) ctx := getContextWithSession(context.Background(), req)
id, _ := mux.Vars(req)["id"] id, _ := mux.Vars(req)["id"]

View file

@ -63,26 +63,31 @@
width: auto; width: auto;
} }
.status-action { .status-action-container {
margin-top: 4px; margin-top: 4px;
} }
.status-action {
display: inline-block;
min-width: 64px;
}
.status-action:last-child {
min-width: unset;
}
.status-action a { .status-action a {
display: inline-block; display: inline-block;
min-width: 64px;
text-decoration: none; text-decoration: none;
color: #333333; color: #333333;
vertical-align: middle; vertical-align: middle;
} }
.status-action a:hover { .status-action a:hover,
.status-action a:hover i {
color: #777777; color: #777777;
} }
.status-action .fa {
margin: 0 4px 0 0;
}
.status-action a.status-time { .status-action a.status-time {
width: auto; width: auto;
} }
@ -368,3 +373,16 @@
margin-right: 2px; margin-right: 2px;
} }
.user-list-container {
margin: 8px 0;
}
.user-list-item a {
text-decoration: none;
}
.user-list-item>div {
display: inline-block;
vertical-align: top;
margin: 0 4px 8px 0;
}

View file

@ -1,6 +1,6 @@
{{template "header.tmpl" .HeaderData}} {{template "header.tmpl" .HeaderData}}
<div class="page-title"> Error </div> <div class="page-title"> Error </div>
<div class="error-text"> {{.}} </div> <div class="error-text"> {{.Error}} </div>
<div> <div>
<a href="/timeline/home">Home</a> <a href="/timeline/home">Home</a>
<a href="/signin">Sign In</a> <a href="/signin">Sign In</a>

7
templates/likedby.tmpl Normal file
View file

@ -0,0 +1,7 @@
{{template "header.tmpl" .HeaderData}}
{{template "navigation.tmpl" .NavbarData}}
<div class="page-title"> Liked By </div>
{{template "userlist.tmpl" .Users}}
{{template "footer.tmpl"}}

View file

@ -0,0 +1,7 @@
{{template "header.tmpl" .HeaderData}}
{{template "navigation.tmpl" .NavbarData}}
<div class="page-title"> Retweeted By </div>
{{template "userlist.tmpl" .Users}}
{{template "footer.tmpl"}}

View file

@ -85,46 +85,57 @@
{{end}} {{end}}
{{end}} {{end}}
</div> </div>
<div class="status-action-container">
<div class="status-action"> <div class="status-action">
<a class="status-you" href="/thread/{{.ID}}?reply=true#status-{{.ID}}" title="reply"> <a class="status-you" href="/thread/{{.ID}}?reply=true#status-{{.ID}}" title="reply">
<i class="fa fa-reply"></i> <i class="fa fa-reply"></i>
</a>
<a class="status-action-count" href="/thread/{{.ID}}#status-{{.ID}}">
<span> {{DisplayInteractionCount .RepliesCount}} </span> <span> {{DisplayInteractionCount .RepliesCount}} </span>
</a> </a>
</div>
<div class="status-action">
{{if or (eq .Visibility "private") (eq .Visibility "direct")}} {{if or (eq .Visibility "private") (eq .Visibility "direct")}}
<a class="status-retweet" title="this status cannot be retweeted"> <a class="status-retweet" title="this status cannot be retweeted">
<i class="fa fa-retweet"></i> <i class="fa fa-retweet"></i>
<span> {{DisplayInteractionCount .ReblogsCount}} </span>
</a> </a>
{{else}} {{else}}
{{if .Reblogged}} {{if .Reblogged}}
<a class="status-retweet" href="/unretweet/{{.ID}}" title="undo retweet"> <a class="status-retweet" href="/unretweet/{{.ID}}" title="undo retweet">
<i class="fa fa-retweet retweeted"></i> <i class="fa fa-retweet retweeted"></i>
<span> {{DisplayInteractionCount .ReblogsCount}} </span>
</a> </a>
{{else}} {{else}}
<a class="status-retweet" href="/retweet/{{.ID}}" title="retweet"> <a class="status-retweet" href="/retweet/{{.ID}}" title="retweet">
<i class="fa fa-retweet"></i> <i class="fa fa-retweet"></i>
<span> {{DisplayInteractionCount .ReblogsCount}} </span>
</a> </a>
{{end}} {{end}}
{{end}} {{end}}
<a class="status-action-count" href="/retweetedby/{{.ID}}" title="click to see the the list">
<span> {{DisplayInteractionCount .ReblogsCount}} </span>
</a>
</div>
<div class="status-action">
{{if .Favourited}} {{if .Favourited}}
<a class="status-like" href="/unlike/{{.ID}}" title="unlike"> <a class="status-like" href="/unlike/{{.ID}}" title="unlike">
<i class="fa fa-star liked"></i> <i class="fa fa-star liked"></i>
<span> {{DisplayInteractionCount .FavouritesCount}} </span>
</a> </a>
{{else}} {{else}}
<a class="status-like" href="/like/{{.ID}}" title="like"> <a class="status-like" href="/like/{{.ID}}" title="like">
<i class="fa fa-star-o"></i> <i class="fa fa-star-o"></i>
<span> {{DisplayInteractionCount .FavouritesCount}} </span>
</a> </a>
{{end}} {{end}}
<a class="status-action-count" href="/likedby/{{.ID}}" title="click to see the the list">
<span> {{DisplayInteractionCount .FavouritesCount}} </span>
</a>
</div>
<div class="status-action">
<a class="status-time" href="/thread/{{.ID}}#status-{{.ID}}"> <a class="status-time" href="/thread/{{.ID}}#status-{{.ID}}">
<time datetime="{{FormatTimeRFC3339 .CreatedAt}}" title="{{.CreatedAt}}"> {{TimeSince .CreatedAt}} </time> <time datetime="{{FormatTimeRFC3339 .CreatedAt}}" title="{{.CreatedAt}}"> {{TimeSince .CreatedAt}} </time>
</a> </a>
</div> </div>
</div> </div>
</div> </div>
</div>
{{end}} {{end}}
{{end}} {{end}}
</div> </div>

17
templates/userlist.tmpl Normal file
View file

@ -0,0 +1,17 @@
<div class="user-list-container">
{{range .}}
<div class="user-list-item">
<div>
<a class="img-link" href="/user/{{.ID}}">
<img class="status-profile-img" src="{{.AvatarStatic}}" alt="avatar" />
</a>
</div>
<div>
<div class="status-dname"> {{EmojiFilter .DisplayName .Emojis}} </div>
<a class="img-link" href="/user/{{.ID}}">
<div class="status-uname"> {{.Acct}} </div>
</a>
</div>
</div>
{{end}}
</div>