Андрей (andy) wrote in changelog,
Андрей
andy
changelog

[ljcom] r9913: LJINT-362 (Comments for side projects)

Committer: ailyin
LJINT-362 (Comments for side projects)
U   trunk/cgi-bin/LJ/PartnerSite.pm
U   trunk/htdocs/tools/endpoints/comments.bml
Modified: trunk/cgi-bin/LJ/PartnerSite.pm
===================================================================
--- trunk/cgi-bin/LJ/PartnerSite.pm	2011-01-14 09:14:29 UTC (rev 9912)
+++ trunk/cgi-bin/LJ/PartnerSite.pm	2011-01-14 10:02:55 UTC (rev 9913)
@@ -5,6 +5,7 @@
 use Digest::MD5 qw();
 use URI         qw();
 use Class::Accessor;
+use LJ::Auth;
 
 use base qw( Class::Accessor );
 
@@ -76,14 +77,22 @@
 }
 
 sub find_entry_by_docid {
-    my ( $self, $docid ) = @_;
+    my ( $self, $docid, $opts ) = @_;
 
     $docid ||= '';
+    $opts ||= {};
 
     my $memc_key = 'extarticlesmap_jitemid:' . $self->id . ':' . $docid;
 
-    if ( my $jitemid = LJ::MemCache::get($memc_key) ) {
+    my $jitemid = LJ::MemCache::get($memc_key);
+
+    if ($jitemid) {
+        # an entry exists, we pulled it from memcache, so let's return
         return LJ::Entry->new( $self->journal, 'jitemid' => $jitemid );
+    } elsif ( ( defined $jitemid ) and $opts->{'nocreate'} ) {
+        # memcache says that there is no entry, and we don't wish
+        # to create one, so let's return undef
+        return;
     }
 
     my $dbr = LJ::get_db_reader();
@@ -93,10 +102,14 @@
         WHERE partnerid = ? AND docid_hash = ? AND docid = ?
     }, undef, $self->id, _hashfunc($docid), $docid );
 
-    my $jitemid;
-
     if ($row) {
         $jitemid = $row->{'jitemid'};
+    } elsif ( $opts->{'nocreate'} ) {
+        # database didn't find an entry for us, and we don't wish to create
+        # one, so let's return undef, but first, memcache that there
+        # is no entry matching this docid
+        LJ::MemCache::set( $memc_key, 0 );
+        return;
     } else {
         # there is no entry, let's create one
         my $link = $self->article_link($docid);
@@ -271,4 +284,161 @@
 
 }
 
+# note that this function returns a list, not a scalar
+# opts: entry, remote, journal
+sub _convert_comments_info {
+    my ( $self, $comments, $opts ) = @_;
+
+    return unless $comments and ( ref $comments eq 'ARRAY' );
+
+    my $entry = $opts->{'entry'};
+    my $remote = $opts->{'remote'};
+
+    $opts->{'journal'} ||= $entry->journal;
+    my $journal = $opts->{'journal'};
+
+    my $jtalkid_to_dtalkid = sub {
+        my ($jtalkid) = @_;
+
+        return 0 unless $jtalkid;
+        return $jtalkid * 256 + $entry->anum;
+    };
+
+    my @ret;
+
+    foreach my $comment ( @$comments ) {
+        my $poster = $comment->{'upost'};
+
+        my $dtalkid  = $jtalkid_to_dtalkid->( $comment->{'talkid'} );
+        my $parentid = $jtalkid_to_dtalkid->( $comment->{'parenttalkid'} );
+
+        my $subject  = LJ::ehtml( $comment->{'subject'} );
+
+        my $text     = $comment->{'body'};
+        my $cleaner_opts = {
+            'preformatted' => $comment->{'props'}->{'opt_preformatted'},
+            'anon_comment' => ( !$poster or $poster->is_identity ),
+        };
+        LJ::CleanHTML::clean_comment(\$text, $cleaner_opts);
+
+        my %userinfo;
+        if ($poster) {
+            my $userpic_url = '';
+            if ( my $picid = $comment->{'picid'} ) {
+                $userpic_url = "$LJ::USERPIC_ROOT/$picid/" . $poster->userid;
+            }
+
+            my $can_delete
+                = LJ::Talk::can_delete( $remote, $journal, $journal,
+                                        $poster->username );
+
+            %userinfo = (
+                'username'      => $poster->username,
+                'display_name'  => $poster->display_name,
+                'journal_url'   => $poster->journal_base . '/',
+                'profile_url'   => $poster->profile_url,
+                'posters_jtype' => $poster->journaltype,
+                'usericon'      => $userpic_url,
+                'can_delete'    => $can_delete,
+            );
+        } else {
+            %userinfo = (
+                'username'      => '',
+                'can_delete'    => 0,
+            );
+        }
+
+        if ( $poster and $poster->is_identity ) {
+            my $id = $poster->identity;
+
+            $userinfo{'identity'} = {
+                'typeid'        => $id->typeid,
+                'shortcode'     => $id->short_code,
+                'name'          => $id->display_name($poster),
+                'url'           => $id->url($poster),
+
+                'ljuser_display_params'
+                    => $id->ljuser_display_params($poster),
+            };
+        }
+
+        push @ret, {
+            'comment_id' => $dtalkid,
+            'parent_id'  => $parentid,
+
+            'state'      => $comment->{'state'},
+            'is_open'    => ( defined $text and $text ne '' ) ? 1 : 0,
+            'timestamp'  => $comment->{'datepost_unix'},
+
+            'subject'    => $subject,
+            'text'       => $text,
+
+            %userinfo,
+        };
+
+        push @ret,
+            $self->_convert_comments_info( $comment->{'children'}, $opts );
+    }
+
+    return @ret;
+}
+
+# opts: thread, view, page, remote
+sub get_comments_info {
+    my ( $self, $docid, $opts ) = @_;
+
+    $opts ||= {};
+
+    my $journal = $self->journal;
+    my $entry   = $self->find_entry_by_docid( $docid, { 'nocreate' => 1 } );
+    my $jitemid = $entry->jitemid;
+
+    unless ($entry) {
+        # no entry means no comments
+        return {
+            'comments'      => [],
+            'pages'         => undef,
+            'page'          => undef,
+            'journal'       => $journal->username,
+            'auth_token'    => '',
+        };
+    }
+
+    my $remote = $opts->{'remote'};
+
+    my $load_comments_struct = { thread     => $opts->{'thread'},
+                                 view       => $opts->{'view'},
+                                 page       => $opts->{'page'},
+                                 userpicref => {},
+                                 userref    => {},
+                                 init_comobj => 0, };
+
+    my @comments
+        = LJ::Talk::load_comments( $journal, $remote, "L", $jitemid,
+                                   $load_comments_struct );
+
+    my @comments_out = $self->_convert_comments_info( \@comments, {
+        'journal' => $journal,
+        'entry'   => $entry,
+        'remote'  => $remote,
+    });
+
+    my %vars = (
+        'journal' => $journal->username,
+        'jitemid' => $entry->jitemid,
+    );
+
+    my $auth_token = LJ::Auth->ajax_auth_token(
+        $remote, "/delcomment.bml", %vars
+    );
+
+    return {
+        'comments'      => \@comments_out,
+        'pages'         => $load_comments_struct->{'out_pages'},
+        'page'          => $load_comments_struct->{'out_page'},
+        'journal'       => $journal->username,
+        'auth_token'    => $auth_token,
+    };
+}
+
 1;

Modified: trunk/htdocs/tools/endpoints/comments.bml
===================================================================
--- trunk/htdocs/tools/endpoints/comments.bml	2011-01-14 09:14:29 UTC (rev 9912)
+++ trunk/htdocs/tools/endpoints/comments.bml	2011-01-14 10:02:55 UTC (rev 9913)
@@ -71,101 +71,13 @@
     return $answer->( { 'error' => 'rate limit exceeded' } )
         unless LJ::RateLimit->check( $journal, $partner->rate_limits );
 
-    my $entry = $partner->find_entry_by_docid($docid);
-    my $jitemid = $entry->jitemid;
-
-    my $userpics = {};
-    my %user = ();
-
-    my $opts = {
-        thread     => int ($thread / 256),
-        view       => $view,
-        page       => $page,
-        userpicref => $userpics,
-        userref    => \%user,
-        init_comobj => 0,
-    };
-
-    my $remote = LJ::get_remote();
-
-    ## load data
-    my @comments = LJ::Talk::load_comments($journal, $remote, "L", $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} * 256 + $entry->anum;
-                $data->{parent_id}      =
-                    $comment->{parenttalkid}
-                        ? $comment->{parenttalkid} * 256 + $entry->anum
-                        : 0 ;
-
-                $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);
-                }
-
-                $data->{'can_delete'} = LJ::Talk::can_delete( $remote,
-                                                              $journal,
-                                                              $journal,
-                                                              $pu->username );
-
-                ## resulting array
-                ($data, $self->($self, $_->{children} ) )
-            } @$comments;
-    };
-
-    my %vars = (
-        'journal' => $journal->username,
-        'jitemid' => $entry->jitemid,
+    my $data = $partner->get_comments_info(
+        $docid,
+        { 'thread' => int ($thread / 256),
+          'view'   => $view,
+          'page'   => $page,
+          'remote' => LJ::get_remote(), }
     );
 
-    my $auth_token = LJ::Auth->ajax_auth_token(
-        $remote, "/delcomment.bml", %vars
-    );
-
-    my @result = $to_export_format->($to_export_format, \@comments);
-    return $answer->({
-        'comments'      => \@result,
-        'pages'         => $opts->{'out_pages'},
-        'page'          => $opts->{'out_page'},
-        'journal'       => $journal->username,
-        'auth_token'    => $auth_token,
-    });
-
+    return $answer->($data);
 } _code?>

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