summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVitaly Minko <vitaly.minko@gmail.com>2019-06-13 07:03:30 +0300
committerVitaly Minko <vitaly.minko@gmail.com>2019-06-13 07:03:30 +0300
commit8b4ecffbec8eae14416b385907a16f7dc6928b6d (patch)
tree6bb13f3380c63a7c2c950df514648cd1d50ddf2c
parentda85da2c61b13ed98a56889b23ce51e4d6623e18 (diff)
Finished implementing handlers for the profile page.
-rw-r--r--cmd/dscuss-cli/main.go2
-rw-r--r--cmd/dscuss-web/controller/profile_handlers.go68
-rw-r--r--cmd/dscuss-web/main.go2
-rw-r--r--cmd/dscuss-web/view/profile.go2
4 files changed, 72 insertions, 2 deletions
diff --git a/cmd/dscuss-cli/main.go b/cmd/dscuss-cli/main.go
index 8c14419..948f59e 100644
--- a/cmd/dscuss-cli/main.go
+++ b/cmd/dscuss-cli/main.go
@@ -566,7 +566,7 @@ func doSubscribe(c *ishell.Context) {
}
err = loginHandle.Subscribe(topic)
if err != nil {
- c.Printf("Error subscribing to %s: %v\n", topic, err.Error())
+ c.Printf("Error subscribing to %s: %v\n", topic, err)
return
}
c.Println("In order to apply changes you need to logout and login back again.")
diff --git a/cmd/dscuss-web/controller/profile_handlers.go b/cmd/dscuss-web/controller/profile_handlers.go
index 0a4bcbe..c873678 100644
--- a/cmd/dscuss-web/controller/profile_handlers.go
+++ b/cmd/dscuss-web/controller/profile_handlers.go
@@ -23,6 +23,7 @@ import (
"vminko.org/dscuss/cmd/dscuss-web/view"
"vminko.org/dscuss/entity"
"vminko.org/dscuss/errors"
+ "vminko.org/dscuss/subs"
)
func profileHandler(w http.ResponseWriter, r *http.Request, l *dscuss.LoginHandle, s *Session) {
@@ -120,6 +121,65 @@ func removeModeratorHandler(w http.ResponseWriter, r *http.Request, l *dscuss.Lo
http.Redirect(w, r, "/profile", http.StatusSeeOther)
}
+func subscribeHandler(w http.ResponseWriter, r *http.Request, l *dscuss.LoginHandle, s *Session) {
+ if len(r.URL.Query()) != 0 {
+ BadRequestHandler(w, r, "Wrong number of query parameters")
+ return
+ }
+ if !s.IsAuthenticated {
+ ForbiddenHandler(w, r)
+ return
+ }
+ topicStr := r.FormValue("topic")
+ topicStr, err := url.QueryUnescape(topicStr)
+ if err != nil {
+ BadRequestHandler(w, r, topicStr+" is not a valid URL-encoded string.")
+ return
+ }
+ topic, err := subs.NewTopic(topicStr)
+ if err != nil {
+ BadRequestHandler(w, r, topicStr+" is not a valid topic string.")
+ return
+ }
+ err = l.Subscribe(topic)
+ if err == errors.AlreadySubscribed {
+ BadRequestHandler(w, r, "Can't add topic '"+topicStr+"': "+err.Error())
+ } else if err != nil {
+ panic("Error subscribing to '" + topicStr + "': " + err.Error())
+ }
+ //c.Println("In order to apply changes you need to restart the backend.")
+ http.Redirect(w, r, "/profile", http.StatusSeeOther)
+}
+
+func unsubscribeHandler(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
+ }
+ topicStr := r.URL.Query().Get("topic")
+ topic, err := subs.NewTopic(topicStr)
+ if topic == nil {
+ BadRequestHandler(w, r, "Empty topic string is not acceptable.")
+ return
+ }
+ if err != nil {
+ BadRequestHandler(w, r, topicStr+" is not a valid URL-encoded string.")
+ return
+ }
+ err = l.Unsubscribe(topic)
+ if err == errors.AlreadySubscribed {
+ BadRequestHandler(w, r, "Can't add topic '"+topicStr+"': "+err.Error())
+ } else if err != nil {
+ panic("Failed to unsubscribe from '" + topicStr + "': " + err.Error())
+ }
+ //c.Println("In order to apply changes you need to restart the backend.")
+ http.Redirect(w, r, "/profile", http.StatusSeeOther)
+}
+
func MakeProfileHandler(l *dscuss.LoginHandle) http.HandlerFunc {
return makeHandler(profileHandler, l)
}
@@ -131,3 +191,11 @@ func MakeAddModeratorHandler(l *dscuss.LoginHandle) http.HandlerFunc {
func MakeRemoveModeratorHandler(l *dscuss.LoginHandle) http.HandlerFunc {
return makeHandler(removeModeratorHandler, l)
}
+
+func MakeSubscribeHandler(l *dscuss.LoginHandle) http.HandlerFunc {
+ return makeHandler(subscribeHandler, l)
+}
+
+func MakeUnsubscribeHandler(l *dscuss.LoginHandle) http.HandlerFunc {
+ return makeHandler(unsubscribeHandler, l)
+}
diff --git a/cmd/dscuss-web/main.go b/cmd/dscuss-web/main.go
index a14cf86..6e80bec 100644
--- a/cmd/dscuss-web/main.go
+++ b/cmd/dscuss-web/main.go
@@ -134,6 +134,8 @@ func main() {
http.HandleFunc("/profile", controller.MakeProfileHandler(loginHandle))
http.HandleFunc("/addmdr", controller.MakeAddModeratorHandler(loginHandle))
http.HandleFunc("/rmmdr", controller.MakeRemoveModeratorHandler(loginHandle))
+ http.HandleFunc("/sub", controller.MakeSubscribeHandler(loginHandle))
+ http.HandleFunc("/unsub", controller.MakeUnsubscribeHandler(loginHandle))
http.HandleFunc("/", controller.MakeRootHandler(loginHandle))
log.Debugf("Starting HTTP server on port %d\n", webPort)
diff --git a/cmd/dscuss-web/view/profile.go b/cmd/dscuss-web/view/profile.go
index 6854fa1..084e2e0 100644
--- a/cmd/dscuss-web/view/profile.go
+++ b/cmd/dscuss-web/view/profile.go
@@ -39,7 +39,7 @@ const profileHTML = `
{{ range .Subscriptions }}
<tr>
<td>{{ . }}</td>
- <td><a href="/unsub?id={{ . }}">Remove</a></td>
+ <td><a href="/unsub?topic={{ . }}">Remove</a></td>
</tr>
{{ end }}
<tr>