diff options
author | FChannel <> | 2022-06-12 15:36:19 -0700 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | f57d5722e6f2187bea249240eb14c881f989c3c1 (patch) | |
tree | ef8f4d23f9c40e0b0ba1ab2efb03e3795efe8ec0 | |
parent | dac4b267cab31fb3a4ce301f1bcdf364fcb541a1 (diff) |
first pass on federation working
-rw-r--r-- | activitypub/activity.go | 14 | ||||
-rw-r--r-- | activitypub/activityPubStruct.go | 22 | ||||
-rw-r--r-- | activitypub/actor.go | 8 | ||||
-rw-r--r-- | activitypub/object.go | 88 | ||||
-rw-r--r-- | activitypub/util.go | 4 | ||||
-rw-r--r-- | db/database.go | 2 | ||||
-rw-r--r-- | post/util.go | 4 | ||||
-rw-r--r-- | route/routes/actor.go | 8 | ||||
-rw-r--r-- | route/routes/admin.go | 2 | ||||
-rw-r--r-- | route/routes/webfinger.go | 5 | ||||
-rw-r--r-- | route/util.go | 2 |
11 files changed, 40 insertions, 119 deletions
diff --git a/activitypub/activity.go b/activitypub/activity.go index 036a5ce..5d89272 100644 --- a/activitypub/activity.go +++ b/activitypub/activity.go @@ -23,7 +23,7 @@ func (activity Activity) AcceptFollow() Activity { accept.Actor = &nActor accept.Actor.Id = activity.Object.Actor var nObj ObjectBase - accept.Object = &nObj + accept.Object = nObj accept.Object.Actor = activity.Actor.Id var nNested NestedObjectBase accept.Object.Object = &nNested @@ -181,7 +181,7 @@ func (activity Activity) Reject() Activity { accept.AtContext.Context = activity.AtContext.Context accept.Type = "Reject" var nObj ObjectBase - accept.Object = &nObj + accept.Object = nObj var nActor Actor accept.Actor = &nActor accept.Actor.Id = activity.Object.Actor @@ -196,8 +196,8 @@ func (activity Activity) Reject() Activity { } func (activity Activity) Report(reason string) (bool, error) { - if isLocal, err := activity.Object.IsLocal(); !isLocal || err != nil { - return false, util.MakeError(err, "Report") + if isLocal, _ := activity.Object.IsLocal(); !isLocal { + return false, nil } reqActivity := Activity{Id: activity.Object.Id} @@ -288,7 +288,7 @@ func (activity Activity) SetActorFollowing() (Activity, error) { var query string if alreadyFollowing && alreadyFollower { - if res, err := activity.Actor.IsLocal(); err == nil && !res { + if res, _ := activity.Actor.IsLocal(); !res { go activity.Actor.DeleteCache() } else if err != nil { return activity, util.MakeError(err, "SetActorFollowing") @@ -306,10 +306,8 @@ func (activity Activity) SetActorFollowing() (Activity, error) { } if !alreadyFollowing && !alreadyFollower { - if res, err := activity.Actor.IsLocal(); err == nil && !res { + if res, _ := activity.Actor.IsLocal(); !res { go activity.Actor.WriteCache() - } else if err != nil { - return activity, util.MakeError(err, "SetActorFollowing") } query = `insert into following (id, following) values ($1, $2)` diff --git a/activitypub/activityPubStruct.go b/activitypub/activityPubStruct.go index ee1e451..b8e3180 100644 --- a/activitypub/activityPubStruct.go +++ b/activitypub/activityPubStruct.go @@ -93,17 +93,17 @@ type PublicKeyPem struct { type Activity struct { AtContext - Type string `json:"type,omitempty"` - Id string `json:"id,omitempty"` - Actor *Actor `json:"actor,omitempty"` - Name string `json:"name,omitempty"` - Summary string `json:"summary,omitempty"` - Auth string `json:"auth,omitempty"` - To []string `json:"to,omitempty"` - Bto []string `json:"bto,omitempty"` - Cc []string `json:"cc,omitempty"` - Published time.Time `json:"published,omitempty"` - Object *ObjectBase `json:"object,omitempty"` + Type string `json:"type,omitempty"` + Id string `json:"id,omitempty"` + Actor *Actor `json:"actor,omitempty"` + Name string `json:"name,omitempty"` + Summary string `json:"summary,omitempty"` + Auth string `json:"auth,omitempty"` + To []string `json:"to,omitempty"` + Bto []string `json:"bto,omitempty"` + Cc []string `json:"cc,omitempty"` + Published time.Time `json:"published,omitempty"` + Object ObjectBase `json:"object,omitempty"` } type ObjectBase struct { diff --git a/activitypub/actor.go b/activitypub/actor.go index e54088d..b69c831 100644 --- a/activitypub/actor.go +++ b/activitypub/actor.go @@ -818,8 +818,8 @@ func (actor Actor) IsAlreadyFollower(follow string) (bool, error) { } func (actor Actor) IsLocal() (bool, error) { - actor, err := GetActorFromDB(actor.Id) - return actor.Id != "", util.MakeError(err, "IsLocal") + actor, _ = GetActorFromDB(actor.Id) + return actor.Id != "", nil } func (actor Actor) IsValid() (Actor, bool, error) { @@ -1048,7 +1048,7 @@ func (actor Actor) WriteCache() error { } for _, e := range collection.OrderedItems { - if err := e.WriteCache(); err != nil { + if _, err := e.WriteCache(); err != nil { return util.MakeError(err, "WriteCache") } } @@ -1077,7 +1077,7 @@ func (actor Actor) MakeFollowActivity(follow string) (Activity, error) { } followActivity.Actor = &nactor - followActivity.Object = &obj + followActivity.Object = obj followActivity.Object.Actor = follow followActivity.To = append(followActivity.To, follow) diff --git a/activitypub/object.go b/activitypub/object.go index 891cb23..e26514d 100644 --- a/activitypub/object.go +++ b/activitypub/object.go @@ -26,7 +26,7 @@ func (obj ObjectBase) CreateActivity(activityType string) (Activity, error) { newActivity.Type = activityType newActivity.Published = obj.Published newActivity.Actor = &actor - newActivity.Object = &obj + newActivity.Object = obj for _, e := range obj.To { if obj.Actor != e { @@ -114,7 +114,7 @@ func (obj ObjectBase) DeleteAndRepliesRequest() error { } activity.Actor.Id = nObj.OrderedItems[0].Actor - activity.Object = &nObj.OrderedItems[0] + activity.Object = nObj.OrderedItems[0] objActor, _ := GetActor(nObj.OrderedItems[0].Actor) followers, err := objActor.GetFollower() @@ -736,7 +736,7 @@ func (obj ObjectBase) IsLocal() (bool, error) { query := `select id from activitystream where id=$1` if err := config.DB.QueryRow(query, obj.Id).Scan(&nID); err != nil { - return false, util.MakeError(err, "GetType") + return false, nil } return true, nil @@ -1138,7 +1138,7 @@ func (obj ObjectBase) WriteAttachmentCache() error { return nil } -func (obj ObjectBase) WriteCache() error { +func (obj ObjectBase) _WriteCache() error { var id string obj.Name = util.EscapeString(obj.Name) @@ -1153,7 +1153,7 @@ func (obj ObjectBase) WriteCache() error { query = `insert into cacheactivitystream (id, type, name, content, published, updated, attributedto, actor, tripcode, sensitive) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)` _, err = config.DB.Exec(query, obj.Id, obj.Type, obj.Name, obj.Content, obj.Published, obj.Updated, obj.AttributedTo, obj.Actor, obj.TripCode, obj.Sensitive) - return util.MakeError(err, "WriteCache") + return util.MakeError(err, "_WriteCache") } return nil @@ -1264,79 +1264,9 @@ func (obj ObjectBase) WriteReply() error { return nil } -func (obj ObjectBase) WriteReplyCache() error { - for i, e := range obj.InReplyTo { - res, err := obj.InReplyTo[0].IsReplyInThread(e.Id) - if err != nil { - return util.MakeError(err, "WriteReplyCache") - } - - if i == 0 || res { - query := `select id from replies where id=$1` - - rows, err := config.DB.Query(query, obj.Id) - if err != nil { - return util.MakeError(err, "WriteReplyCache") - } - defer rows.Close() - - var id string - rows.Next() - err = rows.Scan(&id) - if err != nil { - return util.MakeError(err, "WriteReplyCache") - } else if id != "" { - return nil // TODO: error? - } - - query = `insert into cachereplies (id, inreplyto) values ($1, $2)` - - _, err = config.DB.Exec(query, obj.Id, e.Id) - if err != nil { - return util.MakeError(err, "WriteReplyCache") - } - } - } - - if len(obj.InReplyTo) < 1 { - query := `insert into cachereplies (id, inreplyto) values ($1, $2)` - - _, err := config.DB.Exec(query, obj.Id, "") - return util.MakeError(err, "WriteReplyCache") - } - - return nil -} - -func (obj ObjectBase) WriteReplyLocal(replyto string) error { - var nID string - - query := `select id from replies where id=$1 and inreplyto=$2` - if err := config.DB.QueryRow(query, obj.Id, replyto).Scan(&nID); err != nil { - query := `insert into replies (id, inreplyto) values ($1, $2)` - if _, err := config.DB.Exec(query, obj.Id, replyto); err != nil { - return util.MakeError(err, "WriteReplyLocal") - } - } - - var val string - - query = `select inreplyto from replies where id=$1` - if err := config.DB.QueryRow(query, replyto).Scan(&val); err != nil { - updated := time.Now().UTC().Format(time.RFC3339) - query := `update activitystream set updated=$1 where id=$2` - - if _, err := config.DB.Exec(query, updated, replyto); err != nil { - return util.MakeError(err, "WriteReplyLocal") - } - } - - return nil -} - -func (obj ObjectBase) WriteObjectToCache() (ObjectBase, error) { +func (obj ObjectBase) WriteCache() (ObjectBase, error) { if isBlacklisted, err := util.IsPostBlacklist(obj.Content); err != nil || isBlacklisted { - config.Log.Println("\n\nBlacklist post blocked\n\n") + config.Log.Println("Blacklist post blocked") return obj, util.MakeError(err, "WriteObjectToCache") } @@ -1350,14 +1280,14 @@ func (obj ObjectBase) WriteObjectToCache() (ObjectBase, error) { obj.WriteCacheWithAttachment(obj.Attachment[i]) } } else { - obj.WriteCache() + obj._WriteCache() } obj.WriteReply() if obj.Replies.OrderedItems != nil { for _, e := range obj.Replies.OrderedItems { - e.WriteCache() + e._WriteCache() } } diff --git a/activitypub/util.go b/activitypub/util.go index 274493f..a55e4c4 100644 --- a/activitypub/util.go +++ b/activitypub/util.go @@ -159,7 +159,7 @@ func GetActivityFromJson(ctx *fiber.Ctx) (Activity, error) { } nActivity.Name = respActivity.Name - nActivity.Object = &jObj + nActivity.Object = jObj } else if err != nil { return nActivity, util.MakeError(err, "GetActivityFromJson") } @@ -351,7 +351,7 @@ func GetActorFromDB(id string) (Actor, error) { err := config.DB.QueryRow(query, id).Scan(&nActor.Type, &nActor.Id, &nActor.Name, &nActor.PreferredUsername, &nActor.Inbox, &nActor.Outbox, &nActor.Following, &nActor.Followers, &nActor.Restricted, &nActor.Summary, &publicKeyPem) if err != nil { - return nActor, nil + return nActor, util.MakeError(err, "GetActorFromDB") } nActor.PublicKey, err = GetActorPemFromDB(publicKeyPem) diff --git a/db/database.go b/db/database.go index 830edc5..46297ec 100644 --- a/db/database.go +++ b/db/database.go @@ -110,7 +110,7 @@ func CreateNewBoard(actor activitypub.Actor) (activitypub.Actor, error) { nActivity.AtContext.Context = "https://www.w3.org/ns/activitystreams" nActivity.Type = "Follow" nActivity.Actor = &nActor - nActivity.Object = &nObject + nActivity.Object = nObject mActor, err := activitypub.GetActorFromDB(actor.Id) if err != nil { diff --git a/post/util.go b/post/util.go index 67822b0..8db2d02 100644 --- a/post/util.go +++ b/post/util.go @@ -322,7 +322,7 @@ func ObjectFromForm(ctx *fiber.Ctx, obj activitypub.ObjectBase) (activitypub.Obj activity.To = append(activity.To, e.Id) - if local, err := activity.IsLocal(); err == nil && !local { + if local, _ := activity.IsLocal(); !local { actor, err := activitypub.FingerActor(e.Id) if err != nil { return obj, util.MakeError(err, "ObjectFromForm") @@ -331,8 +331,6 @@ func ObjectFromForm(ctx *fiber.Ctx, obj activitypub.ObjectBase) (activitypub.Obj if !util.IsInStringArray(obj.To, actor.Id) { obj.To = append(obj.To, actor.Id) } - } else if err != nil { - return obj, util.MakeError(err, "ObjectFromForm") } } } diff --git a/route/routes/actor.go b/route/routes/actor.go index 2437567..94623f0 100644 --- a/route/routes/actor.go +++ b/route/routes/actor.go @@ -43,8 +43,8 @@ func ActorInbox(ctx *fiber.Ctx) error { case "Create": for _, e := range activity.To { actor := activitypub.Actor{Id: e} - if res, err := actor.IsLocal(); err == nil && res { - if res, err := activity.Actor.IsLocal(); err == nil && res { + if local, _ := actor.IsLocal(); local { + if local, _ := activity.Actor.IsLocal(); !local { reqActivity := activitypub.Activity{Id: activity.Object.Id} col, err := reqActivity.GetCollection() if err != nil { @@ -55,7 +55,7 @@ func ActorInbox(ctx *fiber.Ctx) error { break } - if err := activity.Object.WriteCache(); err != nil { + if _, err := activity.Object.WriteCache(); err != nil { return util.MakeError(err, "ActorInbox") } @@ -69,8 +69,6 @@ func ActorInbox(ctx *fiber.Ctx) error { } //SendToFollowers(e, activity) - } else if err != nil { - return util.MakeError(err, "ActorInbox") } } else if err != nil { return util.MakeError(err, "ActorInbox") diff --git a/route/routes/admin.go b/route/routes/admin.go index 4571955..31e8e8e 100644 --- a/route/routes/admin.go +++ b/route/routes/admin.go @@ -195,7 +195,7 @@ func AdminAddBoard(ctx *fiber.Ctx) error { var nobj activitypub.ObjectBase newActorActivity.Actor = &actor - newActorActivity.Object = &nobj + newActorActivity.Object = nobj newActorActivity.Object.Alias = board.Name newActorActivity.Object.Name = board.PreferredUsername diff --git a/route/routes/webfinger.go b/route/routes/webfinger.go index 3d5fa63..9b5ae25 100644 --- a/route/routes/webfinger.go +++ b/route/routes/webfinger.go @@ -6,7 +6,6 @@ import ( "github.com/FChannel0/FChannel-Server/activitypub" "github.com/FChannel0/FChannel-Server/config" - "github.com/FChannel0/FChannel-Server/util" "github.com/gofiber/fiber/v2" ) @@ -34,11 +33,9 @@ func Webfinger(c *fiber.Ctx) error { } actor := activitypub.Actor{Id: config.TP + "" + actorDomain[1] + "" + actorDomain[0]} - if res, err := actor.IsLocal(); err == nil && !res { + if res, _ := actor.IsLocal(); !res { c.Status(fiber.StatusBadRequest) return c.Send([]byte("actor not local")) - } else if err != nil { - return util.MakeError(err, "Webfinger") } var finger activitypub.Webfinger diff --git a/route/util.go b/route/util.go index defa60e..131cf5a 100644 --- a/route/util.go +++ b/route/util.go @@ -186,7 +186,7 @@ func ParseOutboxRequest(ctx *fiber.Ctx, actor activitypub.Actor) error { return util.MakeError(err, "ParseOutboxRequest") } - if res, err := activity.IsLocal(); err == nil && res { + if res, _ := activity.IsLocal(); res { if res := activity.Actor.VerifyHeaderSignature(ctx); err == nil && !res { ctx.Response().Header.Set("Status", "403") _, err = ctx.Write([]byte("")) |