Skip to content

Commit

Permalink
add a /wms route that returns json
Browse files Browse the repository at this point in the history
This is a first, half-hapharded stab at
it, but maybe it can be useful to start
on jmacdotorg#8
  • Loading branch information
yanick committed Jul 17, 2020
1 parent 55adf1c commit 9402e34
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/Whim.pm
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ sub startup {
$r->get('/')->to('listen#default');
$r->post('/')->to('listen#receive');

$r->get('/wms')->to('display#json');
$r->get('/display_wms')->to('display#display');
$r->get('/summarize_wms')->to('display#summarize');

Expand Down
36 changes: 36 additions & 0 deletions lib/Whim/Controller/Display.pm
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
package Whim::Controller::Display;
use Mojo::Base 'Mojolicious::Controller';
use Whim::Mention;
use List::Util qw/ pairmap /;

use Readonly;
Readonly my $BAD_REQUEST => 400;

sub serialize_wm {
my $wm = shift;

my $data = $wm->TO_JSON;

# TO_JSON doesn't put all the yumminess
# in the hash, so I augment with the
# other stuff I want

if( my $author = $wm->author ) {
$data->{author} = {
map { $_ => $author->$_ } qw/ name url photo /
}
}

if( $wm->author_photo_hash ) {
$data->{author}{local_photo} =
'/author_photos/' . $wm->author_photo_hash;
}

return $data;
}

sub json {
my $self = shift;

return unless $self->_get_wms;

my %mentions = pairmap {
$a => [ map { serialize_wm($_) } @$b ]
} %{ $self->stash->{webmentions} };

$self->render( json => \%mentions );
}

sub display {
my $self = shift;

Expand Down

0 comments on commit 9402e34

Please sign in to comment.