diff options
author | Yorhel <git@yorhel.nl> | 2020-05-11 13:04:45 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2020-05-11 13:04:45 +0200 |
commit | 3c5907b376019b2790813c540ca062a8dff35fdc (patch) | |
tree | 3331eb1169102063a2df10854a21201c7132fbee /lib | |
parent | b4c0012ff0f2a6c4845879b4a752ff9c399ebb68 (diff) |
imgflag: Add sexual/violence user preferences + show/hide character images
Diffstat (limited to 'lib')
-rw-r--r-- | lib/VNWeb/Auth.pm | 2 | ||||
-rw-r--r-- | lib/VNWeb/Chars/Page.pm | 55 | ||||
-rw-r--r-- | lib/VNWeb/DB.pm | 21 | ||||
-rw-r--r-- | lib/VNWeb/User/Edit.pm | 6 |
4 files changed, 72 insertions, 12 deletions
diff --git a/lib/VNWeb/Auth.pm b/lib/VNWeb/Auth.pm index 5a7f4d46..7a4fcbc4 100644 --- a/lib/VNWeb/Auth.pm +++ b/lib/VNWeb/Auth.pm @@ -267,7 +267,7 @@ sub csrfcheck { my @pref_columns = qw/ email_confirmed skin customcss filter_vn filter_release show_nsfw notify_dbedit notify_announce vn_list_own vn_list_wish tags_all tags_cont tags_ero tags_tech spoilers traits_sexual - nodistract_can nodistract_noads nodistract_nofancy + max_sexual max_violence nodistract_can nodistract_noads nodistract_nofancy /; # Returns a user preference column for the current user. Lazily loads all diff --git a/lib/VNWeb/Chars/Page.pm b/lib/VNWeb/Chars/Page.pm index b1d16fc4..5251d95f 100644 --- a/lib/VNWeb/Chars/Page.pm +++ b/lib/VNWeb/Chars/Page.pm @@ -15,9 +15,15 @@ sub enrich_seiyuu { } +sub enrich_image { + enrich_obj image => id => 'SELECT id, width, height, c_votecount AS votecount, c_sexual_avg AS sexual_avg, c_violence_avg AS violence_avg FROM images WHERE id IN', @_; +} + + sub enrich_item { my($c) = @_; + enrich_image $c; enrich_merge vid => 'SELECT id AS vid, title, original FROM vn WHERE id IN', $c->{vns}; enrich_merge rid => 'SELECT id AS rid, title AS rtitle, original AS roriginal FROM releases WHERE id IN', grep $_->{rid}, $c->{vns}->@*; enrich_merge tid => @@ -58,10 +64,52 @@ sub fetch_chars { }, $l; enrich_seiyuu $vid, $l; + enrich_image $l; $l } +# This and enrich_image() can prolly be used for VN cover images as well. +sub image_ { + my($c) = @_; + my $img = $c->{image}; + return p_ 'No image' if !$img; + + # XXX: no clue why I chose these thresholds. + my $sex = $img->{sexual_avg} > 1.3 ? 2 : $img->{sexual_avg} > 0.4 ? 1 : 0 if $img->{votecount}; + my $vio = $img->{violence_avg} > 1.3 ? 2 : $img->{violence_avg} > 0.4 ? 1 : 0 if $img->{votecount}; + my $sexd = ['Safe', 'Suggestive', 'Explicit']->[$sex] if $img->{votecount}; + my $viod = ['Tame', 'Violent', 'Brutal' ]->[$vio] if $img->{votecount}; + my $sexh = $sex > auth->pref('max_sexual')||0 if $img->{votecount}; + my $vioh = $vio > auth->pref('max_violence')||0 if $img->{votecount}; + my $hide_on_click = $sex || $vio || !$img->{votecount}; + + label_ class => 'imghover', style => "width: $img->{width}px; height: $img->{height}px", sub { + input_ type => 'checkbox', class => 'visuallyhidden', !$img->{votecount} || $sexh || $vioh ? () : (checked => 'checked') if $hide_on_click; + div_ class => 'imghover--visible', sub { + img_ src => tuwf->imgurl($img->{id}), alt => $c->{name}; + a_ href => "/img/$img->{id}?view=".viewset(show_nsfw=>1), + $img->{votecount} ? sprintf '%s / %s (%d)', $sexd, $viod, $img->{votecount} : 'Not flagged'; + }; + div_ class => 'imghover--warning', sub { + if($img->{votecount}) { + txt_ 'This image has been flagged as:'; + br_; br_; + txt_ 'Sexual: '; $sexh ? b_ class => 'standout', $sexd : txt_ $sexd; + br_; + txt_ 'Violence '; $vioh ? b_ class => 'standout', $viod : txt_ $viod; + } else { + txt_ 'This image has not yet been flagged'; + } + br_; br_; + span_ class => 'fake_link', 'Show me anyway'; + br_; br_; + b_ class => 'grayedout', 'This warning can be disabled in your account'; + } if $hide_on_click; + } +} + + sub _rev_ { my($c) = @_; revision_ c => $c, \&enrich_item, @@ -85,7 +133,7 @@ sub _rev_ { a_ href => "/c$c->{id}", title => $c->{name}, "c$c->{id}" } ], [ main_spoil => 'Spoiler', fmt => sub { txt_ fmtspoil $_ } ], - [ image => 'Image', empty => 0, fmt => sub { img_ src => tuwf->imgurl($_) } ], + [ image => 'Image', empty => 0, fmt => \&image_ ], [ vns => 'Visual novels', fmt => sub { a_ href => "/v$_->{vid}", title => $_->{original}||$_->{title}, "v$_->{vid}"; if($_->{rid}) { @@ -113,10 +161,7 @@ sub chartable_ { my $view = viewget; div_ mkclass(chardetails => 1, charsep => $sep), sub { - div_ class => 'charimg', sub { - p_ 'No image uploaded yet' if !$c->{image}; - img_ src => tuwf->imgurl($c->{image}), alt => $c->{name} if $c->{image}; - }; + div_ class => 'charimg', sub { image_ $c }; table_ class => 'stripe', sub { thead_ sub { tr_ sub { td_ colspan => 2, sub { $link diff --git a/lib/VNWeb/DB.pm b/lib/VNWeb/DB.pm index b1e0a5fd..b597a21c 100644 --- a/lib/VNWeb/DB.pm +++ b/lib/VNWeb/DB.pm @@ -11,7 +11,7 @@ use VNDB::Schema; our @EXPORT = qw/ sql sql_identifier sql_join sql_comma sql_and sql_or sql_array sql_func sql_fromhex sql_tohex sql_fromtime sql_totime sql_user - enrich enrich_merge enrich_flatten + enrich enrich_merge enrich_flatten enrich_obj db_entry db_edit /; @@ -119,18 +119,21 @@ sub sql_user { # # enrich $name, $key, $merge_col, $sql, @objects; # -# Add a $name field each item in @objects, +# Add a $name field to each item in @objects, # Its value is a (possibly empty) array of hashes with data from $sql, # # enrich_flatten $name, $key, $merge_col, $sql, @objects; # -# Add a $name field each item in @objects, +# Add a $name field to each item in @objects, # Its value is a (possibly empty) array of values from a single column from $sql, # # enrich_merge $key, $sql, @objects; # # Merge all columns returned by $sql into @objects; # +# enrich_obj $key, $merge_col, $sql, @objects; +# +# Replace all non-undef $key fields in @objects with an object returned by $sql. # # Arguments: # @@ -157,7 +160,7 @@ sub _enrich { @array = map +(ref $_ eq 'ARRAY' ? @$_ : $_), @array; # Create a list of unique identifiers to fetch, do nothing if there's nothing to fetch - my %ids = map +($_->{$key},1), @array; + my %ids = map defined($_->{$key}) ? ($_->{$key},1) : (), @array; return if !keys %ids; # Fetch the data @@ -201,6 +204,16 @@ sub enrich_flatten { } +sub enrich_obj { + my($key, $merge_col, $sql, @array) = @_; + _enrich sub { + my($data, $array) = @_; + my %ids = map +($_->{$merge_col}, $_), @$data; + $_->{$key} = defined $_->{$key} ? $ids{ $_->{$key} } : undef for @$array; + }, $key, $sql, @array; +} + + # Database entry API: Intended to provide a low-level read/write interface for # versioned database entires. The same data structure is used for reading and diff --git a/lib/VNWeb/User/Edit.pm b/lib/VNWeb/User/Edit.pm index 8fb2f7ff..329018c2 100644 --- a/lib/VNWeb/User/Edit.pm +++ b/lib/VNWeb/User/Edit.pm @@ -33,6 +33,8 @@ my $FORM = { prefs => { required => 0, type => 'hash', keys => { email => { email => 1 }, show_nsfw => { anybool => 1 }, + max_sexual => { uint => 1, range => [ 0, 2 ] }, + max_violence => { uint => 1, range => [ 0, 2 ] }, traits_sexual => { anybool => 1 }, tags_all => { anybool => 1 }, tags_cont => { anybool => 1 }, @@ -79,7 +81,7 @@ TUWF::get qr{/$RE{uid}/edit}, sub { $u->{prefs} = $u->{id} == auth->uid || auth->permUsermod ? tuwf->dbRowi( - 'SELECT show_nsfw, traits_sexual, tags_all, tags_cont, tags_ero, tags_tech, spoilers, skin, customcss + 'SELECT show_nsfw, max_sexual, max_violence, traits_sexual, tags_all, tags_cont, tags_ero, tags_tech, spoilers, skin, customcss , nodistract_noads, nodistract_nofancy, support_enabled, uniname, pubskin_enabled FROM users WHERE id =', \$u->{id} ) : undef; @@ -116,7 +118,7 @@ elm_api UserEdit => $FORM_OUT, $FORM_IN, sub { return elm_Taken if $p->{uniname} && tuwf->dbVali('SELECT 1 FROM users WHERE id <>', \$data->{id}, 'AND username =', \lc($p->{uniname})); $set{$_} = $p->{$_} for qw/ - show_nsfw traits_sexual tags_all tags_cont tags_ero tags_tech spoilers skin customcss + show_nsfw max_sexual max_violence traits_sexual tags_all tags_cont tags_ero tags_tech spoilers skin customcss nodistract_noads nodistract_nofancy support_enabled uniname pubskin_enabled /; } |