3 # DW::Controller::RPC::CutExpander
5 # AJAX endpoint that returns the expanded text for a cut tag.
10 # Copyright (c) 2009-2011 by Dreamwidth Studios, LLC.
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'.
17 package DW::Controller::RPC::CutExpander;
24 DW::Routing->register_string( "/__rpc_cuttag", \&cutexpander_handler, app => 1, user => 1, format => 'json' );
26 sub cutexpander_handler {
29 # gets the request and args
30 my $r = DW::Request->get;
31 my $args = $r->get_args;
33 my $remote = LJ::get_remote();
37 my ( $code, $message ) = @_;
39 $r->print( objToJson( { error => $message } ) );
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;
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;
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 } ) );
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" ) );
69 # loads the cutttext for the given entry
71 my ( $entry_obj, $remote, $cutid ) = @_;
73 # most of this is taken from S2->Entry_from_entryobj, modified for this
74 # more limited purpose.
78 my $anum = $entry_obj->anum;
79 my $jitemid = $entry_obj->jitemid;
80 my $ditemid = $entry_obj->ditemid;
82 # $journal: journal posted to
83 my $journalid = $entry_obj->journalid;
84 my $journal = LJ::load_userid( $journalid );
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 );
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};
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,
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,
106 LJ::CleanHTML::clean_event( \$text, $cleanhtml_opts );
108 LJ::expand_embedded( $journal, $jitemid, $remote, \$text );