diff options
author | Yorhel <git@yorhel.nl> | 2020-03-17 16:55:32 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2020-03-17 16:55:32 +0100 |
commit | c83c083dbf7c38c45cb7b2f11d05d1824f3622e7 (patch) | |
tree | aaa77fca7e64ad9fc06f0f4b32ff5531720d5bfd | |
parent | db930ad462311de0de63109a1af419c5786bd45c (diff) |
imgflag: Make 30 recent image votes available in the UI
-rw-r--r-- | elm/ImageFlagging.elm | 10 | ||||
-rw-r--r-- | lib/VNWeb/Elm.pm | 2 | ||||
-rw-r--r-- | lib/VNWeb/Misc/ImageFlagging.pm | 52 |
3 files changed, 43 insertions, 21 deletions
diff --git a/elm/ImageFlagging.elm b/elm/ImageFlagging.elm index eac85256..2a34b796 100644 --- a/elm/ImageFlagging.elm +++ b/elm/ImageFlagging.elm @@ -19,7 +19,7 @@ import Gen.Images as GI import Gen.ImageVote as GIV -main : Program () Model Msg +main : Program GI.Recv Model Msg main = Browser.element { init = \e -> (init e, Cmd.none) , view = view @@ -43,11 +43,11 @@ type alias Model = , saveState : Api.State } -init : () -> Model -init _ = +init : GI.Recv -> Model +init d = { warn = True - , images = Array.empty - , index = 0 + , images = Array.fromList d.history + , index = List.length d.history , desc = (Nothing, Nothing) , changes = Dict.empty , saved = False diff --git a/lib/VNWeb/Elm.pm b/lib/VNWeb/Elm.pm index 04f03643..f0712a72 100644 --- a/lib/VNWeb/Elm.pm +++ b/lib/VNWeb/Elm.pm @@ -31,7 +31,7 @@ our @EXPORT = qw/ # elm_Changed $id, $revision; # # These API responses are available in Elm in the `Gen.Api.Response` union type. -my %apis = ( +our %apis = ( Unauth => [], # Not authorized Unchanged => [], # No changes Success => [], diff --git a/lib/VNWeb/Misc/ImageFlagging.pm b/lib/VNWeb/Misc/ImageFlagging.pm index 8a1dc489..7faf7b86 100644 --- a/lib/VNWeb/Misc/ImageFlagging.pm +++ b/lib/VNWeb/Misc/ImageFlagging.pm @@ -4,17 +4,30 @@ use VNWeb::Prelude; # TODO: /img/<imageid> endpoint to open the imageflagging UI for a particular image. -TUWF::get qr{/img/vote}, sub { - return tuwf->resDenied if !auth->permImgvote; - framework_ title => 'Image flagging', sub { - # TODO: Include recent votes - elm_ 'ImageFlagging'; - }; -}; + +sub enrich_image { + my($l) = @_; + enrich_merge id => sql(' + SELECT i.id, i.width, i.height, i.c_votecount AS votecount + , i.c_sexual_avg AS sexual_avg, i.c_sexual_stddev AS sexual_stddev + , i.c_violence_avg AS violence_avg, i.c_violence_stddev AS violence_stddev + , iv.sexual AS my_sexual, iv.violence AS my_violence + FROM images i + LEFT JOIN image_votes iv ON iv.id = i.id AND iv.uid =', \auth->uid, ' + WHERE i.id IN'), $l; + enrich_merge id => q{SELECT image AS id, 'v' AS entry_type, id AS entry_id, title AS entry_title FROM vn WHERE image IN}, grep $_->{id} =~ /cv/, @$l; + enrich_merge id => q{SELECT vs.scr AS id, 'v' AS entry_type, v.id AS entry_id, v.title AS entry_title FROM vn_screenshots vs JOIN vn v ON v.id = vs.id AND vs.scr IN}, grep $_->{id} =~ /sf/, @$l; + enrich_merge id => q{SELECT image AS id, 'c' AS entry_type, id AS entry_id, name AS entry_title FROM chars WHERE image IN}, grep $_->{id} =~ /ch/, @$l; + $_->{url} = tuwf->imgurl($_->{id}) for @$l; +} +my $SEND = form_compile any => { + history => $VNWeb::Elm::apis{ImageResult}[0] +}; + # Fetch a list of images for the user to vote on. -elm_api Images => undef, {}, sub { +elm_api Images => $SEND, {}, sub { return elm_Unauth if !auth->permImgvote; # TODO: Return nothing when the user has voted on >90% of the images? @@ -33,12 +46,12 @@ elm_api Images => undef, {}, sub { # - 80% of all images have c_weight > 0, so that leaves ~3.5k images # - To actually fetch 100 rows on average, the user should not have voted on more than ~97% of the images. # ^ But TABLESAMPLE SYSTEM isn't perfectly uniform, so we need some headroom for outliers. - # ^ Doing a CLUSTER on random value may also help. + # ^ Doing a regular CLUSTER on a random value may help with getting a more uniform sampling. # # This probably won't give (many?) rows on the dev database; A nicer solution # would calculate an appropriate sampling percentage based on actual data. my $l = tuwf->dbAlli(' - SELECT id, width, height, c_votecount AS votecount, c_sexual_avg AS sexual_avg, c_sexual_stddev AS sexual_stddev, c_violence_avg AS violence_avg, c_violence_stddev AS violence_stddev + SELECT id FROM images i TABLESAMPLE SYSTEM (1+1) WHERE c_weight > 0 AND NOT EXISTS(SELECT 1 FROM image_votes iv WHERE iv.id = i.id AND iv.uid =', \auth->uid, ') @@ -46,11 +59,7 @@ elm_api Images => undef, {}, sub { LIMIT 100' ); warn sprintf 'Weighted random image sampling query returned %d < 100 rows for u%d', scalar @$l, auth->uid if @$l < 100; - enrich_merge id => q{SELECT image AS id, 'v' AS entry_type, id AS entry_id, title AS entry_title FROM vn WHERE image IN}, grep $_->{id} =~ /cv/, @$l; - enrich_merge id => q{SELECT vs.scr AS id, 'v' AS entry_type, v.id AS entry_id, v.title AS entry_title FROM vn_screenshots vs JOIN vn v ON v.id = vs.id AND vs.scr IN}, grep $_->{id} =~ /sf/, @$l; - enrich_merge id => q{SELECT image AS id, 'c' AS entry_type, id AS entry_id, name AS entry_title FROM chars WHERE image IN}, grep $_->{id} =~ /ch/, @$l; - $_->{url} = tuwf->imgurl($_->{id}) for @$l; - $_->{my_sexual} = $_->{my_violence} = undef for @$l; + enrich_image $l; elm_ImageResult $l; }; @@ -71,4 +80,17 @@ elm_api ImageVote => undef, { elm_Success }; + + +TUWF::get qr{/img/vote}, sub { + return tuwf->resDenied if !auth->permImgvote; + + my $recent = [ reverse tuwf->dbAlli('SELECT id FROM image_votes WHERE uid =', \auth->uid, 'ORDER BY date DESC LIMIT', \30)->@* ]; + enrich_image $recent; + + framework_ title => 'Image flagging', sub { + elm_ 'ImageFlagging', $SEND, { history => $recent }; + }; +}; + 1; |