summaryrefslogtreecommitdiff
path: root/lib/VNDB/Handler/Discussions.pm
blob: 066935908017d1775ff690a5470753a850cabd18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399

package VNDB::Handler::Discussions;

use strict;
use warnings;
use YAWF ':html', 'xml_escape';
use POSIX 'ceil';
use VNDB::Func;


YAWF::register(
  qr{t([1-9]\d*)(?:/([1-9]\d*))?}    => \&thread,
  qr{t([1-9]\d*)\.([1-9]\d*)}        => \&redirect,
  qr{t/(db|an|[vpu])([1-9]\d*)?}     => \&tagbrowse,
  qr{t([1-9]\d*)/reply}              => \&edit,
  qr{t([1-9]\d*)\.([1-9]\d*)/edit}   => \&edit,
  qr{t/(db|an|[vpu])([1-9]\d*)?/new} => \&edit,
  qr{t}                              => \&index,
);


sub thread {
  my($self, $tid, $page) = @_;
  $page ||= 1;

  my $t = $self->dbThreadGet(id => $tid, what => 'tagtitles')->[0];
  return 404 if !$t->{id} || $t->{hidden} && !$self->authCan('boardmod');

  my $p = $self->dbPostGet(tid => $tid, results => 25, page => $page);
  return 404 if !$p->[0];

  $self->htmlHeader(title => $t->{title});

  div class => 'mainbox';
   h1 $t->{title};
   h2 'Posted in';
   ul;
    for (sort { $a->{type}.$a->{iid} cmp $b->{type}.$b->{iid} } @{$t->{tags}}) {
      li;
       a href => "/t/$_->{type}", $self->{discussion_tags}{$_->{type}};
       if($_->{iid}) {
         txt ' > ';
         a style => 'font-weight: bold', href => "/t/$_->{type}$_->{iid}", "$_->{type}$_->{iid}";
         txt ':';
         a href => "/$_->{type}$_->{iid}", title => $_->{original}, $_->{title};
       }
      end;
    }
   end;
  end;

  $self->htmlBrowseNavigate("/t$tid/", $page, $t->{count} > $page*25, 't', 1);
  div class => 'mainbox thread';
   table;
    for my $i (0..$#$p) {
      local $_ = $p->[$i];
      my $class = $i % 2 ? 'odd ' : '';
      $class .= 'deleted' if $_->{hidden};
      Tr class => $class;
       td class => 'tc1';
        a href => "/t$tid.$_->{num}", name => $_->{num}, "#$_->{num}";
        if(!$_->{hidden}) {
          txt ' by ';
          lit userstr $_;
          br;
          lit date $_->{date}, 'full';
        }
       end;
       td class => 'tc2';
        if($self->authCan('boardmod') || $self->authInfo->{id} && $_->{uid} == $self->authInfo->{id} && !$_->{hidden}) {
          i class => 'edit';
           txt '< ';
           a href => "/t$tid.$_->{num}/edit", 'edit';
           txt ' >';
          end;
        }
        if($_->{hidden}) {
          i class => 'deleted', 'Post deleted.';
        } else {
          lit bb2html $_->{msg};
          i class => 'lastmod', 'Last modified on '.date($_->{edited}, 'full') if $_->{edited};
        }
       end;
      end;
    }
   end;
  end;
  $self->htmlBrowseNavigate("/t$tid/", $page, $t->{count} > $page*25, 'b', 1);

  if($t->{locked}) {
    div class => 'mainbox';
     h1 'Reply';
     p class => 'center', 'This thread has been locked, you can\'t reply to it anymore.';
    end;
  } elsif($t->{count} < $page*25 && $self->authCan('board')) {
    form action => "/t$tid/reply", method => 'post', 'accept-charset' => 'UTF-8';
     div class => 'mainbox';
      fieldset class => 'submit';
       h2 'Quick reply';
       textarea name => 'msg', id => 'msg', rows => 4, cols => 50, '';
       br;
       input type => 'submit', value => 'Reply', class => 'submit';
      end;
     end;
    end;
  } elsif(!$self->authCan('board')) {
    div class => 'mainbox';
     h1 'Reply';
     p class => 'center', 'You must be logged in to reply to this thread.';
    end;
  }

  $self->htmlFooter;
}


sub redirect {
  my($self, $tid, $num) = @_;
  $self->resRedirect("/t$tid".($num > 25 ? '/'.ceil($num/25) : '').'#'.$num, 'perm');
}


# Arguments, action
#  tid          reply
#  tid, 1       edit thread
#  tid, num     edit post
#  type, (iid)  start new thread
sub edit {
  my($self, $tid, $num) = @_;
  $num ||= 0;

  # in case we start a new thread, parse tag
  my $tag = '';
  if($tid !~ /^\d+$/) {
    return 404 if $tid =~ /(db|an)/ && $num || $tid =~ /[vpu]/ && !$num;
    $tag = $tid.($num||'');
    $tid = 0;
    $num = 0;
  }

  # get thread and post, if any
  my $t = $tid && $self->dbThreadGet(id => $tid, what => 'tags')->[0];
  return 404 if $tid && !$t->{id};

  my $p = $num && $self->dbPostGet(tid => $tid, num => $num)->[0];
  return 404 if $num && !$p->{num};

  # are we allowed to perform this action?
  return $self->htmlDenied if !$self->authCan('board')
    || ($tid && ($t->{locked} || $t->{hidden}) && !$self->authCan('boardmod'))
    || ($num && $p->{uid} != $self->authInfo->{id} && !$self->authCan('boardmod'));

  # check form etc...
  my $frm;
  if($self->reqMethod eq 'POST') {
    $frm = $self->formValidate(
      !$tid || $num == 1 ? (
        { name => 'title', maxlength => 50 },
        { name => 'tags', maxlength => 50 },
      ) : (),
      $self->authCan('boardmod') ? (
        { name => 'locked', required => 0 },
        { name => 'hidden', required => 0 },
        { name => 'nolastmod', required => 0 },
      ) : (),
      { name => 'msg', maxlenght => 5000 },
    );

    # parse and validate the tags
    my @tags;
    if(!$frm->{_err} && $frm->{tags}) {
      for (split /[ ,]/, $frm->{tags}) {
        my($ty, $id) = ($1, $2) if /^([a-z]{1,2})([0-9]*)$/;
        push @tags, [ $ty, $id ];
        push @{$frm->{_err}}, [ 'tags', 'wrongtag', $_ ] if
             !$ty || !$self->{discussion_tags}{$ty}
          || $ty eq 'an' && ($id || !$self->authCan('boardmod'))
          || $ty eq 'db' && $id
          || $ty eq 'v'  && (!$id || !$self->dbVNGet(id => $id)->[0]{id})
          || $ty eq 'p'  && (!$id || !$self->dbProducerGet(id => $id)->[0]{id})
          || $ty eq 'u'  && (!$id || !$self->dbUserGet(uid => $id)->[0]{id});
      }
    }

    if(!$frm->{_err}) {
      my($ntid, $nnum) = ($tid, $num);

      # create/edit thread
      if(!$tid || $num == 1) {
        my %thread = (
          title => $frm->{title},
          tags => \@tags,
          hidden => $frm->{hidden},
          locked => $frm->{locked},
        );
        $self->dbThreadEdit($tid, %thread)  if $tid;
        $ntid = $self->dbThreadAdd(%thread) if !$tid;
      }

      # create/edit post
      my %post = (
        msg => $frm->{msg},
        hidden => $num != 1 && $frm->{hidden},
        lastmod => !$num || $frm->{nolastmod} ? 0 : time,
      );
      $self->dbPostEdit($tid, $num, %post)   if $num;
      $nnum = $self->dbPostAdd($ntid, %post) if !$num;

      $self->multiCmd("ircnotify t$ntid.$nnum") if !$num && !$frm->{hidden};

      return $self->resRedirect("/t$ntid".($nnum > 25 ? '/'.ceil($nnum/25) : '').'#'.$nnum, 'post');
    }
  }

  # fill out form if we have some data
  if($p) {
    $frm->{msg} ||= $p->{msg};
    $frm->{hidden} = $p->{hidden} if $num != 1 && !exists $frm->{hidden};
    if($num == 1) {
      $frm->{tags} ||= join ' ', sort map $_->[1]?$_->[0].$_->[1]:$_->[0], @{$t->{tags}};
      $frm->{title} ||= $t->{title};
      $frm->{locked}  = $t->{locked} if !exists $frm->{locked};
      $frm->{hidden}  = $t->{hidden} if !exists $frm->{hidden};
    }
  }
  $frm->{tags} ||= $tag;
  $frm->{nolastmod} = 1 if $num && $self->authCan('boardmod') && !exists $frm->{nolastmod};

  # generate html
  my $title = !$tid ? 'Start new thread' :
              !$num ? 'Reply to '.$t->{title} :
                      'Edit post';
  my $url = !$tid ? "/t/$tag/new" : !$num ? "/t$tid/reply" : "/t$tid.$num/edit";
  $self->htmlHeader(title => $title, noindex => 1);
  $self->htmlForm({ frm => $frm, action => $url }, $title => [
    [ static => label => 'Username', content => userstr($self->authInfo->{id}, $self->authInfo->{username}) ],
    !$tid || $num == 1 ? (
      [ input  => short => 'title', name => 'Thread title' ],
      [ input  => short => 'tags',  name => 'Tags' ],
      [ static => content => 'Read <a href="/d9.2">d9.2</a> for information about how to use tags' ],
      $self->authCan('boardmod') ? (
        [ check => name => 'Locked', short => 'locked' ],
      ) : (),
    ) : (
      [ static => label => 'Topic', content => qq|<a href="/t$tid">|.xml_escape($t->{title}).'</a>' ],
    ),
    $self->authCan('boardmod') ? (
      [ check => name => 'Hidden', short => 'hidden' ],
      $num ? (
        [ check => name => 'Don\'t update last modified field', short => 'nolastmod' ],
      ) : (),
    ) : (),
    [ text   => name => 'Message', short => 'msg', rows => 10 ],
    [ static => content => 'See <a href="/d9.3">d9.3</a> for the allowed formatting codes' ],
  ]);
  $self->htmlFooter;
}


sub tagbrowse {
  my($self, $type, $iid) = @_;
  $iid ||= '';
  return 404 if $type =~ /(db|an)/ && $iid;

  my $f = $self->formValidate(
    { name => 'p', required => 0, default => 1, template => 'int' },
  );
  return 404 if $f->{_err};

  my $obj = !$iid ? undef :
    $type eq 'u' ? $self->dbUserGet(uid => $iid)->[0] :
    $type eq 'p' ? $self->dbProducerGet(id => $iid)->[0] :
                   $self->dbVNGet(id => $iid)->[0];
  return 404 if $iid && !$obj;
  my $ititle = $obj && ($obj->{title}||$obj->{name}||$obj->{username});
  my $title = !$obj ? $self->{discussion_tags}{$type} : 'Related discussions for '.$ititle;

  my($list, $np) = $self->dbThreadGet(
    type => $type,
    $iid ? (iid => $iid) : (),
    results => 50,
    page => $f->{p},
    what => 'firstpost lastpost tagtitles',
    order => $type eq 'an' ? 't.id DESC' : 'tpl.date DESC',
  );

  $self->htmlHeader(title => $title, noindex => !@$list);

  $self->htmlMainTabs($type, $obj, 'disc') if $iid;
  div class => 'mainbox';
   h1 $title;
   p;
    a href => '/t', 'Discussion board';
    txt ' > ';
    a href => "/t/$type", $self->{discussion_tags}{$type};
    if($iid) {
      txt ' > ';
      a style => 'font-weight: bold', href => "/t/$type$iid", "$type$iid";
      txt ':';
      a href => "/$type$iid", $ititle;
    }
   end;
   p class => 'center';
    if(!@$list) {
      b 'No related threads found';
      br; br;
      a href => "/t/$type$iid/new", 'Why not create one yourself?';
    } else {
      a href => '/t/'.($iid ? $type.$iid : 'db').'/new', 'Start a new thread';
    }
   end;
  end;

  _threadlist($self, $list, $f, $np, "/t/$type$iid") if @$list;

  $self->htmlFooter;
}


sub index {
  my $self = shift;

  $self->htmlHeader(title => 'Discussion board index');
  div class => 'mainbox';
   h1 'Discussion board index';
   p class => 'browseopts';
    a href => '/t/'.$_, $self->{discussion_tags}{$_}
      for (qw|an db v p u|);
   end;
  end;

  for (qw|an db v p u|) {
    my $list = $self->dbThreadGet(
      type => $_,
      results => 5,
      page => 1,
      what => 'firstpost lastpost tagtitles',
      order => 'tpl.date DESC',
    );
    h1 class => 'boxtitle';
     a href => "/t/$_", $self->{discussion_tags}{$_};
    end;
    _threadlist($self, $list, {p=>1}, 0, "/t");
  }

  $self->htmlFooter;
}


sub _threadlist {
  my($self, $list, $f, $np, $url) = @_;
  $self->htmlBrowse(
    items    => $list,
    options  => $f,
    nextpage => $np,
    pageurl  => $url,
    class    => 'discussions',
    header   => [
      [ 'Topic' ], [ 'Replies' ], [ 'Starter' ], [ 'Last post' ]
    ],
    row      => sub {
      my($self, $n, $o) = @_;
      Tr $n % 2 ? ( class => 'odd' ) : ();
       td class => 'tc1';
        a $o->{locked} ? ( class => 'locked' ) : (), href => "/t$o->{id}", shorten $o->{title}, 50;
       end;
       td class => 'tc2', $o->{count}-1;
       td class => 'tc3';
        lit userstr $o->{fuid}, $o->{fusername};
       end;
       td class => 'tc4', rowspan => 2;
        lit userstr $o->{luid}, $o->{lusername};
        lit ' @ ';
        a href => "/t$o->{id}.$o->{count}";
         lit date $o->{ldate};
        end;
       end;
      end;
      Tr $n % 2 ? ( class => 'odd' ) : ();
       td colspan => 4, class => 'tags';
        txt ' > ';
        my $i = 1;
        for(sort { $a->{type}.$a->{iid} cmp $b->{type}.$b->{iid} } @{$o->{tags}}) {
          last if $i++ > 5;
          txt ', ' if $i > 2;
          a href => "/t/$_->{type}".($_->{iid}||''),
            title => $_->{original}||$self->{discussion_tags}{$_->{type}},
            shorten $_->{title}||$self->{discussion_tags}{$_->{type}}, 30;
        }
        txt ', ...' if @{$o->{tags}} > 5;
       end;
      end;
    }
  );
}


1;