vadvs (vadvs) wrote in changelog,
vadvs
vadvs
changelog

[ljcom] r9848: LJSUP-7527: New console command for invi...

Committer: vsukhanov
LJSUP-7527: New console command for inviting
A   trunk/cgi-bin/LJ/Console/Command/Invite.pm
Added: trunk/cgi-bin/LJ/Console/Command/Invite.pm
===================================================================
--- trunk/cgi-bin/LJ/Console/Command/Invite.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/Console/Command/Invite.pm	2010-12-15 04:53:36 UTC (rev 9848)
@@ -0,0 +1,109 @@
+package LJ::Console::Command::Invite;
+
+use strict;
+use base qw(LJ::Console::Command);
+use Carp qw(croak);
+
+sub cmd { "invite" }
+
+sub desc { "Invite users to join a community" }
+
+sub args_desc { [
+                 'community'    => "Invite users to joing this community",
+                 'permition'    => "Gran permition to every user in the list. They are 'member', 'post', 'preapprove', 'moderate', 'admin'",
+                 'username'     => "Invited user",
+                 ] }
+
+sub usage { 'invite to <community> [ +<permition1> +<permition2> ] <username> <username> <username>' }
+
+sub can_execute {
+    warn "Can execute";
+    return LJ::get_remote() ? 1 : 0;
+}
+
+sub execute {
+    my ($self, $cmd, @args) = @_;
+
+    ## 1. Parse and normalize command arguments 
+    ##      - community_name
+    ##      - list of permitions (rights)
+    ##      - list of invited users
+    ##
+    ## 2. then validate args and send invites.
+    ##
+
+    ## Stage 1.
+    ## "to" is used in command syntax for human readability only
+    $cmd = shift @args if $cmd eq 'to';
+
+    ## 1.1. Community
+    my $community_name = $cmd;
+
+    ## 1.2. Permitions
+    my @rights = ();
+    my %aliases = ("posting access" => "post",          ## some well known permitions have
+                   "moderator"      => "moderate",      ## other internal names.
+                   "unmoderated"    => "preapprove",    ## 
+                   "maintainer"     => "admin",         ##
+                   );
+    
+    ## permitions are with plus (+) sign at the begining
+    while ($args[0] =~ s/^\+//){
+        my $right = shift @args;
+        
+        ## normalize
+        $right = lc $right;
+        $right =~ s/\,$//; ## if comma is used as a separator
+        $right = $aliases{$right} if exists $aliases{$right};
+
+        push @rights => $right;
+    }
+    @rights = ('member', 'post') unless @rights; # default
+    
+    ## 
+    my %allowed_rights = map { $_ => 1 } 
+                         qw/member post preapprove moderate admin/;
+    foreach my $right (@rights){
+        return $self->error("Unknown permition '$right'")
+            unless exists $allowed_rights{$right};
+    }
+
+    ## 1.3. the rest of args is a users list
+    my @users = @args;
+    @users = map { s/\,$//; $_ } @users; # comma can be used as a separator, 
+                                         # but lex parser keeps commas as last char of an arg.
+    ## end of argument parsing
+
+    ## Stage 2.
+    my $remote = LJ::get_remote();
+
+    ## 2.1 Verify community
+    my $community = LJ::load_user($community_name);
+    return $self->error("'$community_name' is an unknown account")
+        unless $community;
+    return $self->error("'$community_name' is not a community account")
+        unless $community->is_community;
+    return $self->error("Your should be a maintainer of the community to invite users")
+        unless $remote->can_manage($community);
+
+    ## 2.2. Send invites to users.
+    foreach my $username (@users) {
+        my $u = LJ::load_user($username);
+        unless ($u){
+            $self->error("Unknown user '$username'");
+            next;
+        }
+        
+        my $res = LJ::send_comm_invite($u, $community, $remote, \@rights);
+        if ($res){
+            ## success
+            $self->print("'$username' has been invited successfully");
+        } else {
+            $self->error("Error inviting '$username': " . LJ::last_error());
+        }
+    }
+
+   return 1;
+}
+
+1;

Tags: ljcom, pm, vadvs
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