]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Forum.pm
Basic infrastructure for private messages/threads
[ndwebbie.git] / lib / NDWeb / Controller / Forum.pm
1 package NDWeb::Controller::Forum;
2
3 use strict;
4 use warnings;
5 use parent 'Catalyst::Controller';
6
7 use NDWeb::Include;
8
9 =head1 NAME
10
11 NDWeb::Controller::Forum - Catalyst Controller
12
13 =head1 DESCRIPTION
14
15 Catalyst Controller.
16
17 =head1 METHODS
18
19 =cut
20
21 =head2 index 
22
23 =cut
24
25 sub index :Path :Args(0) {
26         my ( $self, $c ) = @_;
27         my $dbh = $c->model;
28
29         my $boards = $dbh->prepare(q{SELECT fcid,category,fb.fbid,fb.board
30                         ,count(NULLIF(COALESCE(fp.fpid::BOOLEAN,FALSE)
31                                 AND COALESCE(fp.time > ftv.time,TRUE),FALSE)) AS unread
32                         ,date_trunc('seconds',max(fp.time)::timestamp) as last_post
33                         FROM forum_categories
34                                 JOIN forum_boards fb USING (fcid)
35                                 LEFT OUTER JOIN forum_threads ft USING (fbid)
36                                 LEFT OUTER JOIN forum_posts fp USING (ftid)
37                                 LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $1) ftv USING (ftid)
38                         WHERE EXISTS (SELECT fbid FROM forum_access WHERE fbid = fb.fbid AND gid IN (SELECT groups($1)))
39                                 OR ft.ftid IN (SELECT ftid FROM forum_priv_access WHERE uid = $1)
40                         GROUP BY fcid,category,fb.fbid, fb.board
41                         ORDER BY fcid,fb.fbid
42                 });
43                 $boards->execute($c->stash->{UID});
44
45         my @categories;
46         my $category = {fcid => 0};
47         while (my $board = $boards->fetchrow_hashref){
48                 if ($category->{fcid} != $board->{fcid}){
49                         $category = {fcid => $board->{fcid}, category => $board->{category}};
50                         push @categories,$category;
51                 }
52                 push @{$category->{boards}},$board;
53         }
54         $c->stash(categories => \@categories);
55 }
56
57 sub allUnread : Local {
58         my ( $self, $c ) = @_;
59         my $dbh = $c->model;
60
61         my $threads = $dbh->prepare(q{SELECT fcid,category,fbid,board,ft.ftid,u.username,ft.subject,
62                 count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) AS unread,count(fp.fpid) AS posts,
63                 date_trunc('seconds',max(fp.time)::timestamp) as last_post,
64                 min(fp.time)::date as posting_date, ft.sticky
65                 FROM forum_categories fc 
66                         JOIN forum_boards fb USING (fcid) 
67                         JOIN forum_threads ft USING (fbid)
68                         JOIN forum_posts fp USING (ftid) 
69                         JOIN users u ON u.uid = ft.uid
70                         LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $1) ftv ON ftv.ftid = ft.ftid
71                 WHERE (fbid > 0 AND
72                                 fb.fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($1)))
73                         ) OR ft.ftid IN (SELECT ftid FROM forum_priv_access WHERE uid = $1)
74                 GROUP BY fcid,category,fbid,board,ft.ftid, ft.subject,ft.sticky,u.username
75                 HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= 1 
76                 ORDER BY fcid,fbid,sticky DESC,last_post DESC
77                 });
78
79         $threads->execute($c->stash->{UID});
80         my @categories;
81         my $category = {fcid => 0};
82         my $board = {fbid => 0};
83         while (my $thread = $threads->fetchrow_hashref){
84                 if ($category->{fcid} != $thread->{fcid}){
85                         $category = {fcid => $thread->{fcid}, category => $thread->{category}};
86                         push @categories,$category;
87                 }
88                 if ($board->{fbid} != $thread->{fbid}){
89                         $board = {fbid => $thread->{fbid}, board => $thread->{board}};
90                         push @{$category->{boards}},$board;
91                 }
92                 delete $thread->{fcid};
93                 delete $thread->{fbid};
94                 delete $thread->{category};
95                 delete $thread->{board};
96                 push @{$board->{threads}},$thread;
97         }
98         $c->stash(categories => \@categories);
99         $c->stash(time => $dbh->selectrow_array('SELECT now()::timestamp',undef));
100 }
101
102
103 sub search : Local {
104         my ( $self, $c ) = @_;
105
106         my $dbh = $c->model;
107
108         my @queries;
109         if ($c->req->param('search')){
110                 push @queries,'('.$c->req->param('search').')';
111         }
112         my %cat = (body => 'D', topic => 'A', author => 'B');
113         for ('body','topic','author'){
114                 if ($c->req->param($_)){
115                         my @words = split /\W+/,$c->req->param($_);
116                         my $op = $c->req->param('all'.$_) ? '&' : '|';
117                         my $cat = $cat{$_};
118                         my $query = join " $op ", map {"$_:$cat"} @words;
119                         push @queries,"($query)";
120                 }
121         }
122         my $search = join ' & ', @queries;
123
124         if ($search){
125                 my $posts = $dbh->prepare(q{SELECT fp.ftid,u.username,ft.subject
126                         ,ts_headline(fp.message,to_tsquery($2)) AS headline
127                         ,ts_rank_cd(fp.textsearch, to_tsquery($2),32) AS rank
128                         FROM forum_boards fb 
129                                 JOIN forum_threads ft USING (fbid)
130                                 JOIN forum_posts fp USING (ftid)
131                                 JOIN users u ON fp.uid = u.uid
132                         WHERE (fb.fbid IN (SELECT fbid FROM forum_access
133                                                 WHERE gid IN (SELECT groups($1)))
134                                         OR ft.ftid IN (SELECT ftid FROM forum_priv_access WHERE uid = $1)
135                                 ) AND fp.textsearch @@@ to_tsquery($2)
136                         ORDER BY rank DESC
137                 });
138                 eval {
139                         $posts->execute($c->stash->{UID},$search);
140                         my @posts;
141                         while (my $post = $posts->fetchrow_hashref){
142                                 push @posts,$post;
143                         }
144                         $c->stash(searchresults => \@posts);
145                 };
146                 if ($@){
147                         $c->stash( searcherror => $dbh->errstr);
148                 }
149         }
150
151 }
152
153
154 sub board : Local {
155         my ( $self, $c, $board ) = @_;
156         my $dbh = $c->model;
157
158         $c->stash(time => $dbh->selectrow_array('SELECT now()::timestamp',undef));
159
160         $c->forward('findBoard');
161         $board = $c->stash->{board};
162
163         my $threads = $dbh->prepare(q{SELECT ft.ftid,u.username,ft.subject
164                 ,count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) AS unread,count(fp.fpid) AS posts
165                 ,date_trunc('seconds',max(fp.time)::timestamp) as last_post
166                 ,min(fp.time)::date as posting_date, ft.sticky
167                 FROM forum_threads ft 
168                         JOIN forum_posts fp USING (ftid)
169                         JOIN users u ON u.uid = ft.uid
170                         LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
171                 WHERE ft.fbid = $1 AND (
172                         ft.fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($2)))
173                         OR ft.ftid IN (SELECT ftid FROM forum_priv_access WHERE uid = $2)
174                         )
175                 GROUP BY ft.ftid, ft.subject,ft.sticky,u.username
176                 ORDER BY sticky DESC,last_post DESC
177         });
178         $threads->execute($board->{fbid},$c->stash->{UID});
179         my @threads;
180         while (my $thread = $threads->fetchrow_hashref){
181                 push @threads,$thread;
182         }
183         $c->stash(threads => \@threads);
184
185         if ($board->{moderate}){
186                 my $categories = $dbh->prepare(q{SELECT fcid,category FROM forum_categories ORDER BY fcid});
187                 my $boards = $dbh->prepare(q{SELECT fb.fbid,fb.board, bool_or(fa.post) AS post
188                         FROM forum_boards fb NATURAL JOIN forum_access fa
189                         WHERE fb.fcid = $1 AND
190                                 gid IN (SELECT groups($2))
191                         GROUP BY fb.fbid,fb.board
192                         ORDER BY fb.fbid
193                 });
194                 $categories->execute;
195                 my @categories;
196                 while (my $category = $categories->fetchrow_hashref){
197                         $boards->execute($category->{fcid},$c->stash->{UID});
198
199                         my @boards;
200                         while (my $b = $boards->fetchrow_hashref){
201                                 next if (not $b->{post} or $b->{fbid} == $board->{fbid});
202                                 push @boards,$b;
203                         }
204                         $category->{boards} = \@boards;
205                         push @categories,$category if @boards;
206                 }
207                 $c->stash(categories => \@categories);
208         }
209 }
210
211
212 sub thread : Local {
213         my ( $self, $c, $thread ) = @_;
214         my $dbh = $c->model;
215
216         $c->forward('findThread');
217         unless ($c->stash->{thread}){
218                 $c->stash(template => 'access_denied.tt2');
219                 return;
220         }
221         $c->forward('findPosts');
222         $c->forward('markThreadAsRead') if $c->user_exists;
223 }
224
225 sub findPosts :Private {
226         my ( $self, $c, $thread ) = @_;
227         my $dbh = $c->model;
228
229         my $posts = $dbh->prepare(q{
230                 SELECT u.uid,u.username,date_trunc('seconds',fp.time::timestamp) AS time
231                         ,fp.message,COALESCE(fp.time > ftv.time,TRUE) AS unread
232                 FROM forum_threads ft
233                         JOIN forum_posts fp USING (ftid)
234                         JOIN users u ON u.uid = fp.uid
235                         LEFT OUTER JOIN 
236                                 (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
237                 WHERE ft.ftid = $1
238                 ORDER BY fp.time ASC
239                 });
240         $posts->execute($thread,$c->stash->{UID});
241
242         my @posts;
243         while (my $post = $posts->fetchrow_hashref){
244                 $post->{message} = parseMarkup($post->{message});
245                 push @posts,$post;
246         }
247
248         $c->stash(posts => \@posts);
249 }
250
251
252 sub markBoardAsRead : Local {
253         my ( $self, $c, $board, $time ) = @_;
254         my $dbh = $c->model;
255
256         $c->forward('findBoard');
257         $board = $c->stash->{board};
258
259         my $threads = $dbh->prepare(q{SELECT ft.ftid,ft.subject
260                         ,count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) AS unread
261                         ,count(fp.fpid) AS posts, max(fp.time)::timestamp as last_post
262                         FROM forum_threads ft 
263                                 JOIN forum_posts fp USING (ftid) 
264                                 LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
265                         WHERE ft.fbid = $1 AND fp.time <= $3
266                         GROUP BY ft.ftid, ft.subject
267                         HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= 1
268                 });
269         $threads->execute($board->{fbid},$c->user->id,$time);
270         $dbh->begin_work;
271         while (my $thread = $threads->fetchrow_hashref){
272                 $c->forward('markThreadAsRead',[$thread->{ftid}]);
273         }
274         $dbh->commit;
275         $c->res->redirect($c->req->referer);
276 }
277
278 sub markThreadAsRead : Private {
279         my ( $self, $c, $thread ) = @_;
280         my $dbh = $c->model;
281
282         my $rows = $dbh->do(q{UPDATE forum_thread_visits SET time = now() 
283                 WHERE uid =     $1 AND ftid = $2
284                 },undef,$c->user->id,$thread);
285         if ($rows == 0){
286                 $dbh->do(q{INSERT INTO forum_thread_visits (uid,ftid)
287                         VALUES ($1,$2)}
288                         ,undef,$c->user->id,$thread);
289         }
290 }
291
292 sub moveThreads : Local {
293         my ( $self, $c, $board ) = @_;
294         my $dbh = $c->model;
295
296         $c->forward('findBoard',[$c->req->param('board')]);
297         my $toboard = $c->stash->{board};
298         unless ($toboard->{moderate}){
299                 $c->acl_access_denied('test',$c->action,'No moderator access for target board.')
300         }
301
302         $c->forward('findBoard');
303         $board = $c->stash->{board};
304         unless ($board->{moderate}){
305                 $c->acl_access_denied('test',$c->action,'No moderator access for source board.')
306         }
307
308         my $log = "Moved these threads:\n\n";
309         $dbh->begin_work;
310         my $moveThread = $dbh->prepare(q{UPDATE forum_threads SET fbid = $1 WHERE ftid = $2 AND fbid = $3});
311         for my $param ($c->req->param){
312                 if ($param =~ /t:(\d+)/){
313                         $moveThread->execute($toboard->{fbid},$1,$board->{fbid});
314                         if ($moveThread->rows > 0){
315                                 $log .= "$1\n";
316                         }
317                 }
318         }
319
320         $log .= "\nFrom board: $board->{board} ($board->{fbid})";
321         $log .= "\nTo board: $toboard->{board} ($toboard->{fbid})";
322         $dbh->do(q{INSERT INTO forum_posts (ftid,uid,message)
323                 VALUES((SELECT ftid FROM users WHERE uid = $1),$1,$2)
324                 }, undef, $c->user->id, $log);
325         $dbh->commit;
326         
327         $c->res->redirect($c->uri_for('board',$board->{fbid}));
328 }
329
330 sub newThread : Local {
331         my ( $self, $c, $board ) = @_;
332
333         $c->forward('findBoard');
334         $board = $c->stash->{board};
335
336         unless ($c->stash->{board}->{post}){
337                 $c->acl_access_denied('test',$c->action,'No post access to board.')
338         }
339
340         $c->forward('insertThread');
341         $c->forward('addPost',[$c->stash->{thread}]);
342 }
343
344 sub insertThread : Private {
345         my ( $self, $c, $board ) = @_;
346         my $dbh = $c->model;
347
348         my $insert = $dbh->prepare(q{INSERT INTO forum_threads (ftid,fbid,subject,uid)
349                 VALUES(DEFAULT,$1,$2,$3) RETURNING (ftid);
350                 });
351         $insert->execute($board,html_escape($c->req->param('subject')),$c->stash->{UID});
352         $c->stash(thread => $insert->fetchrow);
353         $insert->finish;
354 }
355
356 sub addPost : Local {
357         my ( $self, $c, $thread ) = @_;
358         my $dbh = $c->model;
359
360         if ($c->req->param('cmd') eq 'Submit'){
361                 $c->forward('findThread');
362                 unless ($c->stash->{thread}->{post}){
363                         $c->acl_access_denied('test',$c->action,'No post access to board.')
364                 }
365                 $c->forward('insertPost');
366                 $c->res->redirect($c->uri_for('thread',$thread));
367         }elsif ($c->req->param('cmd') eq 'Preview'){
368                 $c->forward('thread');
369                 $c->forward('previewPost');
370                 $c->stash(template => 'forum/thread.tt2');
371         }
372 }
373
374 sub setSticky : Local {
375         my ( $self, $c, $thread, $sticky ) = @_;
376         my $dbh = $c->model;
377
378         $c->forward('findThread');
379         unless ($c->stash->{thread}->{moderate}){
380                 $c->acl_access_denied('test',$c->action,'No moderator access to board.')
381         }
382
383         $dbh->do(q{UPDATE forum_threads SET sticky = $2 WHERE ftid = $1}
384                 , undef,$thread, $sticky);
385         $c->res->redirect($c->uri_for('thread',$thread));
386 }
387
388 sub findThread : Private {
389         my ( $self, $c, $thread ) = @_;
390         my $dbh = $c->model;
391         my $findThread = $dbh->prepare(q{SELECT ft.ftid,ft.subject
392                 ,COALESCE(bool_or(fa.post),true) AS post, bool_or(fa.moderate) AS moderate
393                 ,ft.fbid,fb.board,fb.fcid,ft.sticky,fc.category
394                 FROM forum_boards fb
395                         NATURAL JOIN forum_threads ft
396                         NATURAL JOIN forum_categories fc
397                         LEFT OUTER JOIN (SELECT * FROM forum_access
398                                 WHERE gid IN (SELECT groups($2))
399                         ) fa USING (fbid)
400                 WHERE ft.ftid = $1 AND (fa.post IS NOT NULL
401                         OR ft.ftid IN (SELECT ftid FROM forum_priv_access WHERE uid = $2))
402                 GROUP BY ft.ftid,ft.subject,ft.fbid,fb.board,fb.fcid,ft.sticky,fc.category
403         });
404         $thread = $dbh->selectrow_hashref($findThread,undef,$thread,$c->stash->{UID});
405         $c->stash(thread => $thread);
406 }
407
408 sub findBoard : Private {
409         my ( $self, $c, $board ) = @_;
410         my $dbh = $c->model;
411
412         my $boards = $dbh->prepare(q{SELECT fb.fbid,fb.board, bool_or(fa.post) AS post, bool_or(fa.moderate) AS moderate,fb.fcid, fc.category
413                         FROM forum_boards fb
414                                 NATURAL JOIN forum_categories fc
415                                 LEFT OUTER JOIN (SELECT * FROM forum_access
416                                         WHERE fbid = $1 AND gid IN (SELECT groups($2))
417                                 ) fa USING (fbid)
418                         WHERE fb.fbid = $1
419                         GROUP BY fb.fbid,fb.board,fb.fcid,fc.category
420                 });
421         $board = $dbh->selectrow_hashref($boards,undef,$board,$c->stash->{UID});
422
423         $c->stash(board => $board);
424 }
425
426 sub previewPost : Private {
427         my ( $self, $c) = @_;
428         push @{$c->stash->{posts}}, {
429                 unread => 1,
430                 username => 'PREVIEW',
431                 message => parseMarkup(html_escape $c->req->param('message')),
432         };
433         $c->stash(previewMessage => html_escape $c->req->param('message'));
434 }
435
436 sub insertPost : Private {
437         my ( $self, $c, $thread ) = @_;
438         my $dbh = $c->model;
439
440         my $insert = $dbh->prepare(q{INSERT INTO forum_posts (ftid,message,uid)
441                 VALUES($1,$2,$3)});
442         $insert->execute($thread,html_escape($c->req->param('message')),$c->stash->{UID});
443 }
444
445 =head1 AUTHOR
446
447 Michael Andreen (harv@ruin.nu)
448
449 =head1 LICENSE
450
451 GPL 2.0, or later.
452
453 =cut
454
455 1;