cpolis/cmd/ui/articles.go

162 lines
4.9 KiB
Go
Raw Normal View History

package ui
import (
"html/template"
"log"
"net/http"
"time"
"git.streifling.com/jason/rss"
2024-03-01 11:30:31 +01:00
"github.com/google/uuid"
"streifling.com/jason/cpolis/cmd/data"
)
2024-03-03 09:16:49 +01:00
func ShowHub(s *data.CookieStore) http.HandlerFunc {
2024-03-01 11:30:31 +01:00
return func(w http.ResponseWriter, r *http.Request) {
2024-03-03 09:16:49 +01:00
session, err := s.Get(r, "cookie")
if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
}
2024-03-02 09:09:55 +01:00
tmpl, err := template.ParseFiles("web/templates/hub.html")
2024-03-03 09:16:49 +01:00
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", session.Values["role"])
2024-03-01 11:30:31 +01:00
}
}
2024-03-03 13:56:49 +01:00
func WriteArticle(tl *data.TagList) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles("web/templates/editor.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", tl.Get())
}
}
2024-03-03 13:56:49 +01:00
func FinishArticle(al *data.ArticleList, s *data.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2024-03-01 21:01:38 +01:00
article := new(data.Article)
var err error
article.Title, err = data.ConvertToPlain(r.PostFormValue("editor-title"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
article.Desc, err = data.ConvertToPlain(r.PostFormValue("editor-desc"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
article.Content, err = data.ConvertToHTML(r.PostFormValue("editor-text"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-03-05 18:20:34 +01:00
r.ParseForm()
article.Tags = append(article.Tags, r.Form["tags"]...)
2024-03-03 09:16:49 +01:00
session, err := s.Get(r, "cookie")
if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
}
2024-03-01 11:30:31 +01:00
article.UUID = uuid.New()
2024-03-03 09:16:49 +01:00
article.Author = session.Values["name"].(string)
article.Created = time.Now()
2024-03-03 09:16:49 +01:00
article.AuthorID = session.Values["id"].(int64)
2024-03-01 11:30:31 +01:00
2024-03-03 13:56:49 +01:00
al.Add(article)
2024-03-05 18:20:34 +01:00
al.Save("tmp/unpublished-articles.gob")
2024-03-02 09:09:55 +01:00
tmpl, err := template.ParseFiles("web/templates/hub.html")
2024-03-03 09:16:49 +01:00
tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
}
}
2024-03-03 13:56:49 +01:00
func ShowUnpublishedArticles(al *data.ArticleList) http.HandlerFunc {
2024-03-01 11:30:31 +01:00
return func(w http.ResponseWriter, r *http.Request) {
2024-03-02 09:09:55 +01:00
tmpl, err := template.ParseFiles("web/templates/unpublished-articles.html")
2024-03-03 13:56:49 +01:00
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", al.Get())
2024-03-01 11:30:31 +01:00
}
}
2024-03-03 13:56:49 +01:00
func ReviewArticle(al *data.ArticleList, s *data.CookieStore) http.HandlerFunc {
2024-03-01 11:30:31 +01:00
return func(w http.ResponseWriter, r *http.Request) {
uuid, err := uuid.Parse(r.PostFormValue("uuid"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-03-03 13:56:49 +01:00
for _, article := range al.Get() {
2024-03-01 11:30:31 +01:00
if article.UUID == uuid {
2024-03-02 09:09:55 +01:00
tmpl, err := template.ParseFiles("web/templates/to-be-published.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", article)
2024-03-01 11:30:31 +01:00
return
}
}
2024-03-03 09:16:49 +01:00
session, err := s.Get(r, "cookie")
if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
}
2024-03-02 09:09:55 +01:00
tmpl, err := template.ParseFiles("web/templates/hub.html")
2024-03-03 09:16:49 +01:00
tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
2024-03-01 11:30:31 +01:00
}
}
func PublishArticle(c *data.Channel, al *data.ArticleList, s *data.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2024-03-01 11:30:31 +01:00
uuid, err := uuid.Parse(r.PostFormValue("uuid"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-03-03 13:56:49 +01:00
article, ok := al.Release(uuid)
if !ok {
// TODO: Warnung anzeigen
2024-03-01 11:30:31 +01:00
// msg = "Alle Felder müssen ausgefüllt werden."
2024-03-02 09:09:55 +01:00
// tmpl, err := template.ParseFiles("web/templates/add-user.html")
// template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
return
}
2024-03-03 13:56:49 +01:00
session, err := s.Get(r, "cookie")
if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
}
c.Add(&rss.Item{
Title: article.Title,
Author: session.Values["name"].(string),
PubDate: article.Created.Format(time.RFC1123Z),
Description: article.Desc,
Content: &rss.Content{Value: article.Content},
2024-03-05 18:20:34 +01:00
Categories: article.Tags,
})
c.Save("tmp/rss.gob")
2024-03-02 09:09:55 +01:00
tmpl, err := template.ParseFiles("web/templates/hub.html")
2024-03-03 09:16:49 +01:00
tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
}
}