feature: add 'attachment' field to actor

This commit is contained in:
doesnm 2024-05-14 16:36:09 +03:00
parent dc13a4f4d8
commit 3e0a6353c9

View file

@ -27,6 +27,14 @@ import (
// Actor represents a local actor we can act on // Actor represents a local actor we can act on
// behalf of. // behalf of.
//type Attachment struct {
// Type string
// Name string
// Href string
// Rel string
//}
type Actor struct { type Actor struct {
Name, summary, actorType, iri string Name, summary, actorType, iri string
followersIRI string followersIRI string
@ -41,15 +49,18 @@ type Actor struct {
publicKeyID string publicKeyID string
OnFollow func(map[string]interface{}) OnFollow func(map[string]interface{})
OnReceiveContent func(map[string]interface{}) OnReceiveContent func(map[string]interface{})
attachment []interface {}
} }
// ActorToSave is a stripped down actor representation // ActorToSave is a stripped down actor representation
// with exported properties in order for json to be // with exported properties in order for json to be
// able to marshal it. // able to marshal it.
// see https://stackoverflow.com/questions/26327391/json-marshalstruct-returns // see https://stackoverflow.com/questions/26327391/json-marshalstruct-returns
type ActorToSave struct { type ActorToSave struct {
Name, Summary, ActorType, IRI, PublicKey, PrivateKey string Name, Summary, ActorType, IRI, PublicKey, PrivateKey string
Followers, Following, Rejected, Requested map[string]interface{} Followers, Following, Rejected, Requested map[string]interface{}
Attachment []interface {}
} }
// MakeActor creates and returns a new local actor we can act // MakeActor creates and returns a new local actor we can act
@ -59,6 +70,7 @@ func MakeActor(name, summary, actorType string) (Actor, error) {
following := make(map[string]interface{}) following := make(map[string]interface{})
rejected := make(map[string]interface{}) rejected := make(map[string]interface{})
requested := make(map[string]interface{}) requested := make(map[string]interface{})
var attachment []interface{}
followersIRI := baseURL + name + "/followers" followersIRI := baseURL + name + "/followers"
publicKeyID := baseURL + name + "#main-key" publicKeyID := baseURL + name + "#main-key"
iri := baseURL + name iri := baseURL + name
@ -79,6 +91,7 @@ func MakeActor(name, summary, actorType string) (Actor, error) {
requested: requested, requested: requested,
followersIRI: followersIRI, followersIRI: followersIRI,
publicKeyID: publicKeyID, publicKeyID: publicKeyID,
attachment: attachment,
} }
// set auto accept by default (this could be a configuration value) // set auto accept by default (this could be a configuration value)
@ -199,6 +212,7 @@ func LoadActor(name string) (Actor, error) {
privateKeyPem: jsonData["PrivateKey"].(string), privateKeyPem: jsonData["PrivateKey"].(string),
followersIRI: baseURL + name + "/followers", followersIRI: baseURL + name + "/followers",
publicKeyID: baseURL + name + "#main-key", publicKeyID: baseURL + name + "#main-key",
attachment: jsonData["Attachment"].([]interface{}),
} }
actor.OnFollow = func(activity map[string]interface{}) { actor.Accept(activity) } actor.OnFollow = func(activity map[string]interface{}) { actor.Accept(activity) }
@ -265,6 +279,7 @@ func (a *Actor) save() error {
Requested: a.requested, Requested: a.requested,
PublicKey: a.publicKeyPem, PublicKey: a.publicKeyPem,
PrivateKey: a.privateKeyPem, PrivateKey: a.privateKeyPem,
Attachment: a.attachment,
} }
actorJSON, err := json.MarshalIndent(actorToSave, "", "\t") actorJSON, err := json.MarshalIndent(actorToSave, "", "\t")
@ -296,6 +311,7 @@ func (a *Actor) whoAmI() string {
self["outbox"] = baseURL + a.Name + "/outbox" self["outbox"] = baseURL + a.Name + "/outbox"
self["followers"] = baseURL + a.Name + "/peers/followers" self["followers"] = baseURL + a.Name + "/peers/followers"
self["following"] = baseURL + a.Name + "/peers/following" self["following"] = baseURL + a.Name + "/peers/following"
self["attachment"] = a.attachment
self["publicKey"] = map[string]string{ self["publicKey"] = map[string]string{
"id": baseURL + a.Name + "#main-key", "id": baseURL + a.Name + "#main-key",
"owner": baseURL + a.Name, "owner": baseURL + a.Name,
@ -326,7 +342,7 @@ func (a *Actor) newID() (hash string, url string) {
// CreateNote posts an activityPub note to our followers // CreateNote posts an activityPub note to our followers
// //
func (a *Actor) CreateNote(content, inReplyTo string) { func (a *Actor) CreateNote(content, inReplyTo string,attachment []interface{}) {
// for now I will just write this to the outbox // for now I will just write this to the outbox
hash, id := a.newItemID() hash, id := a.newItemID()
create := make(map[string]interface{}) create := make(map[string]interface{})
@ -342,6 +358,9 @@ func (a *Actor) CreateNote(content, inReplyTo string) {
if inReplyTo != "" { if inReplyTo != "" {
note["inReplyTo"] = inReplyTo note["inReplyTo"] = inReplyTo
} }
if inReplyTo != nil {
note["attachment"] = attachment
}
note["id"] = id note["id"] = id
note["published"] = time.Now().Format(time.RFC3339) note["published"] = time.Now().Format(time.RFC3339)
note["url"] = create["id"] note["url"] = create["id"]