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

[dw-free] sponsor a random active free community

[commit: http://hg.dwscoalition.org/dw-free/rev/759c2d92f7b8]

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

Add the ability to sponsor a random active free community -- similar to
sponsor a random active free user, including the opt-out under the Privacy
tab. One additional requirement: must have at least ten members.

Patch by [personal profile] catness.

Files modified:
  • bin/get-users-for-paid-accounts.pl
  • bin/upgrading/update-db-general.pl
  • cgi-bin/DW/Pay.pm
  • cgi-bin/DW/Setting/RandomPaidGifts.pm
  • htdocs/shop/randomgift.bml
  • htdocs/shop/randomgift.bml.text
--------------------------------------------------------------------------------
diff -r 513857907330 -r 759c2d92f7b8 bin/get-users-for-paid-accounts.pl
--- a/bin/get-users-for-paid-accounts.pl	Mon Jul 05 14:56:15 2010 +0800
+++ b/bin/get-users-for-paid-accounts.pl	Mon Jul 05 15:27:12 2010 +0800
@@ -25,7 +25,7 @@ my $dbslow = LJ::get_dbh( 'slow' );
 my $dbslow = LJ::get_dbh( 'slow' );
 my $dbh = LJ::get_db_writer();
 
-my $userids = $dbslow->selectcol_arrayref( "SELECT userid FROM user WHERE statusvis = 'V' AND journaltype = 'P'" );
+my $userids = $dbslow->selectcol_arrayref( "SELECT userid FROM user WHERE statusvis = 'V' AND journaltype IN ( 'P', 'C' )" );
 my $starttime = $dbslow->selectrow_array( "SELECT UNIX_TIMESTAMP()" );
 
 my $week_ago = $starttime - 60*60*24*7;
@@ -37,23 +37,37 @@ while ( @$userids && ( my @userids_chunk
         my $u = $us->{$userid};
 
         next if $u->is_paid; # must not be a paid user
-        next unless $u->opt_randompaidgifts; # must allow random paid gifts
-        next if LJ::sysban_check( 'pay_user', $u->user ); # must not be sysbanned from payments
         next if $u->timecreate > $month_ago; # must be created more than a month ago
         next if $u->number_of_posts < 10; # must have at least 10 posts
         next if $u->timeupdate < $week_ago; # must have posted in the past week
+        next unless $u->opt_randompaidgifts; # must allow random paid gifts
+        next if LJ::sysban_check( 'pay_user', $u->user ); # must not be sysbanned from payments
+
+        my $members_count = 0;
+        if ( $u->is_community ) {
+            $members_count = scalar $u->member_userids;
+            next if $members_count < 10; # community must have at least 10 members
+        }
 
         # get the number of entries posted and comments left in the past month
         my $dbcr = LJ::get_cluster_reader( $u );
         my $num_posts = $dbcr->selectrow_array( "SELECT COUNT(*) FROM log2 WHERE journalid = ? AND logtime > ?", undef, $userid, LJ::mysql_time( $month_ago ) );
-        my $num_comments = $dbcr->selectrow_array( "SELECT COUNT(*) FROM talkleft WHERE userid = ? AND posttime > ?", undef, $userid, $month_ago );
+        my $num_comments;
+        if ( $u->is_personal ) {
+            $num_comments = $dbcr->selectrow_array( "SELECT COUNT(*) FROM talkleft WHERE userid = ? AND posttime > ?", undef, $userid, $month_ago );
+        }
+        else {
+            $num_comments = $dbcr->selectrow_array( "SELECT COUNT(*) FROM talk2 WHERE journalid = ? AND datepost > ?", undef, $userid, LJ::mysql_time( $month_ago ) );
+        }
 
         # assign point values based on these numbers
         my $post_points = min( 10, $num_posts ) || 0;
         my $comment_points = min( 10, $num_comments ) || 0;
+        my $member_points = min( 10, $members_count ) || 0;
 
         # insert the total points for the user
-        $dbh->do( "INSERT INTO users_for_paid_accounts ( userid, time_inserted, points ) VALUES ( ?, ?, ? )", undef, $userid, $starttime, $post_points + $comment_points );
+        $dbh->do( "INSERT INTO users_for_paid_accounts ( userid, time_inserted, points, journaltype ) VALUES ( ?, ?, ?, ? )", 
+                   undef, $userid, $starttime, $post_points + $comment_points + $member_points, $u->journaltype );
     }
 }
 
diff -r 513857907330 -r 759c2d92f7b8 bin/upgrading/update-db-general.pl
--- a/bin/upgrading/update-db-general.pl	Mon Jul 05 14:56:15 2010 +0800
+++ b/bin/upgrading/update-db-general.pl	Mon Jul 05 15:27:12 2010 +0800
@@ -2957,9 +2957,11 @@ CREATE TABLE users_for_paid_accounts (
     userid int unsigned not null,
     time_inserted int unsigned not null default 0,
     points int(5) unsigned not null default 0,
+    journaltype char(1) NOT NULL DEFAULT 'P',
 
     PRIMARY KEY ( userid, time_inserted ),
-    INDEX ( time_inserted )
+    INDEX ( time_inserted ),
+    INDEX ( journaltype )
 )
 EOC
 
@@ -3066,6 +3068,12 @@ register_alter(sub {
 
     my $dbh = shift;
     my $runsql = shift;
+
+    if (column_type("users_for_paid_accounts", "journaltype") eq "") {
+        do_alter("users_for_paid_accounts",
+                 "ALTER TABLE users_for_paid_accounts ADD journaltype CHAR(1) NOT NULL DEFAULT 'P', ".
+                 "ADD INDEX(journaltype)");
+    }
 
     if (column_type("supportcat", "is_selectable") eq "") {
         do_alter("supportcat",
diff -r 513857907330 -r 759c2d92f7b8 cgi-bin/DW/Pay.pm
--- a/cgi-bin/DW/Pay.pm	Mon Jul 05 14:56:15 2010 +0800
+++ b/cgi-bin/DW/Pay.pm	Mon Jul 05 15:27:12 2010 +0800
@@ -641,20 +641,23 @@ sub num_permanent_accounts_available_est
 ################################################################################
 # DW::Pay::get_random_active_free_user
 #
-# ARGUMENTS: for_u = user that is requesting the random free user (remote if
+# ARGUMENTS: journaltype = type (user 'P' or community 'C') of the requested
+#            user ('P' if not given)
+#            for_u = user that is requesting the random free user (remote if
 #            no user is given)
 #
 # RETURN: a random active free user that for_u can purchase a paid account for,
 #         or undef if there aren't any valid results
 #
 sub get_random_active_free_user {
+    my $journaltype = shift || 'P';
     my $for_u = shift || LJ::get_remote();
 
     my $dbr = LJ::get_db_reader();
     my $rows = $dbr->selectall_arrayref(
         q{SELECT userid, points FROM users_for_paid_accounts
-          ORDER BY RAND() LIMIT 10},
-        { Slice => {} } );
+          WHERE journaltype = ? ORDER BY RAND() LIMIT 10},
+        { Slice => {} }, $journaltype );
 
     my @active_us;
     my $us = LJ::load_userids( map { $_->{userid} } @$rows );
@@ -663,13 +666,14 @@ sub get_random_active_free_user {
         my $points = $row->{points};
         my $u = $us->{$userid};
 
-        next unless $u && $u->is_visible && $u->is_personal;
+        next unless $u && $u->is_visible;
         next if $u->is_paid;
         next unless $u->opt_randompaidgifts;
         next if LJ::sysban_check( 'pay_user', $u->user );
-        next if $for_u && $u->equals( $for_u );
-        next if $for_u && $u->has_banned( $for_u );
-
+        if ( $journaltype eq 'P' ) {
+            next if $for_u && $u->equals( $for_u );
+            next if $for_u && $u->has_banned( $for_u );
+        }
         # each point that a user has gives them an extra chance of being chosen out of the array
         push @active_us, $u;
         if ( $points ) {
diff -r 513857907330 -r 759c2d92f7b8 cgi-bin/DW/Setting/RandomPaidGifts.pm
--- a/cgi-bin/DW/Setting/RandomPaidGifts.pm	Mon Jul 05 14:56:15 2010 +0800
+++ b/cgi-bin/DW/Setting/RandomPaidGifts.pm	Mon Jul 05 15:27:12 2010 +0800
@@ -23,7 +23,7 @@ sub should_render {
 sub should_render {
     my ( $class, $u ) = @_;
 
-    return $u->is_personal && !$u->is_perm ? 1 : 0;
+    return $u->is_perm ? 0 : 1;
 }
 
 sub label {
diff -r 513857907330 -r 759c2d92f7b8 htdocs/shop/randomgift.bml
--- a/htdocs/shop/randomgift.bml	Mon Jul 05 14:56:15 2010 +0800
+++ b/htdocs/shop/randomgift.bml	Mon Jul 05 15:27:12 2010 +0800
@@ -21,7 +21,11 @@ body<=
     use strict;
     use vars qw/ %GET %POST $title /;
 
-    $title = $ML{'.title'};
+    my $type = $GET{type};
+    $type = 'P' unless $type eq 'C';
+    my $othertype = $type eq 'P' ? 'C' : 'P';
+
+    $title = $ML{'.title.'.$type};
 
     if ( LJ::did_post() ) {
         my $username = $POST{username};
@@ -31,29 +35,37 @@ body<=
         }
     }
 
-    my $randomu = DW::Pay::get_random_active_free_user();
+    my $randomu = DW::Pay::get_random_active_free_user( $type );
 
     my $ret;
 
     $ret .= "<p><a href='$LJ::SITEROOT/shop'>&lt;&lt; " . BML::ml( '.backlink', { sitename => $LJ::SITENAMESHORT } ) . "</a></p>";
 
     if ( $randomu ) {
-        $ret .= "<p>" . BML::ml( '.intro', { aopts => "href='$LJ::SITEROOT/shop/randomgift'" } ) . "</p>";
+        $ret .= "<p>" . BML::ml( '.intro.'.$type, { aopts => "href='$LJ::SITEROOT/shop/randomgift?type=$type'" } ) . "</p>";
         $ret .= "<p><strong>$ML{'.label.username'}</strong> " . $randomu->ljuser_display . "<br />";
         $ret .= "<strong>$ML{'.label.createdate'}</strong> " . LJ::mysql_time( $randomu->timecreate ) . "<br />";
         $ret .= "<strong>$ML{'.label.lastupdated'}</strong> " . substr( LJ::mysql_time( $randomu->timeupdate ), 0, 10 ) . "<br />";
         $ret .= "<strong>$ML{'.label.numentries'}</strong> " . $randomu->number_of_posts . "<br />";
-        $ret .= "<strong>$ML{'.label.numcomments'}</strong> " . $randomu->num_comments_posted . "<br />";
-        $ret .= "<strong>$ML{'.label.numcommunities'}</strong> " . scalar $randomu->member_of_userids . "<br />";
+        if ( $type eq 'P' ) {
+            $ret .= "<strong>$ML{'.label.numcomments'}</strong> " . $randomu->num_comments_posted . "<br />";
+            $ret .= "<strong>$ML{'.label.numcommunities'}</strong> " . scalar $randomu->member_of_userids . "<br />";
+        }
+        else {
+            $ret .= "<strong>$ML{'.label.numcomments'}</strong> " . $randomu->num_comments_received . "<br />";
+            $ret .= "<strong>$ML{'.label.nummembers'}</strong> " . scalar $randomu->member_userids . "<br />";
+        }
         $ret .= "</p>";
 
         $ret .= "<form method='post'>";
         $ret .= LJ::html_hidden( username => $randomu->user );
         $ret .= LJ::html_submit( BML::ml( '.form.submit', { username => $randomu->user } ) ) . " ";
-        $ret .= "<a href='$LJ::SITEROOT/shop/randomgift'>$ML{'.form.getanother'}</a>";
+        $ret .= "<a href='$LJ::SITEROOT/shop/randomgift?type=$type'>$ML{'.form.getanother.'.$type}</a>";
+        $ret .= "&nbsp;&nbsp;<a href='$LJ::SITEROOT/shop/randomgift?type=$othertype'>$ML{'.form.switchtype.'.$type}</a>";
         $ret .= "</form><br />";
     } else {
-        $ret .= "<p>$ML{'.nousers'}</p>";
+        $ret .= "<p>$ML{'.nousers.'.$type}</p>";
+        $ret .= "&nbsp;&nbsp;<a href='$LJ::SITEROOT/shop/randomgift?type=$othertype'>$ML{'.form.switchtype.'.$type}</a>";
     }
 
     $ret .= "<p><a href='$LJ::SITEROOT/shop'>&lt;&lt; " . BML::ml( '.backlink', { sitename => $LJ::SITENAMESHORT } ) . "</a></p>";
diff -r 513857907330 -r 759c2d92f7b8 htdocs/shop/randomgift.bml.text
--- a/htdocs/shop/randomgift.bml.text	Mon Jul 05 14:56:15 2010 +0800
+++ b/htdocs/shop/randomgift.bml.text	Mon Jul 05 15:27:12 2010 +0800
@@ -2,11 +2,19 @@
 
 .backlink=Back to [[sitename]] Shop
 
-.form.getanother=Find a different random active free user
+.form.getanother.P=Find a different random active free user
+
+.form.getanother.C=Find a different random active free community
+
+.form.switchtype.C=Find a random active free user
+
+.form.switchtype.P=Find a random active free community
 
 .form.submit=Purchase a Paid Account for [[username]]
 
-.intro=Here is a random active free user who may appreciate a paid account.  If you'd like to find a different random user, just <a [[aopts]]>refresh</a>.
+.intro.P=Here is a random active free user who may appreciate a paid account.  If you'd like to find a different random user, just <a [[aopts]]>refresh</a>.
+
+.intro.C=Here is a random active free community who may appreciate a paid account.  If you'd like to find a different random community, just <a [[aopts]]>refresh</a>.
 
 .label.createdate=Created on:
 
@@ -18,8 +26,14 @@
 
 .label.numentries=Number of entries posted:
 
+.label.nummembers=Number of members:
+
 .label.username=Username:
 
-.nousers=There are currently no active free users.
+.nousers.P=There are currently no active free users.
 
-.title=Find a Random Active Free User
+.nousers.C=There are currently no active free communities.
+
+.title.P=Find a Random Active Free User
+
+.title.C=Find a Random Active Free Community
--------------------------------------------------------------------------------