52 lines
819 B
Go
52 lines
819 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
"within.website/ln"
|
|
)
|
|
|
|
type Pusher func(context.Context, Post) error
|
|
|
|
type Post struct {
|
|
Title string
|
|
URL string
|
|
Tags []string
|
|
}
|
|
|
|
func (p Post) Format() string {
|
|
var sb strings.Builder
|
|
fmt.Fprintf(&sb, "New post: %s\n\n%s\n\n", p.Title, p.URL)
|
|
|
|
for _, tg := range p.Tags {
|
|
tg = strings.ReplaceAll(tg, "-", "")
|
|
fmt.Fprintf(&sb, "#%s ", tg)
|
|
}
|
|
|
|
return sb.String()
|
|
}
|
|
|
|
func (p Post) F() ln.F {
|
|
return ln.F{
|
|
"post_title": p.Title,
|
|
"post_url": p.URL,
|
|
"post_tags": p.Tags,
|
|
}
|
|
}
|
|
|
|
func (mi *Mi) Push(ctx context.Context, p Post) error {
|
|
ln.Log(ctx, p)
|
|
|
|
var result error
|
|
for _, per := range mi.pushers {
|
|
if err := per(ctx, p); err != nil {
|
|
result = multierror.Append(result, err)
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|