fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2010-07-29 09:27 am

[dw-free] Mood of service toy json should not include nulls

[commit: http://hg.dwscoalition.org/dw-free/rev/d74055a213da]

http://bugs.dwscoalition.org/show_bug.cgi?id=2918

Better fix, one that doesn't remove elements from the array, like splice
does.

Patch by [personal profile] exor674.

Files modified:
  • cgi-bin/DW/Controller/Latest/Mood.pm
--------------------------------------------------------------------------------
diff -r 2140dc0e6de4 -r d74055a213da cgi-bin/DW/Controller/Latest/Mood.pm
--- a/cgi-bin/DW/Controller/Latest/Mood.pm	Thu Jul 29 02:21:27 2010 -0700
+++ b/cgi-bin/DW/Controller/Latest/Mood.pm	Thu Jul 29 02:27:30 2010 -0700
@@ -45,11 +45,12 @@ sub mood_handler {
     my $moods = LJ::MemCache::get( "latest_moods" ) || [];
     my $out = {};
     my $num_top = 5;
+    my $count = scalar @$moods;
 
-    if ( scalar @$moods ) {
+    if ( $count ) {
         my %counts;
         my $score = 0;
-        my $count = scalar @$moods;
+        my $to_show = $count > $num_top ? $num_top : $count;
 
         my $metadata = DW::Mood->get_moods;
 
@@ -59,7 +60,8 @@ sub mood_handler {
         }
 
         my @names = sort { $counts{$b} <=> $counts{$a} || $a cmp $b } keys %counts;
-        my %top_counts = map { ($_,$counts{$_}) } splice( @names, 0, $num_top );
+        my @highest = @names[0..( $to_show - 1)];
+        my %top_counts = map { ($_,$counts{$_}) } @highest;
         my @top_mood;
         foreach my $mood ( @names ) {
             if ( $counts{$mood} == $counts{$names[0]} ) {
@@ -72,7 +74,7 @@ sub mood_handler {
         $out->{counts} = \%top_counts;
         $out->{score} = int($score / $count);
         $out->{score} = $r->get_args->{score} if defined $r->get_args->{score};
-        $out->{highest} = [ splice( @names, 0, $num_top ) ];
+        $out->{highest} = \@highest;
         $out->{top_mood} = \@top_mood;
     } else {
         $out->{no_data} = 1;
--------------------------------------------------------------------------------