]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Forum.pm
4145781c7c2e346a69f5e7b618fd78d258cf259c
[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                         GROUP BY fcid,category,fb.fbid, fb.board
40                         ORDER BY fcid,fb.fbid
41                 });
42                 $boards->execute($c->stash->{UID});
43
44         my @categories;
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;
50                 }
51                 push @{$category->{boards}},$board;
52         }
53         $c->stash(categories => \@categories);
54 }
55
56 sub allUnread : Local {
57         my ( $self, $c ) = @_;
58         my $dbh = $c->model;
59
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
70                 WHERE fbid > 0 AND
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
75                 });
76
77         $threads->execute($c->stash->{UID});
78         my @categories;
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;
85                 }
86                 if ($board->{fbid} != $thread->{fbid}){
87                         $board = {fbid => $thread->{fbid}, board => $thread->{board}};
88                         push @{$category->{boards}},$board;
89                 }
90                 delete $thread->{fcid};
91                 delete $thread->{fbid};
92                 delete $thread->{category};
93                 delete $thread->{board};
94                 push @{$board->{threads}},$thread;
95         }
96         $c->stash(categories => \@categories);
97         $c->stash(time => $dbh->selectrow_array('SELECT now()::timestamp',undef));
98 }
99
100
101 sub search : Local {
102         my ( $self, $c ) = @_;
103
104         my $dbh = $c->model;
105
106         my @queries;
107         if ($c->req->param('search')){
108                 push @queries,'('.$c->req->param('search').')';
109         }
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'.$_) ? '&' : '|';
115                         my $cat = $cat{$_};
116                         my $query = join " $op ", map {"$_:$cat"} @words;
117                         push @queries,"($query)";
118                 }
119         }
120         my $search = join ' & ', @queries;
121
122         if ($search){
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
126                         FROM forum_boards fb 
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)
133                         ORDER BY rank DESC
134                 });
135                 eval {
136                         $posts->execute($c->stash->{UID},$search);
137                         my @posts;
138                         while (my $post = $posts->fetchrow_hashref){
139                                 push @posts,$post;
140                         }
141                         $c->stash(searchresults => \@posts);
142                 };
143                 if ($@){
144                         $c->stash( searcherror => $dbh->errstr);
145                 }
146         }
147
148 }
149
150
151 sub board : Local {
152         my ( $self, $c, $board ) = @_;
153         my $dbh = $c->model;
154
155         $c->stash(time => $dbh->selectrow_array('SELECT now()::timestamp',undef));
156
157         $c->forward('findBoard');
158         $board = $c->stash->{board};
159
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
168                 WHERE ft.fbid = $1
169                 GROUP BY ft.ftid, ft.subject,ft.sticky,u.username
170                 ORDER BY sticky DESC,last_post DESC
171         });
172         $threads->execute($board->{fbid},$c->stash->{UID});
173         my @threads;
174         while (my $thread = $threads->fetchrow_hashref){
175                 push @threads,$thread;
176         }
177         $c->stash(threads => \@threads);
178
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
186                         ORDER BY fb.fbid
187                 });
188                 $categories->execute;
189                 my @categories;
190                 while (my $category = $categories->fetchrow_hashref){
191                         $boards->execute($category->{fcid},$c->stash->{UID});
192
193                         my @boards;
194                         while (my $b = $boards->fetchrow_hashref){
195                                 next if (not $b->{post} or $b->{fbid} == $board->{fbid});
196                                 push @boards,$b;
197                         }
198                         $category->{boards} = \@boards;
199                         push @categories,$category if @boards;
200                 }
201                 $c->stash(categories => \@categories);
202         }
203 }
204
205
206 sub thread : Local {
207         my ( $self, $c, $thread ) = @_;
208         my $dbh = $c->model;
209
210         $c->forward('findThread');
211         $thread = $c->stash->{thread};
212
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
219                         LEFT OUTER JOIN 
220                                 (SELECT * FROM forum_thread_visits WHERE uid = $2) ftv ON ftv.ftid = ft.ftid
221                 WHERE ft.ftid = $1
222                 ORDER BY fp.time ASC
223                 });
224         $posts->execute($c->stash->{thread}->{ftid},$c->stash->{UID});
225
226         my @posts;
227         while (my $post = $posts->fetchrow_hashref){
228                 $post->{message} = parseMarkup($post->{message});
229                 push @posts,$post;
230         }
231
232         $c->stash(posts => \@posts);
233         $c->forward('markThreadAsRead') if $c->user_exists;
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->res->redirect($c->req->referer);
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 findThread : Private {
374         my ( $self, $c, $thread ) = @_;
375         my $dbh = $c->model;
376         my $findThread = $dbh->prepare(q{SELECT ft.ftid,ft.subject, bool_or(fa.post) AS post
377                 , bool_or(fa.moderate) AS moderate,ft.fbid,fb.board,fb.fcid,ft.sticky,fc.category
378                 FROM forum_boards fb
379                         NATURAL JOIN forum_access fa
380                         NATURAL JOIN forum_threads ft
381                         NATURAL JOIN forum_categories fc
382                 WHERE ft.ftid = $1 AND gid IN (SELECT groups($2))
383                 GROUP BY ft.ftid,ft.subject,ft.fbid,fb.board,fb.fcid,ft.sticky,fc.category
384         });
385         $thread = $dbh->selectrow_hashref($findThread,undef,$thread,$c->stash->{UID});
386         $c->stash(thread => $thread);
387 }
388
389 sub findBoard : Private {
390         my ( $self, $c, $board ) = @_;
391         my $dbh = $c->model;
392
393         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
394                         FROM forum_boards fb 
395                                 NATURAL JOIN forum_access fa
396                                 NATURAL JOIN forum_categories fc
397                         WHERE fb.fbid = $1 AND
398                                 gid IN (SELECT groups($2))
399                         GROUP BY fb.fbid,fb.board,fb.fcid,fc.category
400                 });
401         $board = $dbh->selectrow_hashref($boards,undef,$board,$c->stash->{UID});
402
403         $c->stash(board => $board);
404 }
405
406 sub previewPost : Private {
407         my ( $self, $c) = @_;
408         push @{$c->stash->{posts}}, {
409                 unread => 1,
410                 username => 'PREVIEW',
411                 message => parseMarkup(html_escape $c->req->param('message')),
412         };
413         $c->stash(previewMessage => html_escape $c->req->param('message'));
414 }
415
416 sub insertPost : Private {
417         my ( $self, $c, $thread ) = @_;
418         my $dbh = $c->model;
419
420         my $insert = $dbh->prepare(q{INSERT INTO forum_posts (ftid,message,uid)
421                 VALUES($1,$2,$3)});
422         $insert->execute($thread,html_escape($c->req->param('message')),$c->stash->{UID});
423 }
424
425 =head1 AUTHOR
426
427 Michael Andreen (harv@ruin.nu)
428
429 =head1 LICENSE
430
431 GPL 2.0, or later.
432
433 =cut
434
435 1;