diff options
author | FChannel <> | 2022-06-04 23:31:12 -0700 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | a87235f8b978126c0ca9f9eb252f58e6de17622c (patch) | |
tree | 592002dda3ac9f4ca1514eb0f473d31fb62faa84 | |
parent | e3c83c1ddaea5f42b6fda66d4b86459fd1349b3b (diff) |
news CRUD working
-rw-r--r-- | main.go | 7 | ||||
-rw-r--r-- | route/routes/news.go | 72 | ||||
-rw-r--r-- | views/admin.html | 2 | ||||
-rw-r--r-- | views/anews.html | 15 | ||||
-rw-r--r-- | views/index.html | 3 | ||||
-rw-r--r-- | views/news.html | 7 |
6 files changed, 93 insertions, 13 deletions
@@ -63,13 +63,14 @@ func main() { app.All("/"+config.Key+"/", routes.AdminIndex) app.Get("/"+config.Key+"/follow", routes.AdminFollow) app.Post("/"+config.Key+"/addboard", routes.AdminAddBoard) - app.Get("/"+config.Key+"/newspost", routes.NewsPost) - app.Get("/"+config.Key+"/newsdelete", routes.NewsDelete) + app.Post("/"+config.Key+"/newspost", routes.NewsPost) + app.Get("/"+config.Key+"/newsdelete/:ts", routes.NewsDelete) app.All("/"+config.Key+"/:actor/follow", routes.AdminFollow) app.Get("/"+config.Key+"/:actor", routes.AdminActorIndex) // News routes - app.Get("/news", routes.NewsGet) + app.Get("/news/:ts", routes.NewsGet) + app.Get("/news", routes.NewsGetAll) // Board managment app.Get("/banmedia", routes.BoardBanMedia) diff --git a/route/routes/news.go b/route/routes/news.go index 14de52e..9d8df66 100644 --- a/route/routes/news.go +++ b/route/routes/news.go @@ -1,6 +1,10 @@ package routes import ( + "html/template" + "net/http" + "strconv" + "github.com/FChannel0/FChannel-Server/activitypub" "github.com/FChannel0/FChannel-Server/config" "github.com/FChannel0/FChannel-Server/db" @@ -11,9 +15,16 @@ import ( ) func NewsGet(ctx *fiber.Ctx) error { - timestamp := 0 + timestamp := ctx.Path()[6:] + ts, err := strconv.Atoi(timestamp) + + if err != nil { + ctx.Status(http.StatusForbidden) + return ctx.Render("404", fiber.Map{}) + } actor, err := activitypub.GetActorFromDB(config.Domain) + if err != nil { return util.MakeError(err, "NewsGet") } @@ -30,13 +41,17 @@ func NewsGet(ctx *fiber.Ctx) error { data.Board.Restricted = actor.Restricted data.NewsItems = make([]db.NewsItem, 1) - data.NewsItems[0], err = db.GetNewsItem(timestamp) + data.NewsItems[0], err = db.GetNewsItem(ts) if err != nil { return util.MakeError(err, "NewsGet") } data.Title = actor.PreferredUsername + ": " + data.NewsItems[0].Title + data.Meta.Description = data.PreferredUsername + " is a federated image board based on ActivityPub. The current version of the code running on the server is still a work-in-progress product, expect a bumpy ride for the time being. Get the server code here: https://git.fchannel.org." + data.Meta.Url = data.Board.Actor.Id + data.Meta.Title = data.Title + data.Themes = &config.Themes data.ThemeCookie = route.GetThemeCookie(ctx) @@ -62,22 +77,63 @@ func NewsGetAll(ctx *fiber.Ctx) error { data.Board.Restricted = actor.Restricted data.NewsItems, err = db.GetNews(0) + if err != nil { return util.MakeError(err, "NewsGetAll") } + data.Meta.Description = data.PreferredUsername + " is a federated image board based on ActivityPub. The current version of the code running on the server is still a work-in-progress product, expect a bumpy ride for the time being. Get the server code here: https://git.fchannel.org." + data.Meta.Url = data.Board.Actor.Id + data.Meta.Title = data.Title + data.Themes = &config.Themes data.ThemeCookie = route.GetThemeCookie(ctx) return ctx.Render("anews", fiber.Map{"page": data}, "layouts/main") } -// TODO routes/NewsPost -func NewsPost(c *fiber.Ctx) error { - return c.SendString("admin post news") +func NewsPost(ctx *fiber.Ctx) error { + actor, err := activitypub.GetActorFromDB(config.Domain) + + if err != nil { + return util.MakeError(err, "NewPost") + } + + if has := actor.HasValidation(ctx); !has { + return nil + } + + var newsitem db.NewsItem + + newsitem.Title = ctx.FormValue("title") + newsitem.Content = template.HTML(ctx.FormValue("summary")) + + if err := db.WriteNews(newsitem); err != nil { + return util.MakeError(err, "NewPost") + } + + return ctx.Redirect("/", http.StatusSeeOther) } -// TODO routes/NewsDelete -func NewsDelete(c *fiber.Ctx) error { - return c.SendString("admin news delete") +func NewsDelete(ctx *fiber.Ctx) error { + actor, err := activitypub.GetActorFromDB(config.Domain) + + if has := actor.HasValidation(ctx); !has { + return nil + } + + timestamp := ctx.Path()[13+len(config.Key):] + + tsint, err := strconv.Atoi(timestamp) + + if err != nil { + ctx.Status(http.StatusForbidden) + return ctx.Render("404", fiber.Map{}) + } + + if err := db.DeleteNewsItem(tsint); err != nil { + return util.MakeError(err, "NewsDelete") + } + + return ctx.Redirect("/news/", http.StatusSeeOther) } diff --git a/views/admin.html b/views/admin.html index 191444b..8c784b5 100644 --- a/views/admin.html +++ b/views/admin.html @@ -47,7 +47,7 @@ <div class="box2" style="margin-bottom: 25px; padding: 12px;"> <h3>Create News</h3> - <form id="news" action="/{{ .page.Key }}/postnews" method="post" enctype="application/x-www-form-urlencoded"> + <form id="news" action="/{{ .page.Key }}/newspost" method="post" enctype="application/x-www-form-urlencoded"> <label>Title:</label><br> <input type="text" name="title" placeholder="New Board" required><input type="submit" value="Post"><br> <label>Content:</label><br> diff --git a/views/anews.html b/views/anews.html new file mode 100644 index 0000000..69378d4 --- /dev/null +++ b/views/anews.html @@ -0,0 +1,15 @@ +<div style="text-align: center; max-width: 800px; margin: 0 auto;"> + <h1>{{ .page.Title }}</h1> + + <div class="newsbox" style="margin-top:50px;padding-top:0;"> + {{ $page := .page }} + {{ range $i, $e := .page.NewsItems }} + <div class="newsbox-news"> + <h3><a href="/news/{{.Time}}">{{unixtoreadable $e.Time}} - {{$e.Title}}</a>{{ if $page.Board.ModCred }} <a href="/{{ $page.Key }}/newsdelete/{{ $e.Time }}">[Delete] </a>{{end}}</h3> + <br> + + <p>{{$e.Content}}</p> + </div> + {{ end }} + </div> +</div> diff --git a/views/index.html b/views/index.html index 2bfed28..00ebfb9 100644 --- a/views/index.html +++ b/views/index.html @@ -28,9 +28,10 @@ {{ if .page.NewsItems }} <div class="newsbox" style="margin-top:50px;"> <h2><a href="/news">{{ .page.PreferredUsername }} News</a></h2> + {{ $page := .page }} {{ range $i, $e := .page.NewsItems }} <div class="newsbox-news"> - <h3><a href="/news/{{.Time}}">{{unixtoreadable $e.Time}} - {{$e.Title}}</a>{{ if $.Board.ModCred }} <a href="/{{ $.Key }}/newsdelete/{{ $e.Time }}">[Delete] </a>{{end}}</h3> + <h3><a href="/news/{{.Time}}">{{unixtoreadable $e.Time}} - {{$e.Title}}</a>{{ if $page.Board.ModCred }} <a href="/{{ $page.Key }}/newsdelete/{{ $e.Time }}">[Delete] </a>{{end}}</h3> <br> <p>{{$e.Content}}</p> diff --git a/views/news.html b/views/news.html new file mode 100644 index 0000000..87c40d2 --- /dev/null +++ b/views/news.html @@ -0,0 +1,7 @@ +<div class="newsbox" style="text-align: left; max-width: 800px; margin: 0 auto;margin-top: 50px;padding-top:0;"> + {{ range .page.NewsItems }} + <div class="newsbox-news"> + <p><h1>{{unixtoreadable .Time}} - {{.Title}}</h1><br>{{.Content}}</p> + </div> + {{ end }} +</div> |