From 0953eb99a20a102e038e19a9d989a26fc5deb607 Mon Sep 17 00:00:00 2001 From: JP Hastings-Spital Date: Sun, 12 Nov 2023 16:48:52 +0000 Subject: [PATCH] Add comments --- assets/scss/_single.scss | 58 +++++++++++++++++++ data/interactions/photos/mums-garden.json | 1 + .../photos/mums-garden/interactions.json | 1 - data/interactions/photos/sunny-brunch.json | 1 + .../photos/sunny-brunch/interactions.json | 1 - layouts/partials/interactions.html | 57 ++++++++++++++++++ layouts/partials/rel-date.html | 14 +++++ layouts/photos/single.html | 2 + tools/syndicate/backfeeder/backfeeder.go | 22 +++++-- tools/syndicate/config.go | 21 +++++-- tools/syndicate/main.go | 6 +- .../services/instagram/interactions.go | 20 ++++++- .../services/mastodon/interactions.go | 4 +- tools/syndicate/shared/types.go | 4 +- 14 files changed, 193 insertions(+), 19 deletions(-) create mode 100644 data/interactions/photos/mums-garden.json delete mode 100644 data/interactions/photos/mums-garden/interactions.json create mode 100644 data/interactions/photos/sunny-brunch.json delete mode 100644 data/interactions/photos/sunny-brunch/interactions.json create mode 100644 layouts/partials/interactions.html create mode 100644 layouts/partials/rel-date.html diff --git a/assets/scss/_single.scss b/assets/scss/_single.scss index d352663b..94237b8e 100644 --- a/assets/scss/_single.scss +++ b/assets/scss/_single.scss @@ -454,4 +454,62 @@ figure { height: 1em; } } +} + +.interactions { + + details { + + summary { + cursor: pointer; + list-style: none; + + &::marker, &::-webkit-details-marker { + display: none; + } + margin-bottom: 8px; + } + + summary::after { + color: var(--accent); + opacity: 0.8; + content: ' (show)'; + } + &[open] summary { + &::after { + content: ' (hide)'; + } + } + + } + + ul.reactions { + margin: 0; + list-style: none; + + li { + display: inline-block; + + &:not(:first-of-type) { + margin: auto 0 auto 8px; + } + + svg { + margin: 0; + } + } + } + + ol.comments { + margin: 0; + list-style: none; + + li { + margin: 1em 0; + } + + .muted { + opacity: 0.6; + } + } } \ No newline at end of file diff --git a/data/interactions/photos/mums-garden.json b/data/interactions/photos/mums-garden.json new file mode 100644 index 00000000..f5166fd7 --- /dev/null +++ b/data/interactions/photos/mums-garden.json @@ -0,0 +1 @@ +{"interactions":[{"emoji":"⭐️","url":"https://pixelfed.social/p/jphastings/619856573800023125","author":{"name":"JP","url":"https://hachyderm.io/users/byjp"},"timestamp":"0001-01-01T00:00:00Z"},{"emoji":"⭐️","url":"https://pixelfed.social/p/jphastings/619856573800023125","author":{"name":"Ben!","url":"https://pixelfed.social/bencord0"},"timestamp":"0001-01-01T00:00:00Z"},{"emoji":"⭐️","url":"https://pixelfed.social/p/jphastings/619856573800023125","author":{"name":"Thomas Zimmermann","url":"https://pixelfed.social/curlingtom"},"timestamp":"0001-01-01T00:00:00Z"},{"emoji":"⭐️","url":"https://pixelfed.social/p/jphastings/619856573800023125","author":{"name":"Ivan Jurišić","url":"https://pixelfed.social/ijurisic"},"timestamp":"0001-01-01T00:00:00Z"}],"reactions":{"⭐️":4}} diff --git a/data/interactions/photos/mums-garden/interactions.json b/data/interactions/photos/mums-garden/interactions.json deleted file mode 100644 index 77c76dca..00000000 --- a/data/interactions/photos/mums-garden/interactions.json +++ /dev/null @@ -1 +0,0 @@ -[{"emoji":"⭐️","author":{"name":"JP","url":"https://hachyderm.io/users/byjp"},"timestamp":"2022-12-14T11:16:00Z"},{"emoji":"⭐️","author":{"name":"Ben!","url":"https://pixelfed.social/bencord0"},"timestamp":"2019-09-14T08:43:33Z"},{"emoji":"⭐️","author":{"name":"Thomas Zimmermann","url":"https://pixelfed.social/curlingtom"},"timestamp":"2022-10-30T14:12:09Z"},{"emoji":"⭐️","author":{"name":"Ivan Jurišić","url":"https://pixelfed.social/ijurisic"},"timestamp":"2019-09-27T22:53:45Z"}] diff --git a/data/interactions/photos/sunny-brunch.json b/data/interactions/photos/sunny-brunch.json new file mode 100644 index 00000000..8917a392 --- /dev/null +++ b/data/interactions/photos/sunny-brunch.json @@ -0,0 +1 @@ +{"interactions":[{"emoji":"⭐️","url":"https://pixelfed.social/p/jphastings/629220605979255535","author":{"name":"Rui Almeida","url":"https://pixelfed.social/RJCA_PT"},"timestamp":"0001-01-01T00:00:00Z"}],"reactions":{"⭐️":1}} diff --git a/data/interactions/photos/sunny-brunch/interactions.json b/data/interactions/photos/sunny-brunch/interactions.json deleted file mode 100644 index 227df95d..00000000 --- a/data/interactions/photos/sunny-brunch/interactions.json +++ /dev/null @@ -1 +0,0 @@ -[{"emoji":"⭐️","author":{"name":"Rui Almeida","url":"https://pixelfed.social/RJCA_PT"},"timestamp":"2022-12-03T15:13:46Z"}] diff --git a/layouts/partials/interactions.html b/layouts/partials/interactions.html new file mode 100644 index 00000000..61c4e414 --- /dev/null +++ b/layouts/partials/interactions.html @@ -0,0 +1,57 @@ +{{ $interactions := index .Site.Data.interactions (split (.Path | path.Dir) "/") }} +{{ with $interactions }} +
+ {{ $most := 0 }} + {{ $emoji := "" }} + {{ range $thisEm, $count := .reactions }} + {{ if gt $count $most }} + {{ $most = $count }} + {{ $emoji = $thisEm }} + {{ end }} + {{ end }} + + + +
+ + + + Reactions & comments + + +
    + {{ range $thisEm, $count := .reactions }} + {{ if gt $count 0 }} +
  • + {{ if eq $thisEm "❤️" }}{{ end }} + {{ if eq $thisEm "⭐️" }}{{ end }} + {{ if eq $thisEm "👍" }}{{ end }} + {{ if eq $thisEm "🔁" }}{{ end }} + {{ if eq $thisEm "💬" }}{{ end }} + + {{ $count }} +
  • + {{ end }} + {{ end }} +
+ +
    + {{ range .interactions }} + {{ if not .comment }} + {{ continue }} + {{ end }} +
  1. + {{ .author.name }}: {{ .comment }} + ({{- partial "rel-date.html" (dict "time" .timestamp "rel" $.Page.Date) -}}) +
  2. + {{ end }} +
+
+
+{{ else }} +

+ + + No comments or reactions +

+{{ end }} \ No newline at end of file diff --git a/layouts/partials/rel-date.html b/layouts/partials/rel-date.html new file mode 100644 index 00000000..2e5fbfdc --- /dev/null +++ b/layouts/partials/rel-date.html @@ -0,0 +1,14 @@ +{{- $desc := "0s" -}} +{{- $diff := (time .time).Sub (time .rel) -}} +{{- if gt $diff.Hours 168 -}} + {{- $desc = printf "%.0fw" (div $diff.Hours 168) -}} +{{- else if gt $diff.Hours 24 -}} + {{- $desc = printf "%.0fd" (div $diff.Hours 24) -}} +{{- else if gt $diff.Minutes 60 -}} + {{- $desc = printf "%.0fh" (div $diff.Minutes 60) -}} +{{- else if gt $diff.Seconds 60 -}} + {{- $desc = printf "%.0fm" (div $diff.Seconds 60) -}} +{{- else -}} + {{- $desc = printf "%.0fs" (div $diff.Seconds 1) -}} +{{- end -}} + \ No newline at end of file diff --git a/layouts/photos/single.html b/layouts/photos/single.html index d6003791..f45a15a7 100644 --- a/layouts/photos/single.html +++ b/layouts/photos/single.html @@ -41,6 +41,8 @@ {{ dateFormat .Site.Params.dateformNumTime .Date }}

+ + {{ partial "interactions.html" . }}
diff --git a/tools/syndicate/backfeeder/backfeeder.go b/tools/syndicate/backfeeder/backfeeder.go index 8a136442..1004a116 100644 --- a/tools/syndicate/backfeeder/backfeeder.go +++ b/tools/syndicate/backfeeder/backfeeder.go @@ -53,17 +53,31 @@ func (b *backfeeder) BackfeedAll(toBackfeed ToBackfeedList) error { } func writeInteractions(dir string, ias []shared.Interaction) error { - if err := os.MkdirAll(dir, 0755); err != nil { + if err := os.MkdirAll(filepath.Dir(dir), 0755); err != nil { return err } - path := filepath.Join(dir, "interactions.json") + path := dir + ".json" f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) if err != nil { return err } defer f.Close() - enc := json.NewEncoder(f) - return enc.Encode(ias) + + split := map[string]interface{}{ + "interactions": ias, + "reactions": make(map[string]int), + } + + for _, ia := range ias { + if ia.Emoji != "" { + split["reactions"].(map[string]int)[ia.Emoji]++ + } + if ia.Comment != "" { + split["reactions"].(map[string]int)["💬"]++ + } + } + + return enc.Encode(split) } diff --git a/tools/syndicate/config.go b/tools/syndicate/config.go index 6fd18f5a..ec5ab5da 100644 --- a/tools/syndicate/config.go +++ b/tools/syndicate/config.go @@ -37,6 +37,8 @@ type config struct { syndicationMatchers map[string]*regexp.Regexp urlToPublicPath func(string) string urlToInteractionsPath func(string) string + + performBackfeed bool } func parseConfig(cfgPath string) (*config, error) { @@ -50,12 +52,16 @@ func parseConfig(cfgPath string) (*config, error) { feeds: []string{}, services: services.New(), syndicationMatchers: make(map[string]*regexp.Regexp), + performBackfeed: cfgData.InteractionsDir != "", urlToPublicPath: func(url string) string { return path.Join(cfgData.PublishRoot, strings.TrimPrefix(url, cfgData.PublishURL)) }, - urlToInteractionsPath: func(url string) string { + } + + if cfg.performBackfeed { + cfg.urlToInteractionsPath = func(url string) string { return path.Join(cfgData.InteractionsDir, strings.TrimPrefix(url, cfgData.PublishURL)) - }, + } } cfg.content, err = doublestar.Glob(os.DirFS("."), cfgData.ContentGlob) @@ -80,11 +86,14 @@ func parseConfig(cfgPath string) (*config, error) { return nil, err } - bf, err := svc.BackfeedMatcher() - if err != nil { - return nil, fmt.Errorf("cannot perform backfeed matching for '%s' because %w", name, err) + if cfg.performBackfeed { + bf, err := svc.BackfeedMatcher() + if err != nil { + return nil, fmt.Errorf("cannot perform backfeed matching for '%s' because %w", name, err) + } + cfg.syndicationMatchers[name] = bf } - cfg.syndicationMatchers[name] = bf + serviceTags = append(serviceTags, name) } cfg.tagMatcher, err = shared.TagMatcher(serviceTags) diff --git a/tools/syndicate/main.go b/tools/syndicate/main.go index 2e276eb1..90db792d 100644 --- a/tools/syndicate/main.go +++ b/tools/syndicate/main.go @@ -34,7 +34,7 @@ func main() { check(err) if len(services) > 0 { - fmt.Fprintf(os.Stderr, "Connecting to %s to syndicate & backfeed…\n", strings.Join(services, ", ")) + fmt.Fprintf(os.Stderr, "Connecting to %s to syndicate…\n", strings.Join(services, ", ")) for _, sname := range services { if err := cfg.services.Init(sname); err != nil { check(fmt.Errorf("couldn't connect to %s: %w", sname, err)) @@ -53,6 +53,10 @@ func main() { } } + if !cfg.performBackfeed { + continue + } + fmt.Fprintf(os.Stderr, "Found %d existing syndications to backfeed from %s\n", len(toBackfeed), feed) if err := bkfd.BackfeedAll(toBackfeed); err != nil { fmt.Fprintf(os.Stderr, "Couldn't backfeed syndications: %v\n", err) diff --git a/tools/syndicate/services/instagram/interactions.go b/tools/syndicate/services/instagram/interactions.go index 62e4cadd..fab691b4 100644 --- a/tools/syndicate/services/instagram/interactions.go +++ b/tools/syndicate/services/instagram/interactions.go @@ -1,7 +1,23 @@ package instagram -import "github.com/by-jp/www.byjp.me/tools/syndicate/shared" +import ( + "fmt" + + "github.com/by-jp/www.byjp.me/tools/syndicate/shared" +) func (s *service) Interactions(url string) ([]shared.Interaction, error) { - return nil, nil + u, err := s.insta.Profiles.ByName(s.username) + if err != nil { + return nil, fmt.Errorf("unable to load your feed to scan for interactions: %w", err) + } + + feed := u.Feed() + fmt.Println(feed, feed.Items, feed.NumResults, feed.Status) + + if err := feed.GetCommentInfo(); err != nil { + return nil, fmt.Errorf("unable load feed comment info: %w", err) + } + + return nil, fmt.Errorf("not implemented") } diff --git a/tools/syndicate/services/mastodon/interactions.go b/tools/syndicate/services/mastodon/interactions.go index 39002f4b..78519956 100644 --- a/tools/syndicate/services/mastodon/interactions.go +++ b/tools/syndicate/services/mastodon/interactions.go @@ -25,12 +25,12 @@ func (s *service) Interactions(url string) ([]shared.Interaction, error) { for _, acc := range favs { ias = append(ias, shared.Interaction{ Emoji: "⭐️", + URL: url, Author: shared.Author{ Name: acc.DisplayName, URL: acc.URL, AvatarURL: acc.Avatar, }, - Timestamp: acc.CreatedAt, }) } @@ -42,12 +42,12 @@ func (s *service) Interactions(url string) ([]shared.Interaction, error) { for _, acc := range reblogs { ias = append(ias, shared.Interaction{ Emoji: "🔁", + URL: url, Author: shared.Author{ Name: acc.DisplayName, URL: acc.URL, AvatarURL: acc.Avatar, }, - Timestamp: acc.CreatedAt, }) } diff --git a/tools/syndicate/shared/types.go b/tools/syndicate/shared/types.go index a1952d53..d547f746 100644 --- a/tools/syndicate/shared/types.go +++ b/tools/syndicate/shared/types.go @@ -28,12 +28,12 @@ type Interaction struct { // eg. Repost is 🔁, Facebook is 👍, Instagram is ♥️, Mastodon is ⭐️, Medium is 👏 Emoji string `json:"emoji,omitempty"` // The URL of the original interaction - URL string `json:"url,omitempty"` + URL string `json:"url"` // If there's a comment associated with the interaction Comment string `json:"comment,omitempty"` // Details of the author Author Author `json:"author"` - Timestamp time.Time `json:"timestamp"` + Timestamp time.Time `json:"timestamp,omitempty"` } type Author struct {