Browse Source

* drop comments

master
Alex 'AdUser' Z 7 years ago
parent
commit
76172d4bd4
  1. 0
      data/comments/.keepme
  2. 9
      lib/LDV.pm
  3. 124
      lib/LDV/Comments.pm
  4. 1
      lib/LDV/I18N/ru.pm
  5. 25
      public/theme/css/default.css
  6. 18
      public/theme/js/main.js
  7. 18
      templates/comments/list.html.ep
  8. 9
      templates/comments/new.html.ep
  9. 4
      templates/comments/none.html.ep

0
data/comments/.keepme

9
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'); });

124
lib/LDV/Comments.pm

@ -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;

1
lib/LDV/I18N/ru.pm

@ -26,7 +26,6 @@ our %Lexicon =
'Update' => 'Обновить',
'Auth required' => 'Требуется авторизация',
'Page not found' => 'Страница не найдена',
'No comments yet' => 'Нет комментариев',
'John Doe' => 'Иван Петров',
'JSC Vector' => 'ООО "Рога и копыта"',

25
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;

18
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

18
templates/comments/list.html.ep

@ -1,18 +0,0 @@
% my $comments = stash('comments') || [];
% my $num = 1;
<h3>Комментарии:</h3>
% foreach my $comment (@{ $comments }) {
<div class="comment">
<div class="cinfo">
<span class="cnum">#<%= $num++ %></span>,&nbsp;
<span class="cuser"><%= $comment->{user} %></span>,&nbsp;
<span class="cdate"><%= from_unixtime $comment->{time} %></span>
</div>
<div class="ctext">
%== markdown $comment->{text}
</div>
</div>
% }
<div id="cadd">
<a href="#" id="cadd"><%= l('Add') %></a>
</div>

9
templates/comments/new.html.ep

@ -1,9 +0,0 @@
% my $pageid = stash('pageid') || '';
<form method="POST" action="/comments/add" id="cadd">
<input type="hidden" value="<%= $pageid %>" name="pageid">
<hr/>
<textarea name="text"></textarea>
<br/>
<input type="submit" value="<%= l('Add') %>">&nbsp;
<span id="resp" style="color: red"></span>
</form>

4
templates/comments/none.html.ep

@ -1,4 +0,0 @@
<div id="cadd">
<%= l('No comments yet') %>.&nbsp;
<a href="#" id="cadd"><%= l('Add') %>?</a>
</div>
Loading…
Cancel
Save