Add liked by and retweeted by page
authorr <r@freesoftwareextremist.com>
Thu, 26 Dec 2019 09:11:24 +0000 (09:11 +0000)
committerr <r@freesoftwareextremist.com>
Thu, 26 Dec 2019 09:44:05 +0000 (09:44 +0000)
12 files changed:
renderer/model.go
renderer/renderer.go
service/auth.go
service/logging.go
service/service.go
service/transport.go
static/main.css
templates/error.tmpl
templates/likedby.tmpl [new file with mode: 0644]
templates/retweetedby.tmpl [new file with mode: 0644]
templates/status.tmpl
templates/userlist.tmpl [new file with mode: 0644]

index 4dfac84bd6174f627848490eea64755f4e5ee071..20d11d82f8b84f66ac96cb696f73f75b450b625c 100644 (file)
@@ -21,6 +21,19 @@ type CommonData struct {
        NavbarData *NavbarData
 }
 
+type ErrorData struct {
+       *CommonData
+       Error string
+}
+
+type HomePageData struct {
+       *CommonData
+}
+
+type SigninData struct {
+       *CommonData
+}
+
 type TimelineData struct {
        *CommonData
        Title       string
@@ -59,6 +72,20 @@ type AboutData struct {
 }
 
 type EmojiData struct {
+       *CommonData
        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
 }
index 48c087912baf119ce7be3cb95efe821453669b5e..061737ceccce94b770048cb9f80c88a7384971b0 100644 (file)
@@ -12,15 +12,17 @@ import (
 )
 
 type Renderer interface {
-       RenderErrorPage(ctx context.Context, writer io.Writer, err error)
-       RenderHomePage(ctx context.Context, writer io.Writer) (err error)
-       RenderSigninPage(ctx context.Context, writer io.Writer) (err error)
+       RenderErrorPage(ctx context.Context, writer io.Writer, data *ErrorData)
+       RenderHomePage(ctx context.Context, writer io.Writer, data *HomePageData) (err error)
+       RenderSigninPage(ctx context.Context, writer io.Writer, data *SigninData) (err error)
        RenderTimelinePage(ctx context.Context, writer io.Writer, data *TimelineData) (err error)
        RenderThreadPage(ctx context.Context, writer io.Writer, data *ThreadData) (err error)
        RenderNotificationPage(ctx context.Context, writer io.Writer, data *NotificationData) (err error)
        RenderUserPage(ctx context.Context, writer io.Writer, data *UserData) (err error)
        RenderAboutPage(ctx context.Context, writer io.Writer, data *AboutData) (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 {
@@ -44,17 +46,17 @@ func NewRenderer(templateGlobPattern string) (r *renderer, err error) {
        }, nil
 }
 
-func (r *renderer) RenderErrorPage(ctx context.Context, writer io.Writer, err error) {
-       r.template.ExecuteTemplate(writer, "error.tmpl", err)
+func (r *renderer) RenderErrorPage(ctx context.Context, writer io.Writer, errorData *ErrorData) {
+       r.template.ExecuteTemplate(writer, "error.tmpl", errorData)
        return
 }
 
-func (r *renderer) RenderHomePage(ctx context.Context, writer io.Writer) (err error) {
-       return r.template.ExecuteTemplate(writer, "homepage.tmpl", nil)
+func (r *renderer) RenderHomePage(ctx context.Context, writer io.Writer, homePageData *HomePageData) (err error) {
+       return r.template.ExecuteTemplate(writer, "homepage.tmpl", homePageData)
 }
 
-func (r *renderer) RenderSigninPage(ctx context.Context, writer io.Writer) (err error) {
-       return r.template.ExecuteTemplate(writer, "signin.tmpl", nil)
+func (r *renderer) RenderSigninPage(ctx context.Context, writer io.Writer, signinData *SigninData) (err error) {
+       return r.template.ExecuteTemplate(writer, "signin.tmpl", signinData)
 }
 
 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)
 }
 
+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 {
        var replacements []string
        for _, e := range emojis {
index cadb050ebb37fd9fd5ebe12a7e46a6b4c7bb1fb8..c40867221f17d3106956fca29e494ac3ed37ef61 100644 (file)
@@ -133,6 +133,22 @@ func (s *authService) ServeEmojiPage(ctx context.Context, client io.Writer, c *m
        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) {
        c, err = s.getClient(ctx)
        if err != nil {
index 87433a435a32c21bf3484e241c747568ba642ef4..fbc63717a747d321748e32593fc54658067e4572 100644 (file)
@@ -109,6 +109,22 @@ func (s *loggingService) ServeEmojiPage(ctx context.Context, client io.Writer, 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) {
        defer func(begin time.Time) {
                s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
index ea0d078359771a8103217119d360e88f5dd03b76..7dc784f38391f66402930cc675122499189872c6 100644 (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)
        ServeAboutPage(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)
        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)
@@ -192,25 +194,48 @@ func (svc *service) GetUserToken(ctx context.Context, sessionID string, c *model
 }
 
 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 {
                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) {
-       svc.renderer.RenderErrorPage(ctx, client, err)
+       var errStr string
+       if err != nil {
+               errStr = err.Error()
+       }
+
+       commonData, err := svc.getCommonData(ctx, client, nil)
+       if err != nil {
+               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) {
-       err = svc.renderer.RenderSigninPage(ctx, client)
+       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,
@@ -517,6 +542,53 @@ func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *mod
        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) {
        data = new(renderer.CommonData)
 
index 438b39d05f805131324754b1a9efddeee852aee0..d4011dbdf56febe0a4e08b363ed808d05371b467 100644 (file)
@@ -98,6 +98,28 @@ func NewHandler(s Service, staticDir string) http.Handler {
                }
        }).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) {
                ctx := getContextWithSession(context.Background(), req)
                id, _ := mux.Vars(req)["id"]
index 99e2cf097b4f335e615acac7a31ce6c1e394390e..87e2cfa0fd92409d5ee7b58d28e21e6cf5bfc841 100644 (file)
        width: auto;
 }
 
-.status-action {
+.status-action-container {
        margin-top: 4px;
 }
 
-.status-action {
+.status-action {
        display: inline-block;
        min-width: 64px;
+}
+
+.status-action:last-child {
+       min-width: unset;
+}
+
+.status-action a {
+       display: inline-block;
        text-decoration: none;
        color: #333333;
        vertical-align: middle;
 }
 
-.status-action a:hover {
+.status-action a:hover,
+.status-action a:hover i {
        color: #777777;
 }
 
-.status-action .fa {
-       margin: 0 4px 0 0;
-}
-
 .status-action a.status-time {
        width: auto;
 }
        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;
+}
index 0389cdd96848be5e830e772d3d1206909a008b01..2049665209d1bad220ed754e43287d14e756a0f0 100644 (file)
@@ -1,6 +1,6 @@
 {{template "header.tmpl" .HeaderData}}
 <div class="page-title"> Error </div>
-<div class="error-text"> {{.}} </div>
+<div class="error-text"> {{.Error}} </div>
 <div>
 <a href="/timeline/home">Home</a>
 <a href="/signin">Sign In</a>
diff --git a/templates/likedby.tmpl b/templates/likedby.tmpl
new file mode 100644 (file)
index 0000000..72855e0
--- /dev/null
@@ -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"}}
diff --git a/templates/retweetedby.tmpl b/templates/retweetedby.tmpl
new file mode 100644 (file)
index 0000000..2d9c539
--- /dev/null
@@ -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"}}
index 6534249993483e3fbabc0570da3cb90246792b73..4e975ad3492f67725c589a8ac6cac838ec809db3 100644 (file)
                        {{end}}
                        {{end}}
                        </div>
-                       <div class="status-action"> 
-                               <a class="status-you" href="/thread/{{.ID}}?reply=true#status-{{.ID}}" title="reply"> 
-                                       <i class="fa fa-reply"></i>
-                                       <span> {{DisplayInteractionCount .RepliesCount}} </span>
-                               </a>
-                               {{if or (eq .Visibility "private") (eq .Visibility "direct")}}
-                               <a class="status-retweet" title="this status cannot be retweeted"> 
-                                       <i class="fa fa-retweet"></i>
-                                       <span> {{DisplayInteractionCount .ReblogsCount}} </span>
-                               </a>
-                               {{else}}
+                       <div class="status-action-container"> 
+                               <div class="status-action">
+                                       <a class="status-you" href="/thread/{{.ID}}?reply=true#status-{{.ID}}" title="reply"> 
+                                               <i class="fa fa-reply"></i>
+                                       </a>
+                                       <a class="status-action-count" href="/thread/{{.ID}}#status-{{.ID}}">
+                                               <span> {{DisplayInteractionCount .RepliesCount}} </span>
+                                       </a>
+                               </div>
+                               <div class="status-action">
+                                       {{if or (eq .Visibility "private") (eq .Visibility "direct")}}
+                                       <a class="status-retweet" title="this status cannot be retweeted"> 
+                                               <i class="fa fa-retweet"></i>
+                                       </a>
+                                       {{else}}
                                        {{if .Reblogged}}
                                        <a class="status-retweet" href="/unretweet/{{.ID}}" title="undo retweet"> 
                                                <i class="fa fa-retweet retweeted"></i>
-                                               <span> {{DisplayInteractionCount .ReblogsCount}} </span>
                                        </a>
                                        {{else}}
                                        <a class="status-retweet" href="/retweet/{{.ID}}" title="retweet"> 
                                                <i class="fa fa-retweet"></i>
+                                       </a>
+                                       {{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}}
+                                       <a class="status-like" href="/unlike/{{.ID}}" title="unlike"> 
+                                               <i class="fa fa-star liked"></i>
+                                       </a>
+                                       {{else}}
+                                       <a class="status-like" href="/like/{{.ID}}" title="like"> 
+                                               <i class="fa fa-star-o"></i>
+                                       </a>
                                        {{end}}
-                               {{end}}
-                               {{if .Favourited}}
-                               <a class="status-like" href="/unlike/{{.ID}}" title="unlike"> 
-                                       <i class="fa fa-star liked"></i>
-                                       <span> {{DisplayInteractionCount .FavouritesCount}} </span>
-                               </a>
-                               {{else}}
-                               <a class="status-like" href="/like/{{.ID}}" title="like"> 
-                                       <i class="fa fa-star-o"></i>
-                                       <span> {{DisplayInteractionCount .FavouritesCount}} </span>
-                               </a>
-                               {{end}}
-                               <a class="status-time" href="/thread/{{.ID}}#status-{{.ID}}"> 
-                                       <time datetime="{{FormatTimeRFC3339 .CreatedAt}}" title="{{.CreatedAt}}"> {{TimeSince .CreatedAt}} </time> 
-                               </a>
+                                       <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}}"> 
+                                               <time datetime="{{FormatTimeRFC3339 .CreatedAt}}" title="{{.CreatedAt}}"> {{TimeSince .CreatedAt}} </time> 
+                                       </a>
+                               </div>
                        </div>
                </div>
        </div>
diff --git a/templates/userlist.tmpl b/templates/userlist.tmpl
new file mode 100644 (file)
index 0000000..d52e67d
--- /dev/null
@@ -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>