mirror of
https://github.com/by-jp/www.byjp.me.git
synced 2025-08-09 22:16:07 +01:00
69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package backfeeder
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/by-jp/www.byjp.me/tools/syndicate/services"
|
|
"github.com/by-jp/www.byjp.me/tools/syndicate/shared"
|
|
)
|
|
|
|
type BackfeedRef struct {
|
|
Source string
|
|
LocalURL string
|
|
}
|
|
|
|
type ToBackfeedList map[string]BackfeedRef
|
|
|
|
type backfeeder struct {
|
|
services *services.List
|
|
done map[string]struct{}
|
|
urlToPath func(string) string
|
|
}
|
|
|
|
func New(services *services.List, urlToPath func(string) string) *backfeeder {
|
|
return &backfeeder{
|
|
services: services,
|
|
done: make(map[string]struct{}),
|
|
urlToPath: urlToPath,
|
|
}
|
|
}
|
|
|
|
func (b *backfeeder) BackfeedAll(toBackfeed ToBackfeedList) error {
|
|
allIAs := make(map[string][]shared.Interaction)
|
|
|
|
for remoteURL, ref := range toBackfeed {
|
|
ias, err := b.services.Service(ref.Source).Interactions(remoteURL)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
path := b.urlToPath(ref.LocalURL)
|
|
allIAs[path] = append(allIAs[path], ias...)
|
|
}
|
|
|
|
for postDir, ias := range allIAs {
|
|
if err := writeInteractions(postDir, ias); err != nil {
|
|
return fmt.Errorf("couldn't write interactions into %s: %w", postDir, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func writeInteractions(dir string, ias []shared.Interaction) error {
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
return err
|
|
}
|
|
path := filepath.Join(dir, "interactions.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)
|
|
}
|