Committer: vtroitsky
LJSUP-11205: Console command selfpromo extended for entry, journal and communityU trunk/cgi-bin/LJ/Console/Command/SelfPromo.pm
Modified: trunk/cgi-bin/LJ/Console/Command/SelfPromo.pm =================================================================== --- trunk/cgi-bin/LJ/Console/Command/SelfPromo.pm 2012-02-10 14:15:58 UTC (rev 11464) +++ trunk/cgi-bin/LJ/Console/Command/SelfPromo.pm 2012-02-10 14:27:01 UTC (rev 11465) @@ -39,26 +39,29 @@ return LJ::check_priv( $remote, 'siteadmin', 'selfpromo' ); } -sub _cancel_entry { +sub _cancel_object { my ( $self, %args ) = @_; - my $entry = $args{'entry'}; + my $type = $args{'type'}; + my $object = $args{'object'}; my $refund = $args{'refund'}; my $reason = $args{'reason'}; + my $class = LJ::Pay::SelfPromo->get_class_by_type($type); + # refund defaults to 1 $refund = 1 unless defined $refund; my $admin = LJ::get_remote(); - my $message = 'admin cancel: ' . $entry->url; + my $message = 'admin cancel: ' . ($type eq 'entry' ? $object->url : $object->display_name); $message .= ', without refund' unless $refund; $message .= '. Reason: '. $reason if $reason; - LJ::statushistory_add( $entry->poster, $admin, 'selfpromo', $message ); + LJ::statushistory_add( $type eq 'entry' ? $object->poster : $object, $admin, 'selfpromo', $message ); - LJ::Pay::SelfPromo->admin_cancel_promo( - 'entry' => $entry, + $class->admin_cancel_promo( + 'object' => $object, 'admin' => $admin, 'refund' => $refund, ); @@ -81,14 +84,17 @@ } # either it's an entry URL, or a username, or something unknown + my $type; my $object_orig = $object; if ( $object =~ m{^http://} ) { $object = LJ::Entry->new_from_url($object); + $type = 'entry'; unless ( $object && $object->valid ) { return $self->error("No entry found at $object_orig"); } } elsif ( $object =~ /^[a-z0-9\-_]+$/i ) { $object = LJ::load_user($object); + $type = 'user'; unless ($object) { return $self->error("No such user $object_orig"); } @@ -112,44 +118,52 @@ return $self->error("Insufficient arguments"); } - LJ::Pay::SelfPromo->check_current_entry; - my $current_entry = LJ::Pay::SelfPromo->current_entry; - if ( $cmd eq 'cancel' ) { - unless ( $object->isa('LJ::Entry') ) { - return $self->error('"cancel" requires an entry URL'); + if ( $type eq 'user' ) { + return $self->error('"cancel" requires personal journal or community') unless ($object->is_community || $object->is_personal); + $type = $object->is_community ? 'community' : 'journal'; } - my $entry = $object; + my $class = LJ::Pay::SelfPromo->get_class_by_type($type); + $class->check_current_promo; + my $current_promo = $class->current_promo_info; - unless ($current_entry) { - return $self->error('No entry currently promoted'); + # use Data::Dumper; + # return $self->error("Type: $type". Dumper($current_promo)); + + unless ($current_promo) { + return $self->error("No $type currently promoted"); } - unless ( $entry->journalid == $current_entry->journalid - && $entry->jitemid == $current_entry->jitemid ) + unless ($current_promo->is_promoting($object)) { return $self->error( - $entry->url . ' is not the entry currently promoted' ); + $object_orig . " is not the $type currently promoted" ); } - $self->_cancel_entry( 'entry' => $entry, 'refund' => !$norefund, 'reason' => $reason ); + $self->_cancel_object( 'type' => $type, 'object' => $object, 'refund' => !$norefund, 'reason' => $reason ); - $self->info( $entry->url . ' cancelled successfully' ); + $self->info( $object_orig . ' cancelled successfully' ); return 1; } if ( $cmd eq 'ban' ) { if ( $object->isa('LJ::User') ) { my $u = $object; + $type = $object->is_community ? 'community' : 'journal'; - if ( $current_entry - && ( $current_entry->poster->equals($u) - || $current_entry->journal->equals($u) ) ) - { - $self->info('The ban will affect the currently promoted ' - . 'entry, so cancelling that.'); - $self->_cancel_entry( 'entry' => $current_entry, 'reason' => $reason ); + foreach my $ptype qw(entry journal community) { + + my $class = LJ::Pay::SelfPromo->get_class_by_type($ptype); + $class->check_current_promo; + my $current_promo = $class->current_promo_info(); + + if ( $current_promo && ( $current_promo->owner->equals($u) || $current_promo->journal->equals($u) || $current_promo->promoter->equals($u)) ) + { + $self->info('The ban will affect the currently promoted '. $ptype . ', so cancelling that.'); + my $current_object = $current_promo->object(); + $self->_cancel_object( 'type' => $ptype, 'object' => $current_object, 'reason' => $reason ); + } } $u->set_prop( 'selfpromo_banned' => 1 ); @@ -162,13 +176,15 @@ if ( $object->isa('LJ::Entry') ) { my $entry = $object; - if ( $current_entry - && $current_entry->journalid == $entry->journalid - && $current_entry->jitemid == $entry->jitemid ) + my $class = LJ::Pay::SelfPromo->get_class_by_type($type); + $class->check_current_promo; + my $current_promo = $class->current_promo_info(); + + if ( $current_promo && $current_promo->is_promoting($entry) ) { $self->info('The ban will affect the currently promoted ' . 'entry, so cancelling that.'); - $self->_cancel_entry( 'entry' => $current_entry, 'reason' => $reason ); + $self->_cancel_object( 'type' => $type, 'object' => $entry, 'reason' => $reason ); } $entry->set_prop( 'selfpromo_banned' => 1 );