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 GROUP BY fcid,category,fb.fbid, fb.board
42 $boards->execute($c->stash->{UID});
45 my $category = {fcid => 0};
46 while (my $board = $boards->fetchrow_hashref){
47 if ($category->{fcid} != $board->{fcid}){
48 $category = {fcid => $board->{fcid}, category => $board->{category}};
49 push @categories,$category;
51 push @{$category->{boards}},$board;
53 $c->stash(categories => \@categories);
56 sub allUnread : Local {
57 my ( $self, $c ) = @_;
60 my $threads = $dbh->prepare(q{SELECT fcid,category,fbid,board,ft.ftid,u.username,ft.subject,
61 count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) AS unread,count(fp.fpid) AS posts,
62 date_trunc('seconds',max(fp.time)::timestamp) as last_post,
63 min(fp.time)::date as posting_date, ft.sticky
64 FROM forum_categories fc
65 JOIN forum_boards fb USING (fcid)
66 JOIN forum_threads ft USING (fbid)
67 JOIN forum_posts fp USING (ftid)
68 JOIN users u ON u.uid = ft.uid
69 LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $1) ftv ON ftv.ftid = ft.ftid
71 fb.fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($1)))
72 GROUP BY fcid,category,fbid,board,ft.ftid, ft.subject,ft.sticky,u.username
73 HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= 1
74 ORDER BY fcid,fbid,sticky DESC,last_post DESC
77 $threads->execute($c->stash->{UID});
79 my $category = {fcid => 0};
80 my $board = {fbid => 0};
81 while (my $thread = $threads->fetchrow_hashref){
82 if ($category->{fcid} != $thread->{fcid}){
83 $category = {fcid => $thread->{fcid}, category => $thread->{category}};
84 push @categories,$category;
86 if ($board->{fbid} != $thread->{fbid}){
87 $board = {fbid => $thread->{fbid}, board => $thread->{board}};
88 push @{$category->{boards}},$board;
90 delete $thread->{fcid};
91 delete $thread->{fbid};
92 delete $thread->{category};
93 delete $thread->{board};
94 push @{$board->{threads}},$thread;
96 $c->stash(categories => \@categories);
97 $c->stash(time => $dbh->selectrow_array('SELECT now()::timestamp',undef));
102 my ( $self, $c ) = @_;
107 if ($c->req->param('search')){
108 push @queries,'('.$c->req->param('search').')';
110 my %cat = (body => 'D', topic => 'A', author => 'B');
111 for ('body','topic','author'){
112 if ($c->req->param($_)){
113 my @words = split /\W+/,$c->req->param($_);
114 my $op = $c->req->param('all'.$_) ? '&' : '|';
116 my $query = join " $op ", map {"$_:$cat"} @words;
117 push @queries,"($query)";
120 my $search = join ' & ', @queries;
123 my $posts = $dbh->prepare(q{SELECT fp.ftid,u.username,ft.subject
124 ,ts_headline(fp.message,to_tsquery($2)) AS headline
125 ,ts_rank_cd(fp.textsearch, to_tsquery($2),32) AS rank
127 JOIN forum_threads ft USING (fbid)
128 JOIN forum_posts fp USING (ftid)
129 JOIN users u ON fp.uid = u.uid
130 WHERE fb.fbid IN (SELECT fbid FROM forum_access
131 WHERE gid IN (SELECT groups($1)))
132 AND fp.textsearch @@@ to_tsquery($2)
136 $posts->execute($c->stash->{UID},$search);
138 while (my $post = $posts->fetchrow_hashref){
141 $c->stash(searchresults => \@posts);
144 $c->stash( searcherror => $dbh->errstr);
152 my ( $self, $c, $board ) = @_;
155 $c->stash(time => $dbh->selectrow_array('SELECT now()::timestamp',undef));
157 $c->forward('findBoard');
158 $board = $c->stash->{board};
160 my $threads = $dbh->prepare(q{SELECT ft.ftid,u.username,ft.subject
161 ,count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) AS unread,count(fp.fpid) AS posts
162 ,date_trunc('seconds',max(fp.time)::timestamp) as last_post
163 ,min(fp.time)::date as posting_date, ft.sticky
164 FROM forum_threads ft
165 JOIN forum_posts fp USING (ftid)
166 JOIN users u ON u.uid = ft.uid
167 LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
169 GROUP BY ft.ftid, ft.subject,ft.sticky,u.username
170 ORDER BY sticky DESC,last_post DESC
172 $threads->execute($board->{fbid},$c->stash->{UID});
174 while (my $thread = $threads->fetchrow_hashref){
175 push @threads,$thread;
177 $c->stash(threads => \@threads);
179 if ($board->{moderate}){
180 my $categories = $dbh->prepare(q{SELECT fcid,category FROM forum_categories ORDER BY fcid});
181 my $boards = $dbh->prepare(q{SELECT fb.fbid,fb.board, bool_or(fa.post) AS post
182 FROM forum_boards fb NATURAL JOIN forum_access fa
183 WHERE fb.fcid = $1 AND
184 gid IN (SELECT groups($2))
185 GROUP BY fb.fbid,fb.board
188 $categories->execute;
190 while (my $category = $categories->fetchrow_hashref){
191 $boards->execute($category->{fcid},$c->stash->{UID});
194 while (my $b = $boards->fetchrow_hashref){
195 next if (not $b->{post} or $b->{fbid} == $board->{fbid});
198 $category->{boards} = \@boards;
199 push @categories,$category if @boards;
201 $c->stash(categories => \@categories);
207 my ( $self, $c, $thread ) = @_;
210 $c->forward('findThread');
211 $thread = $c->stash->{thread};
213 my $posts = $dbh->prepare(q{
214 SELECT u.username,date_trunc('seconds',fp.time::timestamp) AS time
215 ,fp.message,COALESCE(fp.time > ftv.time,TRUE) AS unread
216 FROM forum_threads ft
217 JOIN forum_posts fp USING (ftid)
218 JOIN users u ON u.uid = fp.uid
220 (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
224 $posts->execute($c->stash->{thread}->{ftid},$c->stash->{UID});
227 while (my $post = $posts->fetchrow_hashref){
228 $post->{message} = parseMarkup($post->{message});
232 $c->stash(posts => \@posts);
233 $c->forward('markThreadAsRead') if $c->user_exists;
237 sub markBoardAsRead : Local {
238 my ( $self, $c, $board, $time ) = @_;
241 $c->forward('findBoard');
242 $board = $c->stash->{board};
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
254 $threads->execute($board->{fbid},$c->user->id,$time);
256 while (my $thread = $threads->fetchrow_hashref){
257 $c->forward('markThreadAsRead',[$thread->{ftid}]);
260 $c->res->redirect($c->req->referer);
263 sub markThreadAsRead : Private {
264 my ( $self, $c, $thread ) = @_;
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);
271 $dbh->do(q{INSERT INTO forum_thread_visits (uid,ftid)
273 ,undef,$c->user->id,$thread);
277 sub moveThreads : Local {
278 my ( $self, $c, $board ) = @_;
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.')
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.')
293 my $log = "Moved these threads:\n\n";
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){
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);
312 $c->res->redirect($c->uri_for('board',$board->{fbid}));
315 sub newThread : Local {
316 my ( $self, $c, $board ) = @_;
319 $c->forward('findBoard');
320 $board = $c->stash->{board};
322 unless ($c->stash->{board}->{post}){
323 $c->acl_access_denied('test',$c->action,'No post access to board.')
326 my $insert = $dbh->prepare(q{INSERT INTO forum_threads (ftid,fbid,subject,uid)
327 VALUES(DEFAULT,$1,$2,$3) RETURNING (ftid);
330 $insert->execute($board->{fbid},html_escape($c->req->param('subject')),$c->stash->{UID});
331 my $thread = $insert->fetchrow;
333 $c->forward('addPost',[$thread]);
337 sub addPost : Local {
338 my ( $self, $c, $thread ) = @_;
341 if ($c->req->param('cmd') eq 'Submit'){
342 $c->forward('findThread');
343 unless ($c->stash->{thread}->{post}){
344 $c->acl_access_denied('test',$c->action,'No post access to board.')
346 $c->forward('insertPost');
347 $c->res->redirect($c->uri_for('thread',$thread));
348 }elsif ($c->req->param('cmd') eq 'Preview'){
349 $c->forward('thread');
350 $c->forward('previewPost');
351 $c->stash(template => 'forum/thread.tt2');
355 sub setSticky : Local {
356 my ( $self, $c, $thread, $sticky ) = @_;
359 $c->forward('findThread');
360 unless ($c->stash->{thread}->{moderate}){
361 $c->acl_access_denied('test',$c->action,'No moderator access to board.')
364 $dbh->do(q{UPDATE forum_threads SET sticky = $2 WHERE ftid = $1}
365 , undef,$thread, $sticky);
366 $c->res->redirect($c->uri_for('thread',$thread));
369 sub findThread : Private {
370 my ( $self, $c, $thread ) = @_;
372 my $findThread = $dbh->prepare(q{SELECT ft.ftid,ft.subject, bool_or(fa.post) AS post
373 , bool_or(fa.moderate) AS moderate,ft.fbid,fb.board,fb.fcid,ft.sticky,fc.category
375 NATURAL JOIN forum_access fa
376 NATURAL JOIN forum_threads ft
377 NATURAL JOIN forum_categories fc
378 WHERE ft.ftid = $1 AND gid IN (SELECT groups($2))
379 GROUP BY ft.ftid,ft.subject,ft.fbid,fb.board,fb.fcid,ft.sticky,fc.category
381 $thread = $dbh->selectrow_hashref($findThread,undef,$thread,$c->stash->{UID});
382 $c->stash(thread => $thread);
385 sub findBoard : Private {
386 my ( $self, $c, $board ) = @_;
389 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
391 NATURAL JOIN forum_access fa
392 NATURAL JOIN forum_categories fc
393 WHERE fb.fbid = $1 AND
394 gid IN (SELECT groups($2))
395 GROUP BY fb.fbid,fb.board,fb.fcid,fc.category
397 $board = $dbh->selectrow_hashref($boards,undef,$board,$c->stash->{UID});
399 $c->stash(board => $board);
402 sub previewPost : Private {
403 my ( $self, $c) = @_;
404 push @{$c->stash->{posts}}, {
406 username => 'PREVIEW',
407 message => parseMarkup(html_escape $c->req->param('message')),
409 $c->stash(previewMessage => html_escape $c->req->param('message'));
412 sub insertPost : Private {
413 my ( $self, $c, $thread ) = @_;
416 my $insert = $dbh->prepare(q{INSERT INTO forum_posts (ftid,message,uid)
418 $insert->execute($thread,html_escape($c->req->param('message')),$c->stash->{UID});
423 Michael Andreen (harv@ruin.nu)