Move functions out of the LJ namespace and into the LJ::User namespace. 
Some refactoring. Remove get_postto_list, in favor of $u->posting_access_list

        Patch by kareila. 

diffs (242 lines):

diff -r 45c7c3d47bed -r 3e1dc42af96f cgi-bin/LJ/User.pm
--- a/cgi-bin/LJ/User.pm        Wed Sep 08 17:06:41 2010 +0800
+++ b/cgi-bin/LJ/User.pm        Wed Sep 08 17:19:47 2010 +0800
@@ -3974,6 +3974,37 @@ sub can_post_to {
 }
 
 
+# Get an array of usernames a given user can authenticate as.
+# Valid keys for opts hashref:
+#     - type: filter by given journaltype (P or C)
+#     - cap:  filter by users who have given cap
+#     - showall: override hiding of non-visible/non-read-only journals
+sub get_authas_list {
+    my ( $u, $opts ) = @_;
+
+    # Two valid types, Personal or Community
+    $opts->{type} = undef unless $opts->{type} =~ m/^[PC]$/;
+
+    my $ids = LJ::load_rel_target( $u, 'A' );
+    return undef unless $ids;
+    my %users = %{ LJ::load_userids( @$ids ) };
+
+    return map { $_->user }
+           grep { ! $opts->{cap}  || $_->get_cap( $opts->{cap} ) }
+           grep { ! $opts->{type} || $opts->{type} eq $_->journaltype }
+
+           # unless overridden, hide non-visible/non-read-only journals.
+           # always display the user's acct
+           grep { $opts->{showall} || $_->is_visible || $_->is_readonly || 
$_->equals( $u ) }
+
+           # can't work as an expunged account
+           grep { ! $_->is_expunged && $_->clusterid > 0 }
+
+           # put $u at the top of the list, then sort the rest
+           ( $u,  sort { $a->user cmp $b->user } values %users );
+}
+
+
 # What journals can this user post to?
 sub posting_access_list {
     my $u = shift;
@@ -6559,7 +6590,6 @@ use Carp;
 ###  8. Formatting Content Shown to Users
 ###  9. Logging and Recording Actions
 ###  12. Comment-Related Functions
-###  13. Community-Related Functions and Authas
 ###  15. Email-Related Functions
 ###  16. Entry-Related Functions
 ###  17. Interest-Related Functions
@@ -8135,84 +8165,6 @@ sub delete_all_comments {
 
 
 ########################################################################
-###  13. Community-Related Functions and Authas
-
-=head2 Community-Related Functions and Authas (LJ)
-=cut
-
-# <LJFUNC>
-# name: LJ::get_authas_list
-# des: Get a list of usernames a given user can authenticate as.
-# returns: an array of usernames.
-# args: u, opts?
-# des-opts: Optional hashref.  keys are:
-#           - type: 'P' to only return users of journaltype 'P'.
-#           - cap:  cap to filter users on.
-# </LJFUNC>
-sub get_authas_list {
-    my ($u, $opts) = @_;
-
-    # used to accept a user type, now accept an opts hash
-    $opts = { 'type' => $opts } unless ref $opts;
-
-    # Two valid types, Personal or Community
-    $opts->{'type'} = undef unless $opts->{'type'} =~ m/^(P|C)$/;
-
-    my $ids = LJ::load_rel_target($u, 'A');
-    return undef unless $ids;
-
-    # load_userids_multiple
-    my %users;
-    LJ::load_userids_multiple([ map { $_, \$users{$_} } @$ids ], [$u]);
-
-    return map { $_->user }
-               grep { ! $opts->{'cap'} || LJ::get_cap($_, $opts->{'cap'}) }
-               grep { ! $opts->{'type'} || $opts->{'type'} eq 
$_->{'journaltype'} }
-
-               # unless overridden, hide non-visible/non-read-only journals. 
always display the user's acct
-               grep { $opts->{'showall'} || $_->is_visible || $_->is_readonly 
|| $_->equals( $u ) }
-
-               # can't work as an expunged account
-               grep { ! $_->is_expunged && $_->clusterid > 0 }
-               $u,  sort { $a->user cmp $b->user } values %users;
-}
-
-
-# <LJFUNC>
-# name: LJ::get_postto_list
-# des: Get the list of usernames a given user can post to.
-# returns: an array of usernames
-# args: u, opts?
-# des-opts: Optional hashref.  keys are:
-#           - type: 'P' to only return users of journaltype 'P'.
-#           - cap:  cap to filter users on.
-# </LJFUNC>
-sub get_postto_list {
-    my ($u, $opts) = @_;
-
-    # used to accept a user type, now accept an opts hash
-    $opts = { 'type' => $opts } unless ref $opts;
-
-    # only one valid type right now
-    $opts->{'type'} = 'P' if $opts->{'type'};
-
-    my $ids = LJ::load_rel_target($u, 'P');
-    return undef unless $ids;
-
-    # load_userids_multiple
-    my %users;
-    LJ::load_userids_multiple([ map { $_, \$users{$_} } @$ids ], [$u]);
-
-    return $u->user, sort map { $_->user }
-                         grep { ! $opts->{'cap'} || LJ::get_cap($_, 
$opts->{'cap'}) }
-                         grep { ! $opts->{'type'} || $opts->{'type'} eq 
$_->{'journaltype'} }
-                         grep { $_->clusterid > 0 }
-                         grep { $_->is_visible }
-                         values %users;
-}
-
-
-########################################################################
 ###  15. Email-Related Functions
 
 =head2 Email-Related Functions (LJ)
diff -r 45c7c3d47bed -r 3e1dc42af96f cgi-bin/weblib.pl
--- a/cgi-bin/weblib.pl Wed Sep 08 17:06:41 2010 +0800
+++ b/cgi-bin/weblib.pl Wed Sep 08 17:19:47 2010 +0800
@@ -181,35 +181,35 @@ sub valid_stylesheet_url {
 #           'authas' - current user, gets selected in drop-down;
 #           'label' - label to go before form elements;
 #           'button' - button label for submit button;
-#           others - arguments to pass to [func[LJ::get_authas_list]].
+#           'type' - journaltype (affects label & list filtering)
+#           others - arguments to pass to $u->get_authas_list.
 # </LJFUNC>
 sub make_authas_select {
-    my ($u, $opts) = @_; # type, authas, label, button
+    my ( $u, $opts ) = @_; # type, authas, label, button
 
-    my @list = LJ::get_authas_list($u, $opts);
+    my $authas = $opts->{authas} || $u->user;
+    my $button = $opts->{button} || $BML::ML{'web.authas.btn'};
+    my $label  = $opts->{label};
+
+    $label ||= $opts->{type} eq "C" ? $BML::ML{'web.authas.label.comm'}
+                                    : $BML::ML{'web.authas.label'};
+
+    my @list = $u->get_authas_list( $opts );
 
     # only do most of form if there are options to select from
-    if (@list > 1 || $list[0] ne $u->{'user'}) {
-        my $ret;
-        unless ( $opts->{selectonly} ) {
-            my $label = $BML::ML{'web.authas.label'};
-            $label = $BML::ML{'web.authas.label.comm'} if ($opts->{'type'} eq 
"C");
-            $ret = ($opts->{'label'} || $label) . " ";
-        }
+    if ( @list > 1 || $list[0] ne $u->user ) {
+        my $ret = LJ::html_select( { name => 'authas', selected => $authas,
+                                     class => 'hideable' },
+                                   map { $_, $_ } @list );
 
-        $ret .= LJ::html_select({ 'name' => 'authas',
-                                 'selected' => $opts->{'authas'} || 
$u->{'user'},
-                                 'class' => 'hideable',
-                                 },
-                                 map { $_, $_ } @list) . " ";
-        $ret .= LJ::html_submit(undef, $opts->{'button'} || 
$BML::ML{'web.authas.btn'})
+        $ret = "$label $ret " . LJ::html_submit( undef, $button )
             unless $opts->{selectonly};
 
         return $ret;
     }
 
     # no communities to choose from, give the caller a hidden
-    return  LJ::html_hidden('authas', $opts->{'authas'} || $u->{'user'});
+    return LJ::html_hidden( authas => $authas );
 }
 
 # <LJFUNC>
@@ -220,27 +220,28 @@ sub make_authas_select {
 # returns: string of HTML elements
 # args: u, opts?
 # des-opts: Optional.  Valid keys are:
-#           'postto' - current user, gets selected in drop-down;
+#           'authas' - current user, gets selected in drop-down;
 #           'label' - label to go before form elements;
 #           'button' - button label for submit button;
-#           others - arguments to pass to [func[LJ::get_postto_list]].
 # </LJFUNC>
 sub make_postto_select {
-    my ($u, $opts) = @_; # type, authas, label, button
+    my ( $u, $opts ) = @_;
 
-    my @list = LJ::get_postto_list($u, $opts);
+    my $authas = $opts->{authas} || $u->user;
+    my $label  = $opts->{label}  || $BML::ML{'web.postto.label'};
+    my $button = $opts->{button} || $BML::ML{'web.postto.btn'};
 
+    my @list = $u->posting_access_list;
     # only do most of form if there are options to select from
-    if (@list > 1) {
-        return ($opts->{'label'} || $BML::ML{'web.postto.label'}) . " " .
-               LJ::html_select({ 'name' => 'authas',
-                                 'selected' => $opts->{'authas'} || 
$u->{'user'}},
-                                 map { $_, $_ } @list) . " " .
-               LJ::html_submit(undef, $opts->{'button'} || 
$BML::ML{'web.postto.btn'});
+    if ( @list > 1 ) {
+        return "$label " .
+               LJ::html_select( { name => 'authas', selected => $authas },
+                                map { $_->user, $_->user } @list ) . " " .
+               LJ::html_submit( undef, $button );
     }
 
     # no communities to choose from, give the caller a hidden
-    return  LJ::html_hidden('authas', $opts->{'authas'} || $u->{'user'});
+    return LJ::html_hidden( authas => $authas );
 }
 
 # <LJFUNC>
diff -r 45c7c3d47bed -r 3e1dc42af96f htdocs/register.bml
--- a/htdocs/register.bml       Wed Sep 08 17:06:41 2010 +0800
+++ b/htdocs/register.bml       Wed Sep 08 17:19:47 2010 +0800
@@ -159,7 +159,7 @@ body<=
      return LJ::bad_input($ML{'error.invalidauth'})
         unless $u;
 
-     my @authas = LJ::get_authas_list($remote);
+     my @authas = $remote->get_authas_list;
      my @list;
 
      if (@authas > 1) {  # only display if they can auth as anyone other than 
themselves
_______________________________________________
dw-changelog mailing list
[email protected]
http://lists.dwscoalition.org/mailman/listinfo/dw-changelog

Reply via email to