summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVitaly Minko <vitaly.minko@gmail.com>2019-06-06 18:43:42 +0300
committerVitaly Minko <vitaly.minko@gmail.com>2019-06-06 18:43:42 +0300
commita36fc89449e0284428f18eade1bc0efd9411ea3b (patch)
tree6d4ba51f315619d6c6c2e4e36410954d95b99e94
parentb356c316ffd0d3202ebb0c2c6ab92a1aacfbb26f (diff)
Implemented thread creation in the Web UI.
-rw-r--r--cmd/dscuss-web/controller/start_handler.go89
-rw-r--r--cmd/dscuss-web/main.go1
-rw-r--r--cmd/dscuss-web/view/board.go6
-rw-r--r--cmd/dscuss-web/view/reply.go16
-rw-r--r--cmd/dscuss-web/view/start.go39
-rw-r--r--cmd/dscuss-web/view/templates.go1
6 files changed, 144 insertions, 8 deletions
diff --git a/cmd/dscuss-web/controller/start_handler.go b/cmd/dscuss-web/controller/start_handler.go
new file mode 100644
index 0000000..9826c4c
--- /dev/null
+++ b/cmd/dscuss-web/controller/start_handler.go
@@ -0,0 +1,89 @@
+/*
+This file is part of Dscuss.
+Copyright (C) 2019 Vitaly Minko
+
+This program is free software: you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free Software
+Foundation, either version 3 of the License, or (at your option) any later
+version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along with
+this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+package controller
+
+import (
+ "net/http"
+ "net/url"
+ "vminko.org/dscuss"
+ "vminko.org/dscuss/cmd/dscuss-web/view"
+ "vminko.org/dscuss/entity"
+ "vminko.org/dscuss/subs"
+)
+
+func startHandler(w http.ResponseWriter, r *http.Request, l *dscuss.LoginHandle, s *Session) {
+ if len(r.URL.Query()) > 1 {
+ BadRequestHandler(w, r, "Wrong number of query parameters")
+ return
+ }
+ if !s.IsAuthenticated {
+ ForbiddenHandler(w, r)
+ return
+ }
+ topic := r.FormValue("topic")
+ if r.Method == "POST" {
+ topic, err := url.QueryUnescape(topic)
+ if err != nil {
+ BadRequestHandler(w, r, topic+" is not a valid URL-encoded string.")
+ return
+ }
+ }
+ var msg string
+ var subj string
+ var text string
+ if r.Method == "POST" {
+ t, err := subs.NewTopic(topic)
+ if err != nil {
+ msg = "Specified topic is unacceptable: " + err.Error()
+ goto render
+ }
+ subj = r.PostFormValue("subject")
+ text = r.PostFormValue("text")
+ if (subj == "") || (len(subj) > entity.MaxSubjectLen) {
+ msg = "Specified subject is unacceptable: empty or too long."
+ goto render
+ }
+ if (text == "") || (len(text) > entity.MaxTextLen) {
+ msg = "Specified message text is unacceptable: empty or too long."
+ goto render
+ }
+ thread, err := l.NewThread(subj, text, t)
+ if err != nil {
+ msg = "Error making new dscussion: " + err.Error() + "."
+ goto render
+ }
+ err = l.PostEntity((entity.Entity)(thread))
+ if err != nil {
+ msg = "Error posting new dscussion: " + err.Error() + "."
+ goto render
+ }
+ http.Redirect(w, r, "/board", http.StatusSeeOther)
+ return
+ }
+render:
+ view.Render(w, "start.html", map[string]interface{}{
+ "Common": readCommonData(r, s, l),
+ "Topic": topic,
+ "Subject": subj,
+ "Text": text,
+ "Message": msg,
+ })
+}
+
+func MakeStartHandler(l *dscuss.LoginHandle) http.HandlerFunc {
+ return makeHandler(startHandler, l)
+}
diff --git a/cmd/dscuss-web/main.go b/cmd/dscuss-web/main.go
index 617540a..bad5144 100644
--- a/cmd/dscuss-web/main.go
+++ b/cmd/dscuss-web/main.go
@@ -129,6 +129,7 @@ func main() {
http.HandleFunc("/logout", controller.LogoutHandler)
http.HandleFunc("/thread", controller.MakeThreadHandler(loginHandle))
http.HandleFunc("/reply", controller.MakeReplyHandler(loginHandle))
+ http.HandleFunc("/start", controller.MakeStartHandler(loginHandle))
http.HandleFunc("/", controller.MakeRootHandler(loginHandle))
log.Debugf("Starting HTTP server on port %d\n", webPort)
diff --git a/cmd/dscuss-web/view/board.go b/cmd/dscuss-web/view/board.go
index 38e42a4..bee7a6a 100644
--- a/cmd/dscuss-web/view/board.go
+++ b/cmd/dscuss-web/view/board.go
@@ -20,6 +20,12 @@ package view
const boardHTML = `
{{ define "content" }}
+{{ if .Common.IsWritingPermitted }}
+<div class="btn-row">
+ <a class="link-btn" href="/start{{ if $.Topic }}?topic={{ $.Topic }}{{ end }}">Start</a>
+</div>
+{{end}}
+
<h1> Dscussions in
{{ if .Topic }}
topic {{ .Topic }}
diff --git a/cmd/dscuss-web/view/reply.go b/cmd/dscuss-web/view/reply.go
index 6eb1335..46d6239 100644
--- a/cmd/dscuss-web/view/reply.go
+++ b/cmd/dscuss-web/view/reply.go
@@ -32,14 +32,14 @@ const replyHTML = `
</div>
<div class="row">
<form action="/reply" method="POST" enctype="multipart/form-data">
- <input type="hidden" name="csrf" value="{{ .Common.CSRF }}">
- <input type="hidden" name="id" value="{{ .Parent.ID }}">
- Subject: <input type="text" name="subject" value="{{ .Reply.Subject }}" placeholder="Re: {{.Parent.Subject}}">
- <textarea name="text" rows="12">{{ .Reply.Text }}</textarea>
- {{ if .Message }}
- <span class="alert">{{ .Message }}</span><br>
- {{ end }}
- <input type="submit" name="action" class="no-double-post" value="Submit reply">
+ <input type="hidden" name="csrf" value="{{ .Common.CSRF }}">
+ <input type="hidden" name="id" value="{{ .Parent.ID }}">
+ Subject: <input type="text" name="subject" value="{{ .Reply.Subject }}" placeholder="Re: {{.Parent.Subject}}">
+ <textarea name="text" rows="12">{{ .Reply.Text }}</textarea>
+ {{ if .Message }}
+ <span class="alert">{{ .Message }}</span><br>
+ {{ end }}
+ <input type="submit" name="action" class="no-double-post" value="Submit reply">
</form>
</div>
diff --git a/cmd/dscuss-web/view/start.go b/cmd/dscuss-web/view/start.go
new file mode 100644
index 0000000..a5eb9e3
--- /dev/null
+++ b/cmd/dscuss-web/view/start.go
@@ -0,0 +1,39 @@
+/*
+This file is part of Dscuss.
+Copyright (C) 2019 Vitaly Minko
+
+This program is free software: you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free Software
+Foundation, either version 3 of the License, or (at your option) any later
+version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+PARTICULAR PURPOSE. See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along with
+this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package view
+
+const startHTML = `
+{{ define "content" }}
+
+<h2 id="title">Start new dscussion</h2>
+<div class="row">
+ <form action="/start" method="POST" enctype="multipart/form-data">
+ <input type="hidden" name="csrf" value="{{ .Common.CSRF }}">
+ Topic: <input type="text" name="topic" value="{{ .Topic }}"><br>
+ Subject: <input type="text" name="subject" value="{{ .Subject }}">
+ <textarea name="text" rows="12">{{ .Text }}</textarea>
+ {{ if .Message }}
+ <span class="alert">{{ .Message }}</span><br>
+ {{ end }}
+ <input type="submit" name="action" class="no-double-post" value="Start dscussion">
+ </form>
+</div>
+
+{{ end }}`
+
+/* vim: set filetype=html: */
diff --git a/cmd/dscuss-web/view/templates.go b/cmd/dscuss-web/view/templates.go
index 14508d3..d62b486 100644
--- a/cmd/dscuss-web/view/templates.go
+++ b/cmd/dscuss-web/view/templates.go
@@ -40,6 +40,7 @@ func init() {
templates.Add(base, "board", boardHTML)
templates.Add(base, "thread", threadHTML)
templates.Add(base, "reply", replyHTML)
+ templates.Add(base, "start", startHTML)
}
func Render(w http.ResponseWriter, tmplName string, data interface{}) {