]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Forum.pm
Make it possible to move a thread from the thread view
[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         $c->forward('listModeratorBoards', [$board->{fbid}]) if $board->{moderate};
186         
187 }
188
189 sub thread : Local {
190         my ( $self, $c, $thread ) = @_;
191         my $dbh = $c->model;
192
193         $c->forward('findThread');
194         unless ($c->stash->{thread}){
195                 $c->stash(template => 'access_denied.tt2');
196                 return;
197         }
198         my $query = $dbh->prepare(q{SELECT uid,username FROM users u
199                 JOIN forum_priv_access fta USING (uid) WHERE fta.ftid = $1});
200         $query->execute($thread);
201         $c->stash(access => $query->fetchall_arrayref({}) );
202         $c->forward('findPosts');
203         $c->forward('markThreadAsRead') if $c->user_exists;
204         if ($c->stash->{thread}->{moderate}) {
205                 $c->forward('findUsers');
206                 $c->forward('listModeratorBoards', [$c->stash->{thread}->{fbid}]);
207         }
208 }
209
210 sub findPosts :Private {
211         my ( $self, $c, $thread ) = @_;
212         my $dbh = $c->model;
213
214         my $posts = $dbh->prepare(q{
215                 SELECT u.uid,u.username,date_trunc('seconds',fp.time::timestamp) AS time
216                         ,fp.message,COALESCE(fp.time > ftv.time,TRUE) AS unread
217                 FROM forum_threads ft
218                         JOIN forum_posts fp USING (ftid)
219                         JOIN users u ON u.uid = fp.uid
220                         LEFT OUTER JOIN 
221                                 (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
222                 WHERE ft.ftid = $1
223                 ORDER BY fp.time ASC
224                 });
225         $posts->execute($thread,$c->stash->{UID});
226
227         my @posts;
228         while (my $post = $posts->fetchrow_hashref){
229                 $post->{message} = parseMarkup($post->{message});
230                 push @posts,$post;
231         }
232
233         $c->stash(posts => \@posts);
234 }
235
236
237 sub markBoardAsRead : Local {
238         my ( $self, $c, $board, $time ) = @_;
239         my $dbh = $c->model;
240
241         $c->forward('findBoard');
242         $board = $c->stash->{board};
243
244         my $threads = $dbh->prepare(q{SELECT ft.ftid,ft.subject
245                         ,count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) AS unread
246                         ,count(fp.fpid) AS posts, max(fp.time)::timestamp as last_post
247                         FROM forum_threads ft 
248                                 JOIN forum_posts fp USING (ftid) 
249                                 LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
250                         WHERE ft.fbid = $1 AND fp.time <= $3
251                         GROUP BY ft.ftid, ft.subject
252                         HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= 1
253                 });
254         $threads->execute($board->{fbid},$c->user->id,$time);
255         $dbh->begin_work;
256         while (my $thread = $threads->fetchrow_hashref){
257                 $c->forward('markThreadAsRead',[$thread->{ftid}]);
258         }
259         $dbh->commit;
260         $c->forward('/redirect');
261 }
262
263 sub markThreadAsRead : Private {
264         my ( $self, $c, $thread ) = @_;
265         my $dbh = $c->model;
266
267         my $rows = $dbh->do(q{UPDATE forum_thread_visits SET time = now() 
268                 WHERE uid =     $1 AND ftid = $2
269                 },undef,$c->user->id,$thread);
270         if ($rows == 0){
271                 $dbh->do(q{INSERT INTO forum_thread_visits (uid,ftid)
272                         VALUES ($1,$2)}
273                         ,undef,$c->user->id,$thread);
274         }
275 }
276
277 sub moveThreads : Local {
278         my ( $self, $c, $board ) = @_;
279         my $dbh = $c->model;
280
281         $c->forward('findBoard',[$c->req->param('board')]);
282         my $toboard = $c->stash->{board};
283         unless ($toboard->{moderate}){
284                 $c->acl_access_denied('test',$c->action,'No moderator access for target board.')
285         }
286
287         $c->forward('findBoard');
288         $board = $c->stash->{board};
289         unless ($board->{moderate}){
290                 $c->acl_access_denied('test',$c->action,'No moderator access for source board.')
291         }
292
293         my $log = "Moved these threads:\n\n";
294         $dbh->begin_work;
295         my $moveThread = $dbh->prepare(q{UPDATE forum_threads SET fbid = $1 WHERE ftid = $2 AND fbid = $3});
296         for my $param ($c->req->param){
297                 if ($param =~ /t:(\d+)/){
298                         $moveThread->execute($toboard->{fbid},$1,$board->{fbid});
299                         if ($moveThread->rows > 0){
300                                 $log .= "$1\n";
301                         }
302                 }
303         }
304
305         $log .= "\nFrom board: $board->{board} ($board->{fbid})";
306         $log .= "\nTo board: $toboard->{board} ($toboard->{fbid})";
307         $dbh->do(q{INSERT INTO forum_posts (ftid,uid,message)
308                 VALUES((SELECT ftid FROM users WHERE uid = $1),$1,$2)
309                 }, undef, $c->user->id, $log);
310         $dbh->commit;
311         
312         $c->res->redirect($c->uri_for('board',$board->{fbid}));
313 }
314
315 sub newThread : Local {
316         my ( $self, $c, $board ) = @_;
317
318         $c->forward('findBoard');
319         $board = $c->stash->{board};
320
321         unless ($c->stash->{board}->{post}){
322                 $c->acl_access_denied('test',$c->action,'No post access to board.')
323         }
324
325         $c->forward('insertThread');
326         $c->forward('addPost',[$c->stash->{thread}]);
327 }
328
329 sub insertThread : Private {
330         my ( $self, $c, $board ) = @_;
331         my $dbh = $c->model;
332
333         my $insert = $dbh->prepare(q{INSERT INTO forum_threads (ftid,fbid,subject,uid)
334                 VALUES(DEFAULT,$1,$2,$3) RETURNING (ftid);
335                 });
336         $insert->execute($board,html_escape($c->req->param('subject')),$c->stash->{UID});
337         $c->stash(thread => $insert->fetchrow);
338         $insert->finish;
339 }
340
341 sub addPost : Local {
342         my ( $self, $c, $thread ) = @_;
343         my $dbh = $c->model;
344
345         if ($c->req->param('cmd') eq 'Submit'){
346                 $c->forward('findThread');
347                 unless ($c->stash->{thread}->{post}){
348                         $c->acl_access_denied('test',$c->action,'No post access to board.')
349                 }
350                 $c->forward('insertPost');
351                 $c->res->redirect($c->uri_for('thread',$thread));
352         }elsif ($c->req->param('cmd') eq 'Preview'){
353                 $c->forward('thread');
354                 $c->forward('previewPost');
355                 $c->stash(template => 'forum/thread.tt2');
356         }
357 }
358
359 sub setSticky : Local {
360         my ( $self, $c, $thread, $sticky ) = @_;
361         my $dbh = $c->model;
362
363         $c->forward('findThread');
364         unless ($c->stash->{thread}->{moderate}){
365                 $c->acl_access_denied('test',$c->action,'No moderator access to board.')
366         }
367
368         $dbh->do(q{UPDATE forum_threads SET sticky = $2 WHERE ftid = $1}
369                 , undef,$thread, $sticky);
370         $c->res->redirect($c->uri_for('thread',$thread));
371 }
372
373 sub postthreadaccess : Local {
374         my ( $self, $c, $thread) = @_;
375         my $dbh = $c->model;
376
377         $c->forward('findThread');
378         $dbh->begin_work;
379         unless ($c->stash->{thread}->{moderate}){
380                 $c->acl_access_denied('test',$c->action,'No moderator access to board.')
381         }
382         if ($c->req->param('access')){
383                 $c->req->parameters->{access} = [$c->req->parameters->{access}]
384                         unless ref $c->req->parameters->{access} eq 'ARRAY';
385                 my $query = $dbh->prepare(q{DELETE From forum_priv_access
386                         WHERE ftid = $1 AND uid = ANY ($2)});
387                 $query->execute($thread,$c->req->parameters->{access});
388                 $dbh->do(q{INSERT INTO forum_posts (ftid,uid,message)
389                         VALUES((SELECT ftid FROM users WHERE uid = $1),$1,$2)
390                         }, undef, $c->user->id
391                         ,"Removed access on thread $thread for : @{$c->req->parameters->{access}}");
392         }
393         if ($c->req->param('uid')){
394                 $c->forward('addaccess');
395         }
396         $dbh->commit;
397         $c->res->redirect($c->uri_for('thread',$thread));
398 }
399
400 sub removeownthreadaccess : Local {
401         my ( $self, $c, $thread) = @_;
402         my $dbh = $c->model;
403         $dbh->do(q{DELETE FROM forum_priv_access WHERE uid = $1 AND ftid = $2}
404                 ,undef,$c->user->id,$thread);
405         $c->res->redirect($c->uri_for('allUnread'));
406 }
407
408 sub privmsg : Local {
409         my ( $self, $c, $uid ) = @_;
410
411         $uid ||= 0;
412         $c->stash(uid => $uid);
413
414         $c->forward('findUsers');
415 }
416
417 sub postprivmsg : Local {
418         my ( $self, $c ) = @_;
419         my $dbh = $c->model;
420
421         $dbh->begin_work;
422         $c->forward('insertThread',[-1999]);
423
424         $c->req->parameters->{uid} = [$c->req->parameters->{uid}]
425                 unless ref $c->req->parameters->{uid} eq 'ARRAY';
426         push @{$c->req->parameters->{uid}}, $c->user->id;
427         $c->forward('addaccess',[$c->stash->{thread}]);
428
429         $c->forward('addPost',[$c->stash->{thread}]);
430         $dbh->commit;
431 }
432
433 sub addaccess : Private {
434         my ( $self, $c, $thread) = @_;
435         my $dbh = $c->model;
436
437         $c->req->parameters->{uid} = [$c->req->parameters->{uid}]
438                 unless ref $c->req->parameters->{uid} eq 'ARRAY';
439         my $query = $dbh->prepare(q{INSERT INTO forum_priv_access (ftid,uid)
440                 (SELECT $1,uid FROM users u WHERE uid = ANY ($2) AND NOT uid
441                         IN (SELECT uid FROM forum_priv_access WHERE ftid = $1))});
442         $query->execute($thread,$c->req->parameters->{uid});
443         $dbh->do(q{INSERT INTO forum_posts (ftid,uid,message)
444                 VALUES((SELECT ftid FROM users WHERE uid = $1),$1,$2)
445                 }, undef, $c->user->id
446                 ,"Gave access on thread $thread to : @{$c->req->parameters->{uid}}");
447 }
448
449 sub findUsers : Private {
450         my ( $self, $c ) = @_;
451         my $dbh = $c->model;
452
453         my $query = $dbh->prepare(q{SELECT uid,username FROM users
454                 WHERE uid > 0 AND uid IN (SELECT uid FROM groupmembers)
455                 ORDER BY LOWER(username)});
456         $query->execute;
457
458         $c->stash(users => $query->fetchall_arrayref({}) );
459 }
460
461 sub findThread : Private {
462         my ( $self, $c, $thread ) = @_;
463         my $dbh = $c->model;
464         my $findThread = $dbh->prepare(q{SELECT ft.ftid,ft.subject
465                 ,COALESCE(bool_or(fa.post),true) AS post, bool_or(fa.moderate) AS moderate
466                 ,ft.fbid,fb.board,fb.fcid,ft.sticky,fc.category
467                 FROM forum_boards fb
468                         NATURAL JOIN forum_threads ft
469                         NATURAL JOIN forum_categories fc
470                         LEFT OUTER JOIN (SELECT * FROM forum_access
471                                 WHERE gid IN (SELECT groups($2))
472                         ) fa USING (fbid)
473                 WHERE ft.ftid = $1 AND (fa.post IS NOT NULL
474                         OR ft.ftid IN (SELECT ftid FROM forum_priv_access WHERE uid = $2))
475                 GROUP BY ft.ftid,ft.subject,ft.fbid,fb.board,fb.fcid,ft.sticky,fc.category
476         });
477         $thread = $dbh->selectrow_hashref($findThread,undef,$thread,$c->stash->{UID});
478         $c->stash(thread => $thread);
479 }
480
481 sub findBoard : Private {
482         my ( $self, $c, $board ) = @_;
483         my $dbh = $c->model;
484
485         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
486                         FROM forum_boards fb
487                                 NATURAL JOIN forum_categories fc
488                                 LEFT OUTER JOIN (SELECT * FROM forum_access
489                                         WHERE fbid = $1 AND gid IN (SELECT groups($2))
490                                 ) fa USING (fbid)
491                         WHERE fb.fbid = $1
492                         GROUP BY fb.fbid,fb.board,fb.fcid,fc.category
493                 });
494         $board = $dbh->selectrow_hashref($boards,undef,$board,$c->stash->{UID});
495
496         $c->stash(board => $board);
497 }
498
499 sub previewPost : Private {
500         my ( $self, $c) = @_;
501         push @{$c->stash->{posts}}, {
502                 unread => 1,
503                 username => 'PREVIEW',
504                 message => parseMarkup(html_escape $c->req->param('message')),
505         };
506         $c->stash(previewMessage => html_escape $c->req->param('message'));
507 }
508
509 sub insertPost : Private {
510         my ( $self, $c, $thread ) = @_;
511         my $dbh = $c->model;
512
513         my $insert = $dbh->prepare(q{INSERT INTO forum_posts (ftid,message,uid)
514                 VALUES($1,$2,$3)});
515         $insert->execute($thread,html_escape($c->req->param('message')),$c->stash->{UID});
516 }
517
518 sub listModeratorBoards : Private {
519         my ( $self, $c, $fbid ) = @_;
520         my $dbh = $c->model;
521
522         my $categories = $dbh->prepare(q{SELECT fcid,category FROM forum_categories ORDER BY fcid});
523         my $boards = $dbh->prepare(q{SELECT fb.fbid,fb.board, bool_or(fa.post) AS post
524                 FROM forum_boards fb NATURAL JOIN forum_access fa
525                 WHERE fb.fcid = $1
526                         AND gid IN (SELECT groups($2))
527                         AND moderate
528                 GROUP BY fb.fbid,fb.board
529                 ORDER BY fb.fbid
530                 });
531         $categories->execute;
532         my @categories;
533         while (my $category = $categories->fetchrow_hashref){
534                 $boards->execute($category->{fcid},$c->stash->{UID});
535
536                 my @boards;
537                 while (my $b = $boards->fetchrow_hashref){
538                         next if ($b->{fbid} == $fbid);
539                         push @boards,$b;
540                 }
541                 $category->{boards} = \@boards;
542                 push @categories,$category if @boards;
543         }
544         $c->stash(categories => \@categories);
545 }
546
547 =head1 AUTHOR
548
549 Michael Andreen (harv@ruin.nu)
550
551 =head1 LICENSE
552
553 GPL 2.0, or later.
554
555 =cut
556
557 1;