]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Forum.pm
Possible to mark threads as unread
[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{
30 SELECT fcid,category,fb.fbid,fb.board
31         ,SUM((SELECT count(*) FROM forum_posts WHERE ftid = ft.ftid
32                 AND COALESCE(time > ftv.time,TRUE))) AS unread
33         ,date_trunc('seconds',max(ft.mtime)::timestamp ) AS last_post
34 FROM forum_categories fc
35         JOIN forum_boards fb USING (fcid)
36         LEFT OUTER JOIN forum_threads ft USING (fbid)
37         LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $1)
38                 ftv USING (ftid)
39 WHERE fbid IN (SELECT fbid FROM forum_access
40                 WHERE gid IN (SELECT groups($1)))
41         OR ftid IN (SELECT ftid FROM forum_priv_access
42                 WHERE uid = $1)
43 GROUP BY fcid,category,fb.fbid, fb.board
44 ORDER BY fcid,fb.fbid
45                 });
46                 $boards->execute($c->stash->{UID});
47
48         my @categories;
49         my $category = {fcid => 0};
50         while (my $board = $boards->fetchrow_hashref){
51                 if ($category->{fcid} != $board->{fcid}){
52                         $category = {fcid => $board->{fcid}, category => $board->{category}};
53                         push @categories,$category;
54                 }
55                 push @{$category->{boards}},$board;
56         }
57         $c->stash(categories => \@categories);
58 }
59
60 sub allUnread : Local {
61         my ( $self, $c ) = @_;
62         my $dbh = $c->model;
63
64         my $threads = $dbh->prepare(q{
65 SELECT fcid,category,fbid,board,ft.ftid,u.username,ft.subject,ft.sticky
66         ,(SELECT count(*) FROM forum_posts WHERE ftid = ft.ftid
67                 AND COALESCE(time > ftv.time,TRUE)) AS unread
68         ,ft.posts,date_trunc('seconds',ft.mtime::timestamp) as last_post
69         ,ft.ctime::DATE as posting_date
70 FROM forum_categories fc
71         JOIN forum_boards fb USING (fcid)
72         JOIN forum_threads ft USING (fbid)
73         JOIN users u ON u.uid = ft.uid
74         LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $1)
75                 ftv ON ftv.ftid = ft.ftid
76 WHERE COALESCE(ft.mtime > ftv.time,TRUE)
77         AND ft.ftid IN (SELECT ftid FROM forum_posts WHERE ftid = ft.ftid)
78         AND ((fbid > 0 AND
79                         fb.fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($1))))
80                 OR ft.ftid IN (SELECT ftid FROM forum_priv_access WHERE uid = $1))
81 ORDER BY fcid,fbid,sticky DESC,last_post DESC
82                 });
83
84         $threads->execute($c->stash->{UID});
85         my @categories;
86         my $category = {fcid => 0};
87         my $board = {fbid => 0};
88         while (my $thread = $threads->fetchrow_hashref){
89                 if ($category->{fcid} != $thread->{fcid}){
90                         $category = {fcid => $thread->{fcid}, category => $thread->{category}};
91                         push @categories,$category;
92                 }
93                 if ($board->{fbid} != $thread->{fbid}){
94                         $board = {fbid => $thread->{fbid}, board => $thread->{board}};
95                         push @{$category->{boards}},$board;
96                 }
97                 delete $thread->{fcid};
98                 delete $thread->{fbid};
99                 delete $thread->{category};
100                 delete $thread->{board};
101                 push @{$board->{threads}},$thread;
102         }
103         $c->stash(categories => \@categories);
104         $c->stash(time => $dbh->selectrow_array('SELECT now()::timestamp',undef));
105 }
106
107
108 sub search : Local {
109         my ( $self, $c ) = @_;
110
111         my $dbh = $c->model;
112
113         my @queries;
114         if ($c->req->param('search')){
115                 push @queries,'('.$c->req->param('search').')';
116         }
117         my %cat = (body => 'D', topic => 'A', author => 'B');
118         for ('body','topic','author'){
119                 if ($c->req->param($_)){
120                         my @words = split /\W+/,$c->req->param($_);
121                         my $op = $c->req->param('all'.$_) ? '&' : '|';
122                         my $cat = $cat{$_};
123                         my $query = join " $op ", map {"$_:$cat"} @words;
124                         push @queries,"($query)";
125                 }
126         }
127         my $search = join ' & ', @queries;
128
129         if ($search){
130                 my $posts = $dbh->prepare(q{SELECT fp.ftid,u.username,ft.subject
131                         ,ts_headline(fp.message,to_tsquery($2)) AS headline
132                         ,ts_rank_cd(fp.textsearch, to_tsquery($2),32) AS rank
133                         FROM forum_boards fb 
134                                 JOIN forum_threads ft USING (fbid)
135                                 JOIN forum_posts fp USING (ftid)
136                                 JOIN users u ON fp.uid = u.uid
137                         WHERE (fb.fbid IN (SELECT fbid FROM forum_access
138                                                 WHERE gid IN (SELECT groups($1)))
139                                         OR ft.ftid IN (SELECT ftid FROM forum_priv_access WHERE uid = $1)
140                                 ) AND fp.textsearch @@@ to_tsquery($2)
141                         ORDER BY rank DESC
142                 });
143                 eval {
144                         $posts->execute($c->stash->{UID},$search);
145                         my @posts;
146                         while (my $post = $posts->fetchrow_hashref){
147                                 push @posts,$post;
148                         }
149                         $c->stash(searchresults => \@posts);
150                 };
151                 if ($@){
152                         $c->stash( searcherror => $dbh->errstr);
153                 }
154         }
155
156 }
157
158
159 sub board : Local {
160         my ( $self, $c, $board ) = @_;
161         my $dbh = $c->model;
162
163         $c->stash(time => $dbh->selectrow_array('SELECT now()::timestamp',undef));
164
165         $c->forward('findBoard');
166         $board = $c->stash->{board};
167         if ( !defined $board->{fbid}){
168                 $c->detach('/default');
169         }
170
171         my $threads = $dbh->prepare(q{
172 SELECT ft.ftid,u.username,ft.subject,ft.posts, ft.sticky
173         ,(SELECT count(*) FROM forum_posts WHERE ftid = ft.ftid
174                 AND COALESCE(time > ftv.time,TRUE)) AS unread
175         ,ft.ctime::DATE as posting_date
176         ,date_trunc('seconds',ft.mtime::timestamp) as last_post
177 FROM forum_threads ft
178         JOIN users u USING(uid)
179         LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $2)
180                 ftv ON ftv.ftid = ft.ftid
181 WHERE ft.posts > 0 AND ft.fbid = $1 AND (
182                 ft.fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($2)))
183                 OR ft.ftid IN (SELECT ftid FROM forum_priv_access WHERE uid = $2)
184         )
185 GROUP BY ft.ftid, ft.subject,ft.sticky,u.username,ft.ctime,ft.mtime,ft.posts,ftv.time
186 ORDER BY sticky DESC,last_post DESC
187         });
188         $threads->execute($board->{fbid},$c->stash->{UID});
189         my @threads;
190         while (my $thread = $threads->fetchrow_hashref){
191                 push @threads,$thread;
192         }
193
194         if ( !(defined $board->{post}) && @threads == 0){
195                 $c->acl_access_denied('test',$c->action,'No access to board')
196         }
197         $c->stash(threads => \@threads);
198
199         $c->stash(title => "$board->{board} ($board->{category})");
200
201         $c->forward('listModeratorBoards', [$board->{fbid}]) if $board->{moderate};
202         
203 }
204
205 sub thread : Local {
206         my ( $self, $c, $thread ) = @_;
207         my $dbh = $c->model;
208
209         $c->forward('findThread');
210         $thread = $c->stash->{thread};
211         unless ($thread){
212                 $c->stash(template => 'default.tt2');
213                 $c->res->status(404);
214                 return;
215         }
216         my $query = $dbh->prepare(q{SELECT uid,username FROM users u
217                 JOIN forum_priv_access fta USING (uid) WHERE fta.ftid = $1});
218         $query->execute($thread->{ftid});
219         $c->stash(access => $query->fetchall_arrayref({}) );
220         $c->stash(title => $thread->{subject}
221                 . " ($thread->{category} - $thread->{board})");
222         $c->forward('findPosts');
223         $c->forward('markThreadAsRead') if $c->user_exists;
224         if ($c->stash->{thread}->{moderate}) {
225                 $c->forward('findUsers');
226                 $c->forward('listModeratorBoards', [$c->stash->{thread}->{fbid}]);
227         }
228 }
229
230 sub findPosts :Private {
231         my ( $self, $c, $thread ) = @_;
232         my $dbh = $c->model;
233
234         my $posts = $dbh->prepare(q{
235                 SELECT u.uid,u.username,date_trunc('seconds',fp.time::timestamp) AS time
236                         ,fp.message,COALESCE(fp.time > ftv.time,TRUE) AS unread
237                 FROM forum_threads ft
238                         JOIN forum_posts fp USING (ftid)
239                         JOIN users u ON u.uid = fp.uid
240                         LEFT OUTER JOIN 
241                                 (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
242                 WHERE ft.ftid = $1
243                 ORDER BY fp.time ASC
244                 });
245         $posts->execute($thread,$c->stash->{UID});
246
247         my @posts;
248         while (my $post = $posts->fetchrow_hashref){
249                 $post->{message} = parseMarkup($post->{message});
250                 push @posts,$post;
251         }
252
253         $c->stash(posts => \@posts);
254 }
255
256
257 sub markBoardAsRead : Local {
258         my ( $self, $c, $board, $time ) = @_;
259         my $dbh = $c->model;
260
261         $c->forward('findBoard');
262         $board = $c->stash->{board};
263
264         my $threads = $dbh->prepare(q{SELECT ft.ftid,ft.subject
265                         ,count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) AS unread
266                         ,count(fp.fpid) AS posts, max(fp.time)::timestamp as last_post
267                         FROM forum_threads ft 
268                                 JOIN forum_posts fp USING (ftid) 
269                                 LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
270                         WHERE ft.fbid = $1 AND fp.time <= $3
271                         GROUP BY ft.ftid, ft.subject
272                         HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= 1
273                 });
274         $threads->execute($board->{fbid},$c->user->id,$time);
275         $dbh->begin_work;
276         while (my $thread = $threads->fetchrow_hashref){
277                 $c->forward('markThreadAsRead',[$thread->{ftid}]);
278         }
279         $dbh->commit;
280         $c->forward('/redirect');
281 }
282
283 sub markThreadAsRead : Private {
284         my ( $self, $c, $thread ) = @_;
285         my $dbh = $c->model;
286
287         my $rows = $dbh->do(q{UPDATE forum_thread_visits SET time = now() 
288                 WHERE uid =     $1 AND ftid = $2
289                 },undef,$c->user->id,$thread);
290         if ($rows == 0){
291                 $dbh->do(q{INSERT INTO forum_thread_visits (uid,ftid)
292                         VALUES ($1,$2)}
293                         ,undef,$c->user->id,$thread);
294         }
295 }
296
297 sub markThreadAsUnread : Local {
298         my ( $self, $c, $thread ) = @_;
299         my $dbh = $c->model;
300
301         my ($fbid) = $dbh->selectrow_array(q{
302 SELECT fbid FROM forum_threads WHERE ftid = $1
303                 },undef, $thread);
304
305         $dbh->do(q{
306 DELETE FROM forum_thread_visits WHERE uid = $1 AND ftid = $2
307                 }, undef, $c->user->id, $thread);
308         $c->res->redirect($c->uri_for('board',$fbid));
309 }
310
311 sub moveThreads : Local {
312         my ( $self, $c, $board ) = @_;
313         my $dbh = $c->model;
314
315         $c->forward('findBoard',[$c->req->param('board')]);
316         my $toboard = $c->stash->{board};
317         unless ($toboard->{moderate}){
318                 $c->acl_access_denied('test',$c->action,'No moderator access for target board.')
319         }
320
321         $c->forward('findBoard');
322         $board = $c->stash->{board};
323         unless ($board->{moderate}){
324                 $c->acl_access_denied('test',$c->action,'No moderator access for source board.')
325         }
326
327         my $log = "Moved these threads:\n\n";
328         $dbh->begin_work;
329         my $moveThread = $dbh->prepare(q{UPDATE forum_threads SET fbid = $1 WHERE ftid = $2 AND fbid = $3});
330         for my $param ($c->req->param){
331                 if ($param =~ /t:(\d+)/){
332                         $moveThread->execute($toboard->{fbid},$1,$board->{fbid});
333                         if ($moveThread->rows > 0){
334                                 $log .= "$1\n";
335                         }
336                 }
337         }
338
339         $log .= "\nFrom board: $board->{board} ($board->{fbid})";
340         $log .= "\nTo board: $toboard->{board} ($toboard->{fbid})";
341         $dbh->do(q{INSERT INTO forum_posts (ftid,uid,message)
342                 VALUES((SELECT ftid FROM users WHERE uid = $1),$1,$2)
343                 }, undef, $c->user->id, $log);
344         $dbh->commit;
345         
346         $c->res->redirect($c->uri_for('board',$board->{fbid}));
347 }
348
349 sub newThread : Local {
350         my ( $self, $c, $board ) = @_;
351
352         $c->forward('findBoard');
353         $board = $c->stash->{board};
354
355         unless ($c->stash->{board}->{post}){
356                 $c->acl_access_denied('test',$c->action,'No post access to board.')
357         }
358
359         $c->forward('insertThread');
360         $c->forward('addPost',[$c->stash->{thread}]);
361 }
362
363 sub insertThread : Private {
364         my ( $self, $c, $board ) = @_;
365         my $dbh = $c->model;
366
367         my $insert = $dbh->prepare(q{INSERT INTO forum_threads (ftid,fbid,subject,uid)
368                 VALUES(DEFAULT,$1,$2,$3) RETURNING (ftid);
369                 });
370         $insert->execute($board,html_escape($c->req->param('subject')),$c->stash->{UID});
371         $c->stash(thread => $insert->fetchrow);
372         $insert->finish;
373 }
374
375 sub addPost : Local {
376         my ( $self, $c, $thread ) = @_;
377         my $dbh = $c->model;
378
379         if ($c->req->param('cmd') eq 'Submit'){
380                 $c->forward('findThread');
381                 unless ($c->stash->{thread}->{post}){
382                         $c->acl_access_denied('test',$c->action,'No post access to board.')
383                 }
384                 $c->forward('insertPost');
385                 $c->res->redirect($c->uri_for('thread',$thread));
386         }elsif ($c->req->param('cmd') eq 'Preview'){
387                 $c->forward('thread');
388                 $c->forward('previewPost');
389                 $c->stash(template => 'forum/thread.tt2');
390         }
391 }
392
393 sub setSticky : Local {
394         my ( $self, $c, $thread, $sticky ) = @_;
395         my $dbh = $c->model;
396
397         $c->forward('findThread');
398         unless ($c->stash->{thread}->{moderate}){
399                 $c->acl_access_denied('test',$c->action,'No moderator access to board.')
400         }
401
402         $dbh->do(q{UPDATE forum_threads SET sticky = $2 WHERE ftid = $1}
403                 , undef,$thread, $sticky);
404         $c->res->redirect($c->uri_for('thread',$thread));
405 }
406
407 sub postthreadaccess : Local {
408         my ( $self, $c, $thread) = @_;
409         my $dbh = $c->model;
410
411         $c->forward('findThread');
412         $dbh->begin_work;
413         unless ($c->stash->{thread}->{moderate}){
414                 $c->acl_access_denied('test',$c->action,'No moderator access to board.')
415         }
416         if ($c->req->param('access')){
417                 $c->req->parameters->{access} = [$c->req->parameters->{access}]
418                         unless ref $c->req->parameters->{access} eq 'ARRAY';
419                 my $query = $dbh->prepare(q{DELETE From forum_priv_access
420                         WHERE ftid = $1 AND uid = ANY ($2)});
421                 $query->execute($thread,$c->req->parameters->{access});
422                 $dbh->do(q{INSERT INTO forum_posts (ftid,uid,message)
423                         VALUES((SELECT ftid FROM users WHERE uid = $1),$1,$2)
424                         }, undef, $c->user->id
425                         ,"Removed access on thread $thread for : @{$c->req->parameters->{access}}");
426         }
427         if ($c->req->param('uid')){
428                 $c->forward('addaccess');
429         }
430         $dbh->commit;
431         $c->res->redirect($c->uri_for('thread',$thread));
432 }
433
434 sub removeownthreadaccess : Local {
435         my ( $self, $c, $thread) = @_;
436         my $dbh = $c->model;
437         $dbh->do(q{DELETE FROM forum_priv_access WHERE uid = $1 AND ftid = $2}
438                 ,undef,$c->user->id,$thread);
439         $c->res->redirect($c->uri_for('allUnread'));
440 }
441
442 sub privmsg : Local {
443         my ( $self, $c, $uid ) = @_;
444
445         $uid ||= 0;
446         $c->stash(uid => $uid);
447
448         $c->forward('findUsers');
449 }
450
451 sub postprivmsg : Local {
452         my ( $self, $c ) = @_;
453         my $dbh = $c->model;
454
455         $dbh->begin_work;
456         $c->forward('insertThread',[-1999]);
457
458         $c->req->parameters->{uid} = [$c->req->parameters->{uid}]
459                 unless ref $c->req->parameters->{uid} eq 'ARRAY';
460         push @{$c->req->parameters->{uid}}, $c->user->id;
461         $c->forward('addaccess',[$c->stash->{thread}]);
462
463         $c->forward('addPost',[$c->stash->{thread}]);
464         $dbh->commit;
465 }
466
467 sub addaccess : Private {
468         my ( $self, $c, $thread) = @_;
469         my $dbh = $c->model;
470
471         $c->req->parameters->{uid} = [$c->req->parameters->{uid}]
472                 unless ref $c->req->parameters->{uid} eq 'ARRAY';
473         my $query = $dbh->prepare(q{INSERT INTO forum_priv_access (ftid,uid)
474                 (SELECT $1,uid FROM users u WHERE uid = ANY ($2) AND NOT uid
475                         IN (SELECT uid FROM forum_priv_access WHERE ftid = $1))});
476         $query->execute($thread,$c->req->parameters->{uid});
477         $dbh->do(q{INSERT INTO forum_posts (ftid,uid,message)
478                 VALUES((SELECT ftid FROM users WHERE uid = $1),$1,$2)
479                 }, undef, $c->user->id
480                 ,"Gave access on thread $thread to : @{$c->req->parameters->{uid}}");
481 }
482
483 sub findUsers : Private {
484         my ( $self, $c ) = @_;
485         my $dbh = $c->model;
486
487         my $query = $dbh->prepare(q{SELECT uid,username FROM users
488                 WHERE uid > 0 AND uid IN (SELECT uid FROM groupmembers)
489                 ORDER BY username});
490         $query->execute;
491
492         $c->stash(users => $query->fetchall_arrayref({}) );
493 }
494
495 sub findThread : Private {
496         my ( $self, $c, $thread ) = @_;
497         my $dbh = $c->model;
498         my $findThread = $dbh->prepare(q{SELECT ft.ftid,ft.subject
499                 ,COALESCE(bool_or(fa.post),true) AS post, bool_or(fa.moderate) AS moderate
500                 ,ft.fbid,fb.board,fb.fcid,ft.sticky,fc.category
501                 FROM forum_boards fb
502                         NATURAL JOIN forum_threads ft
503                         NATURAL JOIN forum_categories fc
504                         LEFT OUTER JOIN (SELECT fa.* FROM forum_access fa
505                                 JOIN (SELECT groups($2) AS gid) g USING (gid)
506                         ) fa USING (fbid)
507                 WHERE ft.ftid = $1 AND (fa.post IS NOT NULL
508                         OR ft.ftid IN (SELECT ftid FROM forum_priv_access WHERE uid = $2))
509                 GROUP BY ft.ftid,ft.subject,ft.fbid,fb.board,fb.fcid,ft.sticky,fc.category
510         });
511         $thread = $dbh->selectrow_hashref($findThread,undef,$thread,$c->stash->{UID});
512         $c->stash(thread => $thread);
513 }
514
515 sub findBoard : Private {
516         my ( $self, $c, $board ) = @_;
517         my $dbh = $c->model;
518
519         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
520                         FROM forum_boards fb
521                                 NATURAL JOIN forum_categories fc
522                                 LEFT OUTER JOIN (SELECT * FROM forum_access
523                                         WHERE fbid = $1 AND gid IN (SELECT groups($2))
524                                 ) fa USING (fbid)
525                         WHERE fb.fbid = $1
526                         GROUP BY fb.fbid,fb.board,fb.fcid,fc.category
527                 });
528         $board = $dbh->selectrow_hashref($boards,undef,$board,$c->stash->{UID});
529
530         $c->stash(board => $board);
531 }
532
533 sub previewPost : Private {
534         my ( $self, $c) = @_;
535         push @{$c->stash->{posts}}, {
536                 unread => 1,
537                 username => 'PREVIEW',
538                 message => parseMarkup(html_escape $c->req->param('message')),
539         };
540         $c->stash(previewMessage => html_escape $c->req->param('message'));
541 }
542
543 sub insertPost : Private {
544         my ( $self, $c, $thread ) = @_;
545         my $dbh = $c->model;
546
547         my $insert = $dbh->prepare(q{INSERT INTO forum_posts (ftid,message,uid)
548                 VALUES($1,$2,$3)});
549         $insert->execute($thread,html_escape($c->req->param('message')),$c->stash->{UID});
550 }
551
552 sub listModeratorBoards : Private {
553         my ( $self, $c, $fbid ) = @_;
554         my $dbh = $c->model;
555
556         my $categories = $dbh->prepare(q{SELECT fcid,category FROM forum_categories ORDER BY fcid});
557         my $boards = $dbh->prepare(q{SELECT fb.fbid,fb.board, bool_or(fa.post) AS post
558                 FROM forum_boards fb NATURAL JOIN forum_access fa
559                 WHERE fb.fcid = $1
560                         AND gid IN (SELECT groups($2))
561                         AND moderate
562                 GROUP BY fb.fbid,fb.board
563                 ORDER BY fb.fbid
564                 });
565         $categories->execute;
566         my @categories;
567         while (my $category = $categories->fetchrow_hashref){
568                 $boards->execute($category->{fcid},$c->stash->{UID});
569
570                 my @boards;
571                 while (my $b = $boards->fetchrow_hashref){
572                         next if ($b->{fbid} == $fbid);
573                         push @boards,$b;
574                 }
575                 $category->{boards} = \@boards;
576                 push @categories,$category if @boards;
577         }
578         $c->stash(categories => \@categories);
579 }
580
581 =head1 AUTHOR
582
583 Michael Andreen (harv@ruin.nu)
584
585 =head1 LICENSE
586
587 GPL 2.0, or later.
588
589 =cut
590
591 1;