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
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
}

View file

@ -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")

View file

@ -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 {

View file

@ -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