aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Wang2022-04-15 13:39:50 -0500
committerAnthony Wang2022-04-21 22:14:58 -0500
commitf0c4968c96c42b49e5e04fb6a2c815a9ba01e74b (patch)
tree7536c2011875fcf19c349845ebaedb4a1deb60ad
parentce1a014491dd6943b51adbf73fed540ee37e7903 (diff)
Start cleaning up inbox/outbox code
-rw-r--r--modules/activitypub/database.go8
-rw-r--r--modules/activitypub/follow.go3
-rw-r--r--modules/activitypub/inbox.go24
-rw-r--r--routers/api/v1/activitypub/person.go23
4 files changed, 37 insertions, 21 deletions
diff --git a/modules/activitypub/database.go b/modules/activitypub/database.go
index d041be487..971559ad4 100644
--- a/modules/activitypub/database.go
+++ b/modules/activitypub/database.go
@@ -13,12 +13,12 @@ import (
"github.com/go-fed/activity/streams/vocab"
)
-func databaseAddToInbox(activity vocab.ActivityStreamsActivity) {
- fmt.Println(activity)
+func databaseAddToInbox(t vocab.Type) {
+ fmt.Println(t)
}
-func databaseAddToOutbox(activity vocab.ActivityStreamsActivity) {
- fmt.Println(activity)
+func databaseAddToOutbox(t vocab.Type) {
+ fmt.Println(t)
}
func GetInbox(user *user_model.User) vocab.ActivityStreamsOrderedCollection {
diff --git a/modules/activitypub/follow.go b/modules/activitypub/follow.go
index 2ef9c85ae..c3875473a 100644
--- a/modules/activitypub/follow.go
+++ b/modules/activitypub/follow.go
@@ -6,7 +6,6 @@ package activitypub
import (
"context"
- "fmt"
"strings"
user_model "code.gitea.io/gitea/models/user"
@@ -16,8 +15,6 @@ import (
)
func Follow(ctx context.Context, activity vocab.ActivityStreamsFollow) error {
- fmt.Println("inside follow.go", activity)
-
actorIRI := activity.GetActivityStreamsActor().Begin().GetIRI()
objectIRI := activity.GetActivityStreamsObject().Begin().GetIRI()
diff --git a/modules/activitypub/inbox.go b/modules/activitypub/inbox.go
index 5a0af8aa9..b2fc374a6 100644
--- a/modules/activitypub/inbox.go
+++ b/modules/activitypub/inbox.go
@@ -5,16 +5,28 @@
package activitypub
import (
+ "context"
+
+ "code.gitea.io/gitea/modules/log"
+
+ "github.com/go-fed/activity/streams"
"github.com/go-fed/activity/streams/vocab"
)
// Add an activity to a user's inbox
-func AddToInbox(activity vocab.ActivityStreamsFollow) {
- //databaseAddToInbox(activity)
+func AddToInbox(t vocab.Type) {
+ databaseAddToInbox(t)
- // Probably should use callbacks here
- // https://github.com/owncast/owncast/blob/develop/activitypub/resolvers/resolve.go
- if activity.GetJSONLDType().Name() == "Follow" {
- //follow(activity)
+ resolver, _ := streams.NewTypeResolver(Follow)
+
+ c := context.Background()
+ err := resolver.Resolve(c, t)
+ if err != nil && !streams.IsUnmatchedErr(err) {
+ // Something went wrong
+ log.Error("Failed to resolve", err)
+ } else if streams.IsUnmatchedErr(err) {
+ // Everything went right but the callback didn't match or the ActivityStreams
+ // type is one that wasn't code generated.
+ log.Error("No match", err)
}
}
diff --git a/routers/api/v1/activitypub/person.go b/routers/api/v1/activitypub/person.go
index 4dda9ec80..6eb367940 100644
--- a/routers/api/v1/activitypub/person.go
+++ b/routers/api/v1/activitypub/person.go
@@ -117,8 +117,6 @@ func PersonInboxGet(ctx *context.APIContext) {
// "200":
// "$ref": "#/responses/ActivityPub"
- fmt.Println(ctx)
-
user := user.GetUserByParamsName(ctx, "username")
inbox := activitypub.GetInbox(user)
jsonmap, err := streams.Serialize(inbox)
@@ -146,15 +144,22 @@ func PersonInboxPost(ctx *context.APIContext) {
// "$ref": "#/responses/empty"
r := ctx.Req
- body, _ := io.ReadAll(r.Body)
+ body, err := io.ReadAll(r.Body)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "Error reading request body", err)
+ }
var m map[string]interface{}
json.Unmarshal(body, &m)
- fmt.Println(m)
+ var t vocab.Type
+ t, err = streams.ToType(ctx, m)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "Could not serialize payload", err)
+ }
- resolver, _ := streams.NewJSONResolver(activitypub.Follow)
- c := go_context.Background()
- _ = resolver.Resolve(c, m)
+ fmt.Println(m)
+
+ activitypub.AddToInbox(m)
ctx.Status(http.StatusNoContent)
}
@@ -199,6 +204,8 @@ func PersonOutboxGet(ctx *context.APIContext) {
object.AppendIRI(objectIRI)
follow.SetActivityStreamsObject(object)
+ //activitypub.AddToOutbox(follow.(vocab.ActivityStreamsActivity))
+
user, _ := user_model.GetUserByName("ta180m")
c, _ := activitypub.NewClient(user, "https://git.exozy.me/api/v1/activitypub/user/ta180m#main-key")
@@ -227,7 +234,7 @@ func PersonOutboxPost(ctx *context.APIContext) {
// "204":
// "$ref": "#/responses/empty"
- fmt.Println(ctx)
+ // This code below doesn't actually work :/
r := ctx.Req
body, _ := io.ReadAll(r.Body)