diff --git a/README.md b/README.md index a85c48d..fd41473 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # go-broadcaster-mastodon -Work in progress. +Go package implementing the `aaronland/go-broadcaster` interfaces for broadcasting messages to Mastodon. + +## Documentation + +Documentation is incomplete at this time. ## See also diff --git a/go.mod b/go.mod index 8f5c0fb..ec60559 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/aaronland/go-broadcaster-mastodon go 1.19 require ( - github.com/aaronland/go-broadcaster v0.0.5 + github.com/aaronland/go-broadcaster v0.0.7 github.com/aaronland/go-image-encode v0.0.0-20200215191655-047f61aedbfe github.com/aaronland/go-mastodon-api v1.0.0 github.com/aaronland/go-uid v0.4.0 diff --git a/go.sum b/go.sum index ae33df2..b46035a 100644 --- a/go.sum +++ b/go.sum @@ -192,8 +192,8 @@ github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMx github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g= github.com/aaronland/go-aws-session v0.0.6 h1:Dp1Hvg7s1qM1xJ2byktEdB16i9t6wx6tMcZRpMoSwpo= github.com/aaronland/go-aws-session v0.0.6/go.mod h1:jflsOdd7QkD0ugdTEfU2UYpFjytP8AxiBxP++s10x88= -github.com/aaronland/go-broadcaster v0.0.5 h1:ttkogleJFFQ0kj7kShybvSnMucZZ58GVmsqygrOvO4I= -github.com/aaronland/go-broadcaster v0.0.5/go.mod h1:5hvDfw8G+JdHPvdFc9wOY2k1vyXdsrvCQ89H9QShAh4= +github.com/aaronland/go-broadcaster v0.0.7 h1:5Mi2E/SsxAFCLooXuZc0buGx8b7K1ileTL8L6JtEbOw= +github.com/aaronland/go-broadcaster v0.0.7/go.mod h1:5hvDfw8G+JdHPvdFc9wOY2k1vyXdsrvCQ89H9QShAh4= github.com/aaronland/go-image-encode v0.0.0-20200215191655-047f61aedbfe h1:28o4np7vSR9I0ezWJJbgyntSj1tuJfJWnkYh+t5HPGs= github.com/aaronland/go-image-encode v0.0.0-20200215191655-047f61aedbfe/go.mod h1:cYgNgNYMYq2pi85vYqoUVzXFwOOujIRULt7UbRBEnPU= github.com/aaronland/go-mastodon-api v1.0.0 h1:hKDdC8AV1J0dE0SO3Jf3vRY/b2m56PubATdlZ8jJTbQ= diff --git a/vendor/github.com/aaronland/go-broadcaster/Makefile b/vendor/github.com/aaronland/go-broadcaster/Makefile index 662b8ff..2341c14 100644 --- a/vendor/github.com/aaronland/go-broadcaster/Makefile +++ b/vendor/github.com/aaronland/go-broadcaster/Makefile @@ -1,2 +1,5 @@ +cli: + go build -mod vendor -o bin/broadcast cmd/broadcast/main.go + hello: go run cmd/broadcast/main.go -broadcaster log:// -broadcaster null:// -body "hello world" diff --git a/vendor/github.com/aaronland/go-broadcaster/README.md b/vendor/github.com/aaronland/go-broadcaster/README.md index b830cb0..7832d94 100644 --- a/vendor/github.com/aaronland/go-broadcaster/README.md +++ b/vendor/github.com/aaronland/go-broadcaster/README.md @@ -1,6 +1,164 @@ # go-broadcaster -## See also +Minimalist and opinionated package providing interfaces for "broadcasting" messages with zero or more images. + +## Documentation + +[![Go Reference](https://pkg.go.dev/badge/github.com/aaronland/go-broadcaster.svg)](https://pkg.go.dev/github.com/aaronland/go-broadcaster) + +## Motivation + +This package provides minimalist and opionated interfaces for "broadcasting" simple messages with zero or more images. + +A message consists of an optional title and body as well as zero or more images. How those elements are processed is left to service or target -specific implementations of the `Broadcaster` interfaces. + +That's all it does and doesn't try to account for any other, or more complicated, publishing scenarios. There are other tools and packages for that. + +## This should still be considered "in flux" + +Although the "skeleton" of this package and its interfaces is complete some details may still change. + +## Example + +### Broadcasting a message + +``` +package broadcast + +import ( + "context" + "fmt" + "github.com/aaronland/go-broadcaster" + "log" +) + +func main() { + + ctx := context.Background() + logger := log.Default() + + br, _ := broadcaster.NewBroadcaster(ctx, "log://") + br.SetLogger(ctx, logger) + + msg := &broadcaster.Message{ + Title: "This is the title", + Body: "This is a message", + } + + id, _ := br.BroadcastMessage(ctx, msg) + + fmt.Println(id.String()) + return nil +} +``` + +_Error handling omitted for the sake of brevity._ + +### Implementing the `Broadcaster` interface. + +``` +package broadcaster + +import ( + "context" + "github.com/aaronland/go-uid" + "log" + "time" +) + +func init() { + ctx := context.Background() + RegisterBroadcaster(ctx, "log", NewLogBroadcaster) +} + +// LogBroadcaster implements the `Broadcaster` interface to broadcast messages +// to a `log.Logger` instance. +type LogBroadcaster struct { + Broadcaster + logger *log.Logger +} + +// NewLogBroadcaster returns a new `LogBroadcaster` configured by 'uri' which is expected to +// take the form of: +// +// log:// +// +// By default `LogBroadcaster` instances are configured to broadcast messages to a `log.Default` +// instance. If you want to change that call the `SetLogger` method. +func NewLogBroadcaster(ctx context.Context, uri string) (Broadcaster, error) { + + logger := log.Default() + + b := LogBroadcaster{ + logger: logger, + } + return &b, nil +} + +// BroadcastMessage broadcast the title and body properties of 'msg' to the `log.Logger` instance +// associated with 'b'. It does not publish images yet. Maybe someday it will try to convert images +// to their ascii interpretations but today it does not. It returns the value of the Unix timestamp +// that the log message was broadcast. +func (b *LogBroadcaster) BroadcastMessage(ctx context.Context, msg *Message) (uid.UID, error) { + + b.logger.Printf("%s %s\n", msg.Title, msg.Body) + + now := time.Now() + ts := now.Unix() + + return uid.NewInt64UID(ctx, ts) +} + +// SetLoggers assigns 'logger' to 'b'. +func (b *LogBroadcaster) SetLogger(ctx context.Context, logger *log.Logger) error { + b.logger = logger + return nil +} +``` + +## UIDs + +This package uses the [aaronland/go-uid](https://github.com/aaronland/go-uid) package to encapsulate unique identifiers returns by broadcasting targets. + +## Tools + +``` +$> make cli +go build -mod vendor -o bin/broadcast cmd/broadcast/main.go +``` + +### broadcast + +``` +$> ./bin/broadcast -h + -body string + The body of the message to broadcast. + -broadcaster value + One or more aaronland/go-broadcast URIs. + -image value + Zero or more paths to images to include with the message to broadcast. + -title string + The title of the message to broadcast. +``` + +For example: + +``` +$> ./bin/broadcast -broadcaster log:// -broadcaster null:// -body "hello world" +2022/11/08 08:44:47 hello world +NullUID# Int64UID#1667925887 +``` + +## Other implementations + +### mastodon:// * https://github.com/aaronland/go-broadcaster-mastodon -* https://github.com/aaronland/go-broadcaster-twitter \ No newline at end of file + +### twitter:// + +* https://github.com/aaronland/go-broadcaster-twitter + +## See also + +* https://github.com/aaronland/go-uid \ No newline at end of file diff --git a/vendor/github.com/aaronland/go-broadcaster/app/broadcast/app.go b/vendor/github.com/aaronland/go-broadcaster/app/broadcast/app.go index a52cf3d..bdd36d9 100644 --- a/vendor/github.com/aaronland/go-broadcaster/app/broadcast/app.go +++ b/vendor/github.com/aaronland/go-broadcaster/app/broadcast/app.go @@ -1,3 +1,4 @@ +// Package broadcast provides methods for implementing a command line tool for "broadcasting" messages. package broadcast import ( @@ -6,7 +7,9 @@ import ( "fmt" "github.com/aaronland/go-broadcaster" "github.com/sfomuseum/go-flags/flagset" + "image" "log" + "os" ) func Run(ctx context.Context, logger *log.Logger) error { @@ -31,6 +34,32 @@ func RunWithFlagSet(ctx context.Context, fs *flag.FlagSet, logger *log.Logger) e Body: body, } + count_images := len(image_paths) + + if count_images > 0 { + + msg.Images = make([]image.Image, count_images) + + for idx, path := range image_paths { + + r, err := os.Open(path) + + if err != nil { + return fmt.Errorf("Failed to open image %s, %w", path, err) + } + + defer r.Close() + + im, _, err := image.Decode(r) + + if err != nil { + return fmt.Errorf("Failed to decode image %s, %w", path, err) + } + + msg.Images[idx] = im + } + } + id, err := br.BroadcastMessage(ctx, msg) if err != nil { diff --git a/vendor/github.com/aaronland/go-broadcaster/app/broadcast/flags.go b/vendor/github.com/aaronland/go-broadcaster/app/broadcast/flags.go index a97b447..952b07b 100644 --- a/vendor/github.com/aaronland/go-broadcaster/app/broadcast/flags.go +++ b/vendor/github.com/aaronland/go-broadcaster/app/broadcast/flags.go @@ -6,20 +6,28 @@ import ( "github.com/sfomuseum/go-flags/multi" ) +// One or more aaronland/go-broadcast URIs. var broadcaster_uris multi.MultiCSVString +// The title of the message to broadcast. var title string +// The body of the message to broadcast. var body string +// Zero or more paths to images to include with the message to broadcast. +var image_paths multi.MultiString + func DefaultFlagSet() *flag.FlagSet { fs := flagset.NewFlagSet("broadcast") - fs.Var(&broadcaster_uris, "broadcaster", "...") + fs.Var(&broadcaster_uris, "broadcaster", "One or more aaronland/go-broadcast URIs.") + + fs.StringVar(&title, "title", "", "The title of the message to broadcast.") + fs.StringVar(&body, "body", "", "The body of the message to broadcast.") - fs.StringVar(&title, "title", "", "...") - fs.StringVar(&body, "body", "", "...") + fs.Var(&image_paths, "image", "Zero or more paths to images to include with the message to broadcast.") return fs } diff --git a/vendor/github.com/aaronland/go-broadcaster/broadcaster.go b/vendor/github.com/aaronland/go-broadcaster/broadcaster.go index 2cbf147..599fc2a 100644 --- a/vendor/github.com/aaronland/go-broadcaster/broadcaster.go +++ b/vendor/github.com/aaronland/go-broadcaster/broadcaster.go @@ -5,17 +5,35 @@ import ( "fmt" "github.com/aaronland/go-roster" "github.com/aaronland/go-uid" + "image" "log" "net/url" "sort" "strings" ) +// Broadcaster provides a minimalist interface for "broadcasting" messages to an arbitrary service or target. type Broadcaster interface { + // BroadcastMessage "broadcasts" a `Message` struct. BroadcastMessage(context.Context, *Message) (uid.UID, error) + // SetLogger assigns a specific `log.Logger` instance to be used for logging messages. SetLogger(context.Context, *log.Logger) error } +// Message defines a struct containing properties to "broadcast". The semantics of these +// properties are determined by the server or target -specific implementation of the `Broadcaster` +// interface. +type Message struct { + // Title is a string to use as the title of a "broadcast" message. + Title string + // Body is a string to use as the body of a "broadcast" message. + Body string + // Images is zero or more `image.Image` instances to be included with a "broadcast" messages. + // Images are encoded according to rules implemented by service or target -specific implementation + // of the `Broadcaster` interface. + Images []image.Image +} + var broadcaster_roster roster.Roster // BroadcasterInitializationFunc is a function defined by individual broadcaster package and used to create diff --git a/vendor/github.com/aaronland/go-broadcaster/doc.go b/vendor/github.com/aaronland/go-broadcaster/doc.go index f73f917..b0a1c40 100644 --- a/vendor/github.com/aaronland/go-broadcaster/doc.go +++ b/vendor/github.com/aaronland/go-broadcaster/doc.go @@ -1 +1,2 @@ +// Package broadcaster provided minimalist and opinionated methods for "broadcasting" messages with zero or more images. package broadcaster diff --git a/vendor/github.com/aaronland/go-broadcaster/log.go b/vendor/github.com/aaronland/go-broadcaster/log.go index 7c188aa..c0ca17f 100644 --- a/vendor/github.com/aaronland/go-broadcaster/log.go +++ b/vendor/github.com/aaronland/go-broadcaster/log.go @@ -4,7 +4,6 @@ import ( "context" "github.com/aaronland/go-uid" "log" - "strconv" "time" ) @@ -13,31 +12,45 @@ func init() { RegisterBroadcaster(ctx, "log", NewLogBroadcaster) } +// LogBroadcaster implements the `Broadcaster` interface to broadcast messages +// to a `log.Logger` instance. type LogBroadcaster struct { Broadcaster logger *log.Logger } +// NewLogBroadcaster returns a new `LogBroadcaster` configured by 'uri' which is expected to +// take the form of: +// +// log:// +// +// By default `LogBroadcaster` instances are configured to broadcast messages to a `log.Default` +// instance. If you want to change that call the `SetLogger` method. func NewLogBroadcaster(ctx context.Context, uri string) (Broadcaster, error) { + logger := log.Default() + b := LogBroadcaster{ logger: logger, } return &b, nil } +// BroadcastMessage broadcast the title and body properties of 'msg' to the `log.Logger` instance +// associated with 'b'. It does not publish images yet. Maybe someday it will try to convert images +// to their ascii interpretations but today it does not. It returns the value of the Unix timestamp +// that the log message was broadcast. func (b *LogBroadcaster) BroadcastMessage(ctx context.Context, msg *Message) (uid.UID, error) { - b.logger.Println(msg.Body) + + b.logger.Printf("%s %s\n", msg.Title, msg.Body) now := time.Now() ts := now.Unix() - // pending uid.NewInt64UID - str_ts := strconv.FormatInt(ts, 10) - - return uid.NewStringUID(ctx, str_ts) + return uid.NewInt64UID(ctx, ts) } +// SetLoggers assigns 'logger' to 'b'. func (b *LogBroadcaster) SetLogger(ctx context.Context, logger *log.Logger) error { b.logger = logger return nil diff --git a/vendor/github.com/aaronland/go-broadcaster/message.go b/vendor/github.com/aaronland/go-broadcaster/message.go deleted file mode 100644 index bc929ec..0000000 --- a/vendor/github.com/aaronland/go-broadcaster/message.go +++ /dev/null @@ -1,11 +0,0 @@ -package broadcaster - -import ( - "image" -) - -type Message struct { - Title string - Body string - Images []image.Image -} diff --git a/vendor/modules.txt b/vendor/modules.txt index 286ab18..6999030 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,7 +1,7 @@ # github.com/aaronland/go-aws-session v0.0.6 ## explicit; go 1.12 github.com/aaronland/go-aws-session -# github.com/aaronland/go-broadcaster v0.0.5 +# github.com/aaronland/go-broadcaster v0.0.7 ## explicit; go 1.19 github.com/aaronland/go-broadcaster github.com/aaronland/go-broadcaster/app/broadcast