diff --git a/lib/XRay/Formats/JSONFeed.php b/lib/XRay/Formats/JSONFeed.php index e3110d6..f47e88a 100644 --- a/lib/XRay/Formats/JSONFeed.php +++ b/lib/XRay/Formats/JSONFeed.php @@ -40,29 +40,29 @@ class JSONFeed extends Format { ] ]; - if(isset($item['author']['name'])) { + // First use the feed title/icon/url as author info + $entry['author']['url'] = $feed['home_page_url']; + if(isset($feed['title'])) + $entry['author']['name'] = $feed['title']; + if(isset($feed['icon'])) + $entry['author']['photo'] = $feed['icon']; + + // Override the author if the item contains author info + if(isset($item['author']['name'])) $entry['author']['name'] = $item['author']['name']; - } - if(isset($item['author']['url'])) { + if(isset($item['author']['url'])) $entry['author']['url'] = $item['author']['url']; - } elseif(isset($feed['home_page_url'])) { - $entry['author']['url'] = $feed['home_page_url']; - } - if(isset($item['author']['avatar'])) { + if(isset($item['author']['avatar'])) $entry['author']['photo'] = $item['author']['avatar']; - } - if(isset($item['url'])) { + if(isset($item['url'])) $entry['url'] = $item['url']; - } - if(isset($item['id'])) { + if(isset($item['id'])) $entry['uid'] = $item['id']; - } - if(isset($item['title']) && trim($item['title'])) { + if(isset($item['title']) && trim($item['title'])) $entry['name'] = trim($item['title']); - } $baseURL = isset($entry['url']) ? $entry['url'] : $feedurl; diff --git a/tests/FeedTest.php b/tests/FeedTest.php index 1369521..16edf46 100644 --- a/tests/FeedTest.php +++ b/tests/FeedTest.php @@ -264,6 +264,25 @@ class FeedTest extends PHPUnit_Framework_TestCase { $this->assertEquals('feed', $data->type); } + public function testJSONFeedFallbackAuthor() { + $url = 'http://feed.example.com/jsonfeed-author'; + $response = $this->parse(['url' => $url, 'expect' => 'feed']); + + $body = $response->getContent(); + $this->assertEquals(200, $response->getStatusCode()); + $result = json_decode($body); + $this->assertEquals('feed+json', $result->{'source-format'}); + $data = $result->data; + + $this->assertEquals(11, count($data->items)); + for($i=0; $i<8; $i++) { + $this->assertEquals('entry', $data->items[$i]->type); + $this->assertEquals('Manton Reece', $data->items[$i]->author->name); + $this->assertEquals('https://www.manton.org/', $data->items[$i]->author->url); + $this->assertEquals('https://micro.blog/manton/avatar.jpg', $data->items[$i]->author->photo); + } + } + public function testJSONFeedRelativeImages() { $url = 'http://feed.example.com/jsonfeed'; $response = $this->parse(['url' => $url, 'expect' => 'feed']); diff --git a/tests/data/feed.example.com/jsonfeed-author b/tests/data/feed.example.com/jsonfeed-author new file mode 100644 index 0000000..f829c02 --- /dev/null +++ b/tests/data/feed.example.com/jsonfeed-author @@ -0,0 +1,111 @@ +HTTP/1.1 200 OK +Date: Sat, 11 Nov 2017 15:35:23 GMT +Server: Apache +Vary: Cookie +ETag: "ccd12f270264ab61d4b31870eef8d73e" +Link: ; rel="https://api.w.org/" +Last-Modified: Fri, 10 Nov 2017 16:34:21 GMT +MS-Author-Via: DAV +Content-Type: application/json; charset=UTF-8 + +{ + "version": "https://jsonfeed.org/version/1", + "user_comment": "This feed allows you to read the posts from this site in any feed reader that supports the JSON Feed format. To add this feed to your reader, copy the following URL -- http://www.manton.org/feed/json -- and add it your reader.", + "home_page_url": "https://www.manton.org/", + "icon": "https://micro.blog/manton/avatar.jpg", + "feed_url": "http://www.manton.org/feed/json", + "title": "Manton Reece", + "description": "", + "items": [ + { + "id": "http://www.manton.org/2017/11/5993.html", + "url": "http://www.manton.org/2017/11/5993.html", + "title": "", + "content_html": "

I’ve updated Micro.blog’s Twitter cross-posting to support 280 characters. The apps still color the character counter blue until 140, and red after 280, just in case you want to stick to shorter posts.

\n", + "date_published": "2017-11-10T16:34:21+00:00", + "date_modified": "2017-11-10T16:34:21+00:00" + }, + { + "id": "http://www.manton.org/2017/11/5991.html", + "url": "http://www.manton.org/2017/11/5991.html", + "title": "", + "content_html": "

The 7-day “Photo Challenge” pin is now live on Micro.blog. Thanks again to Doug Lane for kicking things off with prompts to inspire everyone to take more photos.

\n", + "date_published": "2017-11-10T15:31:12+00:00", + "date_modified": "2017-11-10T15:31:12+00:00" + }, + { + "id": "http://www.manton.org/2017/11/5989.html", + "url": "http://www.manton.org/2017/11/5989.html", + "title": "", + "content_html": "

Love this 7-day photo challenge for Micro.blog started by Doug Lane.

\n", + "date_published": "2017-11-09T17:07:06+00:00", + "date_modified": "2017-11-09T17:07:06+00:00" + }, + { + "id": "http://www.manton.org/2017/11/5987.html", + "url": "http://www.manton.org/2017/11/5987.html", + "title": "", + "content_html": "

Brent Simmons is back to podcasting with a new show from The Omni Group. Looks great!

\n", + "date_published": "2017-11-08T19:49:44+00:00", + "date_modified": "2017-11-08T19:49:44+00:00" + }, + { + "id": "http://www.manton.org/2017/11/one-election-day-down-one-more-to-go.html", + "url": "http://www.manton.org/2017/11/one-election-day-down-one-more-to-go.html", + "title": "One election day down, one more to go", + "content_html": "

Josh Marshall of Talking Points Memo on last night’s victory for Democrats:

\n

\n When a President is locked below 40% approval and often closer to 35% approval, his party will face a brutal and unforgiving electorate. This was a fact a decade ago and it\u2019s a fact today. We\u2019ve just been stunned into an unwarranted uncertainty by the fact of Trump\u2019s victory one year ago today.\n

\n

November 2018 feels like a long time from now, but it will get here.

\n", + "date_published": "2017-11-08T18:00:58+00:00", + "date_modified": "2017-11-08T18:02:12+00:00" + }, + { + "id": "http://www.manton.org/2017/11/5983.html", + "url": "http://www.manton.org/2017/11/5983.html", + "title": "", + "content_html": "

I should’ve added an “I voted” pin to Micro.blog for posting voting sticker photos. We’ll do that for 2018.

\n", + "date_published": "2017-11-08T04:38:44+00:00", + "date_modified": "2017-11-08T04:38:44+00:00" + }, + { + "id": "http://www.manton.org/2017/11/5981.html", + "url": "http://www.manton.org/2017/11/5981.html", + "title": "", + "content_html": "

Redesigned the Micro.blog splash page (when you’re not signed in). The most concise expression of the mission statement so far.

\n", + "date_published": "2017-11-07T21:58:25+00:00", + "date_modified": "2017-11-07T21:58:25+00:00" + }, + { + "id": "http://www.manton.org/2017/11/5979.html", + "url": "http://www.manton.org/2017/11/5979.html", + "title": "", + "content_html": "

Coming up on a year since I wrote about how today’s social networks are broken. Still what I believe.

\n", + "date_published": "2017-11-07T21:00:42+00:00", + "date_modified": "2017-11-07T21:00:42+00:00" + }, + { + "id": "http://www.manton.org/2017/11/5977.html", + "url": "http://www.manton.org/2017/11/5977.html", + "title": "", + "content_html": "

Election day in the United States. There’s probably something on the ballot where you live. Doesn’t have to be big to still matter. Vote!

\n", + "date_published": "2017-11-07T16:42:00+00:00", + "date_modified": "2017-11-07T16:42:00+00:00" + }, + { + "id": "http://www.manton.org/2017/11/5975.html", + "url": "http://www.manton.org/2017/11/5975.html", + "title": "", + "content_html": "

\n", + "image": "image.jpg", + "banner_image": "banner_image.jpg", + "date_published": "2017-11-07T15:04:01+00:00", + "date_modified": "2017-11-07T15:04:01+00:00" + }, + { + "id": "http://www.manton.org/2017/11/5975.html", + "title": "", + "content_html": "

\n", + "image": "image.jpg", + "date_published": "2017-11-07T15:04:01+00:00", + "date_modified": "2017-11-07T15:04:01+00:00" + } + ] +}