Refactor post fetching in rss.cgi
[blerg.git] / rss.cgi
diff --git a/rss.cgi b/rss.cgi
index d7abe3d..11e1982 100755 (executable)
--- a/rss.cgi
+++ b/rss.cgi
@@ -56,6 +56,30 @@ Not Found
 DOC
 }
 
+sub fetch_records {
+    my @out;
+    local $_;
+
+    foreach (@_) {
+        my $b = Blerg::Database->open_existing($_->{author});
+        my $data = $b->fetch($_->{record});
+        if (!defined $data) {
+            $b->close;
+            next;
+        }
+        my $timestamp = $b->timestamp($_->{record});
+        $b->close;
+        push @out, {
+            author => $_->{author},
+            record => $_->{record},
+            data => $data,
+            timestamp => $timestamp,
+        };
+    }
+
+    return @out;
+}
+
 sub print_rss {
     my ($type, $name, @items) = @_;
 
@@ -119,21 +143,9 @@ while (my $q = new CGI::Fast) {
             next REQUEST;
         }
 
-        my $bb = Blerg::Database->open_existing($username);
-        my @list = $bb->subscription_list();
-        $bb->close;
-        @list = map {
-            my $b = Blerg::Database->open_existing($_->{author});
-            my $data = $b->fetch($_->{record});
-            my $timestamp = $b->timestamp($_->{record});
-            $b->close;
-            {
-                author => $_->{author},
-                record => $_->{record},
-                data => $data,
-                timestamp => $timestamp,
-            };
-        } @list;
+        my $b = Blerg::Database->open_existing($username);
+        my @list = fetch_records($b->subscription_list());
+        $b->close;
 
         print header(-type => 'application/rss+xml');
         print_rss(feed => $username, @list);
@@ -152,7 +164,7 @@ while (my $q = new CGI::Fast) {
         # And present the content in case their client is broken
         my $i = {
             record => '?failed_redirect',
-               timestamp => time,
+            timestamp => time,
             data => qq{Your RSS aggregator is dumb and isn't following 301 redirects.  Please manually redirect it here: ${baseurl}rss/user/$username}
         };
         print_rss(user => $username, $i);
@@ -183,19 +195,7 @@ while (my $q = new CGI::Fast) {
             $atag = '@' . $tag;
         }
 
-        my @list = Blerg::Database::tag_list($atag, 0, -1);
-        @list = map {
-            my $b = Blerg::Database->open_existing($_->{author});
-            my $data = $b->fetch($_->{record});
-            my $timestamp = $b->timestamp($_->{record});
-            $b->close;
-            {
-                author => $_->{author},
-                record => $_->{record},
-                data => $data,
-                timestamp => $timestamp,
-            };
-        } @list;
+        my @list = fetch_records(Blerg::Database::tag_list($atag, 0, -1));
 
         print header(-type => 'application/rss+xml');
         print_rss($path[0] => $atag, @list);