fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-11-23 05:28 am

[dw-free] Code to add a user to default filters automatically is duplicated

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

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

Move into a function called as $u->add_to_default_filters( $targetu ).

Patch by [personal profile] fu.

Files modified:
  • cgi-bin/DW/User/ContentFilters.pm
  • cgi-bin/LJ/Community.pm
  • htdocs/tools/endpoints/changerelation.bml
  • t/content-filters.t
--------------------------------------------------------------------------------
diff -r a03c68913f7f -r 1562746f926c cgi-bin/DW/User/ContentFilters.pm
--- a/cgi-bin/DW/User/ContentFilters.pm	Tue Nov 22 21:15:03 2011 +0800
+++ b/cgi-bin/DW/User/ContentFilters.pm	Wed Nov 23 13:30:37 2011 +0800
@@ -157,5 +157,22 @@
 }
 *LJ::User::delete_content_filter = \&delete_content_filter;
 
+sub add_to_default_filters {
+    my ( $u, $targetu ) = @_;
+
+    # assume things are okay at first
+    # one mis-add means failure
+    # (but we're still okay if no adds were done)
+    my $ok = 1;
+    foreach my $filter( $u->content_filters ) {
+        next unless $filter->is_default();
+        next if $filter->contains_userid( $targetu->userid );
+
+        $ok = $filter->add_row( userid => $targetu->userid ) && $ok;
+    }
+
+    return $ok;
+}
+*LJ::User::add_to_default_filters = \&add_to_default_filters;
 
 1;
diff -r a03c68913f7f -r 1562746f926c cgi-bin/LJ/Community.pm
--- a/cgi-bin/LJ/Community.pm	Tue Nov 22 21:15:03 2011 +0800
+++ b/cgi-bin/LJ/Community.pm	Wed Nov 23 13:30:37 2011 +0800
@@ -121,15 +121,7 @@
         if $args->{member};
 
     # we watch the community; now automatically add to default view, as most useful behavior
-    if ( $joined ) {
-        my @content_filters = $u->content_filters;
-        foreach my $filter ( @content_filters ) {
-            next unless $filter->is_default();
-            next if $filter->contains_userid( $cu->userid );
-
-            $filter->add_row( userid => $cu->userid );
-        }
-    }
+    $u->add_to_default_filters( $cu ) if $joined;
 
     # now grant necessary abilities
     my %edgelist = (
diff -r a03c68913f7f -r 1562746f926c htdocs/tools/endpoints/changerelation.bml
--- a/htdocs/tools/endpoints/changerelation.bml	Tue Nov 22 21:15:03 2011 +0800
+++ b/htdocs/tools/endpoints/changerelation.bml	Wed Nov 23 13:30:37 2011 +0800
@@ -59,15 +59,7 @@
 
         $success = $remote->add_edge( $targetu, watch => {} );
 
-        if ( $success ) {
-            my @content_filters = $remote->content_filters;
-            foreach my $filter ( @content_filters ) {
-                next unless $filter->is_default();
-                next if $filter->contains_userid( $targetu->userid );
-    
-                $success = $filter->add_row( userid => $targetu->userid ) && $success;
-            }
-        }
+        $success &&= $remote->add_to_default_filters( $targetu );
     } elsif ( $action eq 'removeTrust' ) {
         $success = $remote->remove_edge( $targetu, trust => {} );
     } elsif ( $action eq 'removeWatch' ) {
diff -r a03c68913f7f -r 1562746f926c t/content-filters.t
--- a/t/content-filters.t	Tue Nov 22 21:15:03 2011 +0800
+++ b/t/content-filters.t	Wed Nov 23 13:30:37 2011 +0800
@@ -4,9 +4,11 @@
 use Test::More;
 use lib "$ENV{LJHOME}/cgi-bin";
 require 'ljlib.pl';
-use LJ::Test qw (temp_user);
+use LJ::Test qw ( temp_user temp_comm );
 
-plan tests => 12;
+use LJ::Community;
+
+plan tests => 14;
 
 my $u1 = temp_user();
 my $u2 = temp_user();
@@ -72,4 +74,28 @@
 $fid = $u1->delete_content_filter( name => 'foob' );
 ok( $fid > 0, 'delete filter' );
 
+
 ################################################################################
+note( "in default filter after accepting a community invite" );
+{
+    my $admin_u = temp_user();
+    my $comm_u = temp_comm();
+    my $invite_u = temp_user();
+
+    LJ::set_rel( $comm_u, $admin_u, 'A' );
+
+    LJ::start_request();
+
+    $invite_u->create_content_filter( name => 'default' );
+
+    my $filter;
+    $filter = $invite_u->content_filters( name => 'default' );
+
+    $invite_u->send_comm_invite( $comm_u, $admin_u, [qw ( member )] );
+    ok( ! $filter->contains_userid( $comm_u->userid ) );
+
+    $invite_u->accept_comm_invite( $comm_u );
+    ok( $filter->contains_userid( $comm_u->userid ) );
+}
+
+################################################################################
--------------------------------------------------------------------------------