2024-02-27 14:10:27 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2024-03-05 17:13:59 +01:00
|
|
|
"git.streifling.com/jason/rss"
|
2024-03-01 11:30:31 +01:00
|
|
|
"github.com/google/uuid"
|
2024-02-27 14:10:27 +01:00
|
|
|
"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-02-27 14:10:27 +01:00
|
|
|
}
|
|
|
|
|
2024-03-03 13:56:49 +01:00
|
|
|
func FinishArticle(al *data.ArticleList, s *data.CookieStore) http.HandlerFunc {
|
2024-02-27 14:10:27 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-03-01 21:01:38 +01:00
|
|
|
article := new(data.Article)
|
2024-02-27 14:10:27 +01:00
|
|
|
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)
|
2024-02-27 14:10:27 +01:00
|
|
|
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-02-27 14:10:27 +01:00
|
|
|
|
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-02-27 14:10:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 17:13:59 +01:00
|
|
|
func PublishArticle(c *data.Channel, al *data.ArticleList, s *data.CookieStore) http.HandlerFunc {
|
2024-02-27 14:10:27 +01:00
|
|
|
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)
|
2024-02-27 14:10:27 +01:00
|
|
|
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)
|
2024-02-27 14:10:27 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-03-05 17:13:59 +01:00
|
|
|
c.Add(&rss.Item{
|
2024-02-27 14:10:27 +01:00
|
|
|
Title: article.Title,
|
2024-03-05 17:13:59 +01:00
|
|
|
Author: session.Values["name"].(string),
|
|
|
|
PubDate: article.Created.Format(time.RFC1123Z),
|
2024-02-27 14:10:27 +01:00
|
|
|
Description: article.Desc,
|
2024-03-05 17:13:59 +01:00
|
|
|
Content: &rss.Content{Value: article.Content},
|
2024-03-05 18:20:34 +01:00
|
|
|
Categories: article.Tags,
|
2024-02-27 14:10:27 +01:00
|
|
|
})
|
2024-03-05 17:13:59 +01:00
|
|
|
c.Save("tmp/rss.gob")
|
2024-02-27 14:10:27 +01:00
|
|
|
|
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-02-27 14:10:27 +01:00
|
|
|
}
|
|
|
|
}
|