<?_info
nocache=>1
_info?><?page
title=><?_ml .title _ml?>

body<=
<?_code
{
    use strict;

    my $qs = BML::get_query_string();
    return LJ::bad_input($ML{'.error.invalidargument'})
        unless $qs && $qs =~ /^(\d+)\.(.+)$/;

    my ($aaid, $auth) = ($1, $2);
    my $aa = LJ::is_valid_authaction($aaid, $auth);
    return LJ::bad_input($ML{'.error.invalidargument'})
        unless $aa;
    return LJ::bad_input($ML{'.error.actionperformed'})
        if $aa->{'used'} eq 'Y';

    my $arg = {};
    LJ::decode_url_string($aa->{'arg1'}, $arg);

    ### perform actions according to the action type

    # invite users to communities
    if ($aa->{'action'} eq 'comm_invite') {

        my $dbh = LJ::get_db_writer();

        my $targetid = $arg->{'targetid'};
        return LJ::bad_input($ML{'.error.internerr.invalidaction'}) unless $targetid;

        # add to community
        if ($arg->{'member'}) {
            LJ::add_friend($aa->{userid}, $targetid);
        }

        # set up rels with this community
        my @rels = ();
        push @rels, 'A' if $arg->{'admin'};
        push @rels, 'P' if $arg->{'post'};
        push @rels, 'M' if $arg->{'moderate'};
        push @rels, 'N' if $arg->{'preapprove'};

        if (@rels) {
            LJ::set_rel_multi( map { [$aa->{userid}, $targetid, $_] } @rels );
        }

        # mark this authaction as used
        $dbh->do("UPDATE authactions SET used='Y' WHERE aaid=?", undef, $aa->{'aaid'});

        # return success
        my $username = LJ::get_username($aa->{'userid'});
        return "<?h1 $ML{'.comm.success'} h1?>".
               '<?p '.BML::ml('.comm.text',
               {'comm'=>LJ::ljuser($username, { 'type' => 'C' }),
                'aopts'=>'href="'.$LJ::SITEROOT.'/friends/add.bml?user='.$username.'"'}).
               ' p?>';
    }

    # invite users to shared journals
    if ($aa->{'action'} eq 'shared_invite') {
        my $dbh = LJ::get_db_writer();

        my $targetid = $arg->{'targetid'};
        return LJ::bad_input($ML{'.error.internerr.invalidaction'}) unless $targetid;

        LJ::set_rel($aa->{'userid'}, $targetid, 'P');

        # mark this authaction as used
        $dbh->do("UPDATE authactions SET used='Y' WHERE aaid=?", undef, $aa->{'aaid'});

        my $username = LJ::get_username($aa->{'userid'});
        return "<?h1 $ML{'.shared.success'} h1?>".
               '<?p '.BML::ml('.shared.text',
               {'shared'=>LJ::ljuser($username, { 'type' => 'S' }),
                'aopts'=>'href="'.$LJ::SITEROOT.'/friends/add.bml?user='.$username.'"'}).
               ' p?>';
    }

    # approve someone joining a community
    if ($aa->{action} eq 'comm_join_request') {
        my $dbh = LJ::get_db_writer();

        # get user we're adding
        my $targetid = $arg->{targetid};
        return LJ::bad_input($ML{'.error.internerr.invalidaction'}) unless $targetid;

        # add to community
        return "<?h1 $ML{'Error'} h1?><?p $ML{'.error.approving'} p?>"
            unless LJ::approve_pending_member($aa->{userid}, $targetid);

        # return success
        my $commname = LJ::get_username($aa->{userid});
        my $username = LJ::get_username($targetid);
        return "<?h1 $ML{'.comm.success'} h1?>".
               '<?p ' . BML::ml('.commjoin.text', { 
                           user => LJ::ljuser($username, { type => 'P' }),
                           comm => LJ::ljuser($commname, { type => 'C' }),
                           aopts => "href=\"$LJ::SITEROOT/community/members.bml?comm=$commname\"",
                        }) . ' p?>';
    }

    # not other action types right now
    return LJ::bad_input($ML{'.error.unknownactiontype'});
}
_code?>

<=body
page?>
