/
post_message.go
197 lines (171 loc) · 5.88 KB
/
post_message.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
// Copyright Banrai LLC. All rights reserved. Use of this source code is
// governed by the license that can be found in the LICENSE file.
package ui
import (
"database/sql"
"github.com/Banrai/TeamWork.io/server/database"
"log"
"net/http"
"strings"
)
type Recipient struct {
Person *database.PERSON
Keys []*database.PUBLIC_KEY
}
type NewPostPage struct {
Title string
Alert *Alert
Session *database.SESSION
Person *database.PERSON
Keys []*database.PUBLIC_KEY
Recipients []*Recipient
}
func PostMessage(w http.ResponseWriter, r *http.Request, db database.DBConnection, opts ...interface{}) {
var (
s *database.SESSION
p *database.PERSON
k []*database.PUBLIC_KEY
x []*Recipient
d []*database.MESSAGE_DIGEST
)
alert := new(Alert)
alert.Message = "You need to <a href=\"/help.html#decrypt-session\">login here with your own email address</a> to be able to post a new message. If you have already decrypted a session code, you can <a href=\"/confirm\">login with it here</a>."
messagePosted := false
values := r.URL.Query()
if "POST" == r.Method {
r.ParseForm()
sessionCode, sessionCodeExists := r.PostForm["session"]
personCode, personCodeExists := r.PostForm["person"]
if sessionCodeExists && personCodeExists {
sessionId := strings.Join(sessionCode, "")
personId := strings.Join(personCode, "")
if len(sessionId) > 0 && len(personId) > 0 {
fn := func(stmt map[string]*sql.Stmt) {
// make sure the session is valid
session, sessionErr := ConfirmSessionCode(sessionId, stmt[database.SESSION_CLEANUP], stmt[database.SESSION_LOOKUP_BY_ID])
if sessionErr != nil {
alert.AsError(OTHER_ERROR)
return
}
if len(session.Id) == 0 {
alert.AsError(INVALID_SESSION)
return
}
// attempt to find the person for this session
person, personErr := database.LookupPerson(stmt[database.PERSON_LOOKUP_BY_ID], session.PersonId)
if personErr != nil {
alert.AsError(OTHER_ERROR)
return
}
if len(person.Id) == 0 {
alert.AsError(UNKNOWN)
return
}
if !person.Enabled {
alert.AsError(DISABLED)
return
}
// make sure the session matches the person from the form
if person.Id != personId {
alert.AsError(INVALID_SESSION)
return
}
keys, keysErr := person.LookupPublicKeys(stmt[database.PK_LOOKUP])
if keysErr != nil {
alert.AsError(OTHER_ERROR)
return
}
if len(keys) == 0 {
alert.AsError(NO_KEYS)
return
}
// session, person, and keys are valid
s = session
p = person
k = keys
// see if there are any predefined recipients in the get string
preRecipients := make([]*Recipient, 0)
if recips, recipsExist := values["recipient"]; recipsExist {
for _, recipId := range recips {
if recipId != person.Id {
recip, recipErr := database.LookupPerson(stmt[database.PERSON_LOOKUP_BY_ID], recipId)
if recipErr == nil { // skip any invalid recipients
if len(recip.Id) > 0 && recip.Enabled {
rKeys, rKeyErr := recip.LookupPublicKeys(stmt[database.PK_LOOKUP])
if rKeyErr == nil { // skip any people with invalid keys
recipient := &Recipient{Person: recip, Keys: rKeys}
preRecipients = append(preRecipients, recipient)
}
}
}
}
}
}
x = preRecipients
// see if there is a message to post
messageData, messageDataExists := r.PostForm["message"]
if !messageDataExists {
alert.Message = "Please write a message and hit 'Post' (encryption is optional)"
return
}
// post the message to the database
message := new(database.MESSAGE)
message.Message = strings.Join(messageData, "")
message.PersonId = person.Id
msgId, msgIdErr := message.Add(stmt[database.MESSAGE_INSERT], MESSAGE_DURATION)
if msgIdErr != nil {
alert.AsError("Your message could not be posted at this time")
return
}
message.Id = msgId
// add the list of recipients to the message
recipientList, recipientListExists := r.PostForm["recipients"]
if recipientListExists {
people := make([]*database.PERSON, 0)
for _, recipientEmail := range recipientList {
recipient, recipientErr := database.LookupPerson(stmt[database.PERSON_LOOKUP_BY_EMAIL], recipientEmail)
if recipientErr != nil { // for now, but maybe prepare an alert
log.Println(recipientErr)
} else {
people = append(people, recipient)
}
}
if len(people) > 0 {
for _, msgRecipientErr := range message.AddRecipients(stmt[database.RECIPIENT_INSERT], people) {
if msgRecipientErr != nil { // for now
log.Println(msgRecipientErr)
}
}
}
}
// success
messagePosted = true
}
database.WithDatabase(db, fn)
}
}
}
if s == nil && p == nil {
s = new(database.SESSION)
p = new(database.PERSON)
sessionForm := &CreateSessionPage{Title: TITLE_CREATE_SESSION, Alert: alert, Session: s, Person: p}
CREATE_SESSION_TEMPLATE.Execute(w, sessionForm)
} else {
if messagePosted {
// note the update, and go back to all posts
alert.Message = "Your message has been posted"
fn := func(stmt map[string]*sql.Stmt) {
messages, _ := p.LookupLatestMessages(stmt[database.LATEST_MESSAGES], POSTS_PER_PAGE, 0)
digests, _ := database.GetMessageDigests(stmt[database.PERSON_LOOKUP_BY_ID], stmt[database.RECIPIENTS_BY_MESSAGE], messages, p.Id)
d = digests
}
database.WithDatabase(db, fn)
posts := &DisplayPostsPage{Title: TITLE_POSTS, Alert: alert, Session: s, Person: p, Posts: d}
ALL_POSTS_TEMPLATE.Execute(w, posts)
} else {
// go back to the post-message form
postPage := &NewPostPage{Title: TITLE_ADD_POST, Alert: alert, Session: s, Person: p, Keys: k, Recipients: x}
NEW_POST_TEMPLATE.Execute(w, postPage)
}
}
}