holyandrew (holyandrew) wrote in changelog,
holyandrew
holyandrew
changelog

[ljcom] r12281: LJSUP-12951: Console comands for promo n...

Committer: akanashin
LJSUP-12951: Console comands for promo notifications
A   trunk/cgi-bin/LJ/Console/Command/NotificationPromo.pm
Added: trunk/cgi-bin/LJ/Console/Command/NotificationPromo.pm
===================================================================
--- trunk/cgi-bin/LJ/Console/Command/NotificationPromo.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/Console/Command/NotificationPromo.pm	2012-07-13 08:10:20 UTC (rev 12281)
@@ -0,0 +1,180 @@
+package LJ::Console::Command::NotificationPromo;
+use strict;
+use warnings;
+
+use base qw( LJ::Console::Command );
+
+use LJ::Pay::PromotionSlot qw(:classes :regions);
+use LJ::Pay::PromotedObject qw(:types);
+
+sub cmd {'notificationpromo'}
+
+sub desc {
+    return 'NotificationPromo administration tools';
+}
+
+sub usage {
+    return '<cmd> <object> [region] [norefund] <reason>';
+}
+
+sub args_desc {
+    return [
+        'cmd' => 'The command to run, one of "cancel", "ban", or "unban"',
+
+        'object' =>
+            'For "cancel", "ban" and "unban", either the entry URL or ' .
+            'the username to cancel promotion or to ban or unban.',
+
+        'region' =>
+            'For "cancel" only, an optional flag that specifies ' .
+            'region of the promo. Available regions are: [' . 
+            join(',', LJ::Pay::PromotionSlot->get_all_regions()) . ']. ' .
+            'Default region: '. PROMO_REGION_DEFAULT(),
+ 
+        'norefund' =>
+            'For "cancel" only, an optional flag that specifies ' .
+            'not to issue token refund to the promotion author.',
+
+        'reason' => 
+            'Why you are doing this.',
+    ];
+}
+
+sub can_execute {
+    my $remote = LJ::get_remote();
+#    return LJ::check_priv( $remote, 'siteadmin', 'notificationpromo' );
+return 1;    
+}
+
+sub _cancel_object {
+    my ( $self, %args ) = @_;
+
+    my $type = $args{'type'};
+    my $object  = $args{'object'};
+    my $refund = $args{'refund'};
+    my $reason = $args{'reason'};
+    my $slot = $args{'slot'};
+
+    my $admin = LJ::get_remote();
+
+    my $message = 'admin cancel: ' . $object->name;
+    $message .= ', without refund' unless $refund;
+    $message .= '. Reason: '. $reason if $reason;
+
+    LJ::statushistory_add( $object->owner, $admin, 'notificationpromo', $message );
+
+    $slot->admin_cancel_promo(
+        'slot'      => $slot,
+        'object'    => $object,
+        'admin'     => $admin,
+        'refund'    => $refund,
+    );
+}
+
+sub execute {
+    my ( $self, $cmd, $object, @args_remainder ) = @_;
+    my $admin = LJ::get_remote();
+
+    if ( ! defined $cmd || $cmd eq '' ) {
+        return $self->error('No command specified');
+    }
+
+    unless ( $cmd eq 'cancel' || $cmd eq 'ban' || $cmd eq 'unban' ) {
+        return $self->error("Invalid command $cmd specified");
+    }
+
+    if ( ! defined $object || $object eq '' ) {
+        return $self->error("No object to $cmd specified");
+    }
+
+    my $object_orig = $object;
+
+    $object = LJ::Pay::PromotedObject->new_from_name($object);
+    unless ($object) {
+        if ( $object_orig =~ m{^http://} ) {
+            return $self->error("No entry found at $object_orig");
+        } elsif ( $object_orig =~ /^[a-z0-9\-_]+$/i ) {
+            return $self->error("No such user $object_orig");
+        } else {
+            return $self->error("Invalid object $object_orig passed");
+        }
+    }
+
+    my $country = PROMO_REGION_DEFAULT();
+
+    my ($norefund, $reason) = 0;
+    if (@args_remainder) {
+        my ($arg) = @args_remainder;
+
+        if ( $arg =~ /^(ua|th|my|sg)$/ ) {
+            $country = $arg;
+            shift @args_remainder;
+        }
+
+        if ( $arg eq 'norefund' ) {
+            $norefund = 1;
+            shift @args_remainder;
+        }
+
+        $reason = join ' ', @args_remainder if @args_remainder;
+    }
+
+    unless ( $reason ) {
+        return $self->error("Insufficient arguments");
+    }
+
+
+    if ( $cmd eq 'cancel' ) {
+        my $slot = LJ::Pay::PromotionSlot->find_slot(class => PROMO_NOTIFICATIONS(), type => PROMO_OBJECT_TYPE_ENTRY(), country => $country);
+        $slot->check_current_promotion;
+        my $current_promotion = $slot->current_promotion;
+
+        unless ($current_promotion) {
+            return $self->error("No entry currently promoted in $country promo");
+        }
+
+        unless ($current_promotion->is_promoting($object)) {
+            return $self->error(
+                $object_orig . " is not the entry currently promoted in $country promo" );
+        }
+
+        $self->_cancel_object( 'slot' => $slot, 'type' => PROMO_OBJECT_TYPE_ENTRY(), 'object' => $object, 'refund' => $norefund, 'reason' => $reason );
+
+        $self->info( "$object_orig cancelled successfully from $country promo" );
+        return 1;
+    }
+
+    if ( $cmd eq 'ban' ) {
+            my $entry = $object->object;
+
+            # check all regions
+            foreach my $cc (LJ::Pay::PromotionSlot->get_all_regions()) {
+            
+                my $slot = LJ::Pay::PromotionSlot->find_slot(class => PROMO_NOTIFICATIONS(), type => PROMO_OBJECT_TYPE_ENTRY(), country => $cc);
+
+                $slot->check_current_promotion;
+                my $current_promo = $slot->current_promotion();
+
+                if ( $current_promo && $current_promo->is_promoting($object) ) {
+                    $self->info("The ban will affect the currently promoted entry in $country promo, so cancelling that.");
+                    $self->_cancel_object( 'slot' => $slot, 'type' => PROMO_OBJECT_TYPE_ENTRY(), 'object' => $object, 'reason' => $reason );
+                }
+            }
+
+            $entry->set_prop( 'selfpromo_banned' => 1 );
+            LJ::statushistory_add( $entry->journal, $admin, 'notificationpromo', 'admin banned entry: '. $entry->url .'. Reason: '. $reason );
+
+            $self->info( $entry->url . ' banned successfully.');
+            return 1;
+    }
+
+    if ( $cmd eq 'unban' ) {
+           my $entry = $object->object;
+            $entry->set_prop( 'selfpromo_banned' => undef );
+            LJ::statushistory_add( $object->journal, $admin, 'notificationpromo', 'admin unbanned entry: '. $object->url .'. Reason: '. $reason );
+            $self->info( $object->url . ' unbanned successfully.');
+            return 1;
+    }
+}
+
+1;

Tags: akanashin, holyandrew, ljcom, pm
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments