cgi-bin/DW/Controller/RPC/CutExpander.pm
author fu
Fri Jan 27 13:51:52 2012 +0800
changeset 4239 555a213bdacc
parent 3553 bafea8052e02
permissions -rw-r--r--
http://bugs.dwscoalition.org/show_bug.cgi?id=4230

Fix all the bad my $var = ... if ... constructions.

Patch by sophie.
     1 #!/usr/bin/perl
     2 #
     3 # DW::Controller::RPC::CutExpander
     4 #
     5 # AJAX endpoint that returns the expanded text for a cut tag.
     6 #
     7 # Author:
     8 #      Allen Petersen
     9 #
    10 # Copyright (c) 2009-2011 by Dreamwidth Studios, LLC.
    11 #
    12 # This program is free software; you may redistribute it and/or modify it under
    13 # the same terms as Perl itself. For a copy of the license, please reference
    14 # 'perldoc perlartistic' or 'perldoc perlgpl'.
    15 #
    16 
    17 package DW::Controller::RPC::CutExpander;
    18 
    19 use strict;
    20 use warnings;
    21 use DW::Routing;
    22 use JSON;
    23 
    24 DW::Routing->register_string( "/__rpc_cuttag", \&cutexpander_handler, app => 1, user => 1, format => 'json' );
    25 
    26 sub cutexpander_handler {
    27     my $opts = shift;
    28 
    29     # gets the request and args
    30     my $r = DW::Request->get;
    31     my $args = $r->get_args;
    32 
    33     my $remote = LJ::get_remote();
    34 
    35     # error handler
    36     my $error_out = sub {
    37        my ( $code, $message ) = @_;
    38        $r->status( $code );
    39        $r->print( objToJson( { error => $message } ) );
    40 
    41        return $r->OK;
    42     };
    43 
    44     if ( $args->{ditemid} && $args->{journal} && $args->{cutid} ) {
    45         # all parameters are included; get the entry.
    46         my $ditemid = $args->{ditemid};
    47         my $uid = LJ::get_userid( $args->{journal} );
    48         my $entry = $uid ? LJ::Entry->new( $uid, ditemid => $ditemid ) : undef;
    49 
    50         # FIXME: This returns 200 due to old library, Make return proper when we are jQuery only.
    51         return $error_out->( 200, BML::ml( "error.nopermission" ) ) unless $entry;
    52         
    53         # make sure the user can read the entry
    54         if ( $entry->visible_to( $remote ) ) {
    55             my $text = load_cuttext( $entry, $remote, $args->{cutid} );
    56             # FIXME: temporary fix.
    57             # remove some unicode characters that could cause the returned JSON to break
    58             # like in LJ::ejs, but we don't need to escape quotes, etc (objToJson does that)
    59             $text =~ s/\xE2\x80[\xA8\xA9]//gs;
    60             $r->print( objToJson( { text => $text } ) );
    61             return $r->OK;
    62         }
    63     }
    64 
    65     # FIXME: This returns 200 due to old library, Make return proper when we are jQuery only.
    66     return $error_out->( 200, BML::ml( "error.nopermission" ) );
    67 }
    68 
    69 # loads the cutttext for the given entry
    70 sub load_cuttext {
    71     my ( $entry_obj, $remote, $cutid ) = @_;
    72 
    73     # most of this is taken from S2->Entry_from_entryobj, modified for this
    74     # more limited purpose.
    75     my $get = {};
    76     my $subject = "";
    77 
    78     my $anum = $entry_obj->anum;
    79     my $jitemid = $entry_obj->jitemid;
    80     my $ditemid = $entry_obj->ditemid;
    81 
    82     # $journal: journal posted to
    83     my $journalid = $entry_obj->journalid;
    84     my $journal = LJ::load_userid( $journalid );
    85 
    86     # is style=mine used?  or if remote has it on and this entry is not part of
    87     # their journal.  if either are yes, it needs to be added to comment links
    88     my %opt_stylemine = $remote && $remote->prop( 'opt_stylemine' ) && $remote->id != $journalid ? ( style => 'mine' ) : ();
    89     my $style_args = LJ::viewing_style_args( %$get, %opt_stylemine );
    90     
    91     #load and prepare text of entry
    92     my $text = LJ::CleanHTML::quote_html( $entry_obj->event_raw, $get->{nohtml} );
    93     LJ::item_toutf8( $journal, \$subject, \$text ) if $LJ::UNICODE && $entry_obj->props->{unknown8bit};
    94 
    95     my $suspend_msg = $entry_obj && $entry_obj->should_show_suspend_msg_to( $remote ) ? 1 : 0;
    96     # cleaning the entry text: cuts and such
    97     my $cleanhtml_opts = { cuturl => LJ::item_link( $journal, $jitemid, $anum, $style_args ),
    98         journal => $journal->username,
    99         ditemid => $ditemid,
   100         suspend_msg => $suspend_msg,
   101         unsuspend_supportid => $suspend_msg ? $entry_obj->prop( 'unsuspend_supportid' ) : 0,
   102         preformatted => $entry_obj->prop( "opt_preformatted" ),
   103         cut_retrieve => $cutid, 
   104     };
   105 
   106     LJ::CleanHTML::clean_event( \$text, $cleanhtml_opts );
   107 
   108     LJ::expand_embedded( $journal, $jitemid, $remote, \$text );
   109     
   110     return $text;
   111 }
   112 
   113 1;