From 87359931f0178408a8318e148a259e396388107d Mon Sep 17 00:00:00 2001 From: Blog Manager Robot Date: Thu, 27 Oct 2022 23:13:50 +0000 Subject: [PATCH] Blog history start. --- Web/lib/MJB/Web.pm | 2 + Web/lib/MJB/Web/Controller/Dashboard.pm | 14 +++++ Web/templates/dashboard/_blog_nav.html.ep | 3 ++ Web/templates/dashboard/blog_history.html.ep | 54 +++++++++++++++++++ .../lib/MJB/Backend/Jekyll.pm | 21 +++++++- 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 Web/templates/dashboard/blog_history.html.ep diff --git a/Web/lib/MJB/Web.pm b/Web/lib/MJB/Web.pm index 831ab44..f925c7e 100644 --- a/Web/lib/MJB/Web.pm +++ b/Web/lib/MJB/Web.pm @@ -129,6 +129,8 @@ sub startup ($self) { $auth->get ( '/dashboard/blog/:id/media' )->to('Dashboard#blog_media' )->name('show_dashboard_blog_media' ); $auth->post( '/dashboard/blog/:id/media' )->to('Dashboard#do_blog_media' )->name('do_dashboard_blog_media' ); $auth->post( '/dashboard/blog/:id/media/*file' )->to('Dashboard#do_blog_media_remove' )->name('do_dashboard_blog_media_remove' ); + $auth->get ( '/dashboard/blog/:id/history' )->to('Dashboard#blog_history' )->name('show_dashboard_blog_history' ); + $auth->post( '/dashboard/blog/:id/history' )->to('Dashboard#do_blog_history' )->name('do_dashboard_blog_history' ); # Blog Management $auth->get ( '/blog' )->to('Blog#create' )->name('show_blog_create' ); diff --git a/Web/lib/MJB/Web/Controller/Dashboard.pm b/Web/lib/MJB/Web/Controller/Dashboard.pm index 4e95623..af451f4 100644 --- a/Web/lib/MJB/Web/Controller/Dashboard.pm +++ b/Web/lib/MJB/Web/Controller/Dashboard.pm @@ -349,6 +349,20 @@ sub do_blog_media ( $c ) { $c->redirect_to( $c->url_for( 'show_dashboard_blog_media', { id => $blog->id } ) ); } +sub blog_history ( $c ) { + my $blog = $c->stash->{blog} = $c->db->blog( $c->param('id') ); + + if ( $blog->person->id ne $c->stash->{person}->id ) { + $c->render( + text => "Error: This blog isn't owned by you.", + status => 404, + format => 'txt', + ); + return; + } + + my $history = $c->stash->{history} = $c->jekyll($blog->domain->name)->history; +} 1; diff --git a/Web/templates/dashboard/_blog_nav.html.ep b/Web/templates/dashboard/_blog_nav.html.ep index 7286dac..671ff8c 100644 --- a/Web/templates/dashboard/_blog_nav.html.ep +++ b/Web/templates/dashboard/_blog_nav.html.ep @@ -53,4 +53,7 @@ + diff --git a/Web/templates/dashboard/blog_history.html.ep b/Web/templates/dashboard/blog_history.html.ep new file mode 100644 index 0000000..c0b8bbd --- /dev/null +++ b/Web/templates/dashboard/blog_history.html.ep @@ -0,0 +1,54 @@ +% layout 'standard', title => 'Dashboard', sb_active => 'dashboard'; + +%= include 'dashboard/_blog_nav', page => 'posts' + +% if ( my $confirmation = flash 'confirmation' ) { + +% } + +% if ( $c->stash->{success} ) { + +% } + +% if ( $c->stash->{errors} ) { + +% } + + +% if ( $history ) { + + + + + + + + + + % for my $change ( @{$history} ) { + + + + + + % } + +
When?What?Restore?
<%= $change->{dateref} %><%= $change->{message} %> +
+ + +
+
+% } + diff --git a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm index bbe85c4..b60b8fd 100644 --- a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm +++ b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm @@ -310,13 +310,32 @@ sub delete_post { } sub history { - # Check if the repo exists + my ( $self ) = @_; + + # Check if the repo exists and update the repo if needed + $self->_ensure_repository_is_latest; # Do a git history + my $result = $self->system_command( [ qw(git log --date=relative), q|--pretty=%H %ad %s| ], { + chdir => $self->repo_path, + }); + + my @return; + # Format the results into a data structure + foreach my $line ( split( /\n/, $result->{stdout} ) ) { + if ( $line =~ /^([0-9a-f]{40}) (.+ ago) (.+)$/ ) { + push @return, { + commit => $1, + dateref => $2, + message => $3, + }; + } + } # Return the data structure + return [ @return ]; } # Helper function to ensure the repo exists and has the latest