aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Wang2022-04-21 22:00:01 -0500
committerAnthony Wang2022-04-21 22:15:20 -0500
commit317f6be6b0a913e143d2d3758338d994854caa75 (patch)
treedbd91f0d5291c6381dd5d292a271e4ed409d6aa6
parentf0c4968c96c42b49e5e04fb6a2c815a9ba01e74b (diff)
Use AddToOutbox for sending follow requests
-rw-r--r--modules/activitypub/follow.go3
-rw-r--r--modules/activitypub/outbox.go17
-rw-r--r--routers/api/v1/activitypub/person.go53
3 files changed, 34 insertions, 39 deletions
diff --git a/modules/activitypub/follow.go b/modules/activitypub/follow.go
index c3875473a..2cb831dd7 100644
--- a/modules/activitypub/follow.go
+++ b/modules/activitypub/follow.go
@@ -38,6 +38,7 @@ func Follow(ctx context.Context, activity vocab.ActivityStreamsFollow) error {
object.AppendActivityStreamsFollow(activity)
accept.SetActivityStreamsObject(object)
- //AddToOutbox(accept.(vocab.ActivityStreamsActivity))
+ user, _ := user_model.GetUserByName(objectIRISplit[len(objectIRISplit)-1])
+ AddToOutbox(accept, user, actorIRI)
return nil
}
diff --git a/modules/activitypub/outbox.go b/modules/activitypub/outbox.go
index 6f5e197b1..b49c24c4f 100644
--- a/modules/activitypub/outbox.go
+++ b/modules/activitypub/outbox.go
@@ -6,25 +6,20 @@ package activitypub
import (
"context"
- "strings"
+ "net/url"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/json"
+ "code.gitea.io/gitea/modules/setting"
"github.com/go-fed/activity/streams"
"github.com/go-fed/activity/streams/vocab"
)
// Add an activity to a user's outbox
-func AddToOutbox(activity vocab.ActivityStreamsActivity) {
- databaseAddToOutbox(activity)
+func AddToOutbox(t vocab.Type, user *user_model.User, to *url.URL) {
+ databaseAddToOutbox(t)
- actorIRI := activity.GetActivityStreamsActor().Begin().GetIRI()
-
- s := strings.Split(actorIRI.String(), ",")
- user, _ := user_model.GetUserByName(s[len(s)-1])
-
- to := activity.GetActivityStreamsTo().Begin().GetIRI()
fetched, _ := Fetch(to)
var m map[string]interface{}
json.Unmarshal(fetched, &m)
@@ -38,9 +33,9 @@ func AddToOutbox(activity vocab.ActivityStreamsActivity) {
_ = resolver.Resolve(ctx, m)
inboxIRI := person.GetActivityStreamsInbox().GetIRI().String()
- client, _ := NewClient(user, actorIRI.String()+"#main-key")
+ client, _ := NewClient(user, setting.AppURL+"api/v1/activitypub/user/"+user.Name+"#main-key")
- jsonmap, _ := streams.Serialize(activity)
+ jsonmap, _ := streams.Serialize(t)
body, _ := json.Marshal(jsonmap)
client.Post(body, inboxIRI)
}
diff --git a/routers/api/v1/activitypub/person.go b/routers/api/v1/activitypub/person.go
index 6eb367940..3cdcfa0d5 100644
--- a/routers/api/v1/activitypub/person.go
+++ b/routers/api/v1/activitypub/person.go
@@ -5,14 +5,12 @@
package activitypub
import (
- go_context "context"
"fmt"
"io"
"net/http"
"net/url"
"strings"
- user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/activitypub"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/json"
@@ -157,10 +155,9 @@ func PersonInboxPost(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "Could not serialize payload", err)
}
- fmt.Println(m)
+ fmt.Println(m) // Debugging
- activitypub.AddToInbox(m)
-
+ activitypub.AddToInbox(t)
ctx.Status(http.StatusNoContent)
}
@@ -184,14 +181,20 @@ func PersonOutboxGet(ctx *context.APIContext) {
// Alright so this function is kinda useless right now so let's misuse it for testing following
follow := streams.NewActivityStreamsFollow()
- actorIRI, _ := url.Parse("https://git.exozy.me/api/v1/activitypub/user/ta180m")
+ username := ctx.Params("username")
+
+ actorIRI, _ := url.Parse("https://git.exozy.me/api/v1/activitypub/user/" + username)
objectIRI, _ := url.Parse("https://git.exozy.me/api/v1/activitypub/user/guest")
- id, _ := url.Parse("https://git.exozy.me/Ta180m")
+ id, _ := url.Parse("https://git.exozy.me/" + username)
idProperty := streams.NewJSONLDIdProperty()
idProperty.Set(id)
follow.SetJSONLDId(idProperty)
+ toProperty := streams.NewActivityStreamsToProperty()
+ toProperty.AppendIRI(objectIRI)
+ follow.SetActivityStreamsTo(toProperty)
+
summary := streams.NewActivityStreamsSummaryProperty()
summary.AppendXMLSchemaString("This is a test")
follow.SetActivityStreamsSummary(summary)
@@ -204,16 +207,9 @@ 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")
+ activitypub.AddToOutbox(follow, user.GetUserByParamsName(ctx, "username"), objectIRI)
- jsonmap, _ := streams.Serialize(follow)
- body, _ := json.Marshal(jsonmap)
- resp, _ := c.Post(body, "https://git.exozy.me/api/v1/activitypub/user/guest/inbox")
- fmt.Println(resp)
- ctx.JSON(http.StatusOK, jsonmap)
+ ctx.Status(http.StatusNoContent)
}
// PersonOutboxPost function
@@ -234,21 +230,24 @@ func PersonOutboxPost(ctx *context.APIContext) {
// "204":
// "$ref": "#/responses/empty"
- // This code below doesn't actually work :/
-
+ /*
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)
- var activity vocab.ActivityStreamsActivity
- resolver, _ := streams.NewJSONResolver(func(c go_context.Context, a vocab.ActivityStreamsActivity) error {
- activity = a
- return nil
- })
- c := go_context.Background()
- _ = resolver.Resolve(c, m)
+ var t vocab.Type
+ t, err = streams.ToType(ctx, m)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "Could not serialize payload", err)
+ }
- activitypub.AddToOutbox(activity)
+ fmt.Println(m) // Debugging
+
+ activitypub.AddToOutbox(t)
+ */
ctx.Status(http.StatusNoContent)
}