Refactor logic: move duplicate logic into one central location.

        Patch by kareila. 

diffs (103 lines):

diff -r 9089b813eac1 -r cc1671fdf70e cgi-bin/DW/Controller/Search/Interests.pm
--- a/cgi-bin/DW/Controller/Search/Interests.pm Thu Dec 30 19:25:50 2010 +0800
+++ b/cgi-bin/DW/Controller/Search/Interests.pm Thu Dec 30 19:54:55 2010 +0800
@@ -296,7 +296,7 @@ sub interest_handler {
             my $us = $int_query->( "comminterests" );
             my $updated = LJ::get_timeupdate_multi( keys %$us );
             my @cl = sort { $updated->{$b->id} <=> $updated->{$a->id} || 
$a->user cmp $b->user }
-                     grep { $_ && $_->is_visible && $should_show->( $_ ) } 
values %$us;
+                     grep { $_ && $should_show->( $_ ) } values %$us;
             $rv->{int_comms} = { count => scalar @cl, data => [] };
             foreach ( @cl ) {
                 my $updated = $updated->{$_->id}
@@ -314,9 +314,7 @@ sub interest_handler {
         # user results
         my $us = $int_query->( "userinterests" );
         my @ul = grep { $_
-                        && $_->is_visible                # visible users
                         && ! $_->is_community            # not communities
-                        && ( ! $_->age || $_->age > 13 ) # are over 13
                         && $should_show->( $_ )          # and should show to 
the remote user
                       } values %$us;
         my $navbar;
diff -r 9089b813eac1 -r cc1671fdf70e cgi-bin/LJ/Directory/Results.pm
--- a/cgi-bin/LJ/Directory/Results.pm   Thu Dec 30 19:25:50 2010 +0800
+++ b/cgi-bin/LJ/Directory/Results.pm   Thu Dec 30 19:54:55 2010 +0800
@@ -54,22 +54,14 @@ sub users {
 sub users {
     my $self = shift;
     my $us = LJ::load_userids($self->userids);
+    my $remote = LJ::get_remote();
 
     # gotta do this to preserve the ordering we got
     # (userids sorted in order of last update time)
-    my @users = map { $us->{$_} } $self->userids;
+    my @users = grep { $_ } map { $us->{$_} } $self->userids;
 
-    # only visible users
-    @users = grep { $_ && $_->is_visible } @users;
-
-    # only users over the age of 13
-    @users = grep { !$_->is_person || !$_->age || $_->age > 13 } @users;
-
-    # and only users who the remote user should see
-    my $remote = LJ::get_remote();
-    @users = grep { $_->should_show_in_search_results( for => $remote ) } 
@users;
-
-    return @users;
+    # show only users who the remote user should see
+    return grep { $_->should_show_in_search_results( for => $remote ) } @users;
 }
 
 sub as_string {
diff -r 9089b813eac1 -r cc1671fdf70e cgi-bin/LJ/User.pm
--- a/cgi-bin/LJ/User.pm        Thu Dec 30 19:25:50 2010 +0800
+++ b/cgi-bin/LJ/User.pm        Thu Dec 30 19:54:55 2010 +0800
@@ -4407,25 +4407,34 @@ sub safe_search {
 
 # determine if the user in "for_u" should see $u in a search result
 sub should_show_in_search_results {
-    my $u = shift;
-    my %opts = @_;
-
+    my ( $u, %opts ) = @_;
+
+    # check basic user attributes first
+    return 0 unless $u->is_visible;
+    return 0 if $u->is_person && $u->age && $u->age < 14;
+
+    # now check adult content / safe search
     return 1 unless LJ::is_enabled( 'adult_content' ) && LJ::is_enabled( 
'safe_search' );
 
     my $adult_content = $u->adult_content_calculated;
-
     my $for_u = $opts{for};
-    unless (LJ::isu($for_u)) {
-        return $adult_content eq "none" ? 1 : 0;
-    }
+
+    # only show accounts with no adult content to logged out users
+    return $adult_content eq "none" ? 1 : 0
+        unless LJ::isu( $for_u );
 
     my $safe_search = $for_u->safe_search;
-    return 1 if $safe_search == 0;
-
-    my $adult_content_flag_level = $LJ::CONTENT_FLAGS{$adult_content} ? 
$LJ::CONTENT_FLAGS{$adult_content}->{safe_search_level} : 0;
-
-    return 0 if $adult_content_flag_level && ($safe_search >= 
$adult_content_flag_level);
-    return 1;
+    return 1 if $safe_search == 0;  # user wants to see everyone
+
+    # calculate the safe_search level for this account
+    my $adult_content_flag = $LJ::CONTENT_FLAGS{$adult_content};
+    my $adult_content_flag_level = $adult_content_flag
+                                 ? $adult_content_flag->{safe_search_level}
+                                 : 0;
+
+    # if the level is set, see if it exceeds the desired safe_search level
+    return 1 unless $adult_content_flag_level;
+    return ( $safe_search < $adult_content_flag_level ) ? 1 : 0;
 }
 
 
_______________________________________________
dw-changelog mailing list
[email protected]
http://lists.dwscoalition.org/mailman/listinfo/dw-changelog

Reply via email to