gotosocial/internal/typeutils/wrap.go

69 lines
2.1 KiB
Go
Raw Normal View History

2021-05-28 20:47:18 +00:00
package typeutils
import (
"fmt"
"net/url"
"github.com/go-fed/activity/pub"
2021-05-28 20:47:18 +00:00
"github.com/go-fed/activity/streams"
"github.com/go-fed/activity/streams/vocab"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
2021-05-28 20:47:18 +00:00
"github.com/superseriousbusiness/gotosocial/internal/util"
)
func (c *converter) WrapPersonInUpdate(person vocab.ActivityStreamsPerson, originAccount *gtsmodel.Account) (vocab.ActivityStreamsUpdate, error) {
update := streams.NewActivityStreamsUpdate()
// set the actor
actorURI, err := url.Parse(originAccount.URI)
if err != nil {
return nil, fmt.Errorf("WrapPersonInUpdate: error parsing url %s: %s", originAccount.URI, err)
}
actorProp := streams.NewActivityStreamsActorProperty()
actorProp.AppendIRI(actorURI)
update.SetActivityStreamsActor(actorProp)
// set the ID
newID, err := id.NewRandomULID()
if err != nil {
return nil, err
}
idString := util.GenerateURIForUpdate(originAccount.Username, c.config.Protocol, c.config.Host, newID)
2021-05-28 20:47:18 +00:00
idURI, err := url.Parse(idString)
if err != nil {
return nil, fmt.Errorf("WrapPersonInUpdate: error parsing url %s: %s", idString, err)
}
idProp := streams.NewJSONLDIdProperty()
idProp.SetIRI(idURI)
update.SetJSONLDId(idProp)
// set the person as the object here
objectProp := streams.NewActivityStreamsObjectProperty()
objectProp.AppendActivityStreamsPerson(person)
update.SetActivityStreamsObject(objectProp)
// to should be public
toURI, err := url.Parse(pub.PublicActivityPubIRI)
2021-05-28 20:47:18 +00:00
if err != nil {
return nil, fmt.Errorf("WrapPersonInUpdate: error parsing url %s: %s", pub.PublicActivityPubIRI, err)
2021-05-28 20:47:18 +00:00
}
toProp := streams.NewActivityStreamsToProperty()
toProp.AppendIRI(toURI)
update.SetActivityStreamsTo(toProp)
// bcc followers
followersURI, err := url.Parse(originAccount.FollowersURI)
if err != nil {
return nil, fmt.Errorf("WrapPersonInUpdate: error parsing url %s: %s", originAccount.FollowersURI, err)
}
bccProp := streams.NewActivityStreamsBccProperty()
bccProp.AppendIRI(followersURI)
update.SetActivityStreamsBcc(bccProp)
return update, nil
}