Skip to content

Commit

Permalink
Merge pull request #2996 from metacpan/haarg/accepts-not-detect
Browse files Browse the repository at this point in the history
use accepts header, not ancient browser detection
  • Loading branch information
haarg authored Feb 29, 2024
2 parents 6f302c3 + 6e11118 commit cea5906
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ requires 'Catalyst::Model';
requires 'Catalyst::Plugin::Authentication';
requires 'Catalyst::Plugin::ConfigLoader';
requires 'Catalyst::Plugin::Static::Simple';
requires 'Catalyst::TraitFor::Request::REST::ForBrowsers';
requires 'Catalyst::TraitFor::Request::REST';
requires 'Catalyst::View::JSON';
requires 'Catalyst::View::Xslate';
requires 'CatalystX::Fastly::Role::Response', '0.06';
Expand Down
2 changes: 1 addition & 1 deletion lib/MetaCPAN/Web.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extends 'Catalyst';

__PACKAGE__->request_class_traits( [ qw(
MetaCPAN::Web::Role::Request
Catalyst::TraitFor::Request::REST::ForBrowsers
Catalyst::TraitFor::Request::REST
) ] );

__PACKAGE__->response_class_traits( [ qw(
Expand Down
14 changes: 8 additions & 6 deletions lib/MetaCPAN/Web/Controller/Account/Favorite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ BEGIN { extends 'MetaCPAN::Web::Controller' }

sub add : Local : Args(0) {
my ( $self, $c ) = @_;
$c->stash( { current_view => 'JSON' } );
my $json = $c->req->accepts('application/json');
$c->stash( { current_view => 'JSON' } )
if $json;
$c->detach('/forbidden') unless ( $c->req->method eq 'POST' );
my $user = $c->user;
$c->detach('/forbidden') unless $user;
Expand All @@ -24,17 +26,17 @@ sub add : Local : Args(0) {
$c->purge_author_key( $data->{author} ) if $data->{author};
$c->purge_dist_key( $data->{distribution} ) if $data->{distribution};

if ( $c->req->looks_like_browser ) {
if ($json) {
$c->res->code(400) if ( $res->{error} );
$c->stash->{json}{success} = $res->{error} ? \0 : \1;
}
else {
$c->res->redirect(
$res->{error}
? $c->req->referer
: $c->uri_for('/account/turing/index')
);
}
else {
$c->res->code(400) if ( $res->{error} );
$c->stash->{json}{success} = $res->{error} ? \0 : \1;
}
}

sub list : Local : Args(0) {
Expand Down
1 change: 1 addition & 0 deletions root/static/js/cpan.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ function favDistribution(form) {
type: 'POST',
url: form.attr('action'),
data: data,
dataType: 'json',
success: function() {
var button = form.find('button');
button.toggleClass('active');
Expand Down

0 comments on commit cea5906

Please sign in to comment.