From 889f8da49637e908f203d6d9d195f7d585f8cd03 Mon Sep 17 00:00:00 2001 From: r Date: Sat, 21 Dec 2019 05:48:06 +0000 Subject: Use local url for for mentioned users --- renderer/renderer.go | 23 ++++++++++++++++++----- templates/notification.tmpl | 6 +++--- templates/status.tmpl | 6 +++--- templates/user.tmpl | 2 +- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/renderer/renderer.go b/renderer/renderer.go index 3eb0457..9bbe06e 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -28,7 +28,8 @@ type renderer struct { func NewRenderer(templateGlobPattern string) (r *renderer, err error) { t := template.New("default") t, err = t.Funcs(template.FuncMap{ - "WithEmojis": WithEmojis, + "EmojiFilter": EmojiFilter, + "StatusContentFilter": StatusContentFilter, "DisplayInteractionCount": DisplayInteractionCount, "TimeSince": TimeSince, "FormatTimeRFC3339": FormatTimeRFC3339, @@ -70,12 +71,24 @@ func (r *renderer) RenderUserPage(ctx context.Context, writer io.Writer, data *U return r.template.ExecuteTemplate(writer, "user.tmpl", data) } -func WithEmojis(content string, emojis []mastodon.Emoji) string { - var emojiNameContentPair []string + +func EmojiFilter(content string, emojis []mastodon.Emoji) string { + var replacements []string + for _, e := range emojis { + replacements = append(replacements, ":"+e.ShortCode+":", "\""+e.ShortCode+"\"") + } + return strings.NewReplacer(replacements...).Replace(content) +} + +func StatusContentFilter(content string, emojis []mastodon.Emoji, mentions []mastodon.Mention) string { + var replacements []string for _, e := range emojis { - emojiNameContentPair = append(emojiNameContentPair, ":"+e.ShortCode+":", "\""+e.ShortCode+"\"") + replacements = append(replacements, ":"+e.ShortCode+":", "\""+e.ShortCode+"\"") + } + for _, m := range mentions { + replacements = append(replacements, "\""+m.URL+"\"", "\"/user/"+m.ID+"\"") } - return strings.NewReplacer(emojiNameContentPair...).Replace(content) + return strings.NewReplacer(replacements...).Replace(content) } func DisplayInteractionCount(c int64) string { diff --git a/templates/notification.tmpl b/templates/notification.tmpl index da6164b..271597b 100644 --- a/templates/notification.tmpl +++ b/templates/notification.tmpl @@ -11,7 +11,7 @@
- {{WithEmojis .Account.DisplayName .Account.Emojis}} + {{EmojiFilter .Account.DisplayName .Account.Emojis}} followed you
@@ -31,7 +31,7 @@
- {{WithEmojis .Account.DisplayName .Account.Emojis}} + {{EmojiFilter .Account.DisplayName .Account.Emojis}} retweeted your post
@@ -46,7 +46,7 @@
- {{WithEmojis .Account.DisplayName .Account.Emojis}} + {{EmojiFilter .Account.DisplayName .Account.Emojis}} liked your post
diff --git a/templates/status.tmpl b/templates/status.tmpl index 618398f..b1babf6 100644 --- a/templates/status.tmpl +++ b/templates/status.tmpl @@ -4,7 +4,7 @@ profile-avatar - {{WithEmojis .Account.DisplayName .Account.Emojis}} + {{EmojiFilter .Account.DisplayName .Account.Emojis}} retweeted
@@ -22,7 +22,7 @@
{{if not .HideAccountInfo}}
- {{WithEmojis .Account.DisplayName .Account.Emojis}} + {{EmojiFilter .Account.DisplayName .Account.Emojis}} {{.Account.Acct}} @@ -41,7 +41,7 @@ {{end}} {{end}}
-
{{WithEmojis .Content .Emojis}}
+
{{StatusContentFilter .Content .Emojis .Mentions}}
{{range .MediaAttachments}} {{if eq .Type "image"}} diff --git a/templates/user.tmpl b/templates/user.tmpl index 3347f92..e1a782a 100644 --- a/templates/user.tmpl +++ b/templates/user.tmpl @@ -9,7 +9,7 @@