Skip to content

Commit

Permalink
Comment system
Browse files Browse the repository at this point in the history
  • Loading branch information
symkat committed Nov 25, 2021
1 parent 8386cee commit 6ec8ab7
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 7 deletions.
15 changes: 15 additions & 0 deletions DB/lib/BlogDB/DB/Result/Blog.pm
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,20 @@ sub posts {
} $self->search_related( 'blog_entries')->all ];
}

sub get_comments {
my ( $self ) = @_;

return [ $self->search_related('messages', { parent_id => undef, })->all ];
}

sub get_votes {
my ( $self ) = @_;

return {
total => $self->search_related( 'messages', { parent_id => undef})->sum('vote'),
pos => $self->search_related( 'messages', { parent_id => undef, vote => 1})->count,
neg => $self->search_related( 'messages', { parent_id => undef, vote => -1})->count,
};
}

1;
24 changes: 24 additions & 0 deletions DB/lib/BlogDB/DB/Result/Message.pm
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,28 @@ __PACKAGE__->belongs_to(


# You can replace this text with custom code or comments, and it will be preserved on regeneration

sub get_children {
my ( $self ) = @_;

return [ $self->result_source->schema->resultset('Message')->search({
parent_id => $self->id,
})->all ];
}

sub time_ago {
my ( $self ) = @_;

my $delta = time - $self->created_at->epoch;

return "less than a minute ago" if $delta < 60; # 1 Minute
return "about a minute ago" if $delta < 120; # 2 minute
return int($delta / 60) . " minutes ago" if $delta < 45 * 60; # 45 minutes
return "about an hour ago" if $delta < 60 * 60 * 2; # 2 hours
return int($delta / 3600) . " hours ago" if $delta < 60 * 60 * 18; # 18 hours
return "about an day ago" if $delta < 60 * 60 * 36; # 36 Hours
return int($delta / (3600*24)) . " days ago";
}


1;
4 changes: 4 additions & 0 deletions Web/lib/BlogDB/Web.pm
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ sub startup ($self) {

$r->post ( '/blog/follow' )->to( 'Blog#post_blog_follow' )->name( 'do_follow_blog' );
$r->post ( '/blog/unfollow' )->to( 'Blog#post_blog_unfollow' )->name( 'do_unfollow_blog');

$r->post ( '/blog/comment' )->to( 'Blog#post_blog_comment' )->name( 'do_blog_comment');

# $r->post ( '/blog/unfollow' )->to( 'Blog#post_blog_unfollow' )->name( 'do_unfollow_blog');
# $r->get ( '/view/:id/:name' )->to( 'Blog#get_blog' )->name( 'blog' ); # View A Specific Blog.
#
# $auth->get ( '/blog/new' )->to( 'Blog#get_new_blogs' )->name( 'new_blogs' ); # List pending blogs for approval.
Expand Down
19 changes: 19 additions & 0 deletions Web/lib/BlogDB/Web/Controller/Blog.pm
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,26 @@ sub post_blog_unfollow ($c) {
})->delete;

$c->redirect_to( $c->url_for( 'view_blog', slug => $blog->slug ) );
}

sub post_blog_comment ($c) {
my $blog_id = $c->stash->{form_blog_id} = $c->param('blog_id');
my $message = $c->stash->{form_message} = $c->param('message');
my $rev_pos = $c->stash->{form_rev_pos} = $c->param('rev_pos');
my $rev_neg = $c->stash->{form_rev_neg} = $c->param('rev_neg');
my $parent = $c->stash->{form_parent} = $c->param('parent_id');

# pos = 1, neg = -1, otherwise 0
my $vote = $rev_pos ? 1 : ( $rev_neg ? -1 : 0 );

$c->stash->{person}->create_related('messages', {
blog_id => $blog_id,
content => $message,
parent_id => $parent,
vote => $vote,
});

$c->redirect_to( $c->url_for( 'view_blog', slug => $blog_id ) );
}

sub get_settings ($c) {
Expand Down
31 changes: 31 additions & 0 deletions Web/templates/default/blog/_comment.tx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<div class="card my-4">
<div class="card-body">
<div class="">
<a href="#">u/[% $comment.author.username %]</a>
<small><span title="[% $comment.created_at %]">[% $comment.time_ago %]</span></small>
</div>
<div class="pt-4">
%% $comment.content
</div>
<br />
<a class="card-link" data-bs-toggle="collapse" href="#replyForm[% $comment.id %]" role="button" aria-expanded="false" aria-controls="replyForm[% $comment.id %]">[Reply]</a>
<a href="#" class="card-link">[Permlink]</a>
<div id="replyForm[% $comment.id %]" class="row mx-2 my-4 collapse">
<form method="POST" action="[% $c.url_for( 'do_blog_comment' ) %]">
<input type="hidden" name="parent_id" value="[% $comment.id %]" />
<input type="hidden" name="blog_id" value="[% $blog.id %]" />
<div class="mb-3">
<label for="message" class="form-label">Post a Reply</label>
<textarea class="form-control" name="message" id="message" rows="3"></textarea>
</div>

<button type="submit" class="btn btn-primary float-end">Post Message</button>
</form>
</div>
</div>
%% for $comment.get_children -> $child_comment {
<div style="margin-left: 2em">
%% include "/default/blog/_comment.tx" { comment => $child_comment };
</div>
%% }
</div>
18 changes: 11 additions & 7 deletions Web/templates/default/blog/index.html.tx
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@
</li>
</ul>

<form method="post" action="[% $c.url_for( 'do_review_blog' ) %]">
<input type="hidden" name="blog_id" value="[% $blog.id %]">
<form method="post" action="[% $c.url_for( 'do_blog_comment' ) %]">
<input type="hidden" name="blog_id" value="[% $blog.id %]">

<textarea name="comment" rows="4">[% $form_comment %]</textarea>
<textarea id="message" name="message" rows="4">[% $form_message %]</textarea>

<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" value="1" name="review_pos" id="review_pos" [% $form_review_pos ? " checked " : "" %]>
<label class="form-check-label" for="review_pos">This is a positive review. I recommend this blog.</label>
<input class="form-check-input" type="checkbox" value="1" name="rev_pos" id="rev_pos" [% $form_rev_pos ? " checked " : "" %]>
<label class="form-check-label" for="rev_pos">This is a positive review. I recommend this blog.</label>
</div>

<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" value="1" name="review_neg" id="review_neg" [% $form_review_neg ? " checked " : "" %]>
<label class="form-check-label" for="review_neg">This is a negative review. I dislike this blog.</label>
<input class="form-check-input" type="checkbox" value="1" name="rev_neg" id="rev_neg" [% $form_rev_neg ? " checked " : "" %]>
<label class="form-check-label" for="rev_neg">This is a negative review. I dislike this blog.</label>
</div>

<button type="submit" class="btn btn-primary float-end">Post Review</button>
Expand All @@ -86,5 +86,9 @@

<div class="row" style="height: 20em;"></div>

%% for $blog.get_comments -> $comment {
%% include "/default/blog/_comment.tx" { comment => $comment };
%% }


%% }

0 comments on commit 6ec8ab7

Please sign in to comment.