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) object.AppendActivityStreamsFollow(activity)
accept.SetActivityStreamsObject(object) accept.SetActivityStreamsObject(object)
//AddToOutbox(accept.(vocab.ActivityStreamsActivity)) user, _ := user_model.GetUserByName(objectIRISplit[len(objectIRISplit)-1])
AddToOutbox(accept, user, actorIRI)
return nil return nil
} }

View file

@ -6,25 +6,20 @@ package activitypub
import ( import (
"context" "context"
"strings" "net/url"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/setting"
"github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams"
"github.com/go-fed/activity/streams/vocab" "github.com/go-fed/activity/streams/vocab"
) )
// Add an activity to a user's outbox // Add an activity to a user's outbox
func AddToOutbox(activity vocab.ActivityStreamsActivity) { func AddToOutbox(t vocab.Type, user *user_model.User, to *url.URL) {
databaseAddToOutbox(activity) 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) fetched, _ := Fetch(to)
var m map[string]interface{} var m map[string]interface{}
json.Unmarshal(fetched, &m) json.Unmarshal(fetched, &m)
@ -38,9 +33,9 @@ func AddToOutbox(activity vocab.ActivityStreamsActivity) {
_ = resolver.Resolve(ctx, m) _ = resolver.Resolve(ctx, m)
inboxIRI := person.GetActivityStreamsInbox().GetIRI().String() 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) body, _ := json.Marshal(jsonmap)
client.Post(body, inboxIRI) client.Post(body, inboxIRI)
} }

View file

@ -5,14 +5,12 @@
package activitypub package activitypub
import ( import (
go_context "context"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/activitypub" "code.gitea.io/gitea/modules/activitypub"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/json"
@ -157,10 +155,9 @@ func PersonInboxPost(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "Could not serialize payload", err) 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) 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 // Alright so this function is kinda useless right now so let's misuse it for testing following
follow := streams.NewActivityStreamsFollow() 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") 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 := streams.NewJSONLDIdProperty()
idProperty.Set(id) idProperty.Set(id)
follow.SetJSONLDId(idProperty) follow.SetJSONLDId(idProperty)
toProperty := streams.NewActivityStreamsToProperty()
toProperty.AppendIRI(objectIRI)
follow.SetActivityStreamsTo(toProperty)
summary := streams.NewActivityStreamsSummaryProperty() summary := streams.NewActivityStreamsSummaryProperty()
summary.AppendXMLSchemaString("This is a test") summary.AppendXMLSchemaString("This is a test")
follow.SetActivityStreamsSummary(summary) follow.SetActivityStreamsSummary(summary)
@ -204,16 +207,9 @@ func PersonOutboxGet(ctx *context.APIContext) {
object.AppendIRI(objectIRI) object.AppendIRI(objectIRI)
follow.SetActivityStreamsObject(object) follow.SetActivityStreamsObject(object)
//activitypub.AddToOutbox(follow.(vocab.ActivityStreamsActivity)) activitypub.AddToOutbox(follow, user.GetUserByParamsName(ctx, "username"), objectIRI)
user, _ := user_model.GetUserByName("ta180m") ctx.Status(http.StatusNoContent)
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)
} }
// PersonOutboxPost function // PersonOutboxPost function
@ -234,21 +230,24 @@ func PersonOutboxPost(ctx *context.APIContext) {
// "204": // "204":
// "$ref": "#/responses/empty" // "$ref": "#/responses/empty"
// This code below doesn't actually work :/ /*
r := ctx.Req 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{} var m map[string]interface{}
json.Unmarshal(body, &m) json.Unmarshal(body, &m)
var activity vocab.ActivityStreamsActivity var t vocab.Type
resolver, _ := streams.NewJSONResolver(func(c go_context.Context, a vocab.ActivityStreamsActivity) error { t, err = streams.ToType(ctx, m)
activity = a if err != nil {
return nil ctx.Error(http.StatusInternalServerError, "Could not serialize payload", err)
}) }
c := go_context.Background()
_ = resolver.Resolve(c, m)
activitypub.AddToOutbox(activity) fmt.Println(m) // Debugging
activitypub.AddToOutbox(t)
*/
ctx.Status(http.StatusNoContent) ctx.Status(http.StatusNoContent)
} }