build most of a mastodon messeage from a madon.Status
Iris Lightshard nilix@nilfm.cc
4 files changed,
42 insertions(+),
17 deletions(-)
M
adapter/mastodon.go
→
adapter/mastodon.go
@@ -1,6 +1,7 @@
package adapter import ( + "fmt" . "forge.lightcrystal.systems/lightcrystal/underbbs/models" madon "github.com/McKael/madon" )@@ -39,20 +40,18 @@ func (self *MastoAdapter) Subscribe(filter string) []error {
// TODO: decode separate timelines and hashtags // for now, the filter is just the timeline - // if any existing events channel, close it and create a new one if self.events != nil { close(self.events) } self.events = make(chan madon.StreamEvent) - // if any existing stop channel, close it and create a new one + if self.stop != nil { close(self.stop) } self.stop = make(chan bool) - // make a new done channel + self.done = make(chan bool) - // call StreamListener self.masto.StreamListener(filter, "", self.events, self.stop, self.done) go func() { for e := range self.events {@@ -64,10 +63,10 @@ switch v := e.Data.(type) {
case int64: s, _ := self.masto.GetStatus(v) if s != nil { - msg = mastoUpdateToMessage(*s) + msg = self.mastoUpdateToMessage(*s) } case madon.Status: - msg = mastoUpdateToMessage(v) + msg = self.mastoUpdateToMessage(v) } if msg != nil { self.data <- msg@@ -80,8 +79,7 @@ }()
// 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 - errs := make([]error, 0) - return errs + return nil } func (self *MastoAdapter) SendMessage(msg Message) error { return nil@@ -103,7 +101,31 @@ func (self *MastoAdapter) DefaultSubscriptionFilter() string {
return "home" } -func mastoUpdateToMessage(status madon.Status) *Message { - // decode that fucker - return nil +func (self *MastoAdapter) mastoUpdateToMessage(status madon.Status) *Message { + var parent *madon.Status + + if status.InReplyToID != nil { + parent, _ = self.masto.GetStatus(*status.InReplyToID) + } + msg := Message{ + Protocol: "mastodon", + Content: status.Content, + Uri: status.URI, + Author: Author{ + Id: fmt.Sprintf("%d", status.Account.ID), + Name: status.Account.Username, + // TODO: we can add the fields to the profiledata as well + ProfileData: status.Account.Note, + ProfileUri: status.Account.URL, + ProfilePic: status.Account.Avatar, + }, + Created: status.CreatedAt, + } + if parent != nil { + msg.ReplyTo = &parent.URI + } + // TODO: mentions and replies + msg.Aux = make(map[string]string) + msg.Aux["visibility"] = status.Visibility + return &msg }
M
adapter/nostr.go
→
adapter/nostr.go
@@ -56,7 +56,7 @@ for ev := range sub.Events {
fmt.Print("!") // try sequentially to encode into an underbbs object // and send it to the appropriate channel - m, err := nostrEventToMsg(ev) + m, err := self.nostrEventToMsg(ev) if err == nil { self.data <- m }@@ -94,9 +94,10 @@ func (self *NostrAdapter) DefaultSubscriptionFilter() string {
return "[{\"kinds\":[1]}]" } -func nostrEventToMsg(evt *nostr.Event) (Message, error) { +func (self *NostrAdapter) nostrEventToMsg(evt *nostr.Event) (Message, error) { m := Message{ Protocol: "nostr", + Adapter: self.nickname, } if evt == nil { return m, errors.New("no event")
M
models/msg.go
→
models/msg.go
@@ -9,13 +9,14 @@ type Message struct {
Uri string Author Author Protocol string + Adapter string Content string Attachments []Attachment - ReplyTo *Message - Replies []Message + ReplyTo *string + Replies []*string Mentions []Author Created time.Time - Aux *interface{} + Aux map[string]string } type Author struct {
M
server/api.go
→
server/api.go
@@ -77,7 +77,8 @@ var a adapter.Adapter
switch s.Protocol { case "nostr": a = &adapter.NostrAdapter{} - break + case "mastodon": + a = &adapter.MastoAdapter{} default: break