-
Notifications
You must be signed in to change notification settings - Fork 0
/
reply.go
219 lines (168 loc) · 4.07 KB
/
reply.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package models
import (
"errors"
"html"
"github.com/microcosm-cc/bluemonday"
"github.com/eirka/eirka-libs/config"
"github.com/eirka/eirka-libs/db"
e "github.com/eirka/eirka-libs/errors"
"github.com/eirka/eirka-libs/validate"
)
// ReplyModel holds the request input
type ReplyModel struct {
UID uint
Ib uint
Thread uint
IP string
Comment string
Filename string
Thumbnail string
MD5 string
SHA string
OrigWidth int
OrigHeight int
ThumbWidth int
ThumbHeight int
Image bool
}
// IsValid will check struct validity
func (m *ReplyModel) IsValid() bool {
if m.UID == 0 {
return false
}
if m.Ib == 0 {
return false
}
if m.Thread == 0 {
return false
}
if m.IP == "" {
return false
}
if !m.Image && m.Comment == "" {
return false
}
if m.Image {
if m.Filename == "" {
return false
}
if m.Thumbnail == "" {
return false
}
if m.MD5 == "" {
return false
}
if m.OrigWidth == 0 {
return false
}
if m.OrigHeight == 0 {
return false
}
if m.ThumbWidth == 0 {
return false
}
if m.ThumbHeight == 0 {
return false
}
}
return true
}
// ValidateInput will make sure all the parameters are valid
func (m *ReplyModel) ValidateInput() (err error) {
// Initialize bluemonday
p := bluemonday.StrictPolicy()
// sanitize for html and xss
m.Comment = html.UnescapeString(p.Sanitize(m.Comment))
// There must either be a comment, an image, or an image with a comment
// If theres no image a comment is required
comment := validate.Validate{Input: m.Comment, Max: config.Settings.Limits.CommentMaxLength, Min: config.Settings.Limits.CommentMinLength}
// if there is no image check the comment
if !m.Image {
if comment.IsEmpty() {
return e.ErrNoComment
} else if comment.MinLength() {
return e.ErrCommentShort
} else if comment.MaxLength() {
return e.ErrCommentLong
}
}
// If theres an image and a comment validate comment
if m.Image && !comment.IsEmpty() {
if comment.MinLength() {
return e.ErrCommentShort
} else if comment.MaxLength() {
return e.ErrCommentLong
}
}
return
}
// Status will return info about the thread
func (m *ReplyModel) Status() (err error) {
// Get Database handle
dbase, err := db.GetDb()
if err != nil {
return
}
var closed bool
var total uint
// Check if thread is closed and get the total amount of posts
err = dbase.QueryRow(`SELECT ib_id,thread_closed,count(post_num) FROM threads
INNER JOIN posts on threads.thread_id = posts.thread_id
WHERE threads.thread_id = ? AND post_deleted != 1`, m.Thread).Scan(&m.Ib, &closed, &total)
if err != nil {
return
}
// Error if thread is closed
if closed {
return e.ErrThreadClosed
}
// Close thread if above max posts
if total > config.Settings.Limits.PostsMax {
_, err = dbase.Exec("UPDATE threads SET thread_closed=1 WHERE thread_id = ?",
m.Thread)
if err != nil {
return err
}
return e.ErrThreadClosed
}
return
}
// Post will add the reply to the database with a transaction
func (m *ReplyModel) Post() (err error) {
// check model validity
if !m.IsValid() {
return errors.New("ReplyModel is not valid")
}
// Get transaction handle
tx, err := db.GetTransaction()
if err != nil {
return
}
defer tx.Rollback()
// insert new post
e1, err := tx.Exec(`INSERT INTO posts (thread_id,user_id,post_num,post_time,post_ip,post_text)
SELECT ?,?,max(post_num)+1,NOW(),?,?
FROM posts WHERE thread_id = ?`, m.Thread, m.UID, m.IP, m.Comment, m.Thread)
if err != nil {
return
}
if m.Image {
var pID int64
pID, err = e1.LastInsertId()
if err != nil {
return err
}
// insert image if there is one
_, err = tx.Exec("INSERT INTO images (post_id,image_file,image_thumbnail,image_hash,image_sha,image_orig_height,image_orig_width,image_tn_height,image_tn_width) VALUES (?,?,?,?,?,?,?,?,?)",
pID, m.Filename, m.Thumbnail, m.MD5, m.SHA, m.OrigHeight, m.OrigWidth, m.ThumbHeight, m.ThumbWidth)
if err != nil {
return err
}
}
// Commit transaction
err = tx.Commit()
if err != nil {
return
}
return
}