Statistics
[bpt/portal.git] / support.sml
CommitLineData
5a2812ca
AC
1structure Support :> SUPPORT =
2struct
3
4open Util Sql Init
5
6datatype status =
7 NEW
8 | PENDING
9 | CLOSED
10
11type category = { id : int, grp : int, name : string, descr : string }
12type issue = { id : int, usr : int, cat : int, title : string, priv : bool, status : status, stamp : C.timestamp }
13type post = { id : int, usr : int, iss : int, body : string, stamp : C.timestamp }
14type subscription = { usr : int, cat : int }
15
16
17(* Categories *)
18
19fun mkCatRow [id, grp, name, descr] =
20 {id = C.intFromSql id, grp = C.intFromSql grp, name = C.stringFromSql name,
21 descr = C.stringFromSql descr}
22 | mkCatRow row = rowError ("category", row)
23
24fun lookupCategory id =
25 mkCatRow (C.oneRow (getDb ()) ($`SELECT id, grp, name, descr
26 FROM SupCategory
27 WHERE id = ^(C.intToSql id)`))
28
29fun listCategories () =
30 C.map (getDb ()) mkCatRow ($`SELECT id, grp, name, descr
31 FROM SupCategory
32 ORDER BY name`)
33
34fun mkCatRow' (sub :: rest) =
35 (not (C.isNull sub), mkCatRow rest)
36 | mkCatRow' row = Init.rowError ("category'", row)
37
38fun listCategoriesWithSubscriptions usr =
39 C.map (getDb ()) mkCatRow' ($`SELECT cat, id, grp, name, descr
40 FROM SupCategory LEFT OUTER JOIN SupSubscription
41 ON (usr = ^(C.intToSql usr) AND cat = id)
42 ORDER BY name`)
43
44fun addCategory (grp, name, descr) =
45 let
46 val db = getDb ()
47 val id = nextSeq (db, "SupCategorySeq")
48 in
49 C.dml db ($`INSERT INTO SupCategory (id, grp, name, descr)
50 VALUES (^(C.intToSql id), ^(C.intToSql grp), ^(C.stringToSql name), ^(C.stringToSql descr))`);
51 id
52 end
53
54fun modCategory (cat : category) =
55 let
56 val db = getDb ()
57 in
58 ignore (C.dml db ($`UPDATE SupCategory SET
59 grp = ^(C.intToSql (#grp cat)), name = ^(C.stringToSql (#name cat)),
60 descr = ^(C.stringToSql (#descr cat))
61 WHERE id = ^(C.intToSql (#id cat))`))
62 end
63
64fun deleteCategory id =
65 ignore (C.dml (getDb ()) ($`DELETE FROM SupCategory WHERE id = ^(C.intToSql id)`))
66
67
68(* Issues *)
69
70val statusToSql =
71 fn NEW => "0"
72 | PENDING => "1"
73 | CLOSED => "2"
74
75fun statusFromSql v =
76 case C.intFromSql v of
77 0 => NEW
78 | 1 => PENDING
79 | 2 => CLOSED
80 | _ => raise Fail "Bad support issue status"
81
82fun mkIssueRow [id, usr, cat, title, priv, status, stamp] =
83 {id = C.intFromSql id, usr = C.intFromSql usr, cat = C.intFromSql cat,
84 title = C.stringFromSql title, priv = C.boolFromSql priv,
85 status = statusFromSql status, stamp = C.timestampFromSql stamp}
86 | mkIssueRow row = rowError ("issue", row)
87
88fun lookupIssue id =
89 mkIssueRow (C.oneRow (getDb ()) ($`SELECT id, usr, cat, title, priv, status, stamp
90 FROM SupIssue
91 WHERE id = ^(C.intToSql id)`))
92
93fun listIssues () =
94 C.map (getDb ()) mkIssueRow ($`SELECT id, usr, cat, title, priv, status, stamp
95 FROM SupIssue
96 ORDER BY stamp DESC`)
97
5d851d7c
AC
98fun mkIssueRow' (name :: rest) = (C.stringFromSql name, mkIssueRow rest)
99 | mkIssueRow' r = Init.rowError ("issue'", r)
100
101fun listOpenIssues usr =
102 C.map (getDb ()) mkIssueRow' ($`SELECT WebUser.name, SupIssue.id, SupIssue.usr, SupIssue.cat, title, priv, status, stamp
103 FROM SupIssue JOIN SupCategory ON cat = SupCategory.id
104 JOIN WebUser ON WebUser.id = SupIssue.usr
105 WHERE status < 2
106 AND (usr = ^(C.intToSql usr)
107 OR ((SELECT COUNT( * ) FROM SupSubscription
108 WHERE SupSubscription.usr = ^(C.intToSql usr)
109 AND SupSubscription.cat = SupIssue.cat) > 0
110 AND (not priv OR (SELECT COUNT( * ) FROM Membership
111 WHERE Membership.usr = ^(C.intToSql usr)
112 AND Membership.grp = SupCategory.grp) > 0)))
113 ORDER BY stamp DESC`)
114
1cb3df3f 115fun listCategoryIssues cat =
5d851d7c
AC
116 C.map (getDb ()) mkIssueRow' ($`SELECT WebUser.name, SupIssue.id, usr, cat, title, priv, status, stamp
117 FROM SupIssue
118 JOIN WebUser ON WebUser.id = usr
119 WHERE cat = ^(C.intToSql cat)
120 ORDER BY stamp DESC`)
1cb3df3f
AC
121
122fun listOpenCategoryIssues (cat, usr) =
5d851d7c
AC
123 C.map (getDb ()) mkIssueRow' ($`SELECT name, SupIssue.id, usr, cat, title, priv, status, stamp
124 FROM SupIssue
125 JOIN WebUser ON WebUser.id = usr
126 WHERE cat = ^(C.intToSql cat)
127 AND status < 2
128 AND (NOT priv OR usr = ^(C.intToSql usr))
129 ORDER BY stamp DESC`)
1cb3df3f
AC
130
131fun listOpenCategoryIssuesAdmin cat =
5d851d7c
AC
132 C.map (getDb ()) mkIssueRow' ($`SELECT name, SupIssue.id, usr, cat, title, priv, status, stamp
133 FROM SupIssue
134 JOIN WebUser ON WebUser.id = usr
135 WHERE cat = ^(C.intToSql cat)
136 AND status < 2
137 ORDER BY stamp DESC`)
1cb3df3f 138
5a2812ca
AC
139fun addIssue (usr, cat, title, priv, status) =
140 let
141 val db = getDb ()
142 val id = nextSeq (db, "SupIssueSeq")
143 in
144 C.dml db ($`INSERT INTO SupIssue (id, usr, cat, title, priv, status, stamp)
145 VALUES (^(C.intToSql id), ^(C.intToSql usr), ^(C.intToSql cat),
146 ^(C.stringToSql title), ^(C.boolToSql priv),
147 ^(statusToSql status), CURRENT_TIMESTAMP)`);
148 id
149 end
150
151fun modIssue (iss : issue) =
152 let
153 val db = getDb ()
154 in
155 ignore (C.dml db ($`UPDATE SupIssue SET
156 usr = ^(C.intToSql (#usr iss)), cat = ^(C.intToSql (#cat iss)),
157 title = ^(C.stringToSql (#title iss)), priv = ^(C.boolToSql (#priv iss)),
158 status = ^(statusToSql (#status iss))
159 WHERE id = ^(C.intToSql (#id iss))`))
160 end
161
162fun deleteIssue id =
163 ignore (C.dml (getDb ()) ($`DELETE FROM SupIssue WHERE id = ^(C.intToSql id)`))
164
165
166(* Posts *)
167
168fun mkPostRow [id, usr, iss, body, stamp] =
169 {id = C.intFromSql id, usr = C.intFromSql usr, iss = C.intFromSql iss,
170 body = C.stringFromSql body, stamp = C.timestampFromSql stamp}
171 | mkPostRow row = rowError ("post", row)
172
173fun lookupPost id =
174 mkPostRow (C.oneRow (getDb ()) ($`SELECT id, usr, iss, body, stamp
175 FROM SupPost
176 WHERE id = ^(C.intToSql id)`))
177
edeb626e
AC
178fun mkPostRow' (name :: rest) = (C.stringFromSql name, mkPostRow rest)
179 | mkPostRow' row = Init.rowError ("post'", row)
180
5a2812ca 181fun listPosts iss =
edeb626e
AC
182 C.map (getDb ()) mkPostRow' ($`SELECT name, SupPost.id, usr, iss, body, SupPost.stamp
183 FROM SupPost JOIN WebUser ON usr = WebUser.id
184 WHERE iss = ^(C.intToSql iss)
185 ORDER BY stamp`)
5a2812ca
AC
186
187fun addPost (usr, iss, body) =
188 let
189 val db = getDb ()
190 val id = nextSeq (db, "SupPostSeq")
191 in
192 C.dml db ($`INSERT INTO SupPost (id, usr, iss, body, stamp)
193 VALUES (^(C.intToSql id), ^(C.intToSql usr), ^(C.intToSql iss),
194 ^(C.stringToSql body), CURRENT_TIMESTAMP)`);
195 id
196 end
197
198fun modPost (post : post) =
199 let
200 val db = getDb ()
201 in
202 ignore (C.dml db ($`UPDATE SupPost SET
203 usr = ^(C.intToSql (#usr post)), iss = ^(C.intToSql (#iss post)),
204 body = ^(C.stringToSql (#body post))
205 WHERE id = ^(C.intToSql (#id post))`))
206 end
207
208fun deletePost id =
209 ignore (C.dml (getDb ()) ($`DELETE FROM SupPost WHERE id = ^(C.intToSql id)`))
210
211
212(* Subscriptions *)
213
214fun mkSubRow [usr, cat] =
215 {usr = C.intFromSql usr, cat = C.intFromSql cat}
216 | mkSubRow row = rowError ("subscription", row)
217
218fun subscribed {usr, cat} =
219 case C.oneRow (getDb ()) ($`SELECT COUNT( * ) FROM SupSubscription
220 WHERE usr = ^(C.intToSql usr) AND cat = ^(C.intToSql cat)`) of
221 [n] => not (C.isNull n) andalso C.intFromSql n > 0
222 | r => Init.rowError ("subscribed", r)
223
224fun subscribe (sub as {usr, cat}) =
225 if subscribed sub then
226 ()
227 else
228 ignore (C.dml (getDb ()) ($`INSERT INTO SupSubscription (usr, cat)
229 VALUES (^(C.intToSql usr), ^(C.intToSql cat))`))
230
231fun unsubscribe {usr, cat} =
232 ignore (C.dml (getDb ()) ($`DELETE FROM SupSubscription
233 WHERE usr = ^(C.intToSql usr) AND cat = ^(C.intToSql cat)`))
234
edeb626e
AC
235val okChars = [#" ", #"-", #".", #"!", #"?", #":", #";", #"'", #"\""]
236
1cb3df3f 237fun validTitle s = CharVector.exists (fn ch => not (Char.isSpace ch)) s
edeb626e
AC
238 andalso CharVector.all (fn ch => Char.isAlphaNum ch orelse List.exists (fn ch' => ch = ch') okChars) s
239
240fun allowedToSee iss =
241 let
242 val iss = lookupIssue iss
243 val cat = lookupCategory (#cat iss)
244 in
245 not (#priv iss) orelse Group.inGroupNum (#grp cat) orelse (Init.getUserId () = #usr iss)
246 end
247
248fun allowedToEdit iss =
249 let
250 val iss = lookupIssue iss
251 val cat = lookupCategory (#cat iss)
252 in
253 Group.inGroupNum (#grp cat) orelse (Init.getUserId () = #usr iss)
254 end
255
5d851d7c 256fun writeRecipients (mail, iss : issue, cat : category, noName) =
edeb626e
AC
257 let
258 val query =
259 if #priv iss then
260 $`SELECT name
261 FROM WebUser JOIN Membership ON (usr = id AND grp = ^(C.intToSql (#grp cat)))`
262 else
263 $`SELECT name
98a5f121
AC
264 FROM WebUser JOIN SupSubscription ON (usr = id AND cat = ^(C.intToSql (#id cat)))
265 UNION SELECT name
266 FROM WebUser JOIN Membership ON (usr = id AND grp = ^(C.intToSql (#grp cat)))`
edeb626e 267
5d851d7c
AC
268 fun doOne [name] =
269 let
270 val name = C.stringFromSql name
271 in
272 if name = noName then
273 ()
274 else
275 (Mail.mwrite (mail, name);
276 Mail.mwrite (mail, ","))
277 end
edeb626e
AC
278 in
279 Mail.mwrite (mail, "Bcc: ");
280 C.app (getDb ()) doOne query;
281 Mail.mwrite (mail, "\n")
282 end
5a2812ca 283
edeb626e
AC
284fun notify (prefix, f) iss =
285 let
286 val iss = lookupIssue iss
287 val cat = lookupCategory (#cat iss)
288 val user = Init.lookupUser (#usr iss)
289
290 val mail = Mail.mopen ()
291 in
292 Mail.mwrite (mail, "From: Hcoop Support System <support@hcoop.net>\nTo: ");
293 Mail.mwrite (mail, #name user);
294 Mail.mwrite (mail, "@hcoop.net\n");
5d851d7c 295 writeRecipients (mail, iss, cat, #name user);
edeb626e
AC
296 Mail.mwrite (mail, "Subject: ");
297 Mail.mwrite (mail, prefix);
298 Mail.mwrite (mail, #title iss);
299 Mail.mwrite (mail, "\n\nURL: ");
300 Mail.mwrite (mail, Init.urlPrefix);
301 Mail.mwrite (mail, "issue?cat=");
302 Mail.mwrite (mail, C.intToSql (#id cat));
303 Mail.mwrite (mail, "&id=");
304 Mail.mwrite (mail, C.intToSql (#id iss));
305 Mail.mwrite (mail, "\n\nSubmitted by: ");
306 Mail.mwrite (mail, #name user);
307 Mail.mwrite (mail, "\n Category: ");
308 Mail.mwrite (mail, #name cat);
309 Mail.mwrite (mail, "\n Issue: ");
310 Mail.mwrite (mail, #title iss);
311 Mail.mwrite (mail, "\n Private: ");
312 Mail.mwrite (mail, if #priv iss then "yes" else "no");
313 Mail.mwrite (mail, "\n\n");
314
315 f (iss, cat, user, mail);
316
317 OS.Process.isSuccess (Mail.mclose mail)
318 end
319
320val notifyCreation = notify ("[New] ",
321 fn (iss, cat, user, mail) =>
322 (case listPosts (#id iss) of
323 [] => ()
324 | [(_, post)] => Mail.mwrite (mail, #body post)
325 | _ => raise Fail "Too many posts for supposedly new support issue"))
326
327fun notifyPost pid =
328 let
329 val post = lookupPost pid
330 val poster = Init.lookupUser (#usr post)
331 in
332 notify ("[Post] ",
333 fn (iss, cat, user, mail) =>
334 (Mail.mwrite (mail, "New post by ");
335 Mail.mwrite (mail, #name poster);
336 Mail.mwrite (mail, ":\n\n");
337 Mail.mwrite (mail, #body post))) (#iss post)
338 end
339
340val statusToString =
341 fn NEW => "New"
342 | PENDING => "Pending"
343 | CLOSED => "Closed"
344
345fun notifyStatus (usr, oldStatus, newStatus, iss) =
346 let
347 val user = Init.lookupUser usr
348 in
349 notify ("[" ^ statusToString newStatus ^ "] ",
039a9529 350 fn (iss, cat, user', mail) =>
edeb626e
AC
351 (Mail.mwrite (mail, #name user);
352 Mail.mwrite (mail, " changed status from ");
353 Mail.mwrite (mail, statusToString oldStatus);
354 Mail.mwrite (mail, " to ");
355 Mail.mwrite (mail, statusToString newStatus);
356 Mail.mwrite (mail, ".\n"))) iss
357 end
358
5a2812ca 359end