From 972223c992ca5aa5e5d93cff3b2ee4e30182025b Mon Sep 17 00:00:00 2001 From: KushBlazingJudah <59340248+KushBlazingJudah@users.noreply.github.com> Date: Sun, 7 Nov 2021 00:32:39 -0300 Subject: restructuring part 5 --- routes/archive.go | 53 ++++++++++++++++++++++++++++ routes/index.go | 9 +++-- routes/news.go | 69 ++++++++++++++++++++++++++++++++++-- routes/outbox.go | 83 +++++++++++++++++++++++++++++++++++++++++-- routes/post.go | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ routes/util.go | 75 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 382 insertions(+), 11 deletions(-) create mode 100644 routes/archive.go create mode 100644 routes/post.go (limited to 'routes') diff --git a/routes/archive.go b/routes/archive.go new file mode 100644 index 0000000..87f3b8b --- /dev/null +++ b/routes/archive.go @@ -0,0 +1,53 @@ +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/util" + "github.com/gofiber/fiber/v2" +) + +func ArchiveGet(ctx *fiber.Ctx) error { + // TODO + collection := ctx.Locals("collection").(activitypub.Collection) + actor := collection.Actor + + var returnData PageData + returnData.Board.Name = actor.Name + returnData.Board.PrefName = actor.PreferredUsername + returnData.Board.InReplyTo = "" + returnData.Board.To = actor.Outbox + returnData.Board.Actor = *actor + returnData.Board.Summary = actor.Summary + returnData.Board.ModCred, _ = getPassword(ctx) + returnData.Board.Domain = config.Domain + returnData.Board.Restricted = actor.Restricted + returnData.Key = config.Key + returnData.ReturnTo = "archive" + + returnData.Board.Post.Actor = actor.Id + + var err error + returnData.Instance, err = db.GetActorFromDB(config.Domain) + + capt, err := db.GetRandomCaptcha() + if err != nil { + return err + } + returnData.Board.Captcha = config.Domain + "/" + capt + returnData.Board.CaptchaCode = util.GetCaptchaCode(returnData.Board.Captcha) + + returnData.Title = "/" + actor.Name + "/ - " + actor.PreferredUsername + + returnData.Boards = db.Boards + + returnData.Posts = collection.OrderedItems + + returnData.Themes = &config.Themes + returnData.ThemeCookie = getThemeCookie(ctx) + + return ctx.Render("archive", fiber.Map{ + "page": returnData, + }, "layouts/main") +} diff --git a/routes/index.go b/routes/index.go index a130796..3599455 100644 --- a/routes/index.go +++ b/routes/index.go @@ -7,7 +7,7 @@ import ( "github.com/gofiber/fiber/v2" ) -func Index(c *fiber.Ctx) error { +func Index(ctx *fiber.Ctx) error { actor, err := db.GetActorFromDB(config.Domain) if err != nil { return err @@ -20,7 +20,7 @@ func Index(c *fiber.Ctx) error { data.Board.Name = "" data.Key = config.Key data.Board.Domain = config.Domain - data.Board.ModCred, _ = getPassword(c) + data.Board.ModCred, _ = getPassword(ctx) data.Board.Actor = actor data.Board.Post.Actor = actor.Id data.Board.Restricted = actor.Restricted @@ -46,10 +46,9 @@ func Index(c *fiber.Ctx) error { } data.Themes = &config.Themes + data.ThemeCookie = getThemeCookie(ctx) - data.ThemeCookie = getThemeCookie(c) - - return c.Render("index", fiber.Map{ + return ctx.Render("index", fiber.Map{ "page": data, }, "layouts/main") } diff --git a/routes/news.go b/routes/news.go index 585614d..98d0019 100644 --- a/routes/news.go +++ b/routes/news.go @@ -1,7 +1,70 @@ package routes -import "github.com/gofiber/fiber/v2" +import ( + "github.com/FChannel0/FChannel-Server/config" + "github.com/FChannel0/FChannel-Server/db" + "github.com/gofiber/fiber/v2" +) -func NewsGet(c *fiber.Ctx) error { - return c.SendString("news get") +func NewsGet(ctx *fiber.Ctx) error { + // TODO + timestamp := 0 + + actor, err := db.GetActorFromDB(config.Domain) + if err != nil { + return err + } + + var data PageData + data.PreferredUsername = actor.PreferredUsername + data.Boards = db.Boards + data.Board.Name = "" + data.Key = config.Key + data.Board.Domain = config.Domain + data.Board.ModCred, _ = getPassword(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) + if err != nil { + return err + } + + data.Title = actor.PreferredUsername + ": " + data.NewsItems[0].Title + + data.Themes = &config.Themes + data.ThemeCookie = getThemeCookie(ctx) + + return ctx.Render("news", fiber.Map{"page": data}, "layouts/main") +} + +func AllNewsGet(ctx *fiber.Ctx) error { + actor, err := db.GetActorFromDB(config.Domain) + if err != nil { + return err + } + + var data PageData + data.PreferredUsername = actor.PreferredUsername + data.Title = actor.PreferredUsername + " News" + data.Boards = db.Boards + data.Board.Name = "" + data.Key = config.Key + data.Board.Domain = config.Domain + data.Board.ModCred, _ = getPassword(ctx) + data.Board.Actor = actor + data.Board.Post.Actor = actor.Id + data.Board.Restricted = actor.Restricted + + data.NewsItems, err = db.GetNewsFromDB(0) + if err != nil { + return err + } + + data.Themes = &config.Themes + data.ThemeCookie = getThemeCookie(ctx) + + return ctx.Render("anews", fiber.Map{"page": data}, "layouts/main") } diff --git a/routes/outbox.go b/routes/outbox.go index 8945bb1..b00f946 100644 --- a/routes/outbox.go +++ b/routes/outbox.go @@ -1,9 +1,86 @@ package routes -import "github.com/gofiber/fiber/v2" +import ( + "strconv" -func Outbox(c *fiber.Ctx) error { + "github.com/FChannel0/FChannel-Server/config" + "github.com/FChannel0/FChannel-Server/db" + "github.com/FChannel0/FChannel-Server/util" + "github.com/gofiber/fiber/v2" +) + +func Outbox(ctx *fiber.Ctx) error { // STUB - return c.SendString("main outbox") + return ctx.SendString("main outbox") +} + +func OutboxGet(ctx *fiber.Ctx) error { + collection, valid, err := wantToServePage(ctx.Params("actor"), 0) + if err != nil { + return err + } else if !valid { + // TODO: 404 template + return ctx.SendString("404") + } + + actor := collection.Actor + + postNum := ctx.Query("page") + page, err := strconv.Atoi(postNum) + if err != nil { + return err + } + + var returnData PageData + + returnData.Board.Name = actor.Name + returnData.Board.PrefName = actor.PreferredUsername + returnData.Board.Summary = actor.Summary + returnData.Board.InReplyTo = "" + returnData.Board.To = actor.Outbox + returnData.Board.Actor = *actor + returnData.Board.ModCred, _ = getPassword(ctx) + returnData.Board.Domain = config.Domain + returnData.Board.Restricted = actor.Restricted + returnData.CurrentPage = page + returnData.ReturnTo = "feed" + + returnData.Board.Post.Actor = actor.Id + + capt, err := db.GetRandomCaptcha() + if err != nil { + return err + } + returnData.Board.Captcha = config.Domain + "/" + capt + returnData.Board.CaptchaCode = util.GetCaptchaCode(returnData.Board.Captcha) + + returnData.Title = "/" + actor.Name + "/ - " + actor.PreferredUsername + + returnData.Key = config.Key + + returnData.Boards = db.Boards + returnData.Posts = collection.OrderedItems + + var offset = 15 + var pages []int + pageLimit := (float64(collection.TotalItems) / float64(offset)) + + if pageLimit > 11 { + pageLimit = 11 + } + + for i := 0.0; i < pageLimit; i++ { + pages = append(pages, int(i)) + } + + returnData.Pages = pages + returnData.TotalPage = len(returnData.Pages) - 1 + + returnData.Themes = &config.Themes + returnData.ThemeCookie = getThemeCookie(ctx) + + return ctx.Render("nposts", fiber.Map{ + "page": returnData, + }, "layouts/main") } diff --git a/routes/post.go b/routes/post.go new file mode 100644 index 0000000..41706c0 --- /dev/null +++ b/routes/post.go @@ -0,0 +1,104 @@ +package routes + +import ( + "regexp" + + "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 PostGet(ctx *fiber.Ctx) error { + actor, err := db.GetActorByNameFromDB(ctx.Params("actor")) + if err != nil { + return err + } + + postId := ctx.Params("post") + + inReplyTo := actor.Id + "/" + postId + + var returnData PageData + returnData.Board.Name = actor.Name + returnData.Board.PrefName = actor.PreferredUsername + returnData.Board.To = actor.Outbox + returnData.Board.Actor = actor + returnData.Board.Summary = actor.Summary + returnData.Board.ModCred, _ = getPassword(ctx) + returnData.Board.Domain = config.Domain + returnData.Board.Restricted = actor.Restricted + returnData.ReturnTo = "feed" + + capt, err := db.GetRandomCaptcha() + if err != nil { + return err + } + returnData.Board.Captcha = config.Domain + "/" + capt + returnData.Board.CaptchaCode = util.GetCaptchaCode(returnData.Board.Captcha) + + returnData.Instance, err = db.GetActorFromDB(config.Domain) + if err != nil { + return err + } + + returnData.Title = "/" + returnData.Board.Name + "/ - " + returnData.Board.PrefName + + returnData.Key = config.Key + + returnData.Boards = db.Boards + + re := regexp.MustCompile("f(\\w|[!@#$%^&*<>])+-(\\w|[!@#$%^&*<>])+") + + if re.MatchString(postId) { // if non local actor post + name := util.GetActorFollowNameFromPath(postId) + + followActors, err := webfinger.GetActorsFollowFromName(actor, name) + if err != nil { + return err + } + + followCollection, err := db.GetActorsFollowPostFromId(followActors, postId) + if err != nil { + return err + } + + if len(followCollection.OrderedItems) > 0 { + returnData.Board.InReplyTo = followCollection.OrderedItems[0].Id + returnData.Posts = append(returnData.Posts, followCollection.OrderedItems[0]) + + actor, err := webfinger.FingerActor(returnData.Board.InReplyTo) + if err != nil { + return err + } + + returnData.Board.Post.Actor = actor.Id + } + } else { + collection, err := db.GetObjectByIDFromDB(inReplyTo) + if err != nil { + return err + } + + if collection.Actor != nil { + returnData.Board.Post.Actor = collection.Actor.Id + returnData.Board.InReplyTo = inReplyTo + + if len(collection.OrderedItems) > 0 { + returnData.Posts = append(returnData.Posts, collection.OrderedItems[0]) + } + } + } + + if len(returnData.Posts) > 0 { + returnData.PostId = util.ShortURL(returnData.Board.To, returnData.Posts[0].Id) + } + + returnData.Themes = &config.Themes + returnData.ThemeCookie = getThemeCookie(ctx) + + return ctx.Render("npost", fiber.Map{ + "page": returnData, + }, "layouts/main") +} diff --git a/routes/util.go b/routes/util.go index a38e969..0a8dc9c 100644 --- a/routes/util.go +++ b/routes/util.go @@ -1,13 +1,17 @@ package routes import ( + "errors" "fmt" "strings" + "github.com/FChannel0/FChannel-Server/activitypub" "github.com/FChannel0/FChannel-Server/db" "github.com/gofiber/fiber/v2" ) +var ErrorPageLimit = errors.New("above page limit") + func getThemeCookie(c *fiber.Ctx) string { cookie := c.Cookies("theme") if cookie != "" { @@ -38,3 +42,74 @@ func getPassword(r *fiber.Ctx) (string, string) { return "", "" } + +func wantToServePage(actorName string, page int) (activitypub.Collection, bool, error) { + var collection activitypub.Collection + serve := false + + // TODO: don't hard code? + if page > 10 { + return collection, serve, ErrorPageLimit + } + + actor, err := db.GetActorByNameFromDB(actorName) + if err != nil { + return collection, false, err + } + + if actor.Id != "" { + collection, err = db.GetObjectFromDBPage(actor.Id, page) + if err != nil { + return collection, false, err + } + + collection.Actor = &actor + return collection, true, nil + } + + return collection, serve, nil +} + +func wantToServeCatalog(actorName string) (activitypub.Collection, bool, error) { + var collection activitypub.Collection + serve := false + + actor, err := db.GetActorByNameFromDB(actorName) + if err != nil { + return collection, false, err + } + + if actor.Id != "" { + collection, err = db.GetObjectFromDBCatalog(actor.Id) + if err != nil { + return collection, false, err + } + + collection.Actor = &actor + return collection, true, nil + } + + return collection, serve, nil +} + +func wantToServeArchive(actorName string) (activitypub.Collection, bool, error) { + var collection activitypub.Collection + serve := false + + actor, err := db.GetActorByNameFromDB(actorName) + if err != nil { + return collection, false, err + } + + if actor.Id != "" { + collection, err = db.GetActorCollectionDBType(actor.Id, "Archive") + if err != nil { + return collection, false, err + } + + collection.Actor = &actor + return collection, true, nil + } + + return collection, serve, nil +} -- cgit v1.2.3