Add a function to count accounts; reduce code duplication.

        Patch by kareila. 

diffs (120 lines):

diff -r f1179dab51c0 -r e218801419bf cgi-bin/LJ/Console/Command/Suspend.pm
--- a/cgi-bin/LJ/Console/Command/Suspend.pm     Thu Jun 17 15:30:06 2010 +0800
+++ b/cgi-bin/LJ/Console/Command/Suspend.pm     Thu Jun 17 17:02:24 2010 +0800
@@ -77,15 +77,12 @@ sub execute {
 
         $self->info("Acting on users matching email $user");
 
-        my $dbr = LJ::get_db_reader();
-        my $userids = $dbr->selectcol_arrayref('SELECT userid FROM email WHERE 
email = ?', undef, $user);
-        return $self->error("Database error: " . $dbr->errstr)
-            if $dbr->err;
+        my @userids = LJ::User->accounts_by_email( $user );
+        return $self->error( "No users found matching the email address 
$user." )
+            unless @userids;
 
-        return $self->error("No users found matching the email address $user.")
-            unless $userids && @$userids;
+        my $us = LJ::load_userids( @userids );
 
-        my $us = LJ::load_userids(@$userids);
         foreach my $u (values %$us) {
             push @users, $u->user;
         }
diff -r f1179dab51c0 -r e218801419bf cgi-bin/LJ/Console/Command/Unsuspend.pm
--- a/cgi-bin/LJ/Console/Command/Unsuspend.pm   Thu Jun 17 15:30:06 2010 +0800
+++ b/cgi-bin/LJ/Console/Command/Unsuspend.pm   Thu Jun 17 17:02:24 2010 +0800
@@ -73,15 +73,11 @@ sub execute {
     } else {
         $self->info("Acting on users matching email $user");
 
-        my $dbr = LJ::get_db_reader();
-        my $userids = $dbr->selectcol_arrayref('SELECT userid FROM email WHERE 
email = ?', undef, $user);
-        return $self->error("Database error: " . $dbr->errstr)
-            if $dbr->err;
+        my @userids = LJ::User->accounts_by_email( $user );
+        return $self->error( "No users found matching the email address 
$user." )
+            unless @userids;
 
-        return $self->error("No users found matching the email address $user.")
-            unless $userids && @$userids;
-
-        my $us = LJ::load_userids(@$userids);
+        my $us = LJ::load_userids( @userids );
 
         foreach my $u (values %$us) {
             push @users, $u->user;
diff -r f1179dab51c0 -r e218801419bf cgi-bin/LJ/User.pm
--- a/cgi-bin/LJ/User.pm        Thu Jun 17 15:30:06 2010 +0800
+++ b/cgi-bin/LJ/User.pm        Thu Jun 17 17:02:24 2010 +0800
@@ -3989,6 +3989,20 @@ sub should_show_in_search_results {
 =head2 Email-Related Functions
 =cut
 
+sub accounts_by_email {
+    my ( $u, $email ) = @_;
+    $email ||= $u->email_raw if LJ::isu( $u );
+    return undef unless $email;
+
+    my $dbr = LJ::get_db_reader() or die "Couldn't get db reader";
+    my $userids = $dbr->selectcol_arrayref(
+                        "SELECT userid FROM email WHERE email=?",
+                        undef, $email );
+    die $dbr->errstr if $dbr->err;
+    return $userids ? @$userids : ();
+}
+
+
 sub delete_email_alias {
     my $u = shift;
 
diff -r f1179dab51c0 -r e218801419bf htdocs/admin/invites/requests.bml
--- a/htdocs/admin/invites/requests.bml Thu Jun 17 15:30:06 2010 +0800
+++ b/htdocs/admin/invites/requests.bml Thu Jun 17 17:02:24 2010 +0800
@@ -38,25 +38,15 @@ body<=
     }
 
     # subroutine for counting accounts registered to user's email.
-    # you'd think User.pm would have a function for this, but no,
-    # so I checked to see how the finduser command did it.
     my $pc_accts = sub {
         my ( $u ) = @_;
-        if ( $u->email_raw ) {
+        if ( my @acctids = $u->accounts_by_email ) {
+            my $us = LJ::load_userids( @acctids );
             my ( $pct, $cct ) = ( 0, 0 );
-            my $dbr = LJ::get_db_reader();
-            my $userids = $dbr->selectcol_arrayref(
-                    "SELECT userid FROM email WHERE email = ?",
-                    undef, $u->email_raw );
-            die $dbr->errstr if $dbr->err;
-            if ( $userids ) {
-                my $us = LJ::load_userids( @$userids );
-                foreach ( @$userids ) {
-                    next unless $us->{$_};
-
-                    $pct++ if $us->{$_}->is_personal;
-                    $cct++ if $us->{$_}->is_comm;
-                }
+            foreach ( @acctids ) {
+                next unless $us->{$_};
+                $pct++ if $us->{$_}->is_personal;
+                $cct++ if $us->{$_}->is_comm;
             }
             return "$pct/$cct";
         } else {
diff -r f1179dab51c0 -r e218801419bf htdocs/lostinfo.bml
--- a/htdocs/lostinfo.bml       Thu Jun 17 15:30:06 2010 +0800
+++ b/htdocs/lostinfo.bml       Thu Jun 17 17:02:24 2010 +0800
@@ -218,10 +218,7 @@ body<=
             unless $email;
 
         my @users;
-        my $dbr = LJ::get_db_reader();
-        my $sth = $dbr->prepare("SELECT userid FROM email WHERE email=?");
-        $sth->execute($email);
-        while (my ($uid) = $sth->fetchrow_array) {
+        foreach my $uid ( LJ::User->accounts_by_email( $email ) ) {
             my $u = LJ::load_userid($uid);
             next unless $u && $u->{clusterid};  # not purged
 
_______________________________________________
dw-changelog mailing list
[email protected]
http://lists.dwscoalition.org/mailman/listinfo/dw-changelog

Reply via email to