From d7fc7cf2f5e9c11a401ee34ed498bca762c72b79 Mon Sep 17 00:00:00 2001 From: r Date: Wed, 18 Dec 2019 22:14:02 +0000 Subject: [PATCH] Add reply links on thread page --- mastodon/status.go | 16 +++++++++++++++ model/status.go | 1 + renderer/model.go | 10 ++++----- service/service.go | 48 ++++++++++++++++++++++++++++++++++--------- static/main.css | 27 +++++++++++++++++++++++- templates/status.tmpl | 17 +++++++++++++-- templates/thread.tmpl | 8 ++------ 7 files changed, 103 insertions(+), 24 deletions(-) create mode 100644 model/status.go diff --git a/mastodon/status.go b/mastodon/status.go index b6110d5..9f029d1 100644 --- a/mastodon/status.go +++ b/mastodon/status.go @@ -10,6 +10,15 @@ import ( "time" ) +type StatusPleroma struct { + InReplyToAccountAcct string `json:"in_reply_to_account_acct"` +} + +type ReplyInfo struct { + ID string `json:"id"` + Number int `json:"number"` +} + // Status is struct to hold status. type Status struct { ID string `json:"id"` @@ -38,6 +47,13 @@ type Status struct { Application Application `json:"application"` Language string `json:"language"` Pinned interface{} `json:"pinned"` + + // Custom fields + Pleroma StatusPleroma `json:"pleroma"` + HideAccountInfo bool `json:"hide_account_info"` + ShowReplies bool `json:"show_replies"` + ReplyMap map[string][]ReplyInfo `json:"reply_map"` + ReplyNumber int `json:"reply_number"` } // Context hold information for mastodon context. diff --git a/model/status.go b/model/status.go new file mode 100644 index 0000000..8b53790 --- /dev/null +++ b/model/status.go @@ -0,0 +1 @@ +package model diff --git a/renderer/model.go b/renderer/model.go index 4529386..2a320cc 100644 --- a/renderer/model.go +++ b/renderer/model.go @@ -36,21 +36,21 @@ func NewTimelinePageTemplateData(statuses []*mastodon.Status, hasNext bool, next } type ThreadPageTemplateData struct { - Status *mastodon.Status - Context *mastodon.Context + Statuses []*mastodon.Status PostReply bool ReplyToID string ReplyContent string + ReplyMap map[string][]mastodon.ReplyInfo NavbarData *NavbarTemplateData } -func NewThreadPageTemplateData(status *mastodon.Status, context *mastodon.Context, postReply bool, replyToID string, replyContent string, navbarData *NavbarTemplateData) *ThreadPageTemplateData { +func NewThreadPageTemplateData(statuses []*mastodon.Status, postReply bool, replyToID string, replyContent string, replyMap map[string][]mastodon.ReplyInfo, navbarData *NavbarTemplateData) *ThreadPageTemplateData { return &ThreadPageTemplateData{ - Status: status, - Context: context, + Statuses: statuses, PostReply: postReply, ReplyToID: replyToID, ReplyContent: replyContent, + ReplyMap: replyMap, NavbarData: navbarData, } } diff --git a/service/service.go b/service/service.go index bb03c26..6c7a37d 100644 --- a/service/service.go +++ b/service/service.go @@ -267,34 +267,46 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *ma return } - context, err := c.GetStatusContext(ctx, id) - if err != nil { - return - } - u, err := c.GetAccountCurrentUser(ctx) if err != nil { return } var content string + var replyToID string if reply { + replyToID = id if u.ID != status.Account.ID { content += "@" + status.Account.Acct + " " } - for _, m := range status.Mentions { - if u.ID != m.ID { - content += "@" + m.Acct + " " + for i := range status.Mentions { + if status.Mentions[i].ID != u.ID && status.Mentions[i].ID != status.Account.ID { + content += "@" + status.Mentions[i].Acct + " " } } } + context, err := c.GetStatusContext(ctx, id) + if err != nil { + return + } + + statuses := append(append(context.Ancestors, status), context.Descendants...) + + replyMap := make(map[string][]mastodon.ReplyInfo) + + for i := range statuses { + statuses[i].ShowReplies = true + statuses[i].ReplyMap = replyMap + addToReplyMap(replyMap, statuses[i].InReplyToID, statuses[i].ID, i+1) + } + navbarData, err := svc.getNavbarTemplateData(ctx, client, c) if err != nil { return } - data := renderer.NewThreadPageTemplateData(status, context, reply, id, content, navbarData) + data := renderer.NewThreadPageTemplateData(statuses, reply, replyToID, content, replyMap, navbarData) err = svc.renderer.RenderThreadPage(ctx, client, data) if err != nil { return @@ -323,7 +335,7 @@ func (svc *service) ServeNotificationPage(ctx context.Context, client io.Writer, switch notifications[i].Type { case "reblog", "favourite": if notifications[i].Status != nil { - notifications[i].Status.Account.ID = "" + notifications[i].Status.HideAccountInfo = true } } if notifications[i].Pleroma != nil && notifications[i].Pleroma.IsSeen { @@ -418,3 +430,19 @@ func (svc *service) PostTweet(ctx context.Context, client io.Writer, c *mastodon return s.ID, nil } + +func addToReplyMap(m map[string][]mastodon.ReplyInfo, key interface{}, val string, number int) { + if key == nil { + return + } + keyStr, ok := key.(string) + if !ok { + return + } + _, ok = m[keyStr] + if !ok { + m[keyStr] = []mastodon.ReplyInfo{} + } + + m[keyStr] = append(m[keyStr], mastodon.ReplyInfo{val, number}) +} diff --git a/static/main.css b/static/main.css index 8865820..7f8cd90 100644 --- a/static/main.css +++ b/static/main.css @@ -7,7 +7,7 @@ } .status-content { - margin: 8px 0; + margin: 4px 0 8px 0; } .status-content p { @@ -18,6 +18,7 @@ height: 48px; width: 48px; margin-right: 8px; + object-fit: contain; } .status { @@ -118,6 +119,7 @@ height: 24px; width: 24px; margin-bottom: -8px; + margin-right: 0px; } .retweet-info .status-dname{ @@ -161,3 +163,26 @@ .notification-follow-uname { margin-top: 8px; } + +.status-reply-to { + vertical-align: center; + font-size: 10pt +} + +.status-reply-container .icon { + font-size: 10pt; + vertical-align: sub; + margin-right: -2px; +} + +.status-reply-text { + font-size: 10pt; +} + +.status-reply { + font-size: 10pt; +} + +.status-reply-info-divider { + margin: 0 4px; +} diff --git a/templates/status.tmpl b/templates/status.tmpl index 7020be0..4dbbe3c 100644 --- a/templates/status.tmpl +++ b/templates/status.tmpl @@ -11,17 +11,30 @@ {{block "status" .}}
- {{if ne .Account.ID ""}} + {{if not .HideAccountInfo}} profile-avatar {{end}}
- {{if ne .Account.ID ""}} + {{if not .HideAccountInfo}}
{{WithEmojis .Account.DisplayName .Account.Emojis}} {{.Account.Acct}}
{{end}} +
+ {{if .InReplyToID}} + + reply to {{.Pleroma.InReplyToAccountAcct}} + {{if index .ReplyMap .ID}} - {{end}} + {{end}} + {{if .ShowReplies}} + {{if index .ReplyMap .ID}} replies: {{end}} + {{range index .ReplyMap .ID}} + #{{.Number}} + {{end}} + {{end}} +
{{WithEmojis .Content .Emojis}}
{{range .MediaAttachments}} diff --git a/templates/thread.tmpl b/templates/thread.tmpl index 29d702b..e36c5ea 100644 --- a/templates/thread.tmpl +++ b/templates/thread.tmpl @@ -2,12 +2,10 @@ {{template "navigation.tmpl" .NavbarData}}
Thread
-{{range .Context.Ancestors}} +{{range .Statuses}} {{template "status.tmpl" .}} -{{end}} -{{template "status.tmpl" .Status}} -{{if .PostReply}} +{{if eq .ID $.ReplyToID}}
@@ -20,8 +18,6 @@
{{end}} -{{range .Context.Descendants}} -{{template "status.tmpl" .}} {{end}} {{template "footer.tmpl"}}