pauamma: Cartooney crab wearing hot pink and acid green facemask holding drink with straw (Default)
Res facta quae tamen fingi potuit ([personal profile] pauamma) wrote in [site community profile] changelog2009-03-09 09:39 pm

[dw-free] http://bugs.dreamwidth.org/show_bug.cgi?id=465

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

http://bugs.dreamwidth.org/show_bug.cgi?id=465

Add DW::User::Edges::CommMembership->can_join method.

Patch by [personal profile] janinedog.

Files modified:
  • cgi-bin/DW/User/Edges/CommMembership.pm
  • cgi-bin/LJ/User.pm
  • cgi-bin/communitylib.pl
--------------------------------------------------------------------------------
diff -r bda589ff8b47 -r 478c4a171c5f cgi-bin/DW/User/Edges/CommMembership.pm
--- a/cgi-bin/DW/User/Edges/CommMembership.pm	Mon Mar 09 21:04:44 2009 +0000
+++ b/cgi-bin/DW/User/Edges/CommMembership.pm	Mon Mar 09 21:39:04 2009 +0000
@@ -41,10 +41,7 @@ sub _add_m_edge {
         or return;
 
     # error check adding an edge
-    return 0
-        unless $from_u->is_person &&
-               $to_u->is_community &&
-               ! LJ::is_banned( $from_u, $to_u );
+    return 0 unless $from_u->can_join( $to_u );
 
     # simply add the reluser edge
     LJ::set_rel( $to_u, $from_u, 'E' );
@@ -136,4 +133,44 @@ sub member_userids {
 *LJ::User::member_userids = \&member_userids;
 
 
+# returns 1/0 depending on if the source is allowed to add a member edge
+# to the target.  note: if you don't pass a target user, then we return
+# a generic 1/0 meaning "this account is allowed to have a member edge".
+sub can_join {
+    my ( $u, $tu ) = @_;
+    $u = LJ::want_user( $u ) or confess 'invalid user object';
+    $tu = LJ::want_user( $tu );
+
+    # if the user is a maintainer, skip every other check
+    return 1 if $tu && $u->can_manage( $tu );
+
+    # a user must be a personal account
+    return 0 unless $u->is_personal;
+
+    # the user must be visible
+    return 0 unless $u->is_visible;
+
+    if ( $tu ) {
+        # the target must be a community
+        return 0 unless $tu->is_community;
+
+        # the target must be visible
+        return 0 unless $tu->is_visible;
+
+        # the target must not have banned the user
+        return 0 if $tu->has_banned( $u );
+
+        # make sure the user isn't underage and trying to join an adult community
+        return 0 unless $u->can_join_adult_comm( comm => $tu );
+
+        # the community must be open membership
+        return 0 unless $tu->is_open_membership;
+    }
+
+    # okay, good to go!
+    return 1;
+}
+*LJ::User::can_join = \&can_join;
+
+
 1;
diff -r bda589ff8b47 -r 478c4a171c5f cgi-bin/LJ/User.pm
--- a/cgi-bin/LJ/User.pm	Mon Mar 09 21:04:44 2009 +0000
+++ b/cgi-bin/LJ/User.pm	Mon Mar 09 21:39:04 2009 +0000
@@ -1766,6 +1766,25 @@ sub clear_prop {
 }
 
 
+sub control_strip_display {
+    my $u = shift;
+
+    # return prop value if it exists and is valid
+    my $prop_val = $u->prop( 'control_strip_display' );
+    return 0 if $prop_val eq 'none';
+    return $prop_val if $prop_val =~ /^\d+$/;
+
+    # otherwise, return the default: all options checked
+    my $ret;
+    my @pageoptions = LJ::run_hook( 'page_control_strip_options' );
+    for ( my $i = 0; $i < scalar @pageoptions; $i++ ) {
+        $ret |= 1 << $i;
+    }
+
+    return $ret ? $ret : 0;
+}
+
+
 # returns the country specified by the user
 sub country {
     my $u = shift;
@@ -1897,6 +1916,18 @@ sub large_journal_icon {
 }
 
 
+sub opt_logcommentips {
+    my $u = shift;
+
+    # return prop value if it exists and is valid
+    my $prop_val = $u->prop( 'opt_logcommentips' );
+    return $prop_val if $prop_val =~ /^[NSA]$/;
+
+    # otherwise, return the default: log for all comments
+    return 'A';
+}
+
+
 sub opt_showcontact {
     my $u = shift;
 
@@ -1941,6 +1972,24 @@ sub opt_showonlinestatus {
     } else {
         return 'F';
     }
+}
+
+
+sub opt_whatemailshow {
+    my $u = shift;
+
+    my $user_email = $LJ::USER_EMAIL && $u->get_cap( 'useremail' ) ? 1 : 0;
+
+    # return prop value if it exists and is valid
+    my $prop_val = $u->prop( 'opt_whatemailshow' );
+    if ( $user_email ) {
+        return $prop_val if $prop_val =~ /^[ALBN]$/;
+    } else {
+        return $prop_val if $prop_val =~ /^[AN]$/;
+    }
+
+    # otherwise, return the default: no email shown
+    return 'N';
 }
 
 
@@ -2107,55 +2156,6 @@ sub _lazy_migrate_infoshow {
     $u->{allow_infoshow} = ' ';
 
     return 1;
-}
-
-
-sub control_strip_display {
-    my $u = shift;
-
-    # return prop value if it exists and is valid
-    my $prop_val = $u->prop( 'control_strip_display' );
-    return 0 if $prop_val eq 'none';
-    return $prop_val if $prop_val =~ /^\d+$/;
-
-    # otherwise, return the default: all options checked
-    my $ret;
-    my @pageoptions = LJ::run_hook( 'page_control_strip_options' );
-    for ( my $i = 0; $i < scalar @pageoptions; $i++ ) {
-        $ret |= 1 << $i;
-    }
-
-    return $ret ? $ret : 0;
-}
-
-
-sub opt_logcommentips {
-    my $u = shift;
-
-    # return prop value if it exists and is valid
-    my $prop_val = $u->prop( 'opt_logcommentips' );
-    return $prop_val if $prop_val =~ /^[NSA]$/;
-
-    # otherwise, return the default: log for all comments
-    return 'A';
-}
-
-
-sub opt_whatemailshow {
-    my $u = shift;
-
-    my $user_email = $LJ::USER_EMAIL && $u->get_cap( 'useremail' ) ? 1 : 0;
-
-    # return prop value if it exists and is valid
-    my $prop_val = $u->prop( 'opt_whatemailshow' );
-    if ( $user_email ) {
-        return $prop_val if $prop_val =~ /^[ALBN]$/;
-    } else {
-        return $prop_val if $prop_val =~ /^[AN]$/;
-    }
-
-    # otherwise, return the default: no email shown
-    return 'N';
 }
 
 
@@ -2965,12 +2965,44 @@ sub can_post_to {
 }
 
 
+sub is_closed_membership {
+    my $u = shift;
+
+    return $u->membership_level eq 'closed' ? 1 : 0;
+}
+
+
+sub is_moderated_membership {
+    my $u = shift;
+
+    return $u->membership_level eq 'moderated' ? 1 : 0;
+}
+
+
+sub is_open_membership {
+    my $u = shift;
+
+    return $u->membership_level eq 'open' ? 1 : 0;
+}
+
+
 # returns an array of maintainer userids
 sub maintainer_userids {
     my $u = shift;
 
     return () unless $u->is_community;
     return @{LJ::load_rel_user_cache( $u->id, 'A' )};
+}
+
+
+# returns the membership level of a community
+sub membership_level {
+    my $u = shift;
+
+    return undef unless $u->is_community;
+
+    my ( $membership_level, $post_level ) = LJ::get_comm_settings( $u );
+    return $membership_level || undef;
 }
 
 
diff -r bda589ff8b47 -r 478c4a171c5f cgi-bin/communitylib.pl
--- a/cgi-bin/communitylib.pl	Mon Mar 09 21:04:44 2009 +0000
+++ b/cgi-bin/communitylib.pl	Mon Mar 09 21:39:04 2009 +0000
@@ -295,9 +295,9 @@ sub join_community {
     return LJ::error('comm_not_found') unless $u && $cu;
     return LJ::error('comm_not_comm') unless $cu->{journaltype} eq 'C';
 
-    # try to join the community, if this returns 0 something happened
-    return undef
-        unless $u->add_edge( $cu, member => 1 );
+    # try to join the community, and return if it didn't work
+    $u->add_edge( $cu, member => 1 );
+    return undef unless $u->member_of( $cu );
 
     # add edges that effect this relationship... if the user sent a fourth
     # argument, use that as a bool.  else, load commrow and use the postlevel.
--------------------------------------------------------------------------------