Browse Source

standardise page rendering packages

Byron Jones 6 years ago
parent
commit
cc965a4963
7 changed files with 124 additions and 131 deletions
  1. 47 40
      lib/LogBot/Web/Channel.pm
  2. 13 0
      lib/LogBot/Web/Config.pm
  3. 1 0
      lib/LogBot/Web/Index.pm
  4. 1 0
      lib/LogBot/Web/List.pm
  5. 37 29
      lib/LogBot/Web/Stats.pm
  6. 11 0
      lib/LogBot/Web/Util.pm
  7. 14 62
      logbot-web

+ 47 - 40
lib/LogBot/Web/Channel.pm

@@ -8,13 +8,13 @@ use DateTime ();
 use Encode qw( decode );
 use LogBot::Database qw( dbh execute_with_timeout );
 use LogBot::Util qw( event_to_short_string );
-use LogBot::Web::Util qw( preprocess_event );
+use LogBot::Web::Util qw( channel_from_param date_from_param preprocess_event );
 
 sub _get_logs {
-    my ($c, $dbh) = @_;
+    my ($c, $dbh, $channel, $date) = @_;
 
-    my $start_time = $c->stash('date')->epoch;
-    my $end_time = $c->stash('date')->clone->add(days => 1)->epoch;
+    my $start_time = $date->epoch;
+    my $end_time = $date->clone->add(days => 1)->epoch;
 
     ## no critic (ProhibitInterpolationOfLiterals)
     #<<<
@@ -26,7 +26,7 @@ sub _get_logs {
     #>>>
     ## use critic
 
-    my $logs = execute_with_timeout($dbh, $sql, [$c->stash('channel')], 5);
+    my $logs = execute_with_timeout($dbh, $sql, [$channel], 5);
     unless (defined $logs) {
         $c->stash(error => 'Request took too long to process and has been cancelled.',);
         $c->render('channel');
@@ -36,29 +36,31 @@ sub _get_logs {
     return ($logs, $sql);
 }
 
-sub logs {
-    my ($c, $params) = @_;
+sub render_logs {
+    my ($c) = @_;
     my $config = $c->stash('config');
 
-    if (my $error = $params->{error}) {
-        $c->stash(
-            channel => '',
-            error   => $error,
-        );
-    }
-    return $c->render('index') unless $c->stash('channel');
+    my $channel = channel_from_param($c) // return;
 
-    $c->stash(
-        logs      => undef,
-        last_date => undef,
-        skip_prev => undef,
-        skip_next => undef,
-    );
+    my $date = date_from_param($c);
+    if (!defined $date) {
+        return $c->redirect_to('/' . substr($channel, 1) . '/' . $c->stash('today')->ymd(''));
+    }
+    my $is_today = $date == $c->stash('today');
+    my $time     = $date->epoch;
 
     my $dbh = dbh($config, cached => 1);
-    my ($logs, $sql) = _get_logs($c, $dbh);
+    my ($logs, $sql) = _get_logs($c, $dbh, $channel, $date);
     return unless defined($logs);
 
+    # store last visited channel
+    $c->cookie(
+        'last-c' => $channel, {
+            path    => '/',
+            expires => time() + 60 * 60 * 24 * 365,
+        }
+    );
+
     # process each event
     my $bot_event_count = 0;
     my $nick_hashes     = {};
@@ -68,30 +70,36 @@ sub logs {
     }
 
     # calc navigation dates
-    if (@{$logs} && $c->stash('is_today')) {
+
+    $c->stash(
+        last_date => undef,
+        skip_prev => undef,
+        skip_next => undef,
+    );
+
+    if (@{$logs} && $is_today) {
         $c->stash(last_date => DateTime->from_epoch(epoch => $logs->[-1]->{time}));
     }
 
-    my $time = $c->stash('date')->epoch;
-
     my $skip_prev_time =
         $dbh->selectrow_array('SELECT time FROM logs WHERE channel = ? AND time < ? ORDER BY time DESC LIMIT 1',
-        undef, $c->stash('channel'), $time);
+        undef, $channel, $time);
     if ($skip_prev_time) {
         $c->stash(skip_prev => DateTime->from_epoch(epoch => $skip_prev_time)->truncate(to => 'day'));
     }
 
-    my $skip_next_time = $dbh->selectrow_array(
-        'SELECT time FROM logs WHERE channel = ? AND time >= ? ORDER BY time ASC LIMIT 1',
-        undef,
-        $c->stash('channel'),
-        $time + 60 * 60 * 24
-    );
+    my $skip_next_time =
+        $dbh->selectrow_array('SELECT time FROM logs WHERE channel = ? AND time >= ? ORDER BY time ASC LIMIT 1',
+        undef, $channel, $time + 60 * 60 * 24);
     if ($skip_next_time) {
         $c->stash(skip_next => DateTime->from_epoch(epoch => $skip_next_time)->truncate(to => 'day'));
     }
 
     $c->stash(
+        channel         => $channel,
+        date            => $date,
+        page            => 'logs',
+        is_today        => $is_today,
         logs            => $logs,
         event_count     => scalar(@{$logs}),
         bot_event_count => $bot_event_count,
@@ -101,20 +109,19 @@ sub logs {
     $c->render('channel');
 }
 
-sub raw {
-    my ($c, $params) = @_;
+sub render_raw {
+    my ($c) = @_;
     my $config = $c->stash('config');
 
-    if (my $error = $params->{error}) {
-        $c->stash(
-            channel => '',
-            error   => $error,
-        );
+    my $channel = channel_from_param($c) // return;
+
+    my $date = date_from_param($c);
+    if (!defined $date) {
+        return $c->redirect_to('/' . substr($channel, 1) . '/' . $c->stash('today')->ymd(''));
     }
-    return $c->render('index') unless $c->stash('channel');
 
     my $dbh = dbh($config, cached => 1);
-    my ($logs) = _get_logs($c, $dbh);
+    my ($logs) = _get_logs($c, $dbh, $channel, $date);
 
     my @lines;
     foreach my $event (@{$logs}) {

+ 13 - 0
lib/LogBot/Web/Config.pm

@@ -0,0 +1,13 @@
+package LogBot::Web::Config;
+use local::lib;
+use v5.10;
+use strict;
+use warnings;
+
+sub render {
+    my ($c, $params) = @_;
+    $c->stash(page => 'config');
+    return $c->render('config');
+}
+
+1;

+ 1 - 0
lib/LogBot/Web/Index.pm

@@ -14,6 +14,7 @@ sub render {
         );
     }
 
+    $c->stash(page => 'index');
     return $c->render('index');
 }
 

+ 1 - 0
lib/LogBot/Web/List.pm

@@ -6,6 +6,7 @@ use warnings;
 
 sub render {
     my ($c, $params) = @_;
+    $c->stash(page => 'list');
     return $c->render('list');
 }
 

+ 37 - 29
lib/LogBot/Web/Stats.pm

@@ -8,18 +8,16 @@ use DateTime ();
 use JSON::XS qw( decode_json );
 use LogBot::Database qw( dbh );
 use LogBot::Util qw( commify file_for plural slurp time_to_datetimestr );
-use LogBot::Web::Util qw( irc_host );
+use LogBot::Web::Util qw( channel_from_param irc_host );
 use Time::Duration qw( ago );
 
 sub render {
-    my ($c, $params) = @_;
+    my ($c, %params) = @_;
     my $config = $c->stash('config');
 
-    if (my $error = $params->{error}) {
-        $c->stash(
-            channel => '',
-            error   => $error,
-        );
+    if ($params{require_channel}) {
+        my $channel = channel_from_param($c) // return;
+        $c->stash(channel => $channel);
     }
 
     $c->stash(
@@ -37,10 +35,15 @@ sub event_time_to_str {
     return (time_to_datetimestr(int($time)), ago(time() - $time, $accuracy // 2));
 }
 
-sub meta {
-    my ($c, $channel) = @_;
+sub render_meta {
+    my ($c, %params) = @_;
     my $config = $c->stash('config');
 
+    my $channel;
+    if ($params{require_channel}) {
+        $channel = channel_from_param($c) // return;
+    }
+
     my $dbh = dbh($config, cached => 1);
 
     my ($last_time, $last_ago, $event_count);
@@ -88,41 +91,46 @@ sub meta {
     my $channels_in = scalar(grep { !($_->{archived} || $_->{blocked} || $_->{disabled}) } values $config->{channels});
     my $archived = scalar(grep { $_->{archived} && !($_->{blocked} || $_->{disabled}) } values $config->{channels});
 
-    return {
-        first_ago      => $first_ago,
-        last_ago       => $last_ago,
-        event_count    => commify($event_count),
-        active_events  => plural($active_events, 'event') . '/day',
-        active_nicks   => plural($active_nicks, 'active user'),
-        channels_in    => 'Logging ' . plural($channels_in, 'channel'),
-        channels_total => plural($archived, 'archived channel'),
-    };
+    $c->render(
+        json => {
+            first_ago      => $first_ago,
+            last_ago       => $last_ago,
+            event_count    => commify($event_count),
+            active_events  => plural($active_events, 'event') . '/day',
+            active_nicks   => plural($active_nicks, 'active user'),
+            channels_in    => 'Logging ' . plural($channels_in, 'channel'),
+            channels_total => plural($archived, 'archived channel'),
+        }
+    );
 }
 
-sub hours {
-    my ($c, $channel) = @_;
+sub render_hours {
+    my ($c, %params) = @_;
     my $config = $c->stash('config');
 
+    my $channel;
+    if ($params{require_channel}) {
+        $channel = channel_from_param($c) // return;
+    }
+
     my $file = file_for($config, 'meta', $channel, 'hours');
-    return -e $file ? slurp($file) : slurp(file_for($config, 'meta', '_empty', 'hours'));
+    $c->render(
+        text => -e $file ? slurp($file) : slurp(file_for($config, 'meta', '_empty', 'hours')),
+        format => 'json',
+    );
 }
 
 sub render_nicks {
-    my ($c, $params) = @_;
+    my ($c) = @_;
     my $config = $c->stash('config');
 
-    if (my $error = $params->{error}) {
-        $c->stash(
-            channel => '',
-            error   => $error,
-        );
-    }
+    my $channel = channel_from_param($c) // return;
 
     my $file = file_for($config, 'meta', $c->stash('channel'), 'nicks');
     my $data = decode_json(-e $file ? slurp($file) : slurp(file_for($config, 'meta', '_empty', 'nicks')));
 
     $c->stash(nicks => $data);
-    return $c->render('stats_nicks');
+    $c->render('stats_nicks');
 }
 
 1;

+ 11 - 0
lib/LogBot/Web/Util.pm

@@ -8,8 +8,10 @@ use Date::Parse qw( str2time );
 use DateTime ();
 use Digest::xxHash qw( xxhash32 );
 use Encode qw ( decode );
+use File::Basename qw( basename );
 use LogBot::Database qw( dbh );
 use LogBot::Util qw( nick_is_bot normalise_channel time_to_ymd ymd_to_time );
+use Module::Load qw( load );
 use Mojo::Path ();
 use Mojo::URL  ();
 use Mojo::Util qw( html_unescape xml_escape );
@@ -17,6 +19,7 @@ use Readonly;
 use URI::Find ();
 
 our @EXPORT_OK = qw(
+    render_init
     nick_hash nick_colour nick_colour_init
     rewrite_old_urls
     url_for_channel irc_host
@@ -27,6 +30,14 @@ our @EXPORT_OK = qw(
 );
 use parent 'Exporter';
 
+sub render_init {
+    my ($path) = @_;
+    foreach my $file (glob($path . '/lib/LogBot/Web/*.pm')) {
+        next if basename($file) eq 'Util.pm';
+        load($file);
+    }
+}
+
 sub nick_hash {
     my ($nick) = @_;
     $nick = lc($nick);

+ 14 - 62
logbot-web

@@ -21,12 +21,7 @@ use IO::Compress::Gzip qw( gzip );
 use LogBot::Config qw( find_config load_all_configs load_config reload_config );
 use LogBot::MemCache ();
 use LogBot::Util qw( time_to_ymd );
-use LogBot::Web::Channel ();
-use LogBot::Web::Index   ();
-use LogBot::Web::List    ();
-use LogBot::Web::Search  ();
-use LogBot::Web::Stats   ();
-use LogBot::Web::Util qw( channel_from_param channel_topics date_from_param linkify nick_colour_init rewrite_old_urls );
+use LogBot::Web::Util qw( channel_from_param channel_topics linkify nick_colour_init render_init rewrite_old_urls );
 use Mojo::ByteStream ();
 use Mojo::Util qw( dumper );
 use Mojolicious::Lite qw( app );
@@ -71,6 +66,7 @@ plugin AccessLog => {
     format => '%h %{X-Network}o - %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"',
 };
 my $memcache = LogBot::MemCache->new(binary => $is_production);
+render_init($RealBin);
 
 # per-request initialisation
 under sub {
@@ -105,7 +101,7 @@ under sub {
         config          => $config,
         networks        => $networks,
         network         => $config->{name},
-        channels        => $config->{channel_list},
+        channels        => $config->{_derived}->{channel_list},
         topics          => channel_topics($config),
         font_s          => $c->cookie('font-s'),
         menu_c          => $c->cookie('menu-c'),
@@ -161,7 +157,7 @@ get '/' => sub {
 # config
 get '/_config' => sub {
     my ($c) = @_;
-    $c->render('config');
+    LogBot::Web::Config::render($c);
 };
 
 # channel list
@@ -178,12 +174,12 @@ get '/_stats' => sub {
 
 get '/_stats/meta' => sub {
     my ($c) = @_;
-    $c->render(json => LogBot::Web::Stats::meta($c));
+    LogBot::Web::Stats::render_meta($c);
 };
 
 get '/_stats/hours' => sub {
     my ($c) = @_;
-    $c->render(text => LogBot::Web::Stats::hours($c), format => 'json');
+    LogBot::Web::Stats::render_hours($c);
 };
 
 # debugging
@@ -227,75 +223,31 @@ get '/#channel' => sub {
 # /channel/date => show logs
 get '/#channel/:date' => [date => qr/\d{8}/] => sub {
     my ($c) = @_;
-    my $channel = channel_from_param($c) // return;
-
-    # store last visited channel
-    $c->cookie(
-        'last-c' => $channel, {
-            path    => '/',
-            expires => time() + 60 * 60 * 24 * 365,
-        }
-    );
-
-    # find date
-    my $date = date_from_param($c);
-    if (!defined $date) {
-        return $c->redirect_to('/' . substr($channel, 1) . '/' . $c->stash('today')->ymd(''));
-    }
-
-    $c->stash(
-        channel  => $channel,
-        date     => $date,
-        page     => 'logs',
-        is_today => $date == $c->stash('today'),
-    );
-
-    # render
-    LogBot::Web::Channel::logs($c);
+    LogBot::Web::Channel::render_logs($c);
 };
 
 get '/#channel/:date/raw' => [date => qr/\d{8}/] => sub {
     my ($c) = @_;
-    my $channel = channel_from_param($c) // return;
-
-    my $date = date_from_param($c);
-    if (!defined $date) {
-        return $c->redirect_to('/' . substr($channel, 1) . '/' . $c->stash('today')->ymd(''));
-    }
-
-    $c->stash(
-        date    => $date,
-        channel => $channel,
-    );
-    LogBot::Web::Channel::raw($c);
+    LogBot::Web::Channel::render_raw($c);
 };
 
 get '/#channel/stats' => sub {
     my ($c) = @_;
-    my $channel = channel_from_param($c) // return;
-
-    $c->stash(channel => $channel);
-    LogBot::Web::Stats::render($c);
+    LogBot::Web::Stats::render($c, require_channel => 1);
 };
 
 get '/#channel/stats/meta' => sub {
     my ($c) = @_;
-    my $channel = channel_from_param($c) // return;
-
-    $c->render(json => LogBot::Web::Stats::meta($c, $channel));
+    LogBot::Web::Stats::render_meta($c, require_channel => 1);
 };
 
 get '/#channel/stats/hours' => sub {
     my ($c) = @_;
-    my $channel = channel_from_param($c) // return;
-
-    $c->render(text => LogBot::Web::Stats::hours($c, $channel), format => 'json');
+    LogBot::Web::Stats::render_hours($c);
 };
 
 get '/#channel/stats/nicks' => sub {
     my ($c) = @_;
-    my $channel = channel_from_param($c) // return;
-
     LogBot::Web::Stats::render_nicks($c);
 };
 
@@ -374,11 +326,11 @@ app->start;
 # build a quick channel list
 sub build_channel_list {
     my ($config) = @_;
-    return if exists $config->{channel_list};
+    return if exists $config->{_derived}->{channel_list};
 
-    $config->{channel_list} = [];
+    $config->{_derived}->{channel_list} = [];
     foreach my $channel (sort keys %{ $config->{channels} }) {
-        push @{ $config->{channel_list} },
+        push @{ $config->{_derived}->{channel_list} },
             { name => $channel, archived => $config->{channels}->{$channel}->{archived} };
     }
 }