fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-01-30 12:57 pm

[dw-free] new feeds pulling in entries in reverse order

[commit: http://hg.dwscoalition.org/dw-free/rev/c7ef5fa9e05c]

http://bugs.dwscoalition.org/show_bug.cgi?id=2623

Add an additional (reversed) sort by eventtime (user-provided time) when
different from logtime (system-provided). Not relying on jitemid sort,
because events may have come in in the wrong order (say an import or a
synsuck). Also add support parsing the entry time as dc:date in addition to
pubDate and some refactoring for handling things in either the nsdc or the
dc namespace. Finally, make sure that we order the feed items by datestamp
in synsuck, when applicable.

Patch by [personal profile] kareila.

Files modified:
  • cgi-bin/DW/Logic/LogItems.pm
  • cgi-bin/LJ/ParseFeed.pm
  • cgi-bin/LJ/SynSuck.pm
--------------------------------------------------------------------------------
diff -r 2a8cca4347d9 -r c7ef5fa9e05c cgi-bin/DW/Logic/LogItems.pm
--- a/cgi-bin/DW/Logic/LogItems.pm	Sun Jan 30 20:48:49 2011 +0800
+++ b/cgi-bin/DW/Logic/LogItems.pm	Sun Jan 30 20:57:31 2011 +0800
@@ -282,12 +282,13 @@ sub watch_items
 
             # sort all the total items by rlogtime (recent at beginning).
             # if there's an in-second tie, the "newer" post is determined by
+            # the later eventtime (if known/different from rlogtime), then by
             # the higher jitemid, which means nothing if the posts aren't in
             # the same journal, but means everything if they are (which happens
-            # almost never for a human, but all the time for RSS feeds, once we
-            # remove the synsucker's 1-second delay between postevents)
-            @items = sort { $a->{'rlogtime'} <=> $b->{'rlogtime'} ||
-                            $b->{'jitemid'}  <=> $a->{'jitemid'}     } @items;
+            # almost never for a human, but all the time for RSS feeds)
+            @items = sort { $a->{rlogtime}  <=> $b->{rlogtime}  ||
+                            $b->{eventtime} <=> $a->{eventtime} ||
+                            $b->{jitemid}   <=> $a->{jitemid}     } @items;
 
             # cut the list down to what we need.
             @items = splice(@items, 0, $getitems) if (@items > $getitems);
@@ -553,7 +554,7 @@ sub recent_items
                allowmask, eventtime, logtime
         FROM log2 USE INDEX ($sort_key)
         WHERE journalid=$userid $sql_select $secwhere $jitemidwhere $securitywhere $posterwhere
-        ORDER BY journalid, $sort_key
+        ORDER BY journalid, $sort_key, jitemid DESC
         $sql_limit
     };
 
diff -r 2a8cca4347d9 -r c7ef5fa9e05c cgi-bin/LJ/ParseFeed.pm
--- a/cgi-bin/LJ/ParseFeed.pm	Sun Jan 30 20:48:49 2011 +0800
+++ b/cgi-bin/LJ/ParseFeed.pm	Sun Jan 30 20:57:31 2011 +0800
@@ -104,7 +104,6 @@ sub parse_feed
         $item->{'text'} = $_->{'description'};
         $item->{'link'} = $_->{'link'} if $_->{'link'};
         $item->{'id'} = $_->{'guid'} if $_->{'guid'};
-        $item->{author} = $_->{dc}->{creator} if $_->{dc} && $_->{dc}->{creator};
 
         my $nsdc = 'http://purl.org/dc/elements/1.1/';
         my $nsenc = 'http://purl.org/rss/1.0/modules/content/';
@@ -114,17 +113,22 @@ sub parse_feed
                 if defined $_->{$nsenc}->{'encoded'};
         }
 
-        if ($_->{'pubDate'}) {
-            my $time = time822_to_time($_->{'pubDate'});
-            $item->{'time'} = $time if $time;
+        my ( $time, $dc_ref );
+        $time = time822_to_time( $_->{pubDate} ) if $_->{pubDate};
+
+        if ( $_->{$nsdc} && ref $_->{$nsdc} eq "HASH" ) {
+            $dc_ref = $_->{$nsdc};
+        } elsif ( $_->{dc} && ref $_->{dc} eq "HASH" ) {
+            $dc_ref = $_->{dc};
         }
-        if ($_->{$nsdc} && ref($_->{$nsdc}) eq "HASH") {
-            if ($_->{$nsdc}->{date}) {
-                my $time = w3cdtf_to_time($_->{$nsdc}->{date});
-                $item->{'time'} = $time if $time;
-            }
+
+        if ( defined $dc_ref ) {
+            $item->{author} = $dc_ref->{creator} if $dc_ref->{creator};
+            $time = w3cdtf_to_time( $dc_ref->{date} ) if $dc_ref->{date};
         }
-        push @{$feed->{'items'}}, $item;
+
+        $item->{time} = $time if $time;
+        push @{ $feed->{items} }, $item;
     }
 
     return ($feed, undef, $feed->{'items'});
diff -r 2a8cca4347d9 -r c7ef5fa9e05c cgi-bin/LJ/SynSuck.pm
--- a/cgi-bin/LJ/SynSuck.pm	Sun Jan 30 20:48:49 2011 +0800
+++ b/cgi-bin/LJ/SynSuck.pm	Sun Jan 30 20:57:31 2011 +0800
@@ -170,7 +170,14 @@ sub parse_items_from_feed {
     # another sanity check
     return ( 0, { type => "noitems" } ) unless ref $feed->{items} eq "ARRAY";
 
-    my @items = reverse @{$feed->{'items'}};
+    my @items = reverse @{ $feed->{items} } or
+        return ( 0, { type => "noitems" } );
+
+    # If the feed appears to be datestamped, resort chronologically,
+    # from earliest to latest - oldest entries are posted first, below.
+    my $timesort = sub { LJ::mysqldate_to_time( $_[0]->{time} ) };
+    @items = sort { $timesort->($a) <=> $timesort->($b) } @items
+        if $items[0]->{time};
 
     # take most recent 20
     splice( @items, 0, @items - $num ) if @items > $num;
--------------------------------------------------------------------------------

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org