summaryrefslogtreecommitdiff
path: root/internal/typeutils
diff options
context:
space:
mode:
authorLibravatar Tobi Smethurst <31960611+tsmethurst@users.noreply.github.com>2021-05-24 18:49:48 +0200
committerLibravatar GitHub <noreply@github.com>2021-05-24 18:49:48 +0200
commite670c32a9147f632d06ee10c170201677ec1e12d (patch)
tree08a49c9942529d3e7457a54b72d334ad36060976 /internal/typeutils
parentfirst draft of Dockerfile (diff)
downloadgotosocial-e670c32a9147f632d06ee10c170201677ec1e12d.tar.xz
Faves (#31)
* start on federating faves * outbound federation of likes working
Diffstat (limited to 'internal/typeutils')
-rw-r--r--internal/typeutils/converter.go3
-rw-r--r--internal/typeutils/internaltoas.go81
2 files changed, 84 insertions, 0 deletions
diff --git a/internal/typeutils/converter.go b/internal/typeutils/converter.go
index 63e201ded..3ced20926 100644
--- a/internal/typeutils/converter.go
+++ b/internal/typeutils/converter.go
@@ -126,6 +126,9 @@ type TypeConverter interface {
// AttachmentToAS converts a gts model media attachment into an activity streams Attachment, suitable for federation
AttachmentToAS(a *gtsmodel.MediaAttachment) (vocab.ActivityStreamsDocument, error)
+
+ // FaveToAS converts a gts model status fave into an activityStreams LIKE, suitable for federation.
+ FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error)
}
type converter struct {
diff --git a/internal/typeutils/internaltoas.go b/internal/typeutils/internaltoas.go
index b7056ccbe..821720e0d 100644
--- a/internal/typeutils/internaltoas.go
+++ b/internal/typeutils/internaltoas.go
@@ -559,3 +559,84 @@ func (c *converter) AttachmentToAS(a *gtsmodel.MediaAttachment) (vocab.ActivityS
return doc, nil
}
+
+/*
+ We want to end up with something like this:
+
+ {
+ "@context": "https://www.w3.org/ns/activitystreams",
+ "actor": "https://ondergrond.org/users/dumpsterqueer",
+ "id": "https://ondergrond.org/users/dumpsterqueer#likes/44584",
+ "object": "https://testingtesting123.xyz/users/gotosocial_test_account/statuses/771aea80-a33d-4d6d-8dfd-57d4d2bfcbd4",
+ "type": "Like"
+ }
+*/
+func (c *converter) FaveToAS(f *gtsmodel.StatusFave) (vocab.ActivityStreamsLike, error) {
+ // check if targetStatus is already pinned to this fave, and fetch it if not
+ if f.GTSStatus == nil {
+ s := &gtsmodel.Status{}
+ if err := c.db.GetByID(f.StatusID, s); err != nil {
+ return nil, fmt.Errorf("FaveToAS: error fetching target status from database: %s", err)
+ }
+ f.GTSStatus = s
+ }
+
+ // check if the targetAccount is already pinned to this fave, and fetch it if not
+ if f.GTSTargetAccount == nil {
+ a := &gtsmodel.Account{}
+ if err := c.db.GetByID(f.TargetAccountID, a); err != nil {
+ return nil, fmt.Errorf("FaveToAS: error fetching target account from database: %s", err)
+ }
+ f.GTSTargetAccount = a
+ }
+
+ // check if the faving account is already pinned to this fave, and fetch it if not
+ if f.GTSFavingAccount == nil {
+ a := &gtsmodel.Account{}
+ if err := c.db.GetByID(f.AccountID, a); err != nil {
+ return nil, fmt.Errorf("FaveToAS: error fetching faving account from database: %s", err)
+ }
+ f.GTSFavingAccount = a
+ }
+
+ // create the like
+ like := streams.NewActivityStreamsLike()
+
+ // set the actor property to the fave-ing account's URI
+ actorProp := streams.NewActivityStreamsActorProperty()
+ actorIRI, err := url.Parse(f.GTSFavingAccount.URI)
+ if err != nil {
+ return nil, fmt.Errorf("FaveToAS: error parsing uri %s: %s", f.GTSFavingAccount.URI, err)
+ }
+ actorProp.AppendIRI(actorIRI)
+ like.SetActivityStreamsActor(actorProp)
+
+ // set the ID property to the fave's URI
+ idProp := streams.NewJSONLDIdProperty()
+ idIRI, err := url.Parse(f.URI)
+ if err != nil {
+ return nil, fmt.Errorf("FaveToAS: error parsing uri %s: %s", f.URI, err)
+ }
+ idProp.Set(idIRI)
+ like.SetJSONLDId(idProp)
+
+ // set the object property to the target status's URI
+ objectProp := streams.NewActivityStreamsObjectProperty()
+ statusIRI, err := url.Parse(f.GTSStatus.URI)
+ if err != nil {
+ return nil, fmt.Errorf("FaveToAS: error parsing uri %s: %s", f.GTSStatus.URI, err)
+ }
+ objectProp.AppendIRI(statusIRI)
+ like.SetActivityStreamsObject(objectProp)
+
+ // set the TO property to the target account's IRI
+ toProp := streams.NewActivityStreamsToProperty()
+ toIRI, err := url.Parse(f.GTSTargetAccount.URI)
+ if err != nil {
+ return nil, fmt.Errorf("FaveToAS: error parsing uri %s: %s", f.GTSTargetAccount.URI, err)
+ }
+ toProp.AppendIRI(toIRI)
+ like.SetActivityStreamsTo(toProp)
+
+ return like, nil
+}