build most of a mastodon messeage from a madon.Status

This commit is contained in:
Iris Lightshard 2024-06-08 14:40:14 -06:00
parent d552fc53b9
commit c2aa71512e
4 changed files with 42 additions and 17 deletions

View file

@ -1,6 +1,7 @@
package adapter package adapter
import ( import (
"fmt"
. "forge.lightcrystal.systems/lightcrystal/underbbs/models" . "forge.lightcrystal.systems/lightcrystal/underbbs/models"
madon "github.com/McKael/madon" madon "github.com/McKael/madon"
) )
@ -39,20 +40,18 @@ func (self *MastoAdapter) Subscribe(filter string) []error {
// TODO: decode separate timelines and hashtags // TODO: decode separate timelines and hashtags
// for now, the filter is just the timeline // for now, the filter is just the timeline
// if any existing events channel, close it and create a new one
if self.events != nil { if self.events != nil {
close(self.events) close(self.events)
} }
self.events = make(chan madon.StreamEvent) self.events = make(chan madon.StreamEvent)
// if any existing stop channel, close it and create a new one
if self.stop != nil { if self.stop != nil {
close(self.stop) close(self.stop)
} }
self.stop = make(chan bool) self.stop = make(chan bool)
// make a new done channel
self.done = make(chan bool) self.done = make(chan bool)
// call StreamListener
self.masto.StreamListener(filter, "", self.events, self.stop, self.done) self.masto.StreamListener(filter, "", self.events, self.stop, self.done)
go func() { go func() {
for e := range self.events { for e := range self.events {
@ -64,10 +63,10 @@ func (self *MastoAdapter) Subscribe(filter string) []error {
case int64: case int64:
s, _ := self.masto.GetStatus(v) s, _ := self.masto.GetStatus(v)
if s != nil { if s != nil {
msg = mastoUpdateToMessage(*s) msg = self.mastoUpdateToMessage(*s)
} }
case madon.Status: case madon.Status:
msg = mastoUpdateToMessage(v) msg = self.mastoUpdateToMessage(v)
} }
if msg != nil { if msg != nil {
self.data <- msg self.data <- msg
@ -80,8 +79,7 @@ func (self *MastoAdapter) Subscribe(filter string) []error {
// in the background, read and translate events from the stream // in the background, read and translate events from the stream
// and check for doneCh closing // and check for doneCh closing
// the stopCh will be closed by a subsequent call to subscribe // the stopCh will be closed by a subsequent call to subscribe
errs := make([]error, 0) return nil
return errs
} }
func (self *MastoAdapter) SendMessage(msg Message) error { func (self *MastoAdapter) SendMessage(msg Message) error {
return nil return nil
@ -103,7 +101,31 @@ func (self *MastoAdapter) DefaultSubscriptionFilter() string {
return "home" return "home"
} }
func mastoUpdateToMessage(status madon.Status) *Message { func (self *MastoAdapter) mastoUpdateToMessage(status madon.Status) *Message {
// decode that fucker var parent *madon.Status
return nil
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
} }

View file

@ -56,7 +56,7 @@ func (self *NostrAdapter) Subscribe(filter string) []error {
fmt.Print("!") fmt.Print("!")
// try sequentially to encode into an underbbs object // try sequentially to encode into an underbbs object
// and send it to the appropriate channel // and send it to the appropriate channel
m, err := nostrEventToMsg(ev) m, err := self.nostrEventToMsg(ev)
if err == nil { if err == nil {
self.data <- m self.data <- m
} }
@ -94,9 +94,10 @@ func (self *NostrAdapter) DefaultSubscriptionFilter() string {
return "[{\"kinds\":[1]}]" return "[{\"kinds\":[1]}]"
} }
func nostrEventToMsg(evt *nostr.Event) (Message, error) { func (self *NostrAdapter) nostrEventToMsg(evt *nostr.Event) (Message, error) {
m := Message{ m := Message{
Protocol: "nostr", Protocol: "nostr",
Adapter: self.nickname,
} }
if evt == nil { if evt == nil {
return m, errors.New("no event") return m, errors.New("no event")

View file

@ -9,13 +9,14 @@ type Message struct {
Uri string Uri string
Author Author Author Author
Protocol string Protocol string
Adapter string
Content string Content string
Attachments []Attachment Attachments []Attachment
ReplyTo *Message ReplyTo *string
Replies []Message Replies []*string
Mentions []Author Mentions []Author
Created time.Time Created time.Time
Aux *interface{} Aux map[string]string
} }
type Author struct { type Author struct {

View file

@ -77,7 +77,8 @@ func apiConfigureAdapters(next http.Handler, subscribers map[*Subscriber][]adapt
switch s.Protocol { switch s.Protocol {
case "nostr": case "nostr":
a = &adapter.NostrAdapter{} a = &adapter.NostrAdapter{}
break case "mastodon":
a = &adapter.MastoAdapter{}
default: default:
break break