From 580dec5b89215310ce34341e11ff17fe38bdb63a Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Sun, 8 May 2022 14:57:40 -0700 Subject: more cleanup, logging and error logging everywhere things are mostly in place can work on "features" and polish --- routes/actor.go | 86 +++++++++++++++++++++++++------------------------- routes/admin.go | 83 +++++++++++++++++++++++++++--------------------- routes/api.go | 9 +++--- routes/archive.go | 11 +++---- routes/index.go | 14 +++++---- routes/news.go | 18 +++++------ routes/outbox.go | 16 +++++----- routes/post.go | 36 ++++++++++----------- routes/util.go | 90 ++++++++++++++++++++++++++--------------------------- routes/webfinger.go | 8 ++--- 10 files changed, 192 insertions(+), 179 deletions(-) (limited to 'routes') diff --git a/routes/actor.go b/routes/actor.go index 81f6dbe..0200d93 100644 --- a/routes/actor.go +++ b/routes/actor.go @@ -3,7 +3,6 @@ package routes import ( "bytes" "errors" - "fmt" "io" "io/ioutil" "mime/multipart" @@ -11,7 +10,6 @@ import ( "github.com/FChannel0/FChannel-Server/activitypub" "github.com/FChannel0/FChannel-Server/config" - "github.com/FChannel0/FChannel-Server/db" "github.com/FChannel0/FChannel-Server/post" "github.com/FChannel0/FChannel-Server/util" "github.com/FChannel0/FChannel-Server/webfinger" @@ -21,21 +19,21 @@ import ( func ActorInbox(ctx *fiber.Ctx) error { activity, err := activitypub.GetActivityFromJson(ctx) if err != nil { - return err + return util.MakeError(err, "ActorInbox") } if activity.Actor.PublicKey.Id == "" { - nActor, err := webfinger.FingerActor(activity.Actor.Id) + nActor, err := activitypub.FingerActor(activity.Actor.Id) if err != nil { - return err + return util.MakeError(err, "ActorInbox") } activity.Actor = &nActor } - if !db.VerifyHeaderSignature(ctx, *activity.Actor) { + if !activity.Actor.VerifyHeaderSignature(ctx) { response := activity.Reject() - return db.MakeActivityRequest(response) + return response.MakeRequestInbox() } switch activity.Type { @@ -44,9 +42,10 @@ func ActorInbox(ctx *fiber.Ctx) error { actor := activitypub.Actor{Id: e} if res, err := actor.IsLocal(); err == nil && res { if res, err := activity.Actor.IsLocal(); err == nil && res { - col, err := activity.Object.GetCollection() + reqActivity := activitypub.Activity{Id: activity.Object.Id} + col, err := reqActivity.GetCollection() if err != nil { - return err + return util.MakeError(err, "ActorInbox") } if len(col.OrderedItems) < 1 { @@ -54,24 +53,24 @@ func ActorInbox(ctx *fiber.Ctx) error { } if err := activity.Object.WriteCache(); err != nil { - return err + return util.MakeError(err, "ActorInbox") } actor, err := activitypub.GetActorFromDB(e) if err != nil { - return err + return util.MakeError(err, "ActorInbox") } - if err := db.ArchivePosts(actor); err != nil { - return err + if err := actor.ArchivePosts(); err != nil { + return util.MakeError(err, "ActorInbox") } //SendToFollowers(e, activity) } else if err != nil { - return err + return util.MakeError(err, "ActorInbox") } } else if err != nil { - return err + return util.MakeError(err, "ActorInbox") } } @@ -81,23 +80,23 @@ func ActorInbox(ctx *fiber.Ctx) error { for _, e := range activity.To { actor, err := activitypub.GetActorFromDB(e) if err != nil { - return err + return util.MakeError(err, "") } if actor.Id != "" && actor.Id != config.Domain { if activity.Object.Replies.OrderedItems != nil { for _, k := range activity.Object.Replies.OrderedItems { if err := k.Tombstone(); err != nil { - return err + return util.MakeError(err, "ActorInbox") } } } if err := activity.Object.Tombstone(); err != nil { - return err + return util.MakeError(err, "ActorInbox") } if err := actor.UnArchiveLast(); err != nil { - return err + return util.MakeError(err, "ActorInbox") } break } @@ -107,26 +106,26 @@ func ActorInbox(ctx *fiber.Ctx) error { case "Follow": for _, e := range activity.To { if res, err := activitypub.GetActorFromDB(e); err == nil && res.Id != "" { - response := db.AcceptFollow(activity) - response, err := response.SetFollower() + response := activity.AcceptFollow() + response, err := response.SetActorFollower() if err != nil { - return err + return util.MakeError(err, "ActorInbox") } - if err := db.MakeActivityRequest(response); err != nil { - return err + if err := response.MakeRequestInbox(); err != nil { + return util.MakeError(err, "ActorInbox") } alreadyFollow := false alreadyFollowing := false autoSub, err := response.Actor.GetAutoSubscribe() if err != nil { - return err + return util.MakeError(err, "ActorInbox") } following, err := response.Actor.GetFollowing() if err != nil { - return err + return util.MakeError(err, "ActorInbox") } for _, e := range following { @@ -135,14 +134,15 @@ func ActorInbox(ctx *fiber.Ctx) error { } } - actor, err := webfinger.FingerActor(response.Object.Actor) + actor, err := activitypub.FingerActor(response.Object.Actor) if err != nil { - return err + return util.MakeError(err, "ActorInbox") } - remoteActorFollowingCol, err := webfinger.GetCollectionFromReq(actor.Following) + reqActivity := activitypub.Activity{Id: actor.Following} + remoteActorFollowingCol, err := reqActivity.GetCollection() if err != nil { - return err + return util.MakeError(err, "ActorInbox") } for _, e := range remoteActorFollowingCol.Items { @@ -152,34 +152,34 @@ func ActorInbox(ctx *fiber.Ctx) error { } if autoSub && !alreadyFollow && alreadyFollowing { - followActivity, err := db.MakeFollowActivity(response.Actor.Id, response.Object.Actor) + followActivity, err := response.Actor.MakeFollowActivity(response.Object.Actor) if err != nil { - return err + return util.MakeError(err, "ActorInbox") } - if res, err := webfinger.FingerActor(response.Object.Actor); err == nil && res.Id != "" { - if err := db.MakeActivityRequestOutbox(followActivity); err != nil { - return err + if res, err := activitypub.FingerActor(response.Object.Actor); err == nil && res.Id != "" { + if err := followActivity.MakeRequestOutbox(); err != nil { + return util.MakeError(err, "ActorInbox") } } else if err != nil { - return err + return util.MakeError(err, "ActorInbox") } } } else if err != nil { - return err + return util.MakeError(err, "ActorInbox") } else { - fmt.Println("follow request for rejected") + config.Log.Println("follow request for rejected") response := activity.Reject() - return db.MakeActivityRequest(response) + return response.MakeRequestInbox() } } break case "Reject": if activity.Object.Object.Type == "Follow" { - fmt.Println("follow rejected") - if _, err := db.SetActorFollowingDB(activity); err != nil { - return err + config.Log.Println("follow rejected") + if _, err := activity.SetActorFollowing(); err != nil { + return util.MakeError(err, "ActorInbox") } } break @@ -192,7 +192,7 @@ func ActorOutbox(ctx *fiber.Ctx) error { //var activity activitypub.Activity actor, err := webfinger.GetActorFromPath(ctx.Path(), "/") if err != nil { - return err + return util.MakeError(err, "ActorOutbox") } if activitypub.AcceptActivity(ctx.Get("Accept")) { diff --git a/routes/admin.go b/routes/admin.go index bd489a5..24c1479 100644 --- a/routes/admin.go +++ b/routes/admin.go @@ -20,7 +20,7 @@ func AdminVerify(ctx *fiber.Ctx) error { identifier := ctx.FormValue("id") code := ctx.FormValue("code") - var verify db.Verify + var verify util.Verify verify.Identifier = identifier verify.Code = code @@ -29,7 +29,7 @@ func AdminVerify(ctx *fiber.Ctx) error { req, err := http.NewRequest("POST", config.Domain+"/auth", bytes.NewBuffer(j)) if err != nil { - return err + return util.MakeError(err, "AdminVerify") } req.Header.Set("Content-Type", config.ActivityStreams) @@ -37,7 +37,7 @@ func AdminVerify(ctx *fiber.Ctx) error { resp, err := http.DefaultClient.Do(req) if err != nil { - return err + return util.MakeError(err, "AdminVerify") } defer resp.Body.Close() @@ -61,29 +61,29 @@ func AdminVerify(ctx *fiber.Ctx) error { // TODO remove this route it is mostly unneeded func AdminAuth(ctx *fiber.Ctx) error { - var verify db.Verify + var verify util.Verify err := json.Unmarshal(ctx.Body(), &verify) if err != nil { - return err + return util.MakeError(err, "AdminAuth") } - v, _ := db.GetVerificationByCode(verify.Code) + v, _ := util.GetVerificationByCode(verify.Code) if v.Identifier == verify.Identifier { _, err := ctx.Write([]byte(v.Board)) - return err + return util.MakeError(err, "AdminAuth") } ctx.Response().Header.SetStatusCode(http.StatusBadRequest) _, err = ctx.Write([]byte("")) - return err + return util.MakeError(err, "AdminAuth") } func AdminIndex(ctx *fiber.Ctx) error { - id, _ := db.GetPasswordFromSession(ctx) + id, _ := util.GetPasswordFromSession(ctx) actor, _ := webfinger.GetActorFromPath(ctx.Path(), "/"+config.Key+"/") if actor.Id == "" { @@ -94,14 +94,15 @@ func AdminIndex(ctx *fiber.Ctx) error { return ctx.Render("verify", fiber.Map{}) } - actor, err := webfinger.GetActor(config.Domain) + actor, err := activitypub.GetActor(config.Domain) if err != nil { - return err + return util.MakeError(err, "AdminIndex") } - follow, _ := webfinger.GetActorCollection(actor.Following) - follower, _ := webfinger.GetActorCollection(actor.Followers) + reqActivity := activitypub.Activity{Id: actor.Following} + follow, _ := reqActivity.GetCollection() + follower, _ := reqActivity.GetCollection() var following []string var followers []string @@ -120,14 +121,14 @@ func AdminIndex(ctx *fiber.Ctx) error { adminData.Actor = actor.Id adminData.Key = config.Key adminData.Domain = config.Domain - adminData.Board.ModCred, _ = db.GetPasswordFromSession(ctx) + adminData.Board.ModCred, _ = util.GetPasswordFromSession(ctx) adminData.Title = actor.Name + " Admin page" adminData.Boards = webfinger.Boards adminData.Board.Post.Actor = actor.Id - adminData.PostBlacklist, _ = util.GetRegexBlacklistDB() + adminData.PostBlacklist, _ = util.GetRegexBlacklist() adminData.Themes = &config.Themes @@ -147,8 +148,9 @@ func AdminFollow(ctx *fiber.Ctx) error { //follow all of boards following if following.MatchString(follow) { - followingActor, _ := webfinger.FingerActor(follow) - col, _ := webfinger.GetActorCollection(followingActor.Following) + followingActor, _ := activitypub.FingerActor(follow) + reqActivity := activitypub.Activity{Id: followingActor.Following} + col, _ := reqActivity.GetCollection() var nObj activitypub.ObjectBase nObj.Id = followingActor.Id @@ -157,18 +159,20 @@ func AdminFollow(ctx *fiber.Ctx) error { for _, e := range col.Items { if isFollowing, _ := actor.IsAlreadyFollowing(e.Id); !isFollowing && e.Id != config.Domain && e.Id != actorId { - followActivity, _ := db.MakeFollowActivity(actorId, e.Id) + actor := activitypub.Actor{Id: actorId} + followActivity, _ := actor.MakeFollowActivity(e.Id) - if actor, _ := webfinger.FingerActor(e.Id); actor.Id != "" { - db.MakeActivityRequestOutbox(followActivity) + if actor, _ := activitypub.FingerActor(e.Id); actor.Id != "" { + followActivity.MakeRequestOutbox() } } } //follow all of boards followers } else if followers.MatchString(follow) { - followersActor, _ := webfinger.FingerActor(follow) - col, _ := webfinger.GetActorCollection(followersActor.Followers) + followersActor, _ := activitypub.FingerActor(follow) + reqActivity := activitypub.Activity{Id: followersActor.Followers} + col, _ := reqActivity.GetCollection() var nObj activitypub.ObjectBase nObj.Id = followersActor.Id @@ -177,25 +181,27 @@ func AdminFollow(ctx *fiber.Ctx) error { for _, e := range col.Items { if isFollowing, _ := actor.IsAlreadyFollowing(e.Id); !isFollowing && e.Id != config.Domain && e.Id != actorId { - followActivity, _ := db.MakeFollowActivity(actorId, e.Id) - if actor, _ := webfinger.FingerActor(e.Id); actor.Id != "" { - db.MakeActivityRequestOutbox(followActivity) + actor := activitypub.Actor{Id: actorId} + followActivity, _ := actor.MakeFollowActivity(e.Id) + if actor, _ := activitypub.FingerActor(e.Id); actor.Id != "" { + followActivity.MakeRequestOutbox() } } } //do a normal follow to a single board } else { - followActivity, _ := db.MakeFollowActivity(actorId, follow) + actor := activitypub.Actor{Id: actorId} + followActivity, _ := actor.MakeFollowActivity(follow) - actor := activitypub.Actor{Id: followActivity.Object.Actor} + actor = activitypub.Actor{Id: followActivity.Object.Actor} if isLocal, _ := actor.IsLocal(); !isLocal && followActivity.Actor.Id == config.Domain { _, err := ctx.Write([]byte("main board can only follow local boards. Create a new board and then follow outside boards from it.")) - return err + return util.MakeError(err, "AdminIndex") } - if actor, _ := webfinger.FingerActor(follow); actor.Id != "" { - db.MakeActivityRequestOutbox(followActivity) + if actor, _ := activitypub.FingerActor(follow); actor.Id != "" { + followActivity.MakeRequestOutbox() } } @@ -211,7 +217,7 @@ func AdminFollow(ctx *fiber.Ctx) error { func AdminAddBoard(ctx *fiber.Ctx) error { actor, _ := activitypub.GetActorFromDB(config.Domain) - if hasValidation := db.HasValidation(ctx, actor); !hasValidation { + if hasValidation := actor.HasValidation(ctx); !hasValidation { return nil } @@ -242,7 +248,7 @@ func AdminAddBoard(ctx *fiber.Ctx) error { newActorActivity.Object.Summary = board.Summary newActorActivity.Object.Sensitive = board.Restricted - db.MakeActivityRequestOutbox(newActorActivity) + newActorActivity.MakeRequestOutbox() return ctx.Redirect("/"+config.Key, http.StatusSeeOther) } @@ -261,9 +267,14 @@ func AdminNewsDelete(c *fiber.Ctx) error { func AdminActorIndex(ctx *fiber.Ctx) error { actor, _ := webfinger.GetActorFromPath(ctx.Path(), "/"+config.Key+"/") - follow, _ := webfinger.GetActorCollection(actor.Following) - follower, _ := webfinger.GetActorCollection(actor.Followers) - reported, _ := activitypub.GetActorCollectionReq(actor.Id + "/reported") + reqActivity := activitypub.Activity{Id: actor.Following} + follow, _ := reqActivity.GetCollection() + + reqActivity.Id = actor.Followers + follower, _ := reqActivity.GetCollection() + + reqActivity.Id = actor.Id + "/reported" + reported, _ := activitypub.GetActorCollectionReq(reqActivity.Id) var following []string var followers []string @@ -285,7 +296,7 @@ func AdminActorIndex(ctx *fiber.Ctx) error { reports = append(reports, r) } - localReports, _ := db.GetLocalReportDB(actor.Name) + localReports, _ := db.GetLocalReport(actor.Name) for _, e := range localReports { var r db.Report diff --git a/routes/api.go b/routes/api.go index 2fb0f3f..080d88d 100644 --- a/routes/api.go +++ b/routes/api.go @@ -6,6 +6,7 @@ import ( "time" "github.com/FChannel0/FChannel-Server/config" + "github.com/FChannel0/FChannel-Server/util" "github.com/gofiber/fiber/v2" ) @@ -20,7 +21,7 @@ func Media(c *fiber.Ctx) error { func RouteImages(ctx *fiber.Ctx, media string) error { req, err := http.NewRequest("GET", config.MediaHashs[media], nil) if err != nil { - return err + return util.MakeError(err, "RouteImages") } client := http.Client{ @@ -29,18 +30,18 @@ func RouteImages(ctx *fiber.Ctx, media string) error { resp, err := client.Do(req) if err != nil { - return err + return util.MakeError(err, "RouteImages") } defer resp.Body.Close() if resp.StatusCode != 200 { fileBytes, err := ioutil.ReadFile("./static/notfound.png") if err != nil { - return err + return util.MakeError(err, "RouteImages") } _, err = ctx.Write(fileBytes) - return err + return util.MakeError(err, "RouteImages") } body, _ := ioutil.ReadAll(resp.Body) diff --git a/routes/archive.go b/routes/archive.go index c0f4ff6..c4950b4 100644 --- a/routes/archive.go +++ b/routes/archive.go @@ -3,14 +3,13 @@ package routes import ( "github.com/FChannel0/FChannel-Server/activitypub" "github.com/FChannel0/FChannel-Server/config" - "github.com/FChannel0/FChannel-Server/db" + "github.com/FChannel0/FChannel-Server/post" "github.com/FChannel0/FChannel-Server/util" "github.com/FChannel0/FChannel-Server/webfinger" "github.com/gofiber/fiber/v2" ) func ArchiveGet(ctx *fiber.Ctx) error { - // TODO collection := ctx.Locals("collection").(activitypub.Collection) actor := collection.Actor @@ -21,7 +20,7 @@ func ArchiveGet(ctx *fiber.Ctx) error { returnData.Board.To = actor.Outbox returnData.Board.Actor = actor returnData.Board.Summary = actor.Summary - returnData.Board.ModCred, _ = db.GetPasswordFromSession(ctx) + returnData.Board.ModCred, _ = util.GetPasswordFromSession(ctx) returnData.Board.Domain = config.Domain returnData.Board.Restricted = actor.Restricted returnData.Key = config.Key @@ -32,12 +31,12 @@ func ArchiveGet(ctx *fiber.Ctx) error { var err error returnData.Instance, err = activitypub.GetActorFromDB(config.Domain) - capt, err := db.GetRandomCaptcha() + capt, err := util.GetRandomCaptcha() if err != nil { - return err + return util.MakeError(err, "ArchiveGet") } returnData.Board.Captcha = config.Domain + "/" + capt - returnData.Board.CaptchaCode = util.GetCaptchaCode(returnData.Board.Captcha) + returnData.Board.CaptchaCode = post.GetCaptchaCode(returnData.Board.Captcha) returnData.Title = "/" + actor.Name + "/ - " + actor.PreferredUsername diff --git a/routes/index.go b/routes/index.go index 8f12664..68fcfd0 100644 --- a/routes/index.go +++ b/routes/index.go @@ -4,6 +4,7 @@ import ( "github.com/FChannel0/FChannel-Server/activitypub" "github.com/FChannel0/FChannel-Server/config" "github.com/FChannel0/FChannel-Server/db" + "github.com/FChannel0/FChannel-Server/util" "github.com/FChannel0/FChannel-Server/webfinger" "github.com/gofiber/fiber/v2" ) @@ -11,7 +12,7 @@ import ( func Index(ctx *fiber.Ctx) error { actor, err := activitypub.GetActorFromDB(config.Domain) if err != nil { - return err + return util.MakeError(err, "Index") } // this is a activitpub json request return json instead of html page @@ -22,18 +23,19 @@ func Index(ctx *fiber.Ctx) error { var data PageData - col, err := webfinger.GetCollectionFromReq("https://fchan.xyz/followers") + reqActivity := activitypub.Activity{Id: "https://fchan.xyz/followers"} + col, err := reqActivity.GetCollection() if err != nil { - return err + return util.MakeError(err, "Index") } if len(col.Items) > 0 { data.InstanceIndex = col.Items } - data.NewsItems, err = db.GetNewsFromDB(3) + data.NewsItems, err = db.GetNews(3) if err != nil { - return err + return util.MakeError(err, "Index") } data.Title = "Welcome to " + actor.PreferredUsername @@ -42,7 +44,7 @@ func Index(ctx *fiber.Ctx) error { data.Board.Name = "" data.Key = config.Key data.Board.Domain = config.Domain - data.Board.ModCred, _ = db.GetPasswordFromSession(ctx) + data.Board.ModCred, _ = util.GetPasswordFromSession(ctx) data.Board.Actor = actor data.Board.Post.Actor = actor.Id data.Board.Restricted = actor.Restricted diff --git a/routes/news.go b/routes/news.go index bd037c2..c091605 100644 --- a/routes/news.go +++ b/routes/news.go @@ -4,17 +4,17 @@ import ( "github.com/FChannel0/FChannel-Server/activitypub" "github.com/FChannel0/FChannel-Server/config" "github.com/FChannel0/FChannel-Server/db" + "github.com/FChannel0/FChannel-Server/util" "github.com/FChannel0/FChannel-Server/webfinger" "github.com/gofiber/fiber/v2" ) func NewsGet(ctx *fiber.Ctx) error { - // TODO timestamp := 0 actor, err := activitypub.GetActorFromDB(config.Domain) if err != nil { - return err + return util.MakeError(err, "NewsGet") } var data PageData @@ -23,15 +23,15 @@ func NewsGet(ctx *fiber.Ctx) error { data.Board.Name = "" data.Key = config.Key data.Board.Domain = config.Domain - data.Board.ModCred, _ = db.GetPasswordFromSession(ctx) + data.Board.ModCred, _ = util.GetPasswordFromSession(ctx) data.Board.Actor = actor data.Board.Post.Actor = actor.Id data.Board.Restricted = actor.Restricted data.NewsItems = make([]db.NewsItem, 1) - data.NewsItems[0], err = db.GetNewsItemFromDB(timestamp) + data.NewsItems[0], err = db.GetNewsItem(timestamp) if err != nil { - return err + return util.MakeError(err, "NewsGet") } data.Title = actor.PreferredUsername + ": " + data.NewsItems[0].Title @@ -45,7 +45,7 @@ func NewsGet(ctx *fiber.Ctx) error { func AllNewsGet(ctx *fiber.Ctx) error { actor, err := activitypub.GetActorFromDB(config.Domain) if err != nil { - return err + return util.MakeError(err, "AllNewsGet") } var data PageData @@ -55,14 +55,14 @@ func AllNewsGet(ctx *fiber.Ctx) error { data.Board.Name = "" data.Key = config.Key data.Board.Domain = config.Domain - data.Board.ModCred, _ = db.GetPasswordFromSession(ctx) + data.Board.ModCred, _ = util.GetPasswordFromSession(ctx) data.Board.Actor = actor data.Board.Post.Actor = actor.Id data.Board.Restricted = actor.Restricted - data.NewsItems, err = db.GetNewsFromDB(0) + data.NewsItems, err = db.GetNews(0) if err != nil { - return err + return util.MakeError(err, "AllNewsGet") } data.Themes = &config.Themes diff --git a/routes/outbox.go b/routes/outbox.go index 902d3e6..1d0d52e 100644 --- a/routes/outbox.go +++ b/routes/outbox.go @@ -5,7 +5,7 @@ import ( "github.com/FChannel0/FChannel-Server/activitypub" "github.com/FChannel0/FChannel-Server/config" - "github.com/FChannel0/FChannel-Server/db" + "github.com/FChannel0/FChannel-Server/post" "github.com/FChannel0/FChannel-Server/util" "github.com/FChannel0/FChannel-Server/webfinger" "github.com/gofiber/fiber/v2" @@ -14,7 +14,7 @@ import ( func Outbox(ctx *fiber.Ctx) error { actor, err := webfinger.GetActorFromPath(ctx.Path(), "/") if err != nil { - return err + return util.MakeError(err, "Outbox") } if activitypub.AcceptActivity(ctx.Get("Accept")) { @@ -40,13 +40,13 @@ func OutboxGet(ctx *fiber.Ctx) error { var page int if postNum := ctx.Query("page"); postNum != "" { if page, err = strconv.Atoi(postNum); err != nil { - return err + return util.MakeError(err, "OutboxGet") } } collection, err := actor.WantToServePage(page) if err != nil { - return err + return util.MakeError(err, "OutboxGet") } var offset = 15 @@ -68,7 +68,7 @@ func OutboxGet(ctx *fiber.Ctx) error { data.Board.InReplyTo = "" data.Board.To = actor.Outbox data.Board.Actor = actor - data.Board.ModCred, _ = db.GetPasswordFromSession(ctx) + data.Board.ModCred, _ = util.GetPasswordFromSession(ctx) data.Board.Domain = config.Domain data.Board.Restricted = actor.Restricted data.CurrentPage = page @@ -76,12 +76,12 @@ func OutboxGet(ctx *fiber.Ctx) error { data.Board.Post.Actor = actor.Id - capt, err := db.GetRandomCaptcha() + capt, err := util.GetRandomCaptcha() if err != nil { - return err + return util.MakeError(err, "OutboxGet") } data.Board.Captcha = config.Domain + "/" + capt - data.Board.CaptchaCode = util.GetCaptchaCode(data.Board.Captcha) + data.Board.CaptchaCode = post.GetCaptchaCode(data.Board.Captcha) data.Title = "/" + actor.Name + "/ - " + actor.PreferredUsername diff --git a/routes/post.go b/routes/post.go index 813f53d..4002be7 100644 --- a/routes/post.go +++ b/routes/post.go @@ -5,7 +5,7 @@ import ( "github.com/FChannel0/FChannel-Server/activitypub" "github.com/FChannel0/FChannel-Server/config" - "github.com/FChannel0/FChannel-Server/db" + "github.com/FChannel0/FChannel-Server/post" "github.com/FChannel0/FChannel-Server/util" "github.com/FChannel0/FChannel-Server/webfinger" "github.com/gofiber/fiber/v2" @@ -36,23 +36,23 @@ func PostGet(ctx *fiber.Ctx) error { if re.MatchString(ctx.Path()) { // if non local actor post name := activitypub.GetActorFollowNameFromPath(ctx.Path()) - followActors, err := webfinger.GetActorsFollowFromName(actor, name) + followActors, err := actor.GetFollowFromName(name) if err != nil { - return err + return util.MakeError(err, "PostGet") } followCollection, err := activitypub.GetActorsFollowPostFromId(followActors, postId) if err != nil { - return err + return util.MakeError(err, "PostGet") } if len(followCollection.OrderedItems) > 0 { data.Board.InReplyTo = followCollection.OrderedItems[0].Id data.Posts = append(data.Posts, followCollection.OrderedItems[0]) - actor, err := webfinger.FingerActor(data.Board.InReplyTo) + actor, err := activitypub.FingerActor(data.Board.InReplyTo) if err != nil { - return err + return util.MakeError(err, "PostGet") } data.Board.Post.Actor = actor.Id @@ -61,7 +61,7 @@ func PostGet(ctx *fiber.Ctx) error { obj := activitypub.ObjectBase{Id: inReplyTo} collection, err := obj.GetCollectionFromPath() if err != nil { - return err + return util.MakeError(err, "PostGet") } if collection.Actor.Id != "" { @@ -83,21 +83,21 @@ func PostGet(ctx *fiber.Ctx) error { data.Board.To = actor.Outbox data.Board.Actor = actor data.Board.Summary = actor.Summary - data.Board.ModCred, _ = db.GetPasswordFromSession(ctx) + data.Board.ModCred, _ = util.GetPasswordFromSession(ctx) data.Board.Domain = config.Domain data.Board.Restricted = actor.Restricted data.ReturnTo = "feed" - capt, err := db.GetRandomCaptcha() + capt, err := util.GetRandomCaptcha() if err != nil { - return err + return util.MakeError(err, "PostGet") } data.Board.Captcha = config.Domain + "/" + capt - data.Board.CaptchaCode = util.GetCaptchaCode(data.Board.Captcha) + data.Board.CaptchaCode = post.GetCaptchaCode(data.Board.Captcha) data.Instance, err = activitypub.GetActorFromDB(config.Domain) if err != nil { - return err + return util.MakeError(err, "PostGet") } data.Key = config.Key @@ -124,7 +124,7 @@ func CatalogGet(ctx *fiber.Ctx) error { actorName := ctx.Params("actor") actor, err := activitypub.GetActorByNameFromDB(actorName) if err != nil { - return err + return util.MakeError(err, "CatalogGet") } collection, err := actor.GetCatalogCollection() @@ -150,7 +150,7 @@ func CatalogGet(ctx *fiber.Ctx) error { data.Board.To = actor.Outbox data.Board.Actor = actor data.Board.Summary = actor.Summary - data.Board.ModCred, _ = db.GetPasswordFromSession(ctx) + data.Board.ModCred, _ = util.GetPasswordFromSession(ctx) data.Board.Domain = config.Domain data.Board.Restricted = actor.Restricted data.Key = config.Key @@ -160,16 +160,16 @@ func CatalogGet(ctx *fiber.Ctx) error { data.Instance, err = activitypub.GetActorFromDB(config.Domain) if err != nil { - return err + return util.MakeError(err, "CatalogGet") } - capt, err := db.GetRandomCaptcha() + capt, err := util.GetRandomCaptcha() if err != nil { - return err + return util.MakeError(err, "CatalogGet") } data.Board.Captcha = config.Domain + "/" + capt - data.Board.CaptchaCode = util.GetCaptchaCode(data.Board.Captcha) + data.Board.CaptchaCode = post.GetCaptchaCode(data.Board.Captcha) data.Title = "/" + data.Board.Name + "/ - catalog" diff --git a/routes/util.go b/routes/util.go index de0c6e0..94337bc 100644 --- a/routes/util.go +++ b/routes/util.go @@ -28,19 +28,19 @@ func getThemeCookie(c *fiber.Ctx) string { return "default" } -func wantToServeCatalog(actorName string) (activitypub.Collection, bool, error) { +func WantToServeCatalog(actorName string) (activitypub.Collection, bool, error) { var collection activitypub.Collection serve := false actor, err := activitypub.GetActorByNameFromDB(actorName) if err != nil { - return collection, false, err + return collection, false, util.MakeError(err, "WantToServeCatalog") } if actor.Id != "" { collection, err = actor.GetCatalogCollection() if err != nil { - return collection, false, err + return collection, false, util.MakeError(err, "WantToServeCatalog") } collection.Actor = actor @@ -50,19 +50,19 @@ func wantToServeCatalog(actorName string) (activitypub.Collection, bool, error) return collection, serve, nil } -func wantToServeArchive(actorName string) (activitypub.Collection, bool, error) { +func WantToServeArchive(actorName string) (activitypub.Collection, bool, error) { var collection activitypub.Collection serve := false actor, err := activitypub.GetActorByNameFromDB(actorName) if err != nil { - return collection, false, err + return collection, false, util.MakeError(err, "WantToServeArchive") } if actor.Id != "" { collection, err = actor.GetCollectionType("Archive") if err != nil { - return collection, false, err + return collection, false, util.MakeError(err, "WantToServeArchive") } collection.Actor = actor @@ -77,18 +77,18 @@ func GetActorPost(ctx *fiber.Ctx, path string) error { collection, err := obj.GetCollectionFromPath() if err != nil { - return err + return util.MakeError(err, "GetActorPost") } if len(collection.OrderedItems) > 0 { enc, err := json.MarshalIndent(collection, "", "\t") if err != nil { - return err + return util.MakeError(err, "GetActorPost") } ctx.Response().Header.Set("Content-Type", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"") _, err = ctx.Write(enc) - return err + return util.MakeError(err, "GetActorPost") } return nil @@ -98,9 +98,9 @@ func ParseOutboxRequest(ctx *fiber.Ctx, actor activitypub.Actor) error { contentType := util.GetContentType(ctx.Get("content-type")) if contentType == "multipart/form-data" || contentType == "application/x-www-form-urlencoded" { - hasCaptcha, err := db.BoardHasAuthType(actor.Name, "captcha") + hasCaptcha, err := util.BoardHasAuthType(actor.Name, "captcha") if err != nil { - return err + return util.MakeError(err, "ParseOutboxRequest") } valid, err := post.CheckCaptcha(ctx.FormValue("captcha")) @@ -112,15 +112,15 @@ func ParseOutboxRequest(ctx *fiber.Ctx, actor activitypub.Actor) error { if header.Size > (7 << 20) { ctx.Response().Header.SetStatusCode(403) _, err := ctx.Write([]byte("7MB max file size")) - return err + return util.MakeError(err, "ParseOutboxRequest") } else if isBanned, err := post.IsMediaBanned(f); err == nil && isBanned { //Todo add logging - fmt.Println("media banned") + config.Log.Println("media banned") ctx.Response().Header.SetStatusCode(403) _, err := ctx.Write([]byte("media banned")) - return err + return util.MakeError(err, "ParseOutboxRequest") } else if err != nil { - return err + return util.MakeError(err, "ParseOutboxRequest") } contentType, _ := util.GetFileContentType(f) @@ -128,40 +128,40 @@ func ParseOutboxRequest(ctx *fiber.Ctx, actor activitypub.Actor) error { if !post.SupportedMIMEType(contentType) { ctx.Response().Header.SetStatusCode(403) _, err := ctx.Write([]byte("file type not supported")) - return err + return util.MakeError(err, "ParseOutboxRequest") } } var nObj = activitypub.CreateObject("Note") nObj, err := post.ObjectFromForm(ctx, nObj) if err != nil { - return err + return util.MakeError(err, "ParseOutboxRequest") } nObj.Actor = config.Domain + "/" + actor.Name nObj, err = nObj.Write() if err != nil { - return err + return util.MakeError(err, "ParseOutboxRequest") } if len(nObj.To) == 0 { - if err := db.ArchivePosts(actor); err != nil { - return err + if err := actor.ArchivePosts(); err != nil { + return util.MakeError(err, "ParseOutboxRequest") } } - activity, err := webfinger.CreateActivity("Create", nObj) + activity, err := nObj.CreateActivity("Create") if err != nil { - return err + return util.MakeError(err, "ParseOutboxRequest") } - activity, err = webfinger.AddFollowersToActivity(activity) + activity, err = activity.AddFollowersTo() if err != nil { - return err + return util.MakeError(err, "ParseOutboxRequest") } - go db.MakeActivityRequest(activity) + go activity.MakeRequestInbox() var id string op := len(nObj.InReplyTo) - 1 @@ -175,23 +175,23 @@ func ParseOutboxRequest(ctx *fiber.Ctx, actor activitypub.Actor) error { ctx.Response().Header.Set("Status", "200") _, err = ctx.Write([]byte(id)) - return err + return util.MakeError(err, "ParseOutboxRequest") } ctx.Response().Header.Set("Status", "403") _, err = ctx.Write([]byte("captcha could not auth")) - return err + return util.MakeError(err, "") } else { // json request activity, err := activitypub.GetActivityFromJson(ctx) if err != nil { - return err + return util.MakeError(err, "ParseOutboxRequest") } if res, err := activity.IsLocal(); err == nil && res { - if res := db.VerifyHeaderSignature(ctx, *activity.Actor); err == nil && !res { + if res := activity.Actor.VerifyHeaderSignature(ctx); err == nil && !res { ctx.Response().Header.Set("Status", "403") _, err = ctx.Write([]byte("")) - return err + return util.MakeError(err, "ParseOutboxRequest") } switch activity.Type { @@ -209,30 +209,30 @@ func ParseOutboxRequest(ctx *fiber.Ctx, actor activitypub.Actor) error { var rActivity activitypub.Activity if validActor && validLocalActor { - rActivity = db.AcceptFollow(activity) - rActivity, err = db.SetActorFollowingDB(rActivity) + rActivity = activity.AcceptFollow() + rActivity, err = rActivity.SetActorFollowing() if err != nil { - return err + return util.MakeError(err, "ParseOutboxRequest") } - if err := db.MakeActivityRequest(activity); err != nil { - return err + if err := activity.MakeRequestInbox(); err != nil { + return util.MakeError(err, "ParseOutboxRequest") } } actor, _ := activitypub.GetActorFromDB(config.Domain) webfinger.FollowingBoards, err = actor.GetFollowing() if err != nil { - return err + return util.MakeError(err, "ParseOutboxRequest") } webfinger.Boards, err = webfinger.GetBoardCollection() if err != nil { - return err + return util.MakeError(err, "ParseOutboxRequest") } break case "Delete": - fmt.Println("This is a delete") + config.Log.Println("This is a delete") ctx.Response().Header.Set("Status", "403") _, err = ctx.Write([]byte("could not process activity")) break @@ -248,9 +248,9 @@ func ParseOutboxRequest(ctx *fiber.Ctx, actor activitypub.Actor) error { summary := activity.Object.Summary restricted := activity.Object.Sensitive - actor, err := db.CreateNewBoardDB(*activitypub.CreateNewActor(name, prefname, summary, config.AuthReq, restricted)) + actor, err := db.CreateNewBoard(*activitypub.CreateNewActor(name, prefname, summary, config.AuthReq, restricted)) if err != nil { - return err + return util.MakeError(err, "ParseOutboxRequest") } if actor.Id != "" { @@ -273,7 +273,7 @@ func ParseOutboxRequest(ctx *fiber.Ctx, actor activitypub.Actor) error { webfinger.FollowingBoards = board webfinger.Boards, err = webfinger.GetBoardCollection() - return err + return util.MakeError(err, "ParseOutboxRequest") } ctx.Response().Header.Set("Status", "403") @@ -285,12 +285,12 @@ func ParseOutboxRequest(ctx *fiber.Ctx, actor activitypub.Actor) error { _, err = ctx.Write([]byte("could not process activity")) } } else if err != nil { - return err + return util.MakeError(err, "ParseOutboxRequest") } else { - fmt.Println("is NOT activity") + config.Log.Println("is NOT activity") ctx.Response().Header.Set("Status", "403") _, err = ctx.Write([]byte("could not process activity")) - return err + return util.MakeError(err, "ParseOutboxRequest") } } @@ -338,7 +338,7 @@ func TemplateFunctions(engine *html.Engine) { engine.AddFunc("isOnion", util.IsOnion) engine.AddFunc("parseReplyLink", func(actorId string, op string, id string, content string) template.HTML { - actor, _ := webfinger.FingerActor(actorId) + actor, _ := activitypub.FingerActor(actorId) title := strings.ReplaceAll(post.ParseLinkTitle(actor.Id+"/", op, content), `/\<`, ">") link := ">>" + util.ShortURL(actor.Outbox, id) + "" return template.HTML(link) diff --git a/routes/webfinger.go b/routes/webfinger.go index 9789e39..495e6c7 100644 --- a/routes/webfinger.go +++ b/routes/webfinger.go @@ -6,7 +6,7 @@ import ( "github.com/FChannel0/FChannel-Server/activitypub" "github.com/FChannel0/FChannel-Server/config" - "github.com/FChannel0/FChannel-Server/webfinger" + "github.com/FChannel0/FChannel-Server/util" "github.com/gofiber/fiber/v2" ) @@ -38,11 +38,11 @@ func Webfinger(c *fiber.Ctx) error { c.Status(fiber.StatusBadRequest) return c.Send([]byte("actor not local")) } else if err != nil { - return err + return util.MakeError(err, "Webfinger") } - var finger webfinger.Webfinger - var link webfinger.WebfingerLink + var finger activitypub.Webfinger + var link activitypub.WebfingerLink finger.Subject = "acct:" + actorDomain[0] + "@" + actorDomain[1] link.Rel = "self" -- cgit v1.2.3