underbbs/adapter/mastodon.go

123 lines
2.7 KiB
Go
Raw Normal View History

2024-06-08 17:58:01 +00:00
package adapter
import (
"fmt"
2024-06-08 17:58:01 +00:00
. "forge.lightcrystal.systems/lightcrystal/underbbs/models"
madon "github.com/McKael/madon"
)
type MastoAdapter struct {
data chan SocketData
nickname string
server string
apiKey string
masto *madon.Client
events chan madon.StreamEvent
stop chan bool
done chan bool
}
var scopes = []string{"read", "write", "follow"}
func (self *MastoAdapter) Init(settings Settings, data chan SocketData) error {
self.nickname = settings.Nickname
self.server = *settings.Server
self.apiKey = *settings.ApiKey
self.data = data
masto, err := madon.NewApp("underbbs", "https://lightcrystal.systems", scopes, madon.NoRedirect, self.server)
if err != nil {
return err
}
self.masto = masto
err = self.masto.SetUserToken(self.apiKey, "", "", []string{})
return err
}
func (self *MastoAdapter) Subscribe(filter string) []error {
// TODO: decode separate timelines and hashtags
// for now, the filter is just the timeline
if self.events != nil {
close(self.events)
}
self.events = make(chan madon.StreamEvent)
2024-06-08 17:58:01 +00:00
if self.stop != nil {
close(self.stop)
}
self.stop = make(chan bool)
2024-06-08 17:58:01 +00:00
self.done = make(chan bool)
2024-06-22 16:39:02 +00:00
err := self.masto.StreamListener(filter, "", self.events, self.stop, self.done)
if err != nil {
return []error{err}
}
2024-06-08 17:58:01 +00:00
go func() {
for e := range self.events {
2024-06-22 16:39:02 +00:00
fmt.Println("event: %s !!!", e.Event)
2024-06-08 17:58:01 +00:00
switch e.Event {
case "error":
case "update":
var msg *Message
switch v := e.Data.(type) {
case int64:
s, _ := self.masto.GetStatus(v)
if s != nil {
msg = self.mastoUpdateToMessage(*s)
2024-06-08 17:58:01 +00:00
}
case madon.Status:
msg = self.mastoUpdateToMessage(v)
2024-06-08 17:58:01 +00:00
}
if msg != nil {
self.data <- msg
}
case "notification":
case "delete":
}
}
}()
// in the background, read and translate events from the stream
// and check for doneCh closing
// the stopCh will be closed by a subsequent call to subscribe
return nil
2024-06-08 17:58:01 +00:00
}
2024-06-22 16:39:02 +00:00
func (self *MastoAdapter) Fetch(query string) error {
2024-06-08 17:58:01 +00:00
return nil
}
2024-06-22 16:39:02 +00:00
func (self *MastoAdapter) Do(action string) error {
2024-06-08 17:58:01 +00:00
return nil
}
func (self *MastoAdapter) DefaultSubscriptionFilter() string {
2024-06-22 16:39:02 +00:00
return "user"
2024-06-08 17:58:01 +00:00
}
func (self *MastoAdapter) mastoUpdateToMessage(status madon.Status) *Message {
msg := Message{
2024-06-30 00:18:31 +00:00
Datagram: Datagram{
Protocol: "mastodon",
Adapter: self.nickname,
Id: fmt.Sprintf("%d", status.ID),
Uri: status.URI,
Type: "message",
},
Content: status.Content,
Author: status.Account.Acct,
Created: status.CreatedAt,
Visibility: status.Visibility,
}
2024-06-30 00:18:31 +00:00
if status.InReplyToID != nil {
idStr := fmt.Sprintf("%d", *status.InReplyToID)
msg.ReplyTo = &idStr
}
// TODO: mentions and replies
2024-06-30 00:18:31 +00:00
return &msg
2024-06-08 17:58:01 +00:00
}