From c8c1b7aeb1dd8df3ec1b9dce85795fcb260603bc Mon Sep 17 00:00:00 2001 From: ngoomie Date: Fri, 23 Aug 2024 22:44:45 -0600 Subject: [PATCH] Change `$self` var names in accordance with #21 and #20 --- lib/CharmBoard.pm | 66 +++++++++---------- lib/CharmBoard/Controller/Index.pm | 10 +-- lib/CharmBoard/Controller/Login.pm | 28 ++++---- lib/CharmBoard/Controller/Logout.pm | 6 +- lib/CharmBoard/Controller/NewThread.pm | 30 +++++---- lib/CharmBoard/Controller/Register.pm | 38 +++++------ lib/CharmBoard/Controller/ViewSubf.pm | 14 ++-- lib/CharmBoard/Model/Forums.pm | 6 +- lib/CharmBoard/Model/Schema/Set/Categories.pm | 8 +-- lib/CharmBoard/Model/Schema/Set/Subforums.pm | 12 ++-- lib/CharmBoard/Model/Schema/Set/Threads.pm | 8 +-- templates/index.html.ep | 6 +- templates/layouts/default/_header.html.ep | 4 +- templates/login.html.ep | 2 +- templates/register.html.ep | 2 +- templates/subf.html.ep | 6 +- templates/thread_compose.html.ep | 4 +- 17 files changed, 127 insertions(+), 123 deletions(-) diff --git a/lib/CharmBoard.pm b/lib/CharmBoard.pm index f8365a7..dac37aa 100644 --- a/lib/CharmBoard.pm +++ b/lib/CharmBoard.pm @@ -12,39 +12,39 @@ use CharmBoard::Util::Crypt::Seasoning; # this method will run once at server start sub startup { - my $self = shift; + my $app = shift; # load plugins that require no additional conf - $self->plugin('TagHelpers'); - $self->plugin('Model', {namespaces => ['CharmBoard::Model']}); + $app->plugin('TagHelpers'); + $app->plugin('Model', {namespaces => ['CharmBoard::Model']}); # load configuration from config file my $config = - $self->plugin('Config' => { file => 'charmboard.conf' }); + $app->plugin('Config' => { file => 'charmboard.conf' }); # set this specific forum's name - $self->helper(board_name => sub { $config->{board_name} }); + $app->helper(board_name => sub { $config->{board_name} }); # load dev env only stuff, if applicable if (lc($config->{environment}) eq 'dev') { - $self->renderer->cache->max_keys(0) + $app->renderer->cache->max_keys(0) } # import Mojolicious secrets - $self->secrets($config->{secrets}); + $app->secrets($config->{secrets}); # import password pepper value - $self->helper(pepper => sub { $config->{pass_crypt}->{pepper} }); + $app->helper(pepper => sub { $config->{pass_crypt}->{pepper} }); ## database setup # ? this could maybe be a given/when { my ($_dsn, $_unicode); - if (lc($self->config->{database}->{type}) eq 'sqlite') { + if (lc($app->config->{database}->{type}) eq 'sqlite') { $_dsn = "dbi:SQLite:" . $config->{database}->{name}; $_unicode = "sqlite_unicode" - } elsif (lc($self->config->{database}->{type}) eq 'mariadb') { + } elsif (lc($app->config->{database}->{type}) eq 'mariadb') { $_dsn = "dbi:mysql:" . $config->{database}->{name}; $_unicode = "mysql_enable_utf" @@ -63,18 +63,18 @@ sub startup { { $_unicode => 1 } ); - $self->helper(schema => sub { $schema }) + $app->helper(schema => sub { $schema }) } # session helpers ## create session - $self->helper(session_create => sub { - my $self = shift; + $app->helper(session_create => sub { + my $app = shift; my $_session_key = seasoning(16); # create session entry in db - $self->schema->resultset('Session')->create({ + $app->schema->resultset('Session')->create({ session_key => $_session_key, user_id => $_[0], session_expiry => time + 604800, @@ -83,59 +83,59 @@ sub startup { }); # now create session cookie - $self->session(is_auth => 1 ); - $self->session(user_id => $_[0] ); - $self->session(session_key => $_session_key); - $self->session(expiration => 604800 ); + $app->session(is_auth => 1 ); + $app->session(user_id => $_[0] ); + $app->session(session_key => $_session_key); + $app->session(expiration => 604800 ); }); ## destroy session - $self->helper(session_destroy => sub { - my $self = shift; + $app->helper(session_destroy => sub { + my $app = shift; - my $_session_key = $self->session('session_key'); + my $_session_key = $app->session('session_key'); # destroy entry for this session in the database - $self->schema->resultset('Session') + $app->schema->resultset('Session') ->search({ session_key => $_session_key }) ->delete; # now nuke the actual session cookie - $self->session(expires => 1); + $app->session(expires => 1); }); ## verify session - $self->helper(session_verify => sub { - my $self = shift; + $app->helper(session_verify => sub { + my $app = shift; my $_validity = 1; my $_catch_error; # get info from user's session cookie and store it in vars - my $_user_id = $self->session('user_id'); - my $_session_key = $self->session('session_key'); - my $_is_auth = $self->session('is_auth'); + my $_user_id = $app->session('user_id'); + my $_session_key = $app->session('session_key'); + my $_is_auth = $app->session('is_auth'); if ($_is_auth) { try { # check to see if session with this id is present in db - ($self->schema->resultset('Session')->search + ($app->schema->resultset('Session')->search ({ 'session_key' => $_session_key }) ->get_column('session_key')->first) or die; # check to see if the current session key's user id matches # that of the user id in the database - $_user_id == ($self->schema->resultset('Session')-> + $_user_id == ($app->schema->resultset('Session')-> session_uid($_session_key)) or die; # check if session is still within valid time as recorded in # the db - time < ($self->schema->resultset('Session')-> + time < ($app->schema->resultset('Session')-> session_expiry($_session_key)) or die; } catch ($_catch_error) { $_validity = undef; - $self->session_destroy; + $app->session_destroy; } } else { $_validity = 0; @@ -145,7 +145,7 @@ sub startup { }); # router - my $r = $self->routes; + my $r = $app->routes; # controller routes ## index page diff --git a/lib/CharmBoard/Controller/Index.pm b/lib/CharmBoard/Controller/Index.pm index 403ec8b..9db6b26 100644 --- a/lib/CharmBoard/Controller/Index.pm +++ b/lib/CharmBoard/Controller/Index.pm @@ -10,15 +10,15 @@ use Mojo::Base 'Mojolicious::Controller', -signatures; use Tree::Simple; sub index { - my $self = shift; + my $c = shift; - if ($self->session_verify eq undef) { - $self->redirect_to('/') + if ($c->session_verify eq undef) { + $c->redirect_to('/') } - $self->render( + $c->render( template => 'index', - category_tree => $self->model('forums')->list_full + category_tree => $c->model('forums')->list_full ) } diff --git a/lib/CharmBoard/Controller/Login.pm b/lib/CharmBoard/Controller/Login.pm index b99fcbe..906229c 100644 --- a/lib/CharmBoard/Controller/Login.pm +++ b/lib/CharmBoard/Controller/Login.pm @@ -11,19 +11,19 @@ use CharmBoard::Util::Crypt::Password; use CharmBoard::Util::Crypt::Seasoning; sub login { - my $self = shift; + my $c = shift; - $self->render( + $c->render( template => 'login', - error => $self->flash('error'), - message => $self->flash('message') + error => $c->flash('error'), + message => $c->flash('message') ) } sub login_do { - my $self = shift; - my $username = $self->param('username'); - my $password = $self->pepper . ':' . $self->param('password'); + my $c = shift; + my $username = $c->param('username'); + my $password = $c->pepper . ':' . $c->param('password'); my $catch_error; @@ -34,7 +34,7 @@ sub login_do { # check user credentials first try { # check to see if user by entered username exists - $user_info = $self->schema->resultset('Users') + $user_info = $c->schema->resultset('Users') ->search({ username => $username }); $user_info or die; @@ -45,27 +45,27 @@ sub login_do { } catch ($catch_error) { # redirect to login page on fail print $catch_error; - $self->flash(error => 'Username or password incorrect.'); - $self->redirect_to('login'); + $c->flash(error => 'Username or password incorrect.'); + $c->redirect_to('login'); } try { # now attempt to create session # get user ID for session creation $user_id = $user_info->get_column('user_id')->first; - $self->session_create($user_id); + $c->session_create($user_id); # redirect to index upon success - $self->redirect_to('/') + $c->redirect_to('/') } catch ($catch_error) { # redirect to login page on fail print $catch_error; - $self->flash( + $c->flash( error => 'Your username and password were correct, but a server error prevented you from logging in. This has been logged so the administrator can fix it.' ); - $self->redirect_to('login') + $c->redirect_to('login') } } diff --git a/lib/CharmBoard/Controller/Logout.pm b/lib/CharmBoard/Controller/Logout.pm index b204b57..6aaf9d8 100644 --- a/lib/CharmBoard/Controller/Logout.pm +++ b/lib/CharmBoard/Controller/Logout.pm @@ -9,12 +9,12 @@ use feature ':5.20'; use Mojo::Base 'Mojolicious::Controller', -signatures; sub logout_do { - my $self = shift; + my $c = shift; - $self->session_destroy; + $c->session_destroy; # redirect to index - $self->redirect_to('/') + $c->redirect_to('/') } 1; diff --git a/lib/CharmBoard/Controller/NewThread.pm b/lib/CharmBoard/Controller/NewThread.pm index 89f330f..e42baea 100644 --- a/lib/CharmBoard/Controller/NewThread.pm +++ b/lib/CharmBoard/Controller/NewThread.pm @@ -9,32 +9,33 @@ use feature ':5.20'; use Mojo::Base 'Mojolicious::Controller', -signatures; sub thread_compose { - my $self = shift; + my $c = shift; - my $subf_id = $self->param('id'); + my $subf_id = $c->param('id'); my $subf_cat = - $self->schema->resultset('Subforums')->cat_from_id($subf_id); + $c->schema->resultset('Subforums')->cat_from_id($subf_id); my $cat_title = - $self->schema->resultset('Categories') + $c->schema->resultset('Categories') ->title_from_id($subf_cat); - $self->render( + $c->render( template => 'thread_compose', subf_id => $subf_id, cat_title => $cat_title, - subf_title => $self->schema->resultset('Subforums') + subf_title => $c->schema->resultset('Subforums') ->title_from_id($subf_id), - error => $self->flash('error'), - message => $self->flash('message') + error => $c->flash('error'), + message => $c->flash('message') ) } sub thread_submit { - my $self = shift; + my $c = shift; - my $thread_title = $self->param('thread-title'); - my $post_content = $self->param('post-content'); + my $thread_title = $c->param('thread-title'); + my $post_content = $c->param('post-content'); my $post_time = time; + my $subf_id = $c->param('id'); my $catch_error; @@ -43,9 +44,12 @@ sub thread_submit { ($thread_title, $post_content) or die "Please fill both the title and post content fields" } catch ($catch_error) { - $self->flash(error => $catch_error); - $self->redirect_to('/:id/new') + $c->flash(error => $catch_error); + $c->redirect_to('board/:id/new') } + + # now send it + } 1; diff --git a/lib/CharmBoard/Controller/Register.pm b/lib/CharmBoard/Controller/Register.pm index 1591636..86582f6 100644 --- a/lib/CharmBoard/Controller/Register.pm +++ b/lib/CharmBoard/Controller/Register.pm @@ -11,22 +11,22 @@ use CharmBoard::Util::Crypt::Password; # initial registration page sub register { - my $self = shift; - $self->render( + my $c = shift; + $c->render( template => 'register', - error => $self->flash('error'), - message => $self->flash('message') + error => $c->flash('error'), + message => $c->flash('message') ) } # process submitted registration form sub register_do { - my $self = shift; + my $c = shift; - my $username = $self->param('username'); - my $email = $self->param('email'); - my $password = $self->param('password'); - my $confirm_password = $self->param('confirm-password'); + my $username = $c->param('username'); + my $email = $c->param('email'); + my $password = $c->param('password'); + my $confirm_password = $c->param('confirm-password'); my $catch_error; @@ -50,9 +50,9 @@ sub register_do { # check to make sure username and/or email isn't already in use; # if not, continue with registration ## search for input username and email in database - $user_check = $self->schema->resultset('Users') + $user_check = $c->schema->resultset('Users') ->search({ username => $username })->single; - $email_check = $self->schema->resultset('Users') + $email_check = $c->schema->resultset('Users') ->search({ email => $email })->single; # TODO: compress this into something less redundant @@ -63,18 +63,18 @@ sub register_do { ($email_check) eq undef or die "email already in use." } catch ($catch_error) { - $self->flash(error => $catch_error); - $self->redirect_to('register') + $c->flash(error => $catch_error); + $c->redirect_to('register') } try { - $password = $self->pepper . ':' . $password; + $password = $c->pepper . ':' . $password; # return hashed result + salt ($salt, $hash) = passgen($password) or die; # add user info and pw/salt to DB - $self->schema->resultset('Users')->create({ + $c->schema->resultset('Users')->create({ username => $username, email => $email, password => $hash, @@ -83,17 +83,17 @@ sub register_do { }) or die; - $self->flash(message => 'User registered successfully!'); - $self->redirect_to('register') + $c->flash(message => 'User registered successfully!'); + $c->redirect_to('register') } catch ($catch_error) { print $catch_error; - $self->flash( + $c->flash( error => 'Your registration info was correct, but a server error prevented you from registering. This has been logged so the administrator can fix it.' ); - $self->redirect_to('register') + $c->redirect_to('register') } } diff --git a/lib/CharmBoard/Controller/ViewSubf.pm b/lib/CharmBoard/Controller/ViewSubf.pm index 738dd4d..a17ef28 100644 --- a/lib/CharmBoard/Controller/ViewSubf.pm +++ b/lib/CharmBoard/Controller/ViewSubf.pm @@ -9,23 +9,23 @@ use feature ':5.20'; use Mojo::Base 'Mojolicious::Controller', -signatures; sub subf_view { - my $self = shift; + my $c = shift; - my $subf_id = $self->param('id'); + my $subf_id = $c->param('id'); my $subf_cat = - $self->schema->resultset('Subforums')->cat_from_id($subf_id); + $c->schema->resultset('Subforums')->cat_from_id($subf_id); my $cat_title = - $self->schema->resultset('Categories') + $c->schema->resultset('Categories') ->title_from_id($subf_cat); my @thread_list = - $self->schema->resultset('Threads')->fetch_by_subf($subf_id); + $c->schema->resultset('Threads')->fetch_by_subf($subf_id); - $self->render( + $c->render( template => 'subf', subf_id => $subf_id, cat_title => $cat_title, - subf_title => $self->schema->resultset('Subforums') + subf_title => $c->schema->resultset('Subforums') ->title_from_id($subf_id), thread_list => \@thread_list ) diff --git a/lib/CharmBoard/Model/Forums.pm b/lib/CharmBoard/Model/Forums.pm index 0877661..3124475 100644 --- a/lib/CharmBoard/Model/Forums.pm +++ b/lib/CharmBoard/Model/Forums.pm @@ -10,9 +10,9 @@ use Mojo::Base 'MojoX::Model'; use Tree::Simple; sub list_full { - my $self = shift; + my $c = shift; # fetch a list of all categories - my @_all_cat = $self->{app}->schema->resultset('Categories')->fetch_all; + my @_all_cat = $c->{app}->schema->resultset('Categories')->fetch_all; # create a Tree::Simple object that will contain the list # of categories and the subforums that belong to them @@ -26,7 +26,7 @@ sub list_full { # fetch all subforums that belong to this category @_fetch_subf = - $self->{app}->schema->resultset('Subforums') + $c->{app}->schema->resultset('Subforums') ->fetch_by_cat($_iter_cat); # add each fetched subforum as children of the branch diff --git a/lib/CharmBoard/Model/Schema/Set/Categories.pm b/lib/CharmBoard/Model/Schema/Set/Categories.pm index dd5dbaa..a04d810 100644 --- a/lib/CharmBoard/Model/Schema/Set/Categories.pm +++ b/lib/CharmBoard/Model/Schema/Set/Categories.pm @@ -9,18 +9,18 @@ use feature ':5.20'; use base 'DBIx::Class::ResultSet'; sub fetch_all { - my $_set = shift; + my $set = shift; - my $_fetch = $_set->search({}, { order_by => 'cat_rank' }); + my $_fetch = $set->search({}, { order_by => 'cat_rank' }); return ($_fetch->get_column('cat_id')->all) } sub title_from_id { - my $_set = shift; + my $set = shift; return ( - $_set->search({ 'cat_id' => $_[0] })->get_column('cat_name') + $set->search({ 'cat_id' => $_[0] })->get_column('cat_name') ->first) } diff --git a/lib/CharmBoard/Model/Schema/Set/Subforums.pm b/lib/CharmBoard/Model/Schema/Set/Subforums.pm index f8797ab..a222888 100644 --- a/lib/CharmBoard/Model/Schema/Set/Subforums.pm +++ b/lib/CharmBoard/Model/Schema/Set/Subforums.pm @@ -9,26 +9,26 @@ use feature ':5.20'; use base 'DBIx::Class::ResultSet'; sub fetch_by_cat { - my $_set = shift; + my $set = shift; - my $_fetch = $_set->search({ 'subf_cat' => $_[0] }, + my $_fetch = $set->search({ 'subf_cat' => $_[0] }, { order_by => 'subf_rank' }); return ($_fetch->get_column('subf_id')->all) } sub cat_from_id { - my $_set = shift; + my $set = shift; return ( - $_set->search({ 'subf_id' => $_[0] })->get_column('subf_cat') + $set->search({ 'subf_id' => $_[0] })->get_column('subf_cat') ->first) } sub title_from_id { - my $_set = shift; + my $set = shift; - return ($_set->search({ 'subf_id' => $_[0] }) + return ($set->search({ 'subf_id' => $_[0] }) ->get_column('subf_name')->first) } diff --git a/lib/CharmBoard/Model/Schema/Set/Threads.pm b/lib/CharmBoard/Model/Schema/Set/Threads.pm index 70c5e1f..10a5a35 100644 --- a/lib/CharmBoard/Model/Schema/Set/Threads.pm +++ b/lib/CharmBoard/Model/Schema/Set/Threads.pm @@ -9,18 +9,18 @@ use feature ':5.20'; use base 'DBIx::Class::ResultSet'; sub fetch_by_subf { - my $_set = shift; + my $set = shift; my $_fetch = - $_set->search({ 'thread_subf' => $_[0] }); + $set->search({ 'thread_subf' => $_[0] }); return ($_fetch->get_column('thread_id')->all) } sub title_from_id { - my $_set = shift; + my $set = shift; - return ($_set->search({ 'thread_id' => $_[0] }) + return ($set->search({ 'thread_id' => $_[0] }) ->get_column('thread_title')->first) } diff --git a/templates/index.html.ep b/templates/index.html.ep index dd6b09d..01e03b3 100644 --- a/templates/index.html.ep +++ b/templates/index.html.ep @@ -1,5 +1,5 @@ % layout 'default', -% title => $self->board_name; +% title => $c->board_name; <% my $cat_header = begin %> % my $_cat_id = shift; my $_name = shift; @@ -21,13 +21,13 @@ foreach my $category ($category_tree->getAllChildren) { %> <%= $cat_header->( $category->getNodeValue, - $self->schema->resultset('Categories')-> + $c->schema->resultset('Categories')-> title_from_id($category->getNodeValue)) %> <% foreach my $subforum ($category->getAllChildren) { %> <%= $subf_item->( $subforum->getNodeValue, $category->getNodeValue, - $self->schema->resultset('Subforums')-> + $c->schema->resultset('Subforums')-> title_from_id($subforum->getNodeValue)) %> <% }} %> \ No newline at end of file diff --git a/templates/layouts/default/_header.html.ep b/templates/layouts/default/_header.html.ep index a59b778..e864acd 100644 --- a/templates/layouts/default/_header.html.ep +++ b/templates/layouts/default/_header.html.ep @@ -2,7 +2,7 @@ my $userControls; # TODO: once implemented, put username + profile link first -if ($self->session('is_auth')) { +if ($c->session('is_auth')) { my $username = ''; $userControls = "logout"} else { @@ -10,5 +10,5 @@ else { "login | register"}; %> -

<%== $self->board_name %>

+

<%== $c->board_name %>

<%== $userControls %>

\ No newline at end of file diff --git a/templates/login.html.ep b/templates/login.html.ep index ffa4c1c..61c2fea 100644 --- a/templates/login.html.ep +++ b/templates/login.html.ep @@ -1,5 +1,5 @@ % layout 'default', -% title => $self->board_name . ' - Login'; +% title => $c->board_name . ' - Login'; % if ($error) {

<%= $error %>

%}; diff --git a/templates/register.html.ep b/templates/register.html.ep index 710b299..c4cb544 100644 --- a/templates/register.html.ep +++ b/templates/register.html.ep @@ -1,5 +1,5 @@ % layout 'default', -% title => $self->board_name . ' - Registration'; +% title => $c->board_name . ' - Registration'; % if ($error) {

<%= $error %>

%}; diff --git a/templates/subf.html.ep b/templates/subf.html.ep index b8b94f7..3fa2a59 100644 --- a/templates/subf.html.ep +++ b/templates/subf.html.ep @@ -1,5 +1,5 @@ % layout 'default', -% title => $subf_title . ' - ' . $self->board_name; +% title => $subf_title . ' - ' . $c->board_name; % my @thread_list = @{stash('thread_list')}; <% my $thread_item = begin %> @@ -9,7 +9,7 @@ <% end %> -<%= $self->board_name %> » <%= $cat_title %> » +<%= $c->board_name %> » <%= $cat_title %> » <%= $subf_title %>

@@ -20,6 +20,6 @@ like to make one? foreach my $thread_id (@thread_list) { %> <%= $thread_item->( $thread_id, - $self->schema->resultset('Threads')-> + $c->schema->resultset('Threads')-> title_from_id($thread_id)) %> <% }} %> \ No newline at end of file diff --git a/templates/thread_compose.html.ep b/templates/thread_compose.html.ep index 9aa2310..f747fff 100644 --- a/templates/thread_compose.html.ep +++ b/templates/thread_compose.html.ep @@ -1,6 +1,6 @@ % layout 'default', -% title => 'New thread - ' . $self->board_name; -<%= $self->board_name %> » <%= $cat_title %> » +% title => 'New thread - ' . $c->board_name; +<%= $c->board_name %> » <%= $cat_title %> » <%= $subf_title %> » new thread