From c2aa71512e0b077ec8a89bf72509c58917142b7e Mon Sep 17 00:00:00 2001 From: Iris Lightshard Date: Sat, 8 Jun 2024 14:40:14 -0600 Subject: [PATCH] build most of a mastodon messeage from a madon.Status --- adapter/mastodon.go | 44 +++++++++++++++++++++++++++++++++----------- adapter/nostr.go | 5 +++-- models/msg.go | 7 ++++--- server/api.go | 3 ++- 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/adapter/mastodon.go b/adapter/mastodon.go index 962b85f..b846fbc 100644 --- a/adapter/mastodon.go +++ b/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 @@ func (self *MastoAdapter) Subscribe(filter string) []error { 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 @@ func (self *MastoAdapter) Subscribe(filter string) []error { // 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 } diff --git a/adapter/nostr.go b/adapter/nostr.go index ed87ddd..86d85ab 100644 --- a/adapter/nostr.go +++ b/adapter/nostr.go @@ -56,7 +56,7 @@ func (self *NostrAdapter) Subscribe(filter string) []error { 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") diff --git a/models/msg.go b/models/msg.go index f122f5e..7169908 100644 --- a/models/msg.go +++ b/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 { diff --git a/server/api.go b/server/api.go index 055021e..2923965 100644 --- a/server/api.go +++ b/server/api.go @@ -77,7 +77,8 @@ func apiConfigureAdapters(next http.Handler, subscribers map[*Subscriber][]adapt switch s.Protocol { case "nostr": a = &adapter.NostrAdapter{} - break + case "mastodon": + a = &adapter.MastoAdapter{} default: break