Skip to content

Commit

Permalink
Merge branch 'oalders/fix-feed-urls'
Browse files Browse the repository at this point in the history
  • Loading branch information
tsibley committed Nov 20, 2016
2 parents 2eb4aa4 + fde1079 commit a56fcf3
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 21 deletions.
3 changes: 2 additions & 1 deletion cpanfile
Expand Up @@ -66,9 +66,10 @@ requires 'MetaCPAN::Role', '0.06';
requires 'MooseX::Types::Common::Numeric';
requires 'MooseX::Types::Common::String';
requires 'MooseX::Types::Moose';
requires 'MooseX::Types::URI';
requires 'MooseX::Types::URI', '0.08';
requires 'Net::Fastly', '1.05';
requires 'Path::Tiny', '0.076';
requires 'Params::CheckCompiler';
requires 'Plack', '1.0039';
requires 'Plack::Middleware::ReverseProxy';
requires 'Plack::Middleware::Runtime';
Expand Down
17 changes: 17 additions & 0 deletions cpanfile.snapshot
Expand Up @@ -4074,6 +4074,23 @@ DISTRIBUTIONS
JSON 0
Test::More 0
Test::Warn 0
Params-CheckCompiler-0.07
pathname: D/DR/DROLSKY/Params-CheckCompiler-0.07.tar.gz
provides:
Params::CheckCompiler 0.07
Params::CheckCompiler::Compiler 0.07
Params::CheckCompiler::Exceptions 0.07
requirements:
Eval::Closure 0
Exception::Class 0
Exporter 0
ExtUtils::MakeMaker 0
List::SomeUtils 0
Scalar::Util 0
Sub::Name 0
overload 0
strict 0
warnings 0
Params-Util-1.07
pathname: A/AD/ADAMK/Params-Util-1.07.tar.gz
provides:
Expand Down
75 changes: 59 additions & 16 deletions lib/MetaCPAN/Web/Controller/Feed.pm
Expand Up @@ -2,13 +2,17 @@ package MetaCPAN::Web::Controller::Feed;

use Moose;
use namespace::autoclean;
use feature qw( state );

BEGIN { extends 'MetaCPAN::Web::Controller' }
use XML::Feed;

use DateTime::Format::ISO8601 ();
use HTML::Escape qw/escape_html/;
use DateTime::Format::ISO8601;
use MetaCPAN::Web::Types qw( ArrayRef HashRef Str Uri );
use Params::CheckCompiler qw( validation_for );
use Path::Tiny qw/path/;
use Text::Markdown qw/markdown/;
use XML::Feed ();

use Importer 'MetaCPAN::Web::Elasticsearch::Adapter' =>
qw/ single_valued_arrayref_to_scalar /;
Expand All @@ -23,10 +27,10 @@ sub recent : Chained('feed_index') PathPart Args(0) {
# Set surrogate key and ttl from here as well
$c->forward('/recent/index');

my $data = $c->stash;
$c->stash->{feed} = $self->build_feed(
entries => $c->stash->{recent},
host => URI->new( $c->config->{web_host} ),
title => 'Recent CPAN uploads - MetaCPAN',
entries => $data->{recent}
);
}

Expand Down Expand Up @@ -54,9 +58,10 @@ sub news : Local : Args(0) {
$a_name =~ s/^[^a-z]+//gi;
$str =~ s/\A\s*-+//g;
$e{date} = $str =~ s/^Date:\s*(.*)$//m ? $1 : '2014-01-01T00:00:00';
$e{link} = "/news#$a_name";
$e{author} = 'METACPAN';
$e{date} = $str =~ s/^Date:\s*(.*)$//m ? $1 : '2014-01-01T00:00:00';
$e{link} = '/news';
$e{fragment} = $a_name;
$e{author} = 'METACPAN';
$str =~ s/^\s*|\s*$//g;
#$str =~ s{\[([^]]+)\]\(([^)]+)\)}{<a href="$2">$1</a>}g;
Expand All @@ -67,8 +72,9 @@ sub news : Local : Args(0) {
}

$c->stash->{feed} = $self->build_feed(
title => 'Recent MetaCPAN News',
entries => \@entries,
host => $c->config->{web_host},
title => 'Recent MetaCPAN News',
);
}

Expand Down Expand Up @@ -106,6 +112,14 @@ sub author : Local : Args(1) {
];
my $author_info = $author_cv->recv;

# If the author can be found, we get the hashref of author info. If it
# can't be found, we (confusingly) get a HashRef with "code" and "message"
# keys.

if ( $author_info->{code} && $author_info->{code} == 404 ) {
$c->detach( '/not_found', [] );
}

my $faves_cv = $author_info->{user}
&& $c->model('API::Favorite')->by_user( $author_info->{user} );
my $faves_data
Expand All @@ -117,6 +131,7 @@ sub author : Local : Args(1) {
: [];

$c->stash->{feed} = $self->build_feed(
host => $c->config->{web_host},
title => "Recent CPAN activity of $author - MetaCPAN",
entries => [
sort { $b->{date} cmp $a->{date} }
Expand All @@ -135,6 +150,7 @@ sub distribution : Local : Args(1) {

my $data = $c->model('API::Release')->versions($distribution)->recv;
$c->stash->{feed} = $self->build_feed(
host => $c->config->{web_host},
title => "Recent CPAN uploads of $distribution - MetaCPAN",
entries => [
map { single_valued_arrayref_to_scalar($_) }
Expand All @@ -144,26 +160,53 @@ sub distribution : Local : Args(1) {
}

sub build_entry {
my ( $self, $entry ) = @_;
single_valued_arrayref_to_scalar($entry);
my $self = shift;

state $check = validation_for(
params => {
entry => { type => HashRef },
host => { type => Uri },
}
);

my %args = $check->(@_);
my $entry = $args{entry};

my $e = XML::Feed::Entry->new('RSS');
$e->title( $entry->{name} );
$e->link( $entry->{link}
||= join( q{/}, 'release', $entry->{author}, $entry->{name} ) );

my $link = $args{host}->clone;
$link->path( $entry->{link}
|| join( q{/}, 'release', $entry->{author}, $entry->{name} ) );
$link->fragment( $entry->{fragment} ) if $entry->{fragment}; # for news
$e->link( $link->as_string );

$e->author( $entry->{author} );
$e->issued( DateTime::Format::ISO8601->parse_datetime( $entry->{date} ) );
$e->summary( escape_html( $entry->{abstract} ) );
$e->title( $entry->{name} );
return $e;
}

sub build_feed {
my ( $self, %params ) = @_;
my $self = shift;

state $check = validation_for(
params => {
entries => { type => ArrayRef },
host => { type => Uri, optional => 0, },
title => Str,
}
);

my %params = $check->(@_);

my $feed = XML::Feed->new( 'RSS', version => 2.0 );
$feed->title( $params{title} );
$feed->link('/');
foreach my $entry ( @{ $params{entries} } ) {

$feed->add_entry( $self->build_entry($entry) );
foreach my $entry ( @{ $params{entries} } ) {
$feed->add_entry(
$self->build_entry( entry => $entry, host => $params{host} ) );
}
return $feed->as_xml;
}
Expand Down
1 change: 1 addition & 0 deletions metacpan_web.conf
Expand Up @@ -6,6 +6,7 @@ default_view HTML
api_secure = https://fastapi.metacpan.org
api_external_secure = https://fastapi.metacpan.org
source_host = https://st.aticpan.org
web_host = https://metacpan.org
consumer_key = metacpan.dev
cookie_secret = seekrit
consumer_secret = ClearAirTurbulence
Expand Down
14 changes: 10 additions & 4 deletions t/controller/feed.t
@@ -1,10 +1,11 @@
use strict;
use warnings;
use Test::More;

use MetaCPAN::Web ();
use MetaCPAN::Web::Controller::Feed ();
use MetaCPAN::Web::Test;
use Try::Tiny;
use MetaCPAN::Web;
use MetaCPAN::Web::Controller::Feed;
use Test::More;
use Try::Tiny qw( catch try );

sub get_feed_ok {
my ( $cb, $test, $extra ) = @_;
Expand Down Expand Up @@ -104,6 +105,11 @@ test_psgi app, sub {
);

test_redirect( $cb, 'oalders' );

subtest '404' => sub {
my $res = $cb->( GET '/feed/author/XXX343wi^^^' );
is( $res->code, 404, '404 when author does not exist' );
};
};

sub test_redirect {
Expand Down

0 comments on commit a56fcf3

Please sign in to comment.