-
Notifications
You must be signed in to change notification settings - Fork 1
/
post.go
125 lines (115 loc) · 3.6 KB
/
post.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2015 The Warren Authors
// Use of this source code is governed by an MIT license that can be found in
// the LICENSE file.
package handlers
import (
"fmt"
"html/template"
"log"
"net/http"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
"github.com/warren-community/warren/models"
)
// Display a single post.
func (h *Handlers) DisplayPost(w http.ResponseWriter, r *http.Request, l *log.Logger, params martini.Params, render render.Render) {
entity, err := models.GetEntity(params["entityId"], h.db)
if err != nil {
l.Printf("Could not fetch entity: %+v\n", err)
h.InternalServerError(w, r, render)
return
}
if entity.Id.Hex() == "" {
h.NotFound(w, r, render)
return
}
render.HTML(http.StatusOK, "post/displayPost", map[string]interface{}{
"Title": entity.Title,
"User": h.user,
"Flashes": h.flashes(r, w),
"CSRF": h.session.Values["_csrf_token"],
"Entity": entity,
"IsOwner": entity.BelongsToUser(h.user.Model),
"Content": template.HTML(entity.RenderedContent),
"Tags": entity.Tags,
})
}
// Remove a post from the database.
func (h *Handlers) DeletePost(w http.ResponseWriter, r *http.Request, render render.Render, l *log.Logger) {
if !h.user.IsAuthenticated {
h.session.AddFlash(NewFlash("Please log in to continue", "warning"))
h.session.Save(r, w)
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
entity, err := models.GetEntity(r.FormValue("entityId"), h.db)
if err != nil {
l.Printf("Could not fetch entity: %+v\n", err)
h.InternalServerError(w, r, render)
return
}
if entity.Id.Hex() == "" {
h.NotFound(w, r, render)
return
}
if !entity.BelongsToUser(h.user.Model) {
h.Forbidden(w, r, render)
return
}
err = entity.Delete(h.db)
if err != nil {
l.Printf("Could not delete entity: %+v\n", err)
h.InternalServerError(w, r, render)
return
}
h.session.AddFlash(NewFlash("Post deleted!", "success"))
h.session.Save(r, w)
http.Redirect(w, r, fmt.Sprintf("/~%s", h.user.Model.Username), http.StatusSeeOther)
}
func (h *Handlers) DisplaySharePost(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not implemented", http.StatusNotImplemented)
}
// Share a post with one's followers.
func (h *Handlers) SharePost(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not implemented", http.StatusNotImplemented)
}
// Display the form for creating a post.
func (h *Handlers) DisplayCreatePost(w http.ResponseWriter, r *http.Request, render render.Render) {
if !h.user.IsAuthenticated {
h.session.AddFlash(NewFlash("Please log in to continue", "warning"))
h.session.Save(r, w)
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
render.HTML(http.StatusOK, "post/displayCreatePost", map[string]interface{}{
"Title": "Create post",
"User": h.user,
"Flashes": h.flashes(r, w),
"CSRF": h.session.Values["_csrf_token"],
})
}
// Create a post in the database.
func (h *Handlers) CreatePost(w http.ResponseWriter, r *http.Request, l *log.Logger) {
if !h.user.IsAuthenticated {
h.session.AddFlash(NewFlash("Please log in to continue", "warning"))
h.session.Save(r, w)
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
entity := models.NewEntity(
r.FormValue("contentType"),
h.user.Model.Username,
h.user.Model.Username,
false, // Shared?
r.FormValue("title"),
models.TagStringToTags(r.FormValue("tags")),
r.FormValue("content"),
)
err := entity.Save(h.db, h.esConn)
if err != nil {
l.Print(err.Error())
http.Error(w, "Could not save post", http.StatusInternalServerError)
return
}
http.Redirect(w, r, fmt.Sprintf("/%s", entity.Id.Hex()), http.StatusSeeOther)
}