Skip to content

Commit

Permalink
Add option for multiple author ids
Browse files Browse the repository at this point in the history
  • Loading branch information
emhoracek committed Apr 19, 2021
1 parent 9e6fad6 commit 8808e4c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
15 changes: 14 additions & 1 deletion spec/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mockedTests = do
feedTests :: Spec
feedTests =
describe "rss feed" $ do
it "should make a feed" $ do
it "should make a feed using embedded guest authors" $ do
ctxt <- initFauxRequestNoCache
let wpfeed = WPFeed
"https://myurl.com/feed"
Expand All @@ -62,6 +62,19 @@ feedTests =
(renderFeedContent ctxt)
ft <- toXMLFeed (_wordpress ctxt) wpfeed
ft `shouldBe` "<?xml version='1.0' ?>\n<feed xmlns=\"http://www.w3.org/2005/Atom\">\n <id>https://myurl.com/feed</id>\n <title type=\"text\">My Blog</title>\n <updated>2014-10-20T07:00:00Z</updated>\n <entry>\n <id>https://myurl.com/2014/10/foo-bar/</id>\n <title type=\"html\">&lt;i&gt;Foo&lt;/i&gt; bar</title>\n <updated>2014-10-20T07:00:00Z</updated>\n <published>2014-10-20T07:00:00Z</published>\n <summary type=\"html\">summary</summary>\n <content type=\"html\">This is the title: &lt;i&gt;Foo&lt;/i&gt; bar</content>\n <author>\n <name>Emma Goldman</name>\n </author>\n <link href=\"https://myurl.com/2014/10/foo-bar/\" title=\"&lt;i&gt;Foo&lt;/i&gt; bar\" />\n </entry>\n</feed>\n"
{- it "should make a feed using list of author ids" $ do
ctxt <- initFauxRequestNoCache
let wpfeed = WPFeed
"https://myurl.com/feed"
"My Blog"
Nothing
Nothing
"https://myurl.com"
buildEntryLinks
GuestAuthorsViaReq
(renderFeedContent ctxt)
ft <- toXMLFeed (_wordpress ctxt) wpfeed
ft `shouldBe` "<?xml version='1.0' ?>\n<feed xmlns=\"http://www.w3.org/2005/Atom\">\n <id>https://myurl.com/feed</id>\n <title type=\"text\">My Blog</title>\n <updated>2014-10-20T07:00:00Z</updated>\n <entry>\n <id>https://myurl.com/2014/10/foo-bar/</id>\n <title type=\"html\">&lt;i&gt;Foo&lt;/i&gt; bar</title>\n <updated>2014-10-20T07:00:00Z</updated>\n <published>2014-10-20T07:00:00Z</published>\n <summary type=\"html\">summary</summary>\n <content type=\"html\">This is the title: &lt;i&gt;Foo&lt;/i&gt; bar</content>\n <author>\n <name>Lucy Parsons</name>\n </author>\n <author>\n <name>Emma Goldman</name>\n </author>\n <link href=\"https://myurl.com/2014/10/foo-bar/\" title=\"&lt;i&gt;Foo&lt;/i&gt; bar\" />\n </entry>\n</feed>\n" -}

larcenyFillTests :: Spec
larcenyFillTests = do
Expand Down
18 changes: 17 additions & 1 deletion src/Web/Offset/Feed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ data WPFeed =
, wpGetAuthors :: WPAuthorStyle
, wpRenderEntry :: Object -> IO (Maybe T.Text) }

data WPAuthorStyle = GuestAuthors | DefaultAuthor
data WPAuthorStyle = GuestAuthors | GuestAuthorsViaReq | DefaultAuthor

toXMLFeed :: Wordpress b -> WPFeed -> IO T.Text
toXMLFeed wp wpFeed@(WPFeed uri title icon logo _ _ _ _) = do
Expand Down Expand Up @@ -101,6 +101,7 @@ toEntry wp wpFeed entry@WPEntry{..} = do
let baseEntry = makeEntry guid (TextHTML wpEntryTitle) wpEntryUpdated
authors <- case wpGetAuthors wpFeed of
GuestAuthors -> getAuthorsInline wpEntryJSON
GuestAuthorsViaReq -> getAuthorsViaReq wp wpEntryJSON
DefaultAuthor -> getAuthorViaReq wp wpEntryJSON
return $ baseEntry { entryPublished = Just wpEntryPublished
, entrySummary = Just (TextHTML wpEntrySummary)
Expand Down Expand Up @@ -166,6 +167,21 @@ getAuthorViaReq wp v =
Nothing -> return []
Just authorName ->return (maybeToList authorName)

getAuthorsViaReq :: Wordpress b -> Object -> IO [WPPerson]
getAuthorsViaReq wp v =
do let mAuthorId = parseMaybe (\obj -> obj .: "authors") v :: Maybe [T.Text]
case mAuthorId of
Nothing -> return []
Just authorIds ->
do let authList = map (\id -> ("include[]", id)) authorIds
eRespError <- cachingGetRetry wp (EndpointKey ("wp/v2/authors") authList)
case eRespError of
Left _ -> return []
Right resp ->
case decodeWPResponseBody resp of
Nothing -> return []
Just list -> return list

entryGuid :: T.Text -> Int -> Object -> URI
entryGuid baseURI wpId wpJSON =
unsafeURI $ T.unpack $
Expand Down

0 comments on commit 8808e4c

Please sign in to comment.