vadvs (vadvs) wrote in changelog,
vadvs
vadvs
changelog

[ljcom] r9743: LJINT-363: Comments for side projects

Committer: vsukhanov
LJINT-363: Comments for side projects
A   trunk/htdocs/tools/endpoints/comments.bml
Added: trunk/htdocs/tools/endpoints/comments.bml
===================================================================
--- trunk/htdocs/tools/endpoints/comments.bml	                        (rev 0)
+++ trunk/htdocs/tools/endpoints/comments.bml	2010-11-22 09:16:56 UTC (rev 9743)
@@ -0,0 +1,116 @@
+<?_code {
+    ## return a comments to journal entry based on external uniq id
+    ## 
+    use strict;
+    use Data::Dumper;
+    use LJ::ExternalComments;
+
+    use vars qw/%GET/;
+
+    my $url     = $GET{url};
+    my $rskey   = $GET{rsk};
+    my $page    = int $GET{page} || 1;
+    my $thread  = int $GET{thread} || undef;
+
+    ## wrapper and serialazer.
+    my $answer = sub {
+        my $data = shift;
+        return 'JSONP(' . LJ::JSON->to_json($data) . ')';
+    };
+
+    ## convert remote-site key to lj.com's community
+    my $username = LJ::ExternalComments->key_to_username($rskey);
+    return $answer->({error => "unknown key"})
+        unless $username;
+
+    my $journal = LJ::load_user($username);
+    return $answer->({error => "unknown user"})
+        unless $journal;
+
+    ## convert article's uri on partners site to entryid on LJ.com
+    my $jitemid = LJ::ExternalComments->url_to_jitemid($journal, $url);
+    return $answer->({ comments => [] }) 
+        unless $jitemid; ## url that has no mapping to entry has no comments too.
+
+    # my $ditemid = 17604;
+
+    my $entry = LJ::Entry->new($journal, jitemid => $jitemid);
+
+    my $userpics = {};
+    my %user = ();
+
+    my $opts = {
+        thread     => $thread,
+        #view       => $view_num,
+        page       => $page,
+        userpicref => $userpics,
+        userref    => \%user,
+        #up         => $up,
+        init_comobj => 0,
+    };
+
+    ## this endpoint always assumes that remote user is not logged in.
+    my $remote = undef;
+
+    ## load data
+    my @comments = LJ::Talk::load_comments($journal, $remote, "L", $entry->jitemid, $opts);
+
+    ## convert into new structure
+    my $to_export_format = sub {
+        my ($self, $comments) = @_;
+        return unless ref $comments eq 'ARRAY';
+        return 
+            map { 
+                my $comment = $_;
+                ##
+                my $text = $comment->{body};
+                my $pu   = $comment->{upost};
+                LJ::CleanHTML::clean_comment(\$text, { 'preformatted' => $comment->{'props'}->{'opt_preformatted'},
+                                                       'anon_comment' => (!$pu || $pu->{'journaltype'} eq 'I'),
+                                                        });
+                ##
+                my $data = {};
+                $data->{comment_id}     = $comment->{talkid};
+                $data->{parent_id}      = $comment->{parenttalkid};
+
+                $data->{username}       = $comment->{userpost} || '';
+                if ($pu){
+                    $data->{usericon}      = "$LJ::USERPIC_ROOT/$comment->{picid}/" . $pu->userid 
+                        if $comment->{picid};
+                    $data->{display_name}  = $pu->display_name;
+                    $data->{journal_url}   = $pu->journal_base . "/";
+                    $data->{profile_url}   = $pu->profile_url;
+                    $data->{posters_jtype} = $pu->journaltype;
+                }
+                $data->{timestamp}      = $comment->{datepost_unix};
+
+                $data->{subject}        = $comment->{subject},
+                $data->{text}           = $text;
+
+                $data->{is_open}        = $comment->{body} ? 1 : 0;
+                $data->{state}          = $comment->{state};
+
+                ## Provide identity info
+                if ($pu and my $ident = $pu->identity){
+                    $data->{identity} = {};
+                    $data->{identity}->{typeid}     = $ident->typeid;
+                    $data->{identity}->{short_code} = $ident->short_code;
+                    $data->{identity}->{name}       = $ident->display_name($pu);
+                    $data->{identity}->{ljuser_display_params}
+                                                    = $ident->ljuser_display_params($pu);
+                    $data->{identity}->{url}        = $ident->url($pu);
+                }
+
+                ## resutlted array
+                ($data, $self->($self, $_->{children} ) )
+            } @$comments;
+    };
+
+
+    my @result = $to_export_format->($to_export_format, \@comments);
+    return $answer->({ 
+        comments => \@result,
+        pages    => $opts->{out_pages},
+        });
+
+} _code?>

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