1 package NDWeb::Controller::Forum;
5 use parent 'Catalyst::Controller';
11 NDWeb::Controller::Forum - Catalyst Controller
25 sub index :Path :Args(0) {
26 my ( $self, $c ) = @_;
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
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
43 $boards->execute($c->stash->{UID});
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;
52 push @{$category->{boards}},$board;
54 $c->stash(categories => \@categories);
57 sub allUnread : Local {
58 my ( $self, $c ) = @_;
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
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
79 $threads->execute($c->stash->{UID});
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;
88 if ($board->{fbid} != $thread->{fbid}){
89 $board = {fbid => $thread->{fbid}, board => $thread->{board}};
90 push @{$category->{boards}},$board;
92 delete $thread->{fcid};
93 delete $thread->{fbid};
94 delete $thread->{category};
95 delete $thread->{board};
96 push @{$board->{threads}},$thread;
98 $c->stash(categories => \@categories);
99 $c->stash(time => $dbh->selectrow_array('SELECT now()::timestamp',undef));
104 my ( $self, $c ) = @_;
109 if ($c->req->param('search')){
110 push @queries,'('.$c->req->param('search').')';
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'.$_) ? '&' : '|';
118 my $query = join " $op ", map {"$_:$cat"} @words;
119 push @queries,"($query)";
122 my $search = join ' & ', @queries;
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
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)
139 $posts->execute($c->stash->{UID},$search);
141 while (my $post = $posts->fetchrow_hashref){
144 $c->stash(searchresults => \@posts);
147 $c->stash( searcherror => $dbh->errstr);
155 my ( $self, $c, $board ) = @_;
158 $c->stash(time => $dbh->selectrow_array('SELECT now()::timestamp',undef));
160 $c->forward('findBoard');
161 $board = $c->stash->{board};
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)
175 GROUP BY ft.ftid, ft.subject,ft.sticky,u.username
176 ORDER BY sticky DESC,last_post DESC
178 $threads->execute($board->{fbid},$c->stash->{UID});
180 while (my $thread = $threads->fetchrow_hashref){
181 push @threads,$thread;
183 $c->stash(threads => \@threads);
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
194 $categories->execute;
196 while (my $category = $categories->fetchrow_hashref){
197 $boards->execute($category->{fcid},$c->stash->{UID});
200 while (my $b = $boards->fetchrow_hashref){
201 next if (not $b->{post} or $b->{fbid} == $board->{fbid});
204 $category->{boards} = \@boards;
205 push @categories,$category if @boards;
207 $c->stash(categories => \@categories);
213 my ( $self, $c, $thread ) = @_;
216 $c->forward('findThread');
217 unless ($c->stash->{thread}){
218 $c->stash(template => 'access_denied.tt2');
221 $c->forward('findPosts');
222 $c->forward('markThreadAsRead') if $c->user_exists;
225 sub findPosts :Private {
226 my ( $self, $c, $thread ) = @_;
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
236 (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
240 $posts->execute($thread,$c->stash->{UID});
243 while (my $post = $posts->fetchrow_hashref){
244 $post->{message} = parseMarkup($post->{message});
248 $c->stash(posts => \@posts);
252 sub markBoardAsRead : Local {
253 my ( $self, $c, $board, $time ) = @_;
256 $c->forward('findBoard');
257 $board = $c->stash->{board};
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
269 $threads->execute($board->{fbid},$c->user->id,$time);
271 while (my $thread = $threads->fetchrow_hashref){
272 $c->forward('markThreadAsRead',[$thread->{ftid}]);
275 $c->res->redirect($c->req->referer);
278 sub markThreadAsRead : Private {
279 my ( $self, $c, $thread ) = @_;
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);
286 $dbh->do(q{INSERT INTO forum_thread_visits (uid,ftid)
288 ,undef,$c->user->id,$thread);
292 sub moveThreads : Local {
293 my ( $self, $c, $board ) = @_;
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.')
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.')
308 my $log = "Moved these threads:\n\n";
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){
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);
327 $c->res->redirect($c->uri_for('board',$board->{fbid}));
330 sub newThread : Local {
331 my ( $self, $c, $board ) = @_;
333 $c->forward('findBoard');
334 $board = $c->stash->{board};
336 unless ($c->stash->{board}->{post}){
337 $c->acl_access_denied('test',$c->action,'No post access to board.')
340 $c->forward('insertThread');
341 $c->forward('addPost',[$c->stash->{thread}]);
344 sub insertThread : Private {
345 my ( $self, $c, $board ) = @_;
348 my $insert = $dbh->prepare(q{INSERT INTO forum_threads (ftid,fbid,subject,uid)
349 VALUES(DEFAULT,$1,$2,$3) RETURNING (ftid);
351 $insert->execute($board,html_escape($c->req->param('subject')),$c->stash->{UID});
352 $c->stash(thread => $insert->fetchrow);
356 sub addPost : Local {
357 my ( $self, $c, $thread ) = @_;
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.')
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');
374 sub setSticky : Local {
375 my ( $self, $c, $thread, $sticky ) = @_;
378 $c->forward('findThread');
379 unless ($c->stash->{thread}->{moderate}){
380 $c->acl_access_denied('test',$c->action,'No moderator access to board.')
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));
388 sub findThread : Private {
389 my ( $self, $c, $thread ) = @_;
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
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))
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
404 $thread = $dbh->selectrow_hashref($findThread,undef,$thread,$c->stash->{UID});
405 $c->stash(thread => $thread);
408 sub findBoard : Private {
409 my ( $self, $c, $board ) = @_;
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
414 NATURAL JOIN forum_categories fc
415 LEFT OUTER JOIN (SELECT * FROM forum_access
416 WHERE fbid = $1 AND gid IN (SELECT groups($2))
419 GROUP BY fb.fbid,fb.board,fb.fcid,fc.category
421 $board = $dbh->selectrow_hashref($boards,undef,$board,$c->stash->{UID});
423 $c->stash(board => $board);
426 sub previewPost : Private {
427 my ( $self, $c) = @_;
428 push @{$c->stash->{posts}}, {
430 username => 'PREVIEW',
431 message => parseMarkup(html_escape $c->req->param('message')),
433 $c->stash(previewMessage => html_escape $c->req->param('message'));
436 sub insertPost : Private {
437 my ( $self, $c, $thread ) = @_;
440 my $insert = $dbh->prepare(q{INSERT INTO forum_posts (ftid,message,uid)
442 $insert->execute($thread,html_escape($c->req->param('message')),$c->stash->{UID});
447 Michael Andreen (harv@ruin.nu)