]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Forum.pm
Sending private messages or 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         my $query = $dbh->prepare(q{SELECT uid,username FROM users u
222                 JOIN forum_priv_access fta USING (uid) WHERE fta.ftid = $1});
223         $query->execute($thread);
224         $c->stash(access => $query->fetchall_arrayref({}) );
225         $c->forward('findUsers') if $c->stash->{thread}->{moderate};
226         $c->forward('findPosts');
227         $c->forward('markThreadAsRead') if $c->user_exists;
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->res->redirect($c->req->referer);
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 moveThreads : Local {
298         my ( $self, $c, $board ) = @_;
299         my $dbh = $c->model;
300
301         $c->forward('findBoard',[$c->req->param('board')]);
302         my $toboard = $c->stash->{board};
303         unless ($toboard->{moderate}){
304                 $c->acl_access_denied('test',$c->action,'No moderator access for target board.')
305         }
306
307         $c->forward('findBoard');
308         $board = $c->stash->{board};
309         unless ($board->{moderate}){
310                 $c->acl_access_denied('test',$c->action,'No moderator access for source board.')
311         }
312
313         my $log = "Moved these threads:\n\n";
314         $dbh->begin_work;
315         my $moveThread = $dbh->prepare(q{UPDATE forum_threads SET fbid = $1 WHERE ftid = $2 AND fbid = $3});
316         for my $param ($c->req->param){
317                 if ($param =~ /t:(\d+)/){
318                         $moveThread->execute($toboard->{fbid},$1,$board->{fbid});
319                         if ($moveThread->rows > 0){
320                                 $log .= "$1\n";
321                         }
322                 }
323         }
324
325         $log .= "\nFrom board: $board->{board} ($board->{fbid})";
326         $log .= "\nTo board: $toboard->{board} ($toboard->{fbid})";
327         $dbh->do(q{INSERT INTO forum_posts (ftid,uid,message)
328                 VALUES((SELECT ftid FROM users WHERE uid = $1),$1,$2)
329                 }, undef, $c->user->id, $log);
330         $dbh->commit;
331         
332         $c->res->redirect($c->uri_for('board',$board->{fbid}));
333 }
334
335 sub newThread : Local {
336         my ( $self, $c, $board ) = @_;
337
338         $c->forward('findBoard');
339         $board = $c->stash->{board};
340
341         unless ($c->stash->{board}->{post}){
342                 $c->acl_access_denied('test',$c->action,'No post access to board.')
343         }
344
345         $c->forward('insertThread');
346         $c->forward('addPost',[$c->stash->{thread}]);
347 }
348
349 sub insertThread : Private {
350         my ( $self, $c, $board ) = @_;
351         my $dbh = $c->model;
352
353         my $insert = $dbh->prepare(q{INSERT INTO forum_threads (ftid,fbid,subject,uid)
354                 VALUES(DEFAULT,$1,$2,$3) RETURNING (ftid);
355                 });
356         $insert->execute($board,html_escape($c->req->param('subject')),$c->stash->{UID});
357         $c->stash(thread => $insert->fetchrow);
358         $insert->finish;
359 }
360
361 sub addPost : Local {
362         my ( $self, $c, $thread ) = @_;
363         my $dbh = $c->model;
364
365         if ($c->req->param('cmd') eq 'Submit'){
366                 $c->forward('findThread');
367                 unless ($c->stash->{thread}->{post}){
368                         $c->acl_access_denied('test',$c->action,'No post access to board.')
369                 }
370                 $c->forward('insertPost');
371                 $c->res->redirect($c->uri_for('thread',$thread));
372         }elsif ($c->req->param('cmd') eq 'Preview'){
373                 $c->forward('thread');
374                 $c->forward('previewPost');
375                 $c->stash(template => 'forum/thread.tt2');
376         }
377 }
378
379 sub setSticky : Local {
380         my ( $self, $c, $thread, $sticky ) = @_;
381         my $dbh = $c->model;
382
383         $c->forward('findThread');
384         unless ($c->stash->{thread}->{moderate}){
385                 $c->acl_access_denied('test',$c->action,'No moderator access to board.')
386         }
387
388         $dbh->do(q{UPDATE forum_threads SET sticky = $2 WHERE ftid = $1}
389                 , undef,$thread, $sticky);
390         $c->res->redirect($c->uri_for('thread',$thread));
391 }
392
393 sub postthreadaccess : Local {
394         my ( $self, $c, $thread) = @_;
395         my $dbh = $c->model;
396
397         $c->forward('findThread');
398         $dbh->begin_work;
399         unless ($c->stash->{thread}->{moderate}){
400                 $c->acl_access_denied('test',$c->action,'No moderator access to board.')
401         }
402         if ($c->req->param('access')){
403                 $c->req->parameters->{access} = [$c->req->parameters->{access}]
404                         unless ref $c->req->parameters->{access} eq 'ARRAY';
405                 my $query = $dbh->prepare(q{DELETE From forum_priv_access
406                         WHERE ftid = $1 AND uid = ANY ($2)});
407                 $query->execute($thread,$c->req->parameters->{access});
408                 $dbh->do(q{INSERT INTO forum_posts (ftid,uid,message)
409                         VALUES((SELECT ftid FROM users WHERE uid = $1),$1,$2)
410                         }, undef, $c->user->id
411                         ,"Removed access on thread $thread for : @{$c->req->parameters->{access}}");
412         }
413         if ($c->req->param('uid')){
414                 $c->forward('addaccess');
415         }
416         $dbh->commit;
417         $c->res->redirect($c->uri_for('thread',$thread));
418 }
419
420 sub removeownthreadaccess : Local {
421         my ( $self, $c, $thread) = @_;
422         my $dbh = $c->model;
423         $dbh->do(q{DELETE FROM forum_priv_access WHERE uid = $1 AND ftid = $2}
424                 ,undef,$c->user->id,$thread);
425         $c->res->redirect($c->uri_for('allUnread'));
426 }
427
428 sub privmsg : Local {
429         my ( $self, $c, $uid ) = @_;
430
431         $uid ||= 0;
432         $c->stash(uid => $uid);
433
434         $c->forward('findUsers');
435 }
436
437 sub postprivmsg : Local {
438         my ( $self, $c ) = @_;
439         my $dbh = $c->model;
440
441         $dbh->begin_work;
442         $c->forward('insertThread',[-1999]);
443
444         $c->req->parameters->{uid} = [$c->req->parameters->{uid}]
445                 unless ref $c->req->parameters->{uid} eq 'ARRAY';
446         push @{$c->req->parameters->{uid}}, $c->user->id;
447         $c->forward('addaccess',[$c->stash->{thread}]);
448
449         $c->forward('addPost',[$c->stash->{thread}]);
450         $dbh->commit;
451 }
452
453 sub addaccess : Private {
454         my ( $self, $c, $thread) = @_;
455         my $dbh = $c->model;
456
457         $c->req->parameters->{uid} = [$c->req->parameters->{uid}]
458                 unless ref $c->req->parameters->{uid} eq 'ARRAY';
459         my $query = $dbh->prepare(q{INSERT INTO forum_priv_access (ftid,uid)
460                 (SELECT $1,uid FROM users u WHERE uid = ANY ($2) AND NOT uid
461                         IN (SELECT uid FROM forum_priv_access WHERE ftid = $1))});
462         $query->execute($thread,$c->req->parameters->{uid});
463         $dbh->do(q{INSERT INTO forum_posts (ftid,uid,message)
464                 VALUES((SELECT ftid FROM users WHERE uid = $1),$1,$2)
465                 }, undef, $c->user->id
466                 ,"Gave access on thread $thread to : @{$c->req->parameters->{uid}}");
467 }
468
469 sub findUsers : Private {
470         my ( $self, $c ) = @_;
471         my $dbh = $c->model;
472
473         my $query = $dbh->prepare(q{SELECT uid,username FROM users
474                 WHERE uid > 0 AND uid IN (SELECT uid FROM groupmembers)
475                 ORDER BY LOWER(username)});
476         $query->execute;
477
478         $c->stash(users => $query->fetchall_arrayref({}) );
479 }
480
481 sub findThread : Private {
482         my ( $self, $c, $thread ) = @_;
483         my $dbh = $c->model;
484         my $findThread = $dbh->prepare(q{SELECT ft.ftid,ft.subject
485                 ,COALESCE(bool_or(fa.post),true) AS post, bool_or(fa.moderate) AS moderate
486                 ,ft.fbid,fb.board,fb.fcid,ft.sticky,fc.category
487                 FROM forum_boards fb
488                         NATURAL JOIN forum_threads ft
489                         NATURAL JOIN forum_categories fc
490                         LEFT OUTER JOIN (SELECT * FROM forum_access
491                                 WHERE gid IN (SELECT groups($2))
492                         ) fa USING (fbid)
493                 WHERE ft.ftid = $1 AND (fa.post IS NOT NULL
494                         OR ft.ftid IN (SELECT ftid FROM forum_priv_access WHERE uid = $2))
495                 GROUP BY ft.ftid,ft.subject,ft.fbid,fb.board,fb.fcid,ft.sticky,fc.category
496         });
497         $thread = $dbh->selectrow_hashref($findThread,undef,$thread,$c->stash->{UID});
498         $c->stash(thread => $thread);
499 }
500
501 sub findBoard : Private {
502         my ( $self, $c, $board ) = @_;
503         my $dbh = $c->model;
504
505         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
506                         FROM forum_boards fb
507                                 NATURAL JOIN forum_categories fc
508                                 LEFT OUTER JOIN (SELECT * FROM forum_access
509                                         WHERE fbid = $1 AND gid IN (SELECT groups($2))
510                                 ) fa USING (fbid)
511                         WHERE fb.fbid = $1
512                         GROUP BY fb.fbid,fb.board,fb.fcid,fc.category
513                 });
514         $board = $dbh->selectrow_hashref($boards,undef,$board,$c->stash->{UID});
515
516         $c->stash(board => $board);
517 }
518
519 sub previewPost : Private {
520         my ( $self, $c) = @_;
521         push @{$c->stash->{posts}}, {
522                 unread => 1,
523                 username => 'PREVIEW',
524                 message => parseMarkup(html_escape $c->req->param('message')),
525         };
526         $c->stash(previewMessage => html_escape $c->req->param('message'));
527 }
528
529 sub insertPost : Private {
530         my ( $self, $c, $thread ) = @_;
531         my $dbh = $c->model;
532
533         my $insert = $dbh->prepare(q{INSERT INTO forum_posts (ftid,message,uid)
534                 VALUES($1,$2,$3)});
535         $insert->execute($thread,html_escape($c->req->param('message')),$c->stash->{UID});
536 }
537
538 =head1 AUTHOR
539
540 Michael Andreen (harv@ruin.nu)
541
542 =head1 LICENSE
543
544 GPL 2.0, or later.
545
546 =cut
547
548 1;