Skip to content

Commit 6ec8ab7

Browse files
committed
Comment system
1 parent 8386cee commit 6ec8ab7

File tree

6 files changed

+104
-7
lines changed

6 files changed

+104
-7
lines changed

DB/lib/BlogDB/DB/Result/Blog.pm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,5 +263,20 @@ sub posts {
263263
} $self->search_related( 'blog_entries')->all ];
264264
}
265265

266+
sub get_comments {
267+
my ( $self ) = @_;
268+
269+
return [ $self->search_related('messages', { parent_id => undef, })->all ];
270+
}
271+
272+
sub get_votes {
273+
my ( $self ) = @_;
274+
275+
return {
276+
total => $self->search_related( 'messages', { parent_id => undef})->sum('vote'),
277+
pos => $self->search_related( 'messages', { parent_id => undef, vote => 1})->count,
278+
neg => $self->search_related( 'messages', { parent_id => undef, vote => -1})->count,
279+
};
280+
}
266281

267282
1;

DB/lib/BlogDB/DB/Result/Message.pm

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,28 @@ __PACKAGE__->belongs_to(
197197

198198

199199
# You can replace this text with custom code or comments, and it will be preserved on regeneration
200+
201+
sub get_children {
202+
my ( $self ) = @_;
203+
204+
return [ $self->result_source->schema->resultset('Message')->search({
205+
parent_id => $self->id,
206+
})->all ];
207+
}
208+
209+
sub time_ago {
210+
my ( $self ) = @_;
211+
212+
my $delta = time - $self->created_at->epoch;
213+
214+
return "less than a minute ago" if $delta < 60; # 1 Minute
215+
return "about a minute ago" if $delta < 120; # 2 minute
216+
return int($delta / 60) . " minutes ago" if $delta < 45 * 60; # 45 minutes
217+
return "about an hour ago" if $delta < 60 * 60 * 2; # 2 hours
218+
return int($delta / 3600) . " hours ago" if $delta < 60 * 60 * 18; # 18 hours
219+
return "about an day ago" if $delta < 60 * 60 * 36; # 36 Hours
220+
return int($delta / (3600*24)) . " days ago";
221+
}
222+
223+
200224
1;

Web/lib/BlogDB/Web.pm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ sub startup ($self) {
101101

102102
$r->post ( '/blog/follow' )->to( 'Blog#post_blog_follow' )->name( 'do_follow_blog' );
103103
$r->post ( '/blog/unfollow' )->to( 'Blog#post_blog_unfollow' )->name( 'do_unfollow_blog');
104+
105+
$r->post ( '/blog/comment' )->to( 'Blog#post_blog_comment' )->name( 'do_blog_comment');
106+
107+
# $r->post ( '/blog/unfollow' )->to( 'Blog#post_blog_unfollow' )->name( 'do_unfollow_blog');
104108
# $r->get ( '/view/:id/:name' )->to( 'Blog#get_blog' )->name( 'blog' ); # View A Specific Blog.
105109
#
106110
# $auth->get ( '/blog/new' )->to( 'Blog#get_new_blogs' )->name( 'new_blogs' ); # List pending blogs for approval.

Web/lib/BlogDB/Web/Controller/Blog.pm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,26 @@ sub post_blog_unfollow ($c) {
100100
})->delete;
101101

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

105+
sub post_blog_comment ($c) {
106+
my $blog_id = $c->stash->{form_blog_id} = $c->param('blog_id');
107+
my $message = $c->stash->{form_message} = $c->param('message');
108+
my $rev_pos = $c->stash->{form_rev_pos} = $c->param('rev_pos');
109+
my $rev_neg = $c->stash->{form_rev_neg} = $c->param('rev_neg');
110+
my $parent = $c->stash->{form_parent} = $c->param('parent_id');
111+
112+
# pos = 1, neg = -1, otherwise 0
113+
my $vote = $rev_pos ? 1 : ( $rev_neg ? -1 : 0 );
114+
115+
$c->stash->{person}->create_related('messages', {
116+
blog_id => $blog_id,
117+
content => $message,
118+
parent_id => $parent,
119+
vote => $vote,
120+
});
121+
122+
$c->redirect_to( $c->url_for( 'view_blog', slug => $blog_id ) );
104123
}
105124

106125
sub get_settings ($c) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<div class="card my-4">
2+
<div class="card-body">
3+
<div class="">
4+
<a href="#">u/[% $comment.author.username %]</a>
5+
<small><span title="[% $comment.created_at %]">[% $comment.time_ago %]</span></small>
6+
</div>
7+
<div class="pt-4">
8+
%% $comment.content
9+
</div>
10+
<br />
11+
<a class="card-link" data-bs-toggle="collapse" href="#replyForm[% $comment.id %]" role="button" aria-expanded="false" aria-controls="replyForm[% $comment.id %]">[Reply]</a>
12+
<a href="#" class="card-link">[Permlink]</a>
13+
<div id="replyForm[% $comment.id %]" class="row mx-2 my-4 collapse">
14+
<form method="POST" action="[% $c.url_for( 'do_blog_comment' ) %]">
15+
<input type="hidden" name="parent_id" value="[% $comment.id %]" />
16+
<input type="hidden" name="blog_id" value="[% $blog.id %]" />
17+
<div class="mb-3">
18+
<label for="message" class="form-label">Post a Reply</label>
19+
<textarea class="form-control" name="message" id="message" rows="3"></textarea>
20+
</div>
21+
22+
<button type="submit" class="btn btn-primary float-end">Post Message</button>
23+
</form>
24+
</div>
25+
</div>
26+
%% for $comment.get_children -> $child_comment {
27+
<div style="margin-left: 2em">
28+
%% include "/default/blog/_comment.tx" { comment => $child_comment };
29+
</div>
30+
%% }
31+
</div>

Web/templates/default/blog/index.html.tx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,19 @@
5959
</li>
6060
</ul>
6161

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

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

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

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

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

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

89+
%% for $blog.get_comments -> $comment {
90+
%% include "/default/blog/_comment.tx" { comment => $comment };
91+
%% }
92+
8993

9094
%% }

0 commit comments

Comments
 (0)