Use AddToOutbox for sending follow requests

This commit is contained in:
Anthony Wang 2022-04-21 22:00:01 -05:00
parent 2b44be9a17
commit 17a2d51952
Signed by: a
GPG key ID: BC96B00AEC5F2D76
3 changed files with 34 additions and 39 deletions

View file

@ -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
}

View file

@ -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)
}

View file

@ -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))
activitypub.AddToOutbox(follow, user.GetUserByParamsName(ctx, "username"), objectIRI)
user, _ := user_model.GetUserByName("ta180m")
c, _ := activitypub.NewClient(user, "https://git.exozy.me/api/v1/activitypub/user/ta180m#main-key")
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)
}