<commit_before>package basilisk::Controller::Comments;

use strict;
use warnings;
use parent 'Catalyst::Controller';
use JSON;
use HTML::Scrubber;

#__PACKAGE__->config->{namespace} = '';

sub comments : Global{
   my ( $self, $c, $gameid) = @_;
   #set content: some json list of comments?
   my $req = $c->request;
   my $new_comment = $req->param('new_comment_text');
   #assert that comment is okay.
   
   my $game = $c->model('DB::Game')->find ({'id' => $gameid}, {cache => 1});
   unless ($game){
      $c->detach('fail_comment_nicely', ['invalid_request: no game with that id']);
   }
   if ($new_comment){
      unless ($c->forward('allowed_to_comment')){  #fail nicely
         $c->detach('fail_comment_nicely', [$c->stash->{whynot}]);
      }
      if (length($new_comment) > 300){   #fail nicely
         $c->detach('fail_comment_nicely', ['comment too long']);
      }
      
      $game->create_related ('comments', {
            comment => $new_comment,
            sayeth  => $c->session->{userid},
            time    => time,
      });
   }
   
   my @comments;
   my $comments_rs = $game->search_related ('comments', {},
         {join => ['speaker'],
          select => ['comment', 'time', 'speaker.name'],
          as     => ['comment', 'time', 'pname'],
          order_by=>'time ASC',

   });
   my @moves = $game->search_related ('moves', {},
      { select => ['movenum', 'time'],
        order_by => 'time ASC',
      });
   
   #sanitize and prepare comments with movenums
   my $scrubber = HTML::Scrubber->new( allow => [ qw[ p b i u hr br ] ] );
   my $movenum = 0;
   for my $row ($comments_rs->all){
      my $scrubbed_comment = $scrubber->scrub ($row->comment);
      $scrubbed_comment =~ s/([^\s]{13})[^\s]/$1- /g; #break up long words<commit_msg>rectify accidental submission of duplicate comments<commit_after>package basilisk::Controller::Comments;

use strict;
use warnings;
use parent 'Catalyst::Controller';
use JSON;
use HTML::Scrubber;

#__PACKAGE__->config->{namespace} = '';

sub comments : Global{
   my ( $self, $c, $gameid) = @_;
   #set content: some json list of comments?
   my $req = $c->request;
   my $new_comment = $req->param('new_comment_text');
   #assert that comment is okay.
   
   my $game = $c->model('DB::Game')->find ({'id' => $gameid}, {cache => 1});
   unless ($game){
      $c->detach('fail_comment_nicely', ['invalid_request: no game with that id']);
   }
   if ($new_comment){
      unless ($c->forward('allowed_to_comment')){  #fail nicely
         $c->detach('fail_comment_nicely', [$c->stash->{whynot}]);
      }
      if (length($new_comment) > 300){   #fail nicely
         $c->detach('fail_comment_nicely', ['comment too long']);
      }
      
      #see if user hit submit twice accidentally
      my $previous_comment = $game->find_related('comments', {}, {order_by => 'id DESC'});
      if ($previous_comment and $previous_comment->comment eq $new_comment){
         if ($previous_comment->sayeth == $c->session->{userid}){
            $c->detach('fail_comment_nicely', ['same as prev. comment']);
         }
      }
      
      $game->create_related ('comments', {
            comment => $new_comment,
            sayeth  => $c->session->{userid},
            time    => time,
      });
   }
   
   my @comments;
   my $comments_rs = $game->search_related ('comments', {},
         {join => ['speaker'],
          select => ['comment', 'time', 'speaker.name'],
          as     => ['comment', 'time', 'pname'],
          order_by=>'time ASC',

   });
   my @moves = $game->search_related ('moves', {},
      { select => ['movenum', 'time'],
        order_by => 'time ASC',
      });
   
   #sanitize and prepare comments with movenums
   my $scrubber = HTML::Scrubber->new( allow => [ qw[ p b i u hr br ] ] );
   my $movenum = 0;
   for my $row ($comments_rs->all){
      my $scrubbed_comment = $scrubber->scrub ($row->comment);
      $scrubbed_comment =~ s/([^\s]{13})[^\s]/$1- /g; #break up long words
{'fraction_non_alphanumeric': 0.18670012547051443, 'fraction_numerical': 0.004015056461731493, 'mean_word_length': 2.8216682646212847, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4494494', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1312, 'n_words': 317}