Browse Source

* LDV::Comments : change logic

master
Alex 'AdUser' Z 9 years ago
parent
commit
7379a80453
  1. 87
      lib/LDV/Comments.pm

87
lib/LDV/Comments.pm

@ -6,57 +6,26 @@ use utf8;
use Mojo::Base 'Mojolicious::Controller'; use Mojo::Base 'Mojolicious::Controller';
use File::Slurp qw(read_file write_file);
use Mojo::URL; use Mojo::URL;
sub _gen_id { sub _gen_pageid {
my ($self) = @_; my ($self) = @_;
my $url = $self->req->param('pageid') || my $url = $self->req->param('pageid') ||
$self->req->headers->referrer; $self->req->headers->referrer;
my $maxlen = 64; my $maxlen = 64;
return unless $url; return unless $url;
my $id = Mojo::URL->new($url)->path; my $pageid = Mojo::URL->new($url)->path;
$id =~ s{^/+}{}o; $pageid =~ s{^/+}{}o;
$id =~ s{/+$}{}o; $pageid =~ s{/+$}{}o;
$id =~ y{/.}{-}s; $pageid =~ y{/.}{-}s;
$id =~ s<\.[a-z0-9]{2,4}$><>io; $pageid =~ s<\.[a-z0-9]{2,4}$><>io;
$id = substr($id, -$maxlen, $maxlen); $pageid = substr($pageid, -$maxlen, $maxlen);
$self->app->log->debug("comments id: $id -- $url"); $self->app->log->debug("comments id: $pageid -- $url");
return $id; return $pageid;
}
sub _comments_load {
my ($self) = @_;
my $id = $self->_gen_id
or die("can't get id\n");
my $path = $self->app->home->rel_file("data/comments/$id.json");
return [] unless -f $path;
open my $FH, '<', $path
or die("open (r): $path -- $!\n");
local $/ = undef;
my $data = <$FH>;
close $FH;
return $self->app->json->decode($data);
}
sub _comments_save {
my ($self, $data) = @_;
my $id = $self->_gen_id
or die("can't get id\n");
my $path = $self->app->home->rel_file("data/comments/$id.json");
open my $FH, '>', $path
or die("open (w): $path -- $!\n");
print $FH $self->app->json->encode($data);
close $FH;
return 1;
} }
sub add { sub add {
@ -65,13 +34,26 @@ sub add {
eval { eval {
my $text = $self->req->param('text') my $text = $self->req->param('text')
or die("empty comment\n"); or die("empty comment\n");
my $comments = $self->_comments_load(); my $pageid = $self->_gen_pageid()
my $comment = { or die("can't get id\n");
my %opts = (binmode => ':bytes');
my $comments = [];
my $path = $self->app->home->rel_file("data/comments/$pageid.json");
if (-f $path) {
my $json = read_file($path, %opts);
$comments = $self->app->json->decode($json);
}
push @{ $comments }, {
text => $text, time => time(), text => $text, time => time(),
user => $self->session('username') || 'anonymous', user => $self->session('username') || 'anonymous',
}; };
push @{ $comments }, $comment; write_file($path, \%opts, $self->app->json->encode($comments));
$self->_comments_save($comments);
$path = $self->app->home->rel_file("data/comments/$pageid.html");
$self->stash({comments => $comments});
write_file($path, {binmode => ':utf8'}, $self->render_to_string(template => 'comments/list'));
$self->render(text => 'OK'); $self->render(text => 'OK');
} or do { } or do {
chomp $@; chomp $@;
@ -88,16 +70,15 @@ sub get {
my ($self) = @_; my ($self) = @_;
eval { eval {
my $comments = $self->_comments_load(); my $pageid = $self->_gen_pageid()
die("can't load comments\n") or die("can't get id\n");
unless ref($comments) eq 'ARRAY'; my $path = $self->app->home->rel_file("data/comments/$pageid.html");
if (my $count = scalar @{ $comments }) { if (-f $path) {
$self->app->log->debug("loaded $count comments"); my $comments = read_file($path, binmode => ':utf8');
$self->stash({comments => $comments}); $self->render(text => $comments);
$self->render(template => 'comments/list');
} else { } else {
$self->render(template => 'comments/none'); $self->render(template => 'comments/none');
}; } 1;
} or do { } or do {
chomp $@; chomp $@;
$self->app->log->error($@); $self->app->log->error($@);

Loading…
Cancel
Save