Committer: vsukhanov
LJM-1447: added special mode to sup-rpc command "comment_tree" that allows m.livejournal.com to load only needed for mobile version comments.U trunk/cgi-bin/LJ/SUP/RPC/Command/comments_tree.pm
Modified: trunk/cgi-bin/LJ/SUP/RPC/Command/comments_tree.pm =================================================================== --- trunk/cgi-bin/LJ/SUP/RPC/Command/comments_tree.pm 2010-09-20 08:49:04 UTC (rev 9516) +++ trunk/cgi-bin/LJ/SUP/RPC/Command/comments_tree.pm 2010-09-20 09:45:58 UTC (rev 9517) @@ -39,21 +39,44 @@ my $userid = defined ($params->{userid}) ? int ($params->{userid}) : undef; # -- // -- my $view = $params->{view}; + my $flat = defined ($params->{flat}) ? int $params->{flat} : 0; # disabled by default + my $flat_if_more = $params->{flat_if_more} > 1 ? int $params->{flat_if_more} : undef; + return { error => 'No ditemid' } unless $ditemid; + ## Return some comments special for m.livejournal.com + ## It needs limited data, so lets avoid overhead. + if ($params->{mode} eq 'm.lj'){ + return + m_lj({ + journal => $journal, + ditemid => $ditemid, + thread => $thread, + page => $page, + userid => $userid, + view => $view, + flat => $flat, + }); + } + # Display page according the remote user's rights my $for_user = $userid ? LJ::load_userid($userid) : undef; + my $entry = LJ::Entry->new($journal, ditemid => $ditemid); my $opts = { - ljentry => LJ::Entry->new($journal, ditemid => $ditemid), - getargs => { page => $page, thread => $thread, view => $view }, + ljentry => $entry, + getargs => { page => $page, thread => $thread, view => $view, }, r => LJ::Request->r, }; + ## Enable flat mode directly or for entries with large amount of comments. + my $flatten = 0; + $flatten = 1 if $flat; + $flatten = 1 if $flat_if_more and $entry->reply_count > $flat_if_more; + $opts->{getargs}->{view} = 'flat' if $flatten; - # load page data my $page = LJ::S2::EntryPage($journal, $for_user, $opts); @@ -113,8 +136,93 @@ $res->{$key} = delete $page->{$key}; # move data from one hash to another } + ## + $res->{flatten} = 1 if $flatten; return $res; } +sub m_lj { + my $args = shift; + + my $journal = $args->{journal}; + my $u = LJ::load_userid($args->{userid}); + my $ditemid = $args->{ditemid}; + + my $opts = { + flat => $args->{flat}, + thread => $args->{thread}, + page => $args->{page}, + page_size => ($args->{page_size} || 5), + expand_strategy => 'by_level', + expand_level => 3, + view => undef, + viewall => 0, + init_comobj => 0, + }; + + my $nodeid = $ditemid >> 8; + my $anum = $ditemid % 256; + my @comments = LJ::Talk::load_comments($journal, ($u || undef), 'L', $nodeid, $opts); + + ## normalize response format + my $normalize = sub { + my $sub = shift; + my $comment = shift; + my $text = $comment->{body}; + #my $pu = $comment->{posterid} ? $user{$com->{posterid}} : undef; + my $pu = undef; + LJ::CleanHTML::clean_comment(\$text, { 'preformatted' => $comment->{'props'}->{'opt_preformatted'}, + 'anon_comment' => (!$pu || $pu->{'journaltype'} eq 'I'), + }); + return { + talkid => $comment->{talkid} * 256 + $anum, + subject => $comment->{subject}, + text => $text, + datepost_unix => $comment->{datepost_unix}, + poster => { journal_type => $comment->{upost}->{journaltype}, + username => $comment->{upost}->{name} + }, + posterid => $comment->{posterid}, + state => $comment->{state}, + time => DateTime_unix($comment->{datepost_unix}), + deleted => $comment->{state} eq 'D', + screened => $comment->{state} eq 'S', + + replies => [ map { $sub->($sub, $_) } @{ $comment->{children} } ], + }; + }; + ## map comments info to accomplish identity with common data-structure. + foreach my $comment (@comments){ + $comment = $normalize->($normalize, $comment); + } + + return { + journal_type => $journal->journaltype, + replies => $opts->{out_items}, + pages => $opts->{out_pages}, + comments => \@comments, + }; + +} + +## taken from LJ::S2 +sub DateTime_unix +{ + my $time = shift; + my @gmtime = gmtime($time); + my $dt = { '_type' => 'DateTime' }; + $dt->{'year'} = $gmtime[5]+1900; + $dt->{'month'} = $gmtime[4]+1; + $dt->{'day'} = $gmtime[3]; + $dt->{'hour'} = $gmtime[2]; + $dt->{'min'} = $gmtime[1]; + $dt->{'sec'} = $gmtime[0]; + $dt->{'_dayofweek'} = $gmtime[6] + 1; + return $dt; +} + + + + 1;