aboutsummaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
Diffstat (limited to 'routes')
-rw-r--r--routes/actor.go177
1 files changed, 174 insertions, 3 deletions
diff --git a/routes/actor.go b/routes/actor.go
index 7b57309..d8b6631 100644
--- a/routes/actor.go
+++ b/routes/actor.go
@@ -18,10 +18,176 @@ import (
"github.com/gofiber/fiber/v2"
)
-func ActorInbox(c *fiber.Ctx) error {
- // STUB
+func ActorInbox(ctx *fiber.Ctx) error {
+ activity, err := activitypub.GetActivityFromJson(ctx)
+ if err != nil {
+ return err
+ }
+
+ if activity.Actor.PublicKey.Id == "" {
+ nActor, err := webfinger.FingerActor(activity.Actor.Id)
+ if err != nil {
+ return err
+ }
+
+ activity.Actor = &nActor
+ }
+
+ if !db.VerifyHeaderSignature(ctx, *activity.Actor) {
+ response := activitypub.RejectActivity(activity)
+
+ return db.MakeActivityRequest(response)
+ }
+
+ switch activity.Type {
+ case "Create":
+
+ for _, e := range activity.To {
+ if res, err := activitypub.IsActorLocal(e); err == nil && res {
+ if res, err := activitypub.IsActorLocal(activity.Actor.Id); err == nil && res {
+ col, err := activitypub.GetCollectionFromID(activity.Object.Id)
+ if err != nil {
+ return err
+ }
+
+ if len(col.OrderedItems) < 1 {
+ break
+ }
+
+ if _, err := activitypub.WriteObjectToCache(*activity.Object); err != nil {
+ return err
+ }
+
+ actor, err := activitypub.GetActorFromDB(e)
+ if err != nil {
+ return err
+ }
+
+ if err := db.ArchivePosts(actor); err != nil {
+ return err
+ }
+
+ //SendToFollowers(e, activity)
+ } else if err != nil {
+ return err
+ }
+ } else if err != nil {
+ return err
+ }
+ }
+
+ break
+
+ case "Delete":
+ for _, e := range activity.To {
+ actor, err := activitypub.GetActorFromDB(e)
+ if err != nil {
+ return err
+ }
+
+ if actor.Id != "" && actor.Id != config.Domain {
+ if activity.Object.Replies != nil {
+ for _, k := range activity.Object.Replies.OrderedItems {
+ if err := activitypub.TombstoneObject(k.Id); err != nil {
+ return err
+ }
+ }
+ }
+
+ if err := activitypub.TombstoneObject(activity.Object.Id); err != nil {
+ return err
+ }
+ if err := db.UnArchiveLast(actor.Id); err != nil {
+ return err
+ }
+ break
+ }
+ }
+ break
+
+ case "Follow":
+ for _, e := range activity.To {
+ if res, err := activitypub.GetActorFromDB(e); err == nil && res.Id != "" {
+ response := db.AcceptFollow(activity)
+ response, err := activitypub.SetActorFollowerDB(response)
+ if err != nil {
+ return err
+ }
+
+ if err := db.MakeActivityRequest(response); err != nil {
+ return err
+ }
+
+ alreadyFollow := false
+ alreadyFollowing := false
+ autoSub, err := activitypub.GetActorAutoSubscribeDB(response.Actor.Id)
+ if err != nil {
+ return err
+ }
+
+ following, err := activitypub.GetActorFollowingDB(response.Actor.Id)
+ if err != nil {
+ return err
+ }
+
+ for _, e := range following {
+ if e.Id == response.Object.Id {
+ alreadyFollow = true
+ }
+ }
+
+ actor, err := webfinger.FingerActor(response.Object.Actor)
+ if err != nil {
+ return err
+ }
+
+ remoteActorFollowingCol, err := webfinger.GetCollectionFromReq(actor.Following)
+ if err != nil {
+ return err
+ }
+
+ for _, e := range remoteActorFollowingCol.Items {
+ if e.Id == response.Actor.Id {
+ alreadyFollowing = true
+ }
+ }
+
+ if autoSub && !alreadyFollow && alreadyFollowing {
+ followActivity, err := db.MakeFollowActivity(response.Actor.Id, response.Object.Actor)
+ if err != nil {
+ return err
+ }
+
+ if res, err := webfinger.FingerActor(response.Object.Actor); err == nil && res.Id != "" {
+ if err := db.MakeActivityRequestOutbox(followActivity); err != nil {
+ return err
+ }
+ } else if err != nil {
+ return err
+ }
+ }
+ } else if err != nil {
+ return err
+ } else {
+ fmt.Println("follow request for rejected")
+ response := activitypub.RejectActivity(activity)
- return c.SendString("actor inbox")
+ return db.MakeActivityRequest(response)
+ }
+ }
+ break
+
+ case "Reject":
+ if activity.Object.Object.Type == "Follow" {
+ fmt.Println("follow rejected")
+ if _, err := db.SetActorFollowingDB(activity); err != nil {
+ return err
+ }
+ }
+ break
+ }
+
+ return nil
}
func ActorOutbox(ctx *fiber.Ctx) error {
@@ -31,6 +197,11 @@ func ActorOutbox(ctx *fiber.Ctx) error {
return err
}
+ if activitypub.AcceptActivity(ctx.Get("Accept")) {
+ activitypub.GetActorOutbox(ctx, actor)
+ return nil
+ }
+
contentType := util.GetContentType(ctx.Get("content-type"))
if contentType == "multipart/form-data" || contentType == "application/x-www-form-urlencoded" {