/
note.go
367 lines (322 loc) · 9 KB
/
note.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main
import (
"database/sql"
"fmt"
"github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
"net/http"
"regexp"
"strconv"
"strings"
"sort"
)
const MaximumNotesReturned = 100
type Note struct {
NoteId int64
UserId int64
Text string
Link sql.NullString
LinkType sql.NullString
Date mysql.NullTime
Edited bool
Deleted bool
GroupId int64
}
// TODO: use Marshaler interface
func (n *Note) AsMap() *map[string]interface{} {
m := map[string]interface{}{
"NoteId": n.NoteId,
"UserId": n.UserId,
"Text": n.Text,
"Date": n.Date.Time.Unix(),
"Edited": n.Edited,
"GroupId": n.GroupId,
}
if n.Link.Valid {
m["Link"] = n.Link.String
}
if n.LinkType.Valid {
m["LinkType"] = n.LinkType.String
}
return &m
}
// interface for sorting strings by length
type ByLength []string
func (s ByLength) Len() int {
return len(s)
}
func (s ByLength) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByLength) Less(i, j int) bool {
return len(s[i]) < len(s[j])
}
func parseNote(text string) (note *Note) {
note = new(Note)
note.Text = text
// TODO: find @mentions
// atrx := MustCompile("@([[:alnum:]]{1,16})@((?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]*)?[a-zA-Z0-9])[.])*(?:[a-zA-Z][-a-zA-Z0-9]*[a-zA-Z0-9]|[a-zA-Z]))")
// TODO: to be shortened, @mentions must be users that exist and are not blocking this user
// TODO: defer that process so we can return to the user right away and return 202 Accepted
// TODO: we will replace @mentions with @<number>
// TODO: find the highest @<number> that doesn't collide with existing text
// clients will substitute mentions for highest @<numbers> in reverse order
// find all things that look like links
linkrx := regexp.MustCompile("\\b(?i:https?|ftp)://\\S+")
matches := linkrx.FindAllString(note.Text, -1)
//fmt.Println(matches)
if matches != nil {
// find the longest link
sort.Sort(ByLength(matches))
note.Link.String = matches[len(matches) - 1]
note.Link.Valid = true
// ‡<number> indicates location to insert link
dagIdx := 0
// if for some strange reason someone actually typed that, we search for the highest non-colliding index
dagrx := regexp.MustCompile("‡(\\d+)")
daggers := dagrx.FindAllStringSubmatch(note.Text, -1)
if daggers != nil {
for _, dagger := range daggers {
d2, _ := strconv.Atoi(dagger[1])
if d2 >= dagIdx {
dagIdx = d2 + 1
}
}
}
// replace the link with our symbol
// clients can later replace ‡<highest number> with the link
dagger := fmt.Sprintf("‡%d", dagIdx)
note.Text = strings.Replace(note.Text, note.Link.String, dagger, 1)
}
if len(note.Text) > 140 || len(note.Text) == 0 {
// TODO: return error?
return nil
}
return note
}
// first call r.ParseForm()
func validIntFormValue(r *http.Request, fieldName string, defaultValue int) int {
stringVal := r.PostFormValue(fieldName)
if len(stringVal) == 0 {
return defaultValue
}
intVal, err := strconv.Atoi(stringVal)
if err != nil {
// TODO: or should we 400 on malformed ints?
return defaultValue
}
return intVal
}
func ListNotesHandler(rw http.ResponseWriter, r *http.Request) {
token, err := FetchToken(db, r)
if err != nil {
fmt.Println(err)
sendError(rw, http.StatusInternalServerError, err.Error())
return
}
if token == nil {
fmt.Println(err)
sendError(rw, http.StatusUnauthorized, "Unauthorized")
return
}
// TODO: guest authorization and user id parm
r.ParseForm()
where := " WHERE UserId = ?"
sinceId := validIntFormValue(r, "since_id", 0)
if sinceId > 0 {
where += " AND NoteId > " + strconv.Itoa(sinceId)
}
sinceDate := validIntFormValue(r, "since_date", 0)
if sinceDate > 0 {
where += " AND Date > FROM_UNIXTIME(" + strconv.Itoa(sinceDate) + ")"
}
beforeId := validIntFormValue(r, "before_id", 0)
if beforeId > 0 {
where += " AND NoteId < " + strconv.Itoa(beforeId)
}
beforeDate := validIntFormValue(r, "before_date", 0)
if beforeDate > 0 {
where += " AND Date < FROM_UNIXTIME(" + strconv.Itoa(beforeDate) + ")"
}
count := validIntFormValue(r, "count", MaximumNotesReturned)
if count > MaximumNotesReturned {
count = MaximumNotesReturned
}
limit := " LIMIT " + strconv.Itoa(count)
notes := []Note{}
err = db.Select(¬es, "SELECT * FROM Note" + where + limit, token.UserId)
if err != nil {
fmt.Println(err)
sendError(rw, http.StatusInternalServerError, err.Error())
return
}
notes2 := []interface{}{}
for _, note := range notes {
notes2 = append(notes2, note.AsMap())
}
sendData(rw, http.StatusOK, notes2)
}
func PostNoteHandler(rw http.ResponseWriter, r *http.Request) {
// TODO: we should make an auth middleware, once I can wrap my head around that
token, err := FetchToken(db, r)
if err != nil {
fmt.Println(err)
sendError(rw, http.StatusInternalServerError, err.Error())
return
}
if token == nil {
fmt.Println(err)
sendError(rw, http.StatusUnauthorized, "Unauthorized")
return
}
r.ParseForm()
noteText := r.PostFormValue("note")
note := parseNote(noteText)
if note == nil {
sendError(rw, http.StatusBadRequest, "Bad Request")
return
}
// TODO: groups
group := r.PostFormValue("group")
if len(group) > 0 {
groupId, _ := strconv.Atoi(group)
note.GroupId = int64(groupId)
}
// TODO: defer processing mentions
note.UserId = token.UserId
result, err := db.NamedExec("INSERT INTO `Note` (`UserId`, `Text`, `Link`, `LinkType`, `GroupId`) " +
"VALUES (:UserId, :Text, :Link, :LinkType, :GroupId)", note)
if err != nil {
fmt.Println(err)
sendError(rw, http.StatusInternalServerError, err.Error())
return
}
note.NoteId, err = result.LastInsertId()
if err != nil {
fmt.Println(err)
}
sendData(rw, http.StatusCreated, note.AsMap())
}
func GetNoteHandler(rw http.ResponseWriter, r *http.Request) {
token, err := FetchToken(db, r)
if err != nil {
fmt.Println(err)
sendError(rw, http.StatusInternalServerError, err.Error())
return
}
if token == nil {
fmt.Println(err)
sendError(rw, http.StatusUnauthorized, "Unauthorized")
return
}
noteId, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
fmt.Println(err)
sendError(rw, http.StatusBadRequest, err.Error())
return
}
// TODO: guest authorization
note := new(Note)
err = db.Get(note, "SELECT * FROM Note WHERE NoteId = ?", noteId)
if err == sql.ErrNoRows {
sendError(rw, http.StatusNotFound, "There is no note with that ID.")
return
} else if err != nil {
fmt.Println(err)
sendError(rw, http.StatusInternalServerError, err.Error())
return
}
sendData(rw, http.StatusOK, note.AsMap())
}
func PutNoteHandler(rw http.ResponseWriter, r *http.Request) {
token, err := FetchToken(db, r)
if err != nil {
fmt.Println(err)
sendError(rw, http.StatusInternalServerError, err.Error())
return
}
if token == nil {
fmt.Println(err)
sendError(rw, http.StatusUnauthorized, "Unauthorized")
return
}
noteId, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
fmt.Println(err)
sendError(rw, http.StatusBadRequest, err.Error())
return
}
note := new(Note)
err = db.Get(note, "SELECT * FROM Note WHERE NoteId = ?", noteId)
if err == sql.ErrNoRows {
sendError(rw, http.StatusNotFound, "There is no note with that ID.")
return
} else if err != nil {
fmt.Println(err)
sendError(rw, http.StatusInternalServerError, err.Error())
return
} else if note.UserId != token.UserId {
sendError(rw, http.StatusUnauthorized, "Only the note's author may edit it.")
return
}
r.ParseForm()
noteText := r.PostFormValue("note")
note2 := parseNote(noteText)
if note2 == nil {
sendError(rw, http.StatusBadRequest, "Bad Request")
return
}
note.Text = note2.Text
note.Link = note2.Link
note.Edited = true
_, err = db.NamedExec("UPDATE Note SET Text = :Text, Link = :Link, Edited = 1 "+
"WHERE NoteId = :NoteId", note)
if err != nil {
fmt.Println(err)
sendError(rw, http.StatusInternalServerError, err.Error())
return
}
sendData(rw, http.StatusOK, note.AsMap())
}
func DeleteNoteHandler(rw http.ResponseWriter, r *http.Request) {
// TODO: auth middleware
token, err := FetchToken(db, r)
if err != nil {
fmt.Println(err)
sendError(rw, http.StatusInternalServerError, err.Error())
return
}
if token == nil {
fmt.Println(err)
sendError(rw, http.StatusUnauthorized, "Unauthorized")
return
}
// TODO: noteId parm middleware
noteId, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
fmt.Println(err)
sendError(rw, http.StatusBadRequest, err.Error())
return
}
// TODO: owner auth middleware
var noteUser int64
err = db.Get(¬eUser, "SELECT UserId FROM Note WHERE NoteId = ?", noteId)
if err == sql.ErrNoRows {
sendError(rw, http.StatusNotFound, "There is no note with that ID.")
return
} else if err != nil {
fmt.Println(err)
sendError(rw, http.StatusInternalServerError, err.Error())
return
} else if noteUser != token.UserId {
sendError(rw, http.StatusUnauthorized, "Only the note's author may delete it.")
return
}
_, err = db.Exec("DELETE FROM `Note` WHERE NoteId = ?", noteId)
if err != nil {
fmt.Println(err)
sendError(rw, http.StatusInternalServerError, err.Error())
return
}
sendData(rw, http.StatusNoContent, "")
}