From a9679f8c832616a57d59409e3ff33120522b7900 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 17 Jul 2020 12:51:44 -0400 Subject: [PATCH] add a /wms route that returns json This is a first, half-hapharded stab at it, but maybe it can be useful to start on #8 --- lib/Whim.pm | 1 + lib/Whim/Controller/Display.pm | 38 +++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/Whim.pm b/lib/Whim.pm index 8c83d99..94cf28e 100644 --- a/lib/Whim.pm +++ b/lib/Whim.pm @@ -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'); diff --git a/lib/Whim/Controller/Display.pm b/lib/Whim/Controller/Display.pm index 8b91b86..20d693e 100644 --- a/lib/Whim/Controller/Display.pm +++ b/lib/Whim/Controller/Display.pm @@ -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; @@ -27,7 +63,7 @@ sub _get_wms { my $url = $self->param('url'); unless ($url) { - $self->render( + $self->render( status => $BAD_REQUEST, text => 'No "url" parameter found.', );