diff options
author | KushBlazingJudah <59340248+KushBlazingJudah@users.noreply.github.com> | 2021-10-27 18:18:06 -0300 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | d2277b1f9b17e61456cd312ef54542e1cfa81a40 (patch) | |
tree | 3f7f4b6dffe95c0f9909921dd47700e733b4ee47 /routes | |
parent | 9f8195162295d2a789462e6b13394174a5745100 (diff) |
restructuring, part 1 of many
Diffstat (limited to 'routes')
-rw-r--r-- | routes/404.go | 7 | ||||
-rw-r--r-- | routes/index.go | 48 | ||||
-rw-r--r-- | routes/util.go | 39 |
3 files changed, 92 insertions, 2 deletions
diff --git a/routes/404.go b/routes/404.go new file mode 100644 index 0000000..64ea167 --- /dev/null +++ b/routes/404.go @@ -0,0 +1,7 @@ +package routes + +import "github.com/gofiber/fiber/v2" + +func NotFound(c *fiber.Ctx) error { + return c.Status(404).Render("404", fiber.Map{}, "layouts/main") +} diff --git a/routes/index.go b/routes/index.go index ccd398b..df10d9f 100644 --- a/routes/index.go +++ b/routes/index.go @@ -1,7 +1,51 @@ 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 Index(c *fiber.Ctx) error { - return c.SendString("index") + actor, err := db.GetActor(config.Domain) + if err != nil { + return err + } + + var data PageData + data.Title = "Welcome to " + actor.PreferredUsername + data.PreferredUsername = actor.PreferredUsername + data.Boards = Boards + data.Board.Name = "" + data.Key = *Key + data.Board.Domain = config.Domain + data.Board.ModCred, _ = GetPasswordFromCtx(c) + data.Board.Actor = actor + data.Board.Post.Actor = actor.Id + data.Board.Restricted = actor.Restricted + //almost certainly there is a better algorithm for this but the old one was wrong + //and I suck at math. This works at least. + data.BoardRemainer = make([]int, 3-(len(data.Boards)%3)) + if len(data.BoardRemainer) == 3 { + data.BoardRemainer = make([]int, 0) + } + + col := GetCollectionFromReq("https://fchan.xyz/followers") + + if len(col.Items) > 0 { + data.InstanceIndex = col.Items + } + + data.NewsItems, err = db.GetNewsFromDB(3) + if err != nil { + return err + } + + data.Themes = &Themes + + data.ThemeCookie = getThemeCookie(c) + + return c.Render("index", fiber.Map{ + "page": data, + }, "layouts/main") } diff --git a/routes/util.go b/routes/util.go new file mode 100644 index 0000000..37639c3 --- /dev/null +++ b/routes/util.go @@ -0,0 +1,39 @@ +package routes + +import ( + "fmt" + "strings" + + "github.com/gofiber/fiber/v2" +) + +func getThemeCookie(c *fiber.Ctx) string { + cookie := c.Cookies("theme") + if cookie != "" { + cookies := strings.SplitN(cookie, "=", 2) + return cookies[0] + } + + return "default" +} + +func getPassword(r *fiber.Ctx) (string, string) { + c := r.Cookies("session_token") + + sessionToken := c + + response, err := cache.Do("GET", sessionToken) + if err != nil { + return "", "" + } + + token := fmt.Sprintf("%s", response) + + parts := strings.Split(token, "|") + + if len(parts) > 1 { + return parts[0], parts[1] + } + + return "", "" +} |