Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored Mastodonbee a bit #285

Merged
merged 2 commits into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions bees/mastodonbee/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

// handleUpdateEvent handles incoming Toot updates from mastodon from yourself
// and from people you follow.
func (mod *mastodonBee) handleStatus(status *mastodon.Status) {
func (mod *MastodonBee) handleStatus(status *mastodon.Status) {
ev := bees.Event{
Bee: mod.Name(),
Name: "toot_fetched",
Expand Down Expand Up @@ -70,12 +70,17 @@ func (mod *mastodonBee) handleStatus(status *mastodon.Status) {
Value: status.URL,
Type: "string",
},
{
Name: "created",
Value: status.CreatedAt,
Type: "time.Time",
},
},
}
mod.evchan <- ev
}

func (mod *mastodonBee) handleNotification(notif *mastodon.Notification) {
func (mod *MastodonBee) handleNotification(notif *mastodon.Notification) {
switch notif.Type {

case "follow":
Expand Down
14 changes: 7 additions & 7 deletions bees/mastodonbee/mastodonbee.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
"github.com/muesli/beehive/bees"
)

// mastodonBee is a Bee that can connect to mastodon.
type mastodonBee struct {
// MastodonBee is a Bee that can connect to mastodon.
type MastodonBee struct {
bees.Bee

server string
Expand All @@ -47,7 +47,7 @@ type mastodonBee struct {
}

// Action triggers the action passed to it.
func (mod *mastodonBee) Action(action bees.Action) []bees.Placeholder {
func (mod *MastodonBee) Action(action bees.Action) []bees.Placeholder {
outs := []bees.Placeholder{}

switch action.Name {
Expand Down Expand Up @@ -268,7 +268,7 @@ func (mod *mastodonBee) Action(action bees.Action) []bees.Placeholder {
return outs
}

func (mod *mastodonBee) handleStreamEvent(item interface{}) {
func (mod *MastodonBee) handleStreamEvent(item interface{}) {
switch e := item.(type) {
case *mastodon.UpdateEvent:
mod.handleStatus(e.Status)
Expand All @@ -292,7 +292,7 @@ func (mod *mastodonBee) handleStreamEvent(item interface{}) {
}
}

func (mod *mastodonBee) handleStream() {
func (mod *MastodonBee) handleStream() {
timeline, err := mod.client.StreamingUser(context.Background())
if err != nil {
mod.LogErrorf("Failed to get user stream: %+v", err)
Expand All @@ -310,7 +310,7 @@ func (mod *mastodonBee) handleStream() {
}

// Run executes the Bee's event loop.
func (mod *mastodonBee) Run(eventChan chan bees.Event) {
func (mod *MastodonBee) Run(eventChan chan bees.Event) {
// Create the new api client
c := mastodon.NewClient(&mastodon.Config{
Server: mod.server,
Expand Down Expand Up @@ -339,7 +339,7 @@ func (mod *mastodonBee) Run(eventChan chan bees.Event) {
}

// ReloadOptions parses the config options and initializes the Bee.
func (mod *mastodonBee) ReloadOptions(options bees.BeeOptions) {
func (mod *MastodonBee) ReloadOptions(options bees.BeeOptions) {
mod.SetOptions(options)

options.Bind("server", &mod.server)
Expand Down
31 changes: 18 additions & 13 deletions bees/mastodonbee/mastodonbeefactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ package mastodonbee

import "github.com/muesli/beehive/bees"

// mastodonBeeFactory is a factory for mastodonBees.
type mastodonBeeFactory struct {
// MastodonBeeFactory is a factory for mastodonBees.
type MastodonBeeFactory struct {
bees.BeeFactory
}

// New returns a new Bee instance configured with the supplied options.
func (factory *mastodonBeeFactory) New(name, description string, options bees.BeeOptions) bees.BeeInterface {
bee := mastodonBee{
func (factory *MastodonBeeFactory) New(name, description string, options bees.BeeOptions) bees.BeeInterface {
bee := MastodonBee{
Bee: bees.NewBee(name, factory.ID(), description, options),
}
bee.ReloadOptions(options)
Expand All @@ -40,32 +40,32 @@ func (factory *mastodonBeeFactory) New(name, description string, options bees.Be
}

// ID returns the ID of this Bee.
func (factory *mastodonBeeFactory) ID() string {
func (factory *MastodonBeeFactory) ID() string {
return "mastodonbee"
}

// Name returns the name of this Bee.
func (factory *mastodonBeeFactory) Name() string {
func (factory *MastodonBeeFactory) Name() string {
return "mastodon"
}

// Description returns the description of this Bee.
func (factory *mastodonBeeFactory) Description() string {
func (factory *MastodonBeeFactory) Description() string {
return "Interact with mastodon"
}

// Image returns the filename of an image for this Bee.
func (factory *mastodonBeeFactory) Image() string {
func (factory *MastodonBeeFactory) Image() string {
return factory.ID() + ".png"
}

// LogoColor returns the preferred logo background color (used by the admin interface).
func (factory *mastodonBeeFactory) LogoColor() string {
func (factory *MastodonBeeFactory) LogoColor() string {
return "#003b66"
}

// Options returns the options available to configure this Bee.
func (factory *mastodonBeeFactory) Options() []bees.BeeOptionDescriptor {
func (factory *MastodonBeeFactory) Options() []bees.BeeOptionDescriptor {
opts := []bees.BeeOptionDescriptor{
{
Name: "server",
Expand Down Expand Up @@ -102,7 +102,7 @@ func (factory *mastodonBeeFactory) Options() []bees.BeeOptionDescriptor {
}

// Events describes the available events provided by this Bee.
func (factory *mastodonBeeFactory) Events() []bees.EventDescriptor {
func (factory *MastodonBeeFactory) Events() []bees.EventDescriptor {
events := []bees.EventDescriptor{
{
Namespace: factory.Name(),
Expand Down Expand Up @@ -156,6 +156,11 @@ func (factory *mastodonBeeFactory) Events() []bees.EventDescriptor {
Description: "The url for the toot",
Type: "string",
},
{
Name: "created",
Description: "The timestamp of the toots creation",
Type: "time.Time",
},
},
},
{
Expand Down Expand Up @@ -419,7 +424,7 @@ func (factory *mastodonBeeFactory) Events() []bees.EventDescriptor {
}

// Actions describes the available actions provided by this Bee.
func (factory *mastodonBeeFactory) Actions() []bees.ActionDescriptor {
func (factory *MastodonBeeFactory) Actions() []bees.ActionDescriptor {
actions := []bees.ActionDescriptor{
{
Namespace: factory.Name(),
Expand Down Expand Up @@ -510,6 +515,6 @@ func (factory *mastodonBeeFactory) Actions() []bees.ActionDescriptor {
}

func init() {
f := mastodonBeeFactory{}
f := MastodonBeeFactory{}
bees.RegisterFactory(&f)
}