Get federated following to work
This commit is contained in:
parent
608e4036e5
commit
c1b8463251
|
@ -5,6 +5,8 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
|
||||
"github.com/go-fed/activity/streams"
|
||||
|
@ -12,9 +14,11 @@ import (
|
|||
)
|
||||
|
||||
func databaseAddToInbox(activity vocab.ActivityStreamsActivity) {
|
||||
fmt.Println(activity)
|
||||
}
|
||||
|
||||
func databaseAddToOutbox(activity vocab.ActivityStreamsActivity) {
|
||||
fmt.Println(activity)
|
||||
}
|
||||
|
||||
func GetInbox(user *user_model.User) vocab.ActivityStreamsOrderedCollection {
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
|
@ -13,12 +15,14 @@ import (
|
|||
"github.com/go-fed/activity/streams/vocab"
|
||||
)
|
||||
|
||||
func follow(activity vocab.ActivityStreamsFollow) {
|
||||
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()
|
||||
|
||||
actorIRISplit := strings.Split(actorIRI.String(), ",")
|
||||
objectIRISplit := strings.Split(objectIRI.String(), ",")
|
||||
actorIRISplit := strings.Split(actorIRI.String(), "/")
|
||||
objectIRISplit := strings.Split(objectIRI.String(), "/")
|
||||
users, _ := user_model.GetUserIDsByNames([]string{actorIRISplit[len(actorIRISplit)-1], objectIRISplit[len(objectIRISplit)-1]}, false)
|
||||
user_model.FollowUser(users[0], users[1])
|
||||
|
||||
|
@ -37,5 +41,6 @@ func follow(activity vocab.ActivityStreamsFollow) {
|
|||
object.AppendActivityStreamsFollow(activity)
|
||||
accept.SetActivityStreamsObject(object)
|
||||
|
||||
AddToOutbox(accept.(vocab.ActivityStreamsActivity))
|
||||
//AddToOutbox(accept.(vocab.ActivityStreamsActivity))
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -9,12 +9,12 @@ import (
|
|||
)
|
||||
|
||||
// Add an activity to a user's inbox
|
||||
func AddToInbox(activity vocab.ActivityStreamsActivity) {
|
||||
databaseAddToInbox(activity)
|
||||
func AddToInbox(activity vocab.ActivityStreamsFollow) {
|
||||
//databaseAddToInbox(activity)
|
||||
|
||||
// Probably should use callbacks here
|
||||
// https://github.com/owncast/owncast/blob/develop/activitypub/resolvers/resolve.go
|
||||
if activity.GetJSONLDType().Name() == "Follow" {
|
||||
follow(activity.(vocab.ActivityStreamsFollow))
|
||||
//follow(activity)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,12 +5,14 @@
|
|||
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"
|
||||
|
@ -115,6 +117,8 @@ 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)
|
||||
|
@ -141,21 +145,17 @@ func PersonInboxPost(ctx *context.APIContext) {
|
|||
// "204":
|
||||
// "$ref": "#/responses/empty"
|
||||
|
||||
fmt.Println(ctx)
|
||||
|
||||
r := ctx.Req
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
var m map[string]interface{}
|
||||
json.Unmarshal(body, &m)
|
||||
|
||||
var activity vocab.ActivityStreamsActivity
|
||||
resolver, _ := streams.NewJSONResolver(func(c context.Context, a vocab.ActivityStreamsActivity) error {
|
||||
activity = a
|
||||
return nil
|
||||
})
|
||||
_ = resolver.Resolve(ctx, m)
|
||||
fmt.Println(m)
|
||||
|
||||
resolver, _ := streams.NewJSONResolver(activitypub.Follow)
|
||||
c := go_context.Background()
|
||||
_ = resolver.Resolve(c, m)
|
||||
|
||||
activitypub.AddToInbox(activity)
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
|
@ -176,12 +176,36 @@ func PersonOutboxGet(ctx *context.APIContext) {
|
|||
// "200":
|
||||
// "$ref": "#/responses/ActivityPub"
|
||||
|
||||
user := user.GetUserByParamsName(ctx, "username")
|
||||
inbox := activitypub.GetOutbox(user)
|
||||
jsonmap, err := streams.Serialize(inbox)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "Serialize", err)
|
||||
}
|
||||
// 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")
|
||||
objectIRI, _ := url.Parse("https://git.exozy.me/api/v1/activitypub/user/guest")
|
||||
|
||||
id, _ := url.Parse("https://git.exozy.me/Ta180m")
|
||||
idProperty := streams.NewJSONLDIdProperty()
|
||||
idProperty.Set(id)
|
||||
follow.SetJSONLDId(idProperty)
|
||||
|
||||
summary := streams.NewActivityStreamsSummaryProperty()
|
||||
summary.AppendXMLSchemaString("This is a test")
|
||||
follow.SetActivityStreamsSummary(summary)
|
||||
|
||||
actor := streams.NewActivityStreamsActorProperty()
|
||||
actor.AppendIRI(actorIRI)
|
||||
follow.SetActivityStreamsActor(actor)
|
||||
|
||||
object := streams.NewActivityStreamsObjectProperty()
|
||||
object.AppendIRI(objectIRI)
|
||||
follow.SetActivityStreamsObject(object)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -211,11 +235,12 @@ func PersonOutboxPost(ctx *context.APIContext) {
|
|||
json.Unmarshal(body, &m)
|
||||
|
||||
var activity vocab.ActivityStreamsActivity
|
||||
resolver, _ := streams.NewJSONResolver(func(c context.Context, a vocab.ActivityStreamsActivity) error {
|
||||
resolver, _ := streams.NewJSONResolver(func(c go_context.Context, a vocab.ActivityStreamsActivity) error {
|
||||
activity = a
|
||||
return nil
|
||||
})
|
||||
_ = resolver.Resolve(ctx, m)
|
||||
c := go_context.Background()
|
||||
_ = resolver.Resolve(c, m)
|
||||
|
||||
activitypub.AddToOutbox(activity)
|
||||
ctx.Status(http.StatusNoContent)
|
||||
|
|
Reference in a new issue