From 194a5aed48e001d09913efa3d8f93afae0a2ed47 Mon Sep 17 00:00:00 2001 From: Iris Lightshard Date: Fri, 29 Nov 2024 12:00:54 -0700 Subject: [PATCH] generic AP and honk progress --- adapter/anonAp.go | 97 +++++++++++++++++++++++++++++++++++++++++++---- adapter/honk.go | 2 +- 2 files changed, 91 insertions(+), 8 deletions(-) diff --git a/adapter/anonAp.go b/adapter/anonAp.go index f27c27c..d099cb0 100644 --- a/adapter/anonAp.go +++ b/adapter/anonAp.go @@ -6,6 +6,7 @@ import ( "io" "net/http" "os" + "strings" "time" "forge.lightcrystal.systems/lightcrystal/underbbs/models" @@ -15,6 +16,8 @@ type anonAPAdapter struct { data *chan models.SocketData server string client http.Client + protocol string + nickname string } type apLink struct { @@ -23,6 +26,20 @@ type apLink struct { Type string } +type apAttachment struct { + MediaType string + Type string + Name string + Summary string + Url string +} + +type apTag struct { + Href string + Name string + Type string +} + type apIcon struct { MediaType string Type string @@ -38,13 +55,31 @@ type apActor struct { Url string } +type apActivity struct { + Id string + Content string + AttributedTo string + Context string + Conversation string + Published string + Tag []apTag + Attachment []apAttachment + To string + Url string + Actor *string + Object *string + InReplyTo *string +} + type webFinger struct { Links []apLink } -func (self *anonAPAdapter) Init(data *chan models.SocketData, server string) error { +func (self *anonAPAdapter) Init(data *chan models.SocketData, server, protocol, nickname string) error { self.data = data self.server = server + self.nickname = nickname + self.protocol = protocol return nil } @@ -65,8 +100,39 @@ func (self *anonAPAdapter) makeApRequest(method, url string, data io.Reader) (*h return self.client.Do(req) } -func toMsg(activity map[string]interface{}) *models.Message { - return nil +func (self *anonAPAdapter) toMsg(activity apActivity) *models.Message { + t, err := time.Parse(time.RFC3339, activity.Published) + if err != nil { + t = time.Now() + } + vis := strings.Split(activity.To, "#") + if len(vis) > 1 { + activity.To = vis[1] + } + m := &models.Message{ + Datagram: models.Datagram{ + Id: activity.Id, + Uri: activity.Url, + Type: "message", + Created: t.UnixMilli(), + Updated: nil, + Protocol: self.protocol, + Adapter: self.nickname, + }, + Author: activity.AttributedTo, + Content: activity.Content, + ReplyTo: activity.InReplyTo, + Visibility: activity.To, + } + + for _, a := range activity.Attachment { + m.Attachments = append(m.Attachments, models.Attachment{ + Src: a.Url, + Desc: a.Summary, + }) + } + + return m } func (self *anonAPAdapter) send(data models.SocketData) { @@ -77,21 +143,23 @@ func (self *anonAPAdapter) send(data models.SocketData) { } } -func toAuthor(actor apActor) *models.Author { +func (self *anonAPAdapter) toAuthor(actor apActor) *models.Author { curtime := time.Now().UnixMilli() - self := &models.Author{ + a := &models.Author{ Datagram: models.Datagram{ Id: actor.Id, Uri: actor.Url, Type: "author", Created: curtime, Updated: &curtime, + Protocol: self.protocol, + Adapter: self.nickname, }, Name: actor.PreferredUsername, ProfileData: actor.Summary, ProfilePic: actor.Icon.Url, } - return self + return a } func (self *anonAPAdapter) Fetch(etype string, ids []string) error { @@ -122,12 +190,27 @@ func (self *anonAPAdapter) Fetch(etype string, ids []string) error { authorData := getBodyJson(res) actor := apActor{} json.Unmarshal(authorData, &actor) - author := toAuthor(actor) + author := self.toAuthor(actor) if author != nil { self.send(author) } case "byAuthor": + // get outbox + // for each item in outbox, check if it's a Create/Update or an Announce + // Create/Update you can directly deserialize the object + // if it's an Announce, try to get the object and deserialize it, build a boost out of it case "message": + res, err := self.makeApRequest("GET", id, nil) + if err != nil { + return err + } + activityData := getBodyJson(res) + activity := apActivity{} + json.Unmarshal(activityData, &activity) + message := self.toMsg(activity) + if message != nil { + self.send(message) + } case "children": case "convoy": default: diff --git a/adapter/honk.go b/adapter/honk.go index b2e8b28..2516ec7 100644 --- a/adapter/honk.go +++ b/adapter/honk.go @@ -78,7 +78,7 @@ func (self *HonkAdapter) Fetch(etype string, ids []string) error { // KISS if self.isAnonymous() { aaa := anonAPAdapter{} - aaa.Init(self.data, self.server) + aaa.Init(self.data, self.server, "honk", self.nickname) return aaa.Fetch(etype, ids) }