summaryrefslogtreecommitdiff
path: root/lib/VNWeb
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2019-10-04 18:45:15 +0200
committerYorhel <git@yorhel.nl>2019-10-04 18:45:16 +0200
commite94aef50e3f57a8c58656840f3a35b74906b86ab (patch)
tree13461f3946ef5dd7db3bee8e5490539adb7b3750 /lib/VNWeb
parent625e0b6e80fc4ab15b880217aac794a2720989bf (diff)
v2rw: Convert user pages
Pretty simple conversion, not much special. Did change a few minor display thingies to be more consistent.
Diffstat (limited to 'lib/VNWeb')
-rw-r--r--lib/VNWeb/Misc/History.pm14
-rw-r--r--lib/VNWeb/Prelude.pm6
-rw-r--r--lib/VNWeb/User/Page.pm152
3 files changed, 164 insertions, 8 deletions
diff --git a/lib/VNWeb/Misc/History.pm b/lib/VNWeb/Misc/History.pm
index 401a77aa..9d814644 100644
--- a/lib/VNWeb/Misc/History.pm
+++ b/lib/VNWeb/Misc/History.pm
@@ -4,7 +4,7 @@ use VNWeb::Prelude;
sub fetch {
- my($type, $id, $filt) = @_;
+ my($type, $id, $filt, $opt) = @_;
my $where = sql_and
!$type ? ()
@@ -28,7 +28,7 @@ sub fetch {
WHERE c_i.type = c.type AND c_i.itemid = c.itemid AND c_i.ihid
AND c_i.rev = (SELECT MAX(c_ii.rev) FROM changes c_ii WHERE c_ii.type = c.type AND c_ii.itemid = c.itemid))' : ();
- my($lst, $np) = tuwf->dbPagei({ page => $filt->{p}, results => 50 }, q{
+ my($lst, $np) = tuwf->dbPagei({ page => $filt->{p}, results => $opt->{results}||50 }, q{
SELECT c.id, c.type, c.itemid, c.comments, c.rev,}, sql_totime('c.added'), q{ AS added
, c.requester, u.username
FROM changes c
@@ -61,14 +61,16 @@ sub _filturl {
}
+# Also used by User::Page.
+# %opt: nopage => 1/0, results => $num
sub tablebox_ {
- my($type, $id, $filt) = @_;
+ my($type, $id, $filt, %opt) = @_;
- my($lst, $np) = fetch $type, $id, $filt;
+ my($lst, $np) = fetch $type, $id, $filt, \%opt;
my sub url { _filturl {%$filt, p => $_} }
- paginate_ \&url, $filt->{p}, $np, 't';
+ paginate_ \&url, $filt->{p}, $np, 't' unless $opt{nopage};
div_ class => 'mainbox browse history', sub {
table_ class => 'stripe', sub {
thead_ sub { tr_ sub {
@@ -93,7 +95,7 @@ sub tablebox_ {
} for @$lst;
};
};
- paginate_ \&url, $filt->{p}, $np, 'b';
+ paginate_ \&url, $filt->{p}, $np, 'b' unless $opt{nopage};
}
diff --git a/lib/VNWeb/Prelude.pm b/lib/VNWeb/Prelude.pm
index 506d0592..cfcd0031 100644
--- a/lib/VNWeb/Prelude.pm
+++ b/lib/VNWeb/Prelude.pm
@@ -7,12 +7,13 @@
# use TUWF ':html5_', 'mkclass';
# use Exporter 'import';
# use Time::HiRes 'time';
+# use List::Util 'min', 'max', 'sum';
#
# use VNDBUtil;
# use VNDB::BBCode;
# use VNDB::Types;
# use VNDB::Config;
-# use VNDB::Func 'fmtdate';
+# use VNDB::Func 'fmtdate', 'fmtvote';
# use VNWeb::Auth;
# use VNWeb::HTML;
# use VNWeb::DB;
@@ -48,12 +49,13 @@ sub import {
use TUWF ':html5_', 'mkclass';
use Exporter 'import';
use Time::HiRes 'time';
+ use List::Util 'min', 'max', 'sum';
use VNDBUtil;
use VNDB::BBCode;
use VNDB::Types;
use VNDB::Config;
- use VNDB::Func 'fmtdate';
+ use VNDB::Func 'fmtdate', 'fmtvote';
use VNWeb::Auth;
use VNWeb::HTML;
use VNWeb::DB;
diff --git a/lib/VNWeb/User/Page.pm b/lib/VNWeb/User/Page.pm
new file mode 100644
index 00000000..175ce3de
--- /dev/null
+++ b/lib/VNWeb/User/Page.pm
@@ -0,0 +1,152 @@
+package VNWeb::User::Page;
+
+use VNWeb::Prelude;
+use VNWeb::Misc::History;
+
+
+sub _info_table_ {
+ my($u, $vis) = @_;
+
+ tr_ sub {
+ td_ class => 'key', 'Username';
+ td_ sub {
+ txt_ ucfirst $u->{username};
+ txt_ ' ('; a_ href => "/u$u->{id}", "u$u->{id}";
+ txt_ ')';
+ debug_ $u;
+ };
+ };
+ tr_ sub {
+ td_ 'Registered';
+ td_ fmtdate $u->{registered};
+ };
+ tr_ sub {
+ td_ 'Edits';
+ td_ !$u->{c_changes} ? '-' : sub {
+ a_ href => "/u$u->{id}/hist", $u->{c_changes}
+ };
+ };
+ tr_ sub {
+ td_ 'Votes';
+ td_ !$vis ? 'hidden' : !$u->{c_votes} ? '-' : sub {
+ my $sum = sum map $_->{total}, $u->{votes}->@*;
+ txt_ sprintf '%d vote%s, %.2f average. ', $u->{c_votes}, $u->{c_votes} == 1 ? '' : 's', $sum/$u->{c_votes}/10;
+ a_ href => "/u$u->{id}/votes", 'Browse votes »';
+ }
+ };
+ tr_ sub {
+ my $vns = tuwf->dbVali('SELECT COUNT(*) FROM vnlists WHERE uid =', \$u->{id})||0;
+ my $rel = tuwf->dbVali('SELECT COUNT(*) FROM rlists WHERE uid =', \$u->{id})||0;
+ td_ 'List stats';
+ td_ !$vis ? 'hidden' : !$vns && !$rel ? '-' : sub {
+ txt_ sprintf '%d release%s of %d visual novel%s. ',
+ $rel, $rel == 1 ? '' : 's',
+ $vns, $vns == 1 ? '' : 's';
+ a_ href => "/u$u->{id}/list", 'Browse list »';
+ };
+ };
+ tr_ sub {
+ my $stats = tuwf->dbRowi('SELECT COUNT(DISTINCT tag) AS tags, COUNT(DISTINCT vid) AS vns FROM tags_vn WHERE uid =', \$u->{id});
+ td_ 'Tags';
+ td_ !$u->{c_tags} ? '-' : !$stats->{tags} ? '-' : sub {
+ txt_ sprintf '%d vote%s on %d distinct tag%s and %d visual novel%s. ',
+ $u->{c_tags}, $u->{c_tags} == 1 ? '' : 's',
+ $stats->{tags}, $stats->{tags} == 1 ? '' : 's',
+ $stats->{vns}, $stats->{vns} == 1 ? '' : 's';
+ a_ href => "/g/links?u=$u->{id}", 'Browse tags »';
+ };
+ };
+ tr_ sub {
+ my $stats = tuwf->dbRowi('SELECT COUNT(*) AS posts, COUNT(*) FILTER (WHERE num = 1) AS threads FROM threads_posts WHERE uid =', \$u->{id});
+ td_ 'Forum stats';
+ td_ !$stats->{posts} ? '-' : sub {
+ txt_ sprintf '%d post%s, %d new thread%s. ',
+ $stats->{posts}, $stats->{posts} == 1 ? '' : 's',
+ $stats->{threads}, $stats->{threads} == 1 ? '' : 's';
+ a_ href => "/u$u->{id}/posts", 'Browse posts »';
+ };
+ };
+}
+
+
+sub _votestats_ {
+ my($u) = @_;
+
+ my $sum = sum map $_->{total}, $u->{votes}->@*;
+ my $max = max map $_->{votes}, $u->{votes}->@*;
+
+ table_ class => 'votegraph', sub {
+ thead_ sub { tr_ sub { td_ colspan => 2, 'Vote stats' } };
+ tfoot_ sub { tr_ sub { td_ colspan => 2, sprintf '%d vote%s total, average %.2f', $u->{c_votes}, $u->{c_votes} == 1 ? '' : 's', $sum/$u->{c_votes}/10 } };
+ tr_ sub {
+ my $num = $_;
+ my $votes = [grep $num == $_->{idx}, $u->{votes}->@*]->[0]{votes} || 0;
+ td_ class => 'number', $num;
+ td_ class => 'graph', sub {
+ div_ style => sprintf('width: %dpx', ($votes||0)/$max*250), ' ';
+ txt_ $votes||0;
+ };
+ } for (reverse 1..10);
+ };
+
+ my $recent = tuwf->dbAlli(q{
+ SELECT vn.id, vn.title, vn.original, v.vote,}, sql_totime('v.date'), q{AS date
+ FROM votes v JOIN vn ON vn.id = v.vid WHERE v.uid =}, \$u->{id}, 'ORDER BY v.date DESC LIMIT', \8
+ );
+
+ table_ class => 'recentvotes stripe', sub {
+ thead_ sub { tr_ sub { td_ colspan => 3, sub {
+ txt_ 'Recent votes';
+ b_ sub { txt_ ' ('; a_ href => "/u$u->{id}/votes", 'show all'; txt_ ')' };
+ } } };
+ tr_ sub {
+ my $v = $_;
+ td_ sub { a_ href => "/v$v->{id}", title => $v->{original}||$v->{title}, shorten $v->{title}, 30 };
+ td_ fmtvote $v->{vote};
+ td_ fmtdate $v->{date};
+ } for @$recent;
+ };
+
+ clearfloat_;
+}
+
+
+TUWF::get qr{/$RE{uid}}, sub {
+ my $u = tuwf->dbRowi(q{
+ SELECT id, username, hide_list, c_changes, c_votes, c_tags
+ ,}, sql_totime('registered'), q{ AS registered
+ FROM users
+ WHERE id =}, \tuwf->capture('id')
+ );
+ return tuwf->resNotFound if !$u->{id};
+
+ my $vis = !$u->{hide_list} || (auth && auth->uid == $u->{id}) || auth->permUsermod;
+
+ $u->{votes} = $vis && $u->{c_votes} && tuwf->dbAlli(q{
+ SELECT (vote::numeric/10)::int AS idx, COUNT(vote) as votes, SUM(vote) AS total
+ FROM votes
+ WHERE uid =}, \$u->{id}, q{
+ GROUP BY (vote::numeric/10)::int
+ });
+
+ my $title = "$u->{username}'s profile";
+ framework_ title => $title, index => 0, type => 'u', dbobj => $u,
+ sub {
+ div_ class => 'mainbox userpage', sub {
+ h1_ $title;
+ table_ class => 'stripe', sub { _info_table_ $u, $vis };
+ };
+
+ div_ class => 'mainbox', sub {
+ h1_ 'Vote statistics';
+ div_ class => 'votestats', sub { _votestats_ $u };
+ } if $vis && $u->{c_votes};
+
+ if($u->{c_changes}) {
+ h1_ class => 'boxtitle', sub { a_ href => "/u$u->{id}/hist", 'Recent changes' };
+ VNWeb::Misc::History::tablebox_ u => $u->{id}, {p=>1}, nopage => 1, results => 10;
+ }
+ };
+};
+
+1;