diff --git a/data/comments/.keepme b/data/comments/.keepme deleted file mode 100644 index e69de29..0000000 diff --git a/lib/LDV.pm b/lib/LDV.pm index 37e9a56..23d53d7 100644 --- a/lib/LDV.pm +++ b/lib/LDV.pm @@ -37,15 +37,6 @@ sub startup { my $r = $self->routes; - { # /comments - my $comm = $r->route('/comments') -> to(controller => 'comments'); - $comm->post('/add') ->to(action => 'add'); - $comm->get ('/get') ->to(action => 'get'); - $comm->get ('/new') ->to(action => 'create'); - - mkdir $self->app->home->rel_dir('data/comments'); - } - { # /user my $user = $r->route('/user') -> to(controller => 'user'); $user->get('/') ->to(cb => sub { shift->redirect_to('/user/login'); }); diff --git a/lib/LDV/Comments.pm b/lib/LDV/Comments.pm deleted file mode 100644 index f063715..0000000 --- a/lib/LDV/Comments.pm +++ /dev/null @@ -1,124 +0,0 @@ -package LDV::Comments; - -use strict; -use warnings; -use utf8; - -use Mojo::Base 'Mojolicious::Controller'; - -use File::Slurp qw(read_file write_file); -use Mojo::URL; - -sub _gen_pageid { - my ($self) = @_; - my $url = $self->req->param('pageid') || - $self->req->headers->referrer; - my $maxlen = 64; - - return unless $url; - my $pageid = Mojo::URL->new($url)->path; - - $pageid =~ s{^/+}{}o; - $pageid =~ s{/+$}{}o; - $pageid =~ y{/.}{-}s; - $pageid =~ s<\.[a-z0-9]{2,4}$><>io; - $pageid = substr($pageid, -$maxlen, $maxlen); - $self->app->log->debug("comments id: $pageid -- $url"); - - return $pageid; -} - -sub add { - my ($self) = @_; - - eval { - my $sectoken = $self->session('c_sectoken') - or die("missing security token\n"); - my ($ip, $upto) = ($sectoken =~ m{^([0-9a-f\.:]+)-(\d+)$}io) - or die("malformed security token\n"); - ($upto > time()) - or die("expired security token\n"); - ($ip eq $self->tx->remote_address) - or die("remote address mismatch\n"); - my $text = $self->req->param('text') - or die("empty comment\n"); - my $pageid = $self->_gen_pageid() - or die("can't get pageid\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(), - user => $self->session('username') || 'anonymous', - }; - write_file($path, \%opts, $self->app->json->encode($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'); - } or do { - chomp $@; - $self->app->log->error($@); - $@ = 'internal error' if $@ =~ m{line \d+}o; - $self->res->code(400); - $self->render(text => $@); - }; - - $self->rendered(); -} - -sub get { - my ($self) = @_; - - eval { - my $pageid = $self->_gen_pageid() - or die("can't get id\n"); - my $path = $self->app->home->rel_file("data/comments/$pageid.html"); - if (-f $path) { - my $comments = read_file($path, binmode => ':utf8'); - $self->render(text => $comments); - } else { - $self->render(template => 'comments/none'); - } 1; - } or do { - chomp $@; - $self->app->log->error($@); - $@ = 'internal error' if $@ =~ m{line \d+}o; - $self->res->code(400); - $self->render(text => $@); - }; - - $self->rendered(); -} - -sub create { - my ($self) = @_; - - eval { - die("request error\n") - unless $self->req->is_xhr; - my $ip = $self->tx->remote_address - or die("can't find remote ip\n"); - my $sectoken = sprintf "%s-%d", $ip, time() + 60 * 7; - $self->session(c_sectoken => $sectoken); - my $pageid = $self->_gen_pageid() - or die("can't get pageid\n"); - $self->stash({pageid => $pageid}); - $self->render(template => 'comments/new'); - } or do { - chomp $@; - $self->app->log->error($@); - $@ = 'internal error' if $@ =~ m{line \d+}o; - $self->res->code(400); - $self->render(text => $@); - }; -} - -1; diff --git a/lib/LDV/I18N/ru.pm b/lib/LDV/I18N/ru.pm index d855ace..0d61e89 100644 --- a/lib/LDV/I18N/ru.pm +++ b/lib/LDV/I18N/ru.pm @@ -26,7 +26,6 @@ our %Lexicon = 'Update' => 'Обновить', 'Auth required' => 'Требуется авторизация', 'Page not found' => 'Страница не найдена', - 'No comments yet' => 'Нет комментариев', 'John Doe' => 'Иван Петров', 'JSC Vector' => 'ООО "Рога и копыта"', diff --git a/public/theme/css/default.css b/public/theme/css/default.css index b6f883f..7cb7206 100644 --- a/public/theme/css/default.css +++ b/public/theme/css/default.css @@ -87,31 +87,6 @@ a.footnote { font-size: smaller; } -/* comments */ -div#comments { - padding: 5px; -} - -div.comment { - margin-bottom: 10px; -} - -div.comment div.cinfo { - border-bottom: 1px dashed #506070; - margin-bottom: 5px; - padding: 3px; -} - -form#cadd textarea { - min-width: 400px; - min-height: 100px; -} - -form#cadd input[type=submit] { - background: url('/theme/img/save.png') no-repeat center left; - padding-left: 18px; -} - /* common page parts */ .ui-block { background-color: #FFFFFF; diff --git a/public/theme/js/main.js b/public/theme/js/main.js index bc71fb0..3d13190 100644 --- a/public/theme/js/main.js +++ b/public/theme/js/main.js @@ -11,22 +11,4 @@ $(document).ready(function() { $('#upload').on('change', function() { $('input[name=source]').val(['file']); }); - - /* comments */ - $('#comments').load('/comments/get', function() { - // handle 'add comment' button click - $('a#cadd').click(function(e) { - e.preventDefault(); - $(this).remove(); - $('div#cadd').load('/comments/new', function() { - // form submit with ajax - $('#comments form').submit(function(e) { - e.preventDefault(); - $.post('/comments/add', $(this).serialize()) - .done(function(data) { location.reload(); }) - .fail(function(data) { $('#comments #resp').html(data.responseText); }); - }); // form submit - }); // on form load - }); // #cadd click - }); // #comments load }); // document ready diff --git a/templates/comments/list.html.ep b/templates/comments/list.html.ep deleted file mode 100644 index 7c4d06d..0000000 --- a/templates/comments/list.html.ep +++ /dev/null @@ -1,18 +0,0 @@ -% my $comments = stash('comments') || []; -% my $num = 1; -

Комментарии:

-% foreach my $comment (@{ $comments }) { -
-
- #<%= $num++ %>,  - <%= $comment->{user} %>,  - <%= from_unixtime $comment->{time} %> -
-
-%== markdown $comment->{text} -
-
-% } -
- <%= l('Add') %> -
diff --git a/templates/comments/new.html.ep b/templates/comments/new.html.ep deleted file mode 100644 index 6aa2207..0000000 --- a/templates/comments/new.html.ep +++ /dev/null @@ -1,9 +0,0 @@ -% my $pageid = stash('pageid') || ''; -
- -
- -
-   - -
diff --git a/templates/comments/none.html.ep b/templates/comments/none.html.ep deleted file mode 100644 index f41934f..0000000 --- a/templates/comments/none.html.ep +++ /dev/null @@ -1,4 +0,0 @@ -
- <%= l('No comments yet') %>.  - <%= l('Add') %>? -