Skip to content

Commit

Permalink
[#1035] Create convenience DW::RPC::out/err/alert methods
Browse files Browse the repository at this point in the history
Instead of redefining the print logic in each handler, just do this
once.
  • Loading branch information
afuna committed Nov 6, 2014
1 parent 2a70299 commit 56ab94d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 30 deletions.
39 changes: 9 additions & 30 deletions cgi-bin/DW/Controller/RPC/MiscLegacy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ package DW::Controller::RPC::MiscLegacy;

use strict;
use DW::Routing;
use LJ::JSON;
use DW::RPC;
use LJ::CreatePage;

# do not put any endpoints that do not have the "forked from LJ" header in this file
Expand All @@ -28,8 +28,7 @@ sub check_username_handler {
my $args = $r->get_args;
my $error = LJ::CreatePage->verify_username( $args->{user} );

$r->print( to_json({ error => $error ? $error : "" }) );
return $r->OK;
return DW::RPC->err( $error );
}

sub control_strip_handler {
Expand All @@ -45,37 +44,26 @@ sub control_strip_handler {
$control_strip = LJ::control_strip( user => $user, host => $args->{host}, uri => $args->{uri}, args => $args->{args}, view => $args->{view} );
}

$r->print( to_json( { control_strip => $control_strip } ) );
return $r->OK;
return DW::RPC->out( control_strip => $control_strip );
}

sub get_security_options_handler {
my $r = DW::Request->get;
my $args = $r->get_args;

my $ret = sub {
$r->print( to_json( $_[0] ) );
return $r->OK;
};

my $err = sub {
return $ret->( { 'alert' => $_[0] } );
};


my $remote = LJ::get_remote();
my $user = $args->{user};
my $u = LJ::load_user($user);

return $ret->( {} )
return DW::RPC->out
unless $u;

my %ret = (
is_comm => $u->is_comm ? 1 : 0,
can_manage => $remote && $remote->can_manage( $u ) ? 1 : 0,
);

return $ret->( { ret => \%ret } )
return DW::RPC->out( ret => \%ret )
unless $remote && $remote->can_post_to($u);

unless ( $ret{is_comm} ) {
Expand All @@ -85,37 +73,28 @@ sub get_security_options_handler {

$ret{minsecurity} = $u->newpost_minsecurity;

return $ret->( { ret => \%ret } );
return DW::RPC->out( ret => \%ret );
}

sub get_tags_handler {
my $r = DW::Request->get;
my $args = $r->get_args;

my $err = sub {
my $msg = shift;
$r->print( to_json({
'alert' => $msg,
}) );
return $r->OK;
};

my $remote = LJ::get_remote();
my $user = $args->{user};
my $u = LJ::load_user($user);
my $tags = $u ? $u->tags : {};

return $err->("You cannot view this journal's tags.") unless $remote && $remote->can_post_to($u);
return $err->("You cannot use this journal's tags.") unless $remote->can_add_tags_to($u);
return DW::RPC->alert( "You cannot view this journal's tags." ) unless $remote && $remote->can_post_to($u);
return DW::RPC->alert( "You cannot use this journal's tags." ) unless $remote->can_add_tags_to($u);

my @tag_names;
if (keys %$tags) {
@tag_names = map { $_->{name} } values %$tags;
@tag_names = sort { lc $a cmp lc $b } @tag_names;
}

$r->print( to_json({ tags => \@tag_names }) );
return $r->OK;
return DW::RPC->out( tags => \@tag_names );
}

1;
48 changes: 48 additions & 0 deletions cgi-bin/DW/RPC.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/perl
#
# Authors:
# Afuna <coder.dw@afunamatata.com>
#
# Copyright (c) 2014 by Dreamwidth Studios, LLC.
#
# This program is free software; you may redistribute it and/or modify it under
# the same terms as Perl itself. For a copy of the license, please reference
# 'perldoc perlartistic' or 'perldoc perlgpl'.

package DW::RPC;

use strict;
use LJ::JSON;

=head1 NAME
DW::RPC - Convenience methods to print output for RPC endpoints
=head1 SYNOPSIS
=cut

sub out {
my ( $class, %obj ) = @_;
my $r = DW::Request->get;
$r->print( to_json( \%obj ) );
return $r->OK;
}

# return error as { error => error or "" }
sub err {
my ( $class, $err ) = @_;
my $r = DW::Request->get;
$r->print( to_json( { error => $err ? $err : "" } ) );
return $r->OK;
}

# return error as { alert => ..., error => 1 }
sub alert {
my ( $class, $err ) = @_;
my $r = DW::Request->get;
$r->print( to_json( { alert => $err, error => 1 } ) );
return $r->OK;
}

1;

0 comments on commit 56ab94d

Please sign in to comment.