diff options
author | Yorhel <git@yorhel.nl> | 2020-10-02 10:44:37 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2020-10-02 10:44:37 +0200 |
commit | 42598ddb3d0dd629406ebd7186159e3fc130b19c (patch) | |
tree | e949bbc52f5e5db89d919842065f43f7c3e35926 | |
parent | 97503c414d99e1d14c3d1dd25d74eab0720396df (diff) |
v2rw: Move redirects + cleanup some more
-rw-r--r-- | lib/VNDB/Handler/Misc.pm | 12 | ||||
-rw-r--r-- | lib/VNDB/Handler/VNPage.pm | 8 | ||||
-rw-r--r-- | lib/VNDB/Util/Auth.pm | 50 | ||||
-rw-r--r-- | lib/VNWeb/Misc/Redirects.pm | 42 | ||||
-rw-r--r-- | lib/VNWeb/VN/Page.pm | 4 |
5 files changed, 45 insertions, 71 deletions
diff --git a/lib/VNDB/Handler/Misc.pm b/lib/VNDB/Handler/Misc.pm index 565523e6..d2cb9c0d 100644 --- a/lib/VNDB/Handler/Misc.pm +++ b/lib/VNDB/Handler/Misc.pm @@ -13,18 +13,6 @@ TUWF::register( qr{nospam}, \&nospam, qr{xml/prefs\.xml}, \&prefs, qr{opensearch\.xml}, \&opensearch, - - # redirects for old URLs - qr{u([1-9]\d*)/tags}, sub { $_[0]->resRedirect("/g/links?u=$_[1]", 'perm') }, - qr{(.*[^/]+)/+}, sub { $_[0]->resRedirect("/$_[1]", 'perm') }, - qr{([pv])}, sub { $_[0]->resRedirect("/$_[1]/all", 'perm') }, - qr{v/search}, sub { $_[0]->resRedirect("/v/all?q=".uri_escape($_[0]->reqGet('q')||''), 'perm') }, - qr{notes}, sub { $_[0]->resRedirect('/d8', 'perm') }, - qr{faq}, sub { $_[0]->resRedirect('/d6', 'perm') }, - qr{v([1-9]\d*)/(?:stats|scr)}, - sub { $_[0]->resRedirect("/v$_[1]", 'perm') }, - qr{u/list(/[a-z0]|/all)?}, - sub { my $l = defined $_[1] ? $_[1] : '/all'; $_[0]->resRedirect("/u$l", 'perm') }, ); diff --git a/lib/VNDB/Handler/VNPage.pm b/lib/VNDB/Handler/VNPage.pm index 1198a421..b546c436 100644 --- a/lib/VNDB/Handler/VNPage.pm +++ b/lib/VNDB/Handler/VNPage.pm @@ -9,18 +9,10 @@ use VNDB::Types; TUWF::register( - qr{v/rand} => \&rand, qr{v([1-9]\d*)/releases} => \&releases, - qr{v([1-9]\d*)/staff} => sub { $_[0]->resRedirect("/v$_[1]#staff") }, ); -sub rand { - my $self = shift; - $self->resRedirect('/v'.$self->filFetchDB(vn => undef, undef, {results => 1, sort => 'rand'})->[0]{id}, 'temp'); -} - - # Description of each column, field: # id: Identifier used in URLs # sort_field: Name of the field when sorting diff --git a/lib/VNDB/Util/Auth.pm b/lib/VNDB/Util/Auth.pm index b05c86f9..f3094ff0 100644 --- a/lib/VNDB/Util/Auth.pm +++ b/lib/VNDB/Util/Auth.pm @@ -10,58 +10,10 @@ use VNWeb::Auth; our @EXPORT = qw| - authInit authLogin authLogout authInfo authCan authSetPass authAdminSetPass - authResetPass authIsValidToken authGetCode authCheckCode authPref + authInfo authCan authGetCode authCheckCode authPref |; -# login, arguments: user, password, url-to-redirect-to-on-success -# returns 1 on success (redirected), 0 otherwise (no reply sent) -sub authLogin { - my(undef, $user, $pass, $to) = @_; - my $success = auth->login($user, $pass); - tuwf->resRedirect($to, 'post') if $success; - $success -} - -# clears authentication cookie and redirects to / -sub authLogout { - auth->logout; - tuwf->resRedirect('/', 'temp'); -} - - -# Replaces the user's password with a random token that can be used to reset the password. -sub authResetPass { - my(undef, $mail) = @_; - auth->resetpass($mail) -} - - -sub authIsValidToken { - my(undef, $uid, $token) = @_; - auth->isvalidtoken($uid, $token) -} - - -# uid, new_pass, url_to_redir_to, 'token'|'pass', $token_or_pass -# Changes the user's password, invalidates all existing sessions, creates a new -# session and redirects. -sub authSetPass { - my(undef, $uid, $pass, $redir, $oldtype, $oldpass) = @_; - - my $success = auth->setpass($uid, $oldtype eq 'token' ? $oldpass : undef, $oldtype eq 'pass' ? $oldpass : undef, $pass); - tuwf->resRedirect($redir, 'post') if $success; - $success -} - - -sub authAdminSetPass { - my(undef, $uid, $pass) = @_; - auth->admin_setpass($uid, $pass); -} - - sub authInfo { # Used to return a lot more, but only the id is still used now. # (code using other fields has been migrated) diff --git a/lib/VNWeb/Misc/Redirects.pm b/lib/VNWeb/Misc/Redirects.pm new file mode 100644 index 00000000..964c4e24 --- /dev/null +++ b/lib/VNWeb/Misc/Redirects.pm @@ -0,0 +1,42 @@ +package VNWeb::Misc::Redirects; + +use VNWeb::Prelude; +use VNWeb::Filters; + + +# VNDB URLs don't have a trailing /, redirect if we get one. +TUWF::get qr{(/.+?)/+}, sub { tuwf->resRedirect(tuwf->capture(1).tuwf->reqQuery(), 'perm') }; + +# These two are ancient. +TUWF::get qr{/notes}, sub { tuwf->resRedirect('/d8', 'perm') }; +TUWF::get qr{/faq}, sub { tuwf->resRedirect('/d6', 'perm') }; + +TUWF::get qr{/p}, sub { tuwf->resRedirect('/p/all'.tuwf->reqQuery(), 'perm') }; +TUWF::get qr{/v}, sub { tuwf->resRedirect('/v/all'.tuwf->reqQuery(), 'perm') }; +TUWF::get qr{/v/search}, sub { tuwf->resRedirect('/v/all'.tuwf->reqQuery(), 'perm') }; + +TUWF::get qr{/u/list(/[a-z0]|/all)?}, sub { tuwf->resRedirect('/u'.(tuwf->capture(1)//'/all'), 'perm') }; + +TUWF::get qr{/$RE{uid}/tags}, sub { tuwf->resRedirect('/g/links?u='.tuwf->capture('id'), 'perm') }; + +TUWF::get qr{/$RE{vid}/staff}, sub { tuwf->resRedirect(sprintf '/v%s#staff', tuwf->capture('id')) }; +TUWF::get qr{/$RE{vid}/stats}, sub { tuwf->resRedirect(sprintf '/v%s#stats', tuwf->capture('id')) }; +TUWF::get qr{/$RE{vid}/scr}, sub { tuwf->resRedirect(sprintf '/v%s#screenshots', tuwf->capture('id')) }; + + +TUWF::get qr{/v/rand}, sub { + state $stats ||= tuwf->dbRowi('SELECT COUNT(*) AS total, COUNT(*) FILTER(WHERE NOT hidden) AS subset FROM vn'); + state $sample ||= 100*min 1, (100 / $stats->{subset}) * ($stats->{total} / $stats->{subset}); + + my $filt = auth->pref('filter_vn') && eval { filter_parse v => auth->pref('filter_vn') }; + my $vn = tuwf->dbVali(' + SELECT id + FROM vn v', $filt ? '' : ('TABLESAMPLE SYSTEM (', \$sample, ')'), ' + WHERE NOT hidden AND', filter_vn_query($filt||{}), ' + ORDER BY random() LIMIT 1' + ); + return tuwf->resNotFound if !$vn; + tuwf->resRedirect("/v$vn", 'temp'); +}; + +1; diff --git a/lib/VNWeb/VN/Page.pm b/lib/VNWeb/VN/Page.pm index ed4f6e75..a2d0bb4a 100644 --- a/lib/VNWeb/VN/Page.pm +++ b/lib/VNWeb/VN/Page.pm @@ -495,7 +495,7 @@ sub staff_ { @$c = sort { $a->[1] <=> $b->[1] } @$c; } - div_ class => 'mainbox', 'data-mainbox-summarize' => 200, sub { + div_ class => 'mainbox', id => 'staff', 'data-mainbox-summarize' => 200, sub { h1_ 'Staff'; div_ class => sprintf('vnstaff vnstaff-%d', scalar @$_), sub { ul_ sub { @@ -622,7 +622,7 @@ sub stats_ { } if $v->{c_votecount}; } - div_ class => 'mainbox', sub { + div_ class => 'mainbox', id => 'stats', sub { h1_ 'User stats'; if(!@$stats) { p_ 'Nobody has voted on this visual novel yet...'; |