2015-01-18 19:44:41 +00:00
|
|
|
-- eidolon -- A simple gallery in Haskell and Yesod
|
|
|
|
-- Copyright (C) 2015 Amedeo Molnár
|
|
|
|
--
|
|
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU Affero General Public License as published
|
|
|
|
-- by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
--
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU Affero General Public License for more details.
|
|
|
|
--
|
|
|
|
-- You should have received a copy of the GNU Affero General Public License
|
2015-01-21 09:00:18 +00:00
|
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-01-18 19:44:41 +00:00
|
|
|
|
2014-12-14 19:21:23 +00:00
|
|
|
module Handler.RootFeed where
|
|
|
|
|
|
|
|
import Import
|
2017-01-06 17:28:36 +00:00
|
|
|
import Text.Markdown
|
2014-12-14 19:21:23 +00:00
|
|
|
import qualified Data.Text as T
|
2015-09-30 13:10:21 +00:00
|
|
|
import qualified Data.Text.Lazy as LT
|
|
|
|
import qualified Data.List as L
|
2014-12-14 19:21:23 +00:00
|
|
|
import Data.Maybe
|
|
|
|
import Data.Time
|
2015-09-26 09:15:20 +00:00
|
|
|
import Text.Shakespeare.Text (stext)
|
|
|
|
import Yesod.Feed
|
|
|
|
import Yesod.RssFeed
|
|
|
|
import Yesod.AtomFeed
|
2015-09-30 02:07:08 +00:00
|
|
|
import System.FilePath
|
|
|
|
import qualified System.Posix as P
|
2014-12-14 19:21:23 +00:00
|
|
|
|
2015-09-26 09:15:20 +00:00
|
|
|
getCommentFeedRssR :: MediumId -> Handler RepRss
|
|
|
|
getCommentFeedRssR mId = do
|
|
|
|
feed <- commentFeedBuilder mId
|
|
|
|
rssFeed feed
|
2014-12-14 19:21:23 +00:00
|
|
|
|
2015-09-26 09:15:20 +00:00
|
|
|
getCommentFeedAtomR :: MediumId -> Handler RepAtom
|
|
|
|
getCommentFeedAtomR mId = do
|
|
|
|
feed <- commentFeedBuilder mId
|
|
|
|
atomFeed feed
|
|
|
|
|
|
|
|
commentFeedBuilder :: MediumId -> Handler (Feed (Route App))
|
|
|
|
commentFeedBuilder mId = do
|
|
|
|
medium <- runDB $ get404 mId
|
|
|
|
owner <- runDB $ getJust $ mediumOwner medium
|
|
|
|
cs <- runDB $ selectList [CommentOrigin ==. mId] [Desc CommentTime, LimitTo 100]
|
|
|
|
time <- case cs of
|
|
|
|
x:_ -> return $ commentTime $ entityVal x
|
|
|
|
[] -> liftIO getCurrentTime
|
|
|
|
es <- mapM commentToEntry cs
|
|
|
|
route <- fromJust <$> getCurrentRoute
|
|
|
|
return $ Feed
|
|
|
|
{ feedTitle = "Eidolon :: Newest Comments on " `T.append` (mediumTitle medium)
|
|
|
|
, feedLinkSelf = route
|
|
|
|
, feedLinkHome = MediumR mId
|
|
|
|
, feedAuthor = userSlug owner
|
|
|
|
, feedDescription = [shamlet|
|
|
|
|
These are the latest comments on the medium #{mediumTitle medium} by #{userSlug owner}
|
|
|
|
|]
|
|
|
|
, feedLanguage = "en"
|
|
|
|
, feedUpdated = time
|
2017-01-06 17:28:36 +00:00
|
|
|
, feedLogo = Just (StaticR $ StaticRoute (drop 2 $ map T.pack $ splitDirectories $ mediumThumb medium) [], LT.toStrict $ unMarkdown $ fromMaybe (Markdown "") $ mediumDescription medium)
|
2015-09-26 09:15:20 +00:00
|
|
|
, feedEntries = es
|
|
|
|
}
|
|
|
|
|
2016-10-23 01:21:59 +00:00
|
|
|
commentToEntry :: Entity Comment -> Handler (FeedEntry (Route App))
|
|
|
|
commentToEntry c = do
|
|
|
|
author <- runDB $ get $ commentAuthor $ entityVal c
|
|
|
|
let slug = fromMaybe "" $ userSlug <$> author
|
2015-09-26 09:15:20 +00:00
|
|
|
return FeedEntry
|
|
|
|
{ feedEntryLink = MediumR (commentOrigin $ entityVal c)
|
|
|
|
, feedEntryUpdated = (commentTime $ entityVal c)
|
2016-10-23 01:21:59 +00:00
|
|
|
, feedEntryTitle = LT.toStrict $ [stext|#{slug} wrote:|]
|
2015-09-26 09:15:20 +00:00
|
|
|
, feedEntryContent = [shamlet|#{commentContent $ entityVal c}|]
|
2015-09-30 02:07:08 +00:00
|
|
|
, feedEntryEnclosure = Nothing
|
2015-09-26 09:15:20 +00:00
|
|
|
}
|
2014-12-14 19:21:23 +00:00
|
|
|
|
2015-09-26 09:15:20 +00:00
|
|
|
getAlbumFeedRssR :: AlbumId -> Handler RepRss
|
|
|
|
getAlbumFeedRssR aId = do
|
|
|
|
feed <- albumFeedBuilder aId
|
|
|
|
rssFeed feed
|
2014-12-14 19:21:23 +00:00
|
|
|
|
|
|
|
getAlbumFeedAtomR :: AlbumId -> Handler RepAtom
|
2015-09-26 09:15:20 +00:00
|
|
|
getAlbumFeedAtomR aId = do
|
|
|
|
feed <- albumFeedBuilder aId
|
|
|
|
atomFeed feed
|
|
|
|
|
|
|
|
albumFeedBuilder :: AlbumId -> Handler (Feed (Route App))
|
|
|
|
albumFeedBuilder aId = do
|
|
|
|
album <- runDB $ get404 aId
|
|
|
|
owner <- runDB $ getJust $ albumOwner album
|
|
|
|
ms <- runDB $ selectList [MediumAlbum ==. aId] [Desc MediumTime, LimitTo 100]
|
|
|
|
time <- case ms of
|
|
|
|
x:_ -> return $ mediumTime $ entityVal x
|
|
|
|
[] -> liftIO getCurrentTime
|
|
|
|
es <- mapM mediumToEntry ms
|
|
|
|
route <- fromJust <$> getCurrentRoute
|
|
|
|
return Feed
|
|
|
|
{ feedTitle = "Eidolon :: Newest media in " `T.append` (albumTitle album)
|
|
|
|
, feedLinkSelf = route
|
|
|
|
, feedLinkHome = AlbumR aId
|
|
|
|
, feedAuthor = userSlug owner
|
|
|
|
, feedDescription = [shamlet|
|
|
|
|
These are the latest media uploaded in #{userSlug owner}'s album #{albumTitle album}
|
|
|
|
|]
|
|
|
|
, feedLanguage = "en"
|
|
|
|
, feedUpdated = time
|
2015-09-30 02:07:08 +00:00
|
|
|
, feedLogo = Just (StaticR $ StaticRoute (drop 2 $ map T.pack $ splitDirectories $ fromMaybe "/static/img/album.jpg" $ albumSamplePic album) [], "")
|
2015-09-26 09:15:20 +00:00
|
|
|
, feedEntries = es
|
|
|
|
}
|
2014-12-14 19:21:23 +00:00
|
|
|
|
2015-09-26 09:15:20 +00:00
|
|
|
getNameFeedRssR :: Text -> Handler RepRss
|
|
|
|
getNameFeedRssR name = do
|
|
|
|
Entity uId _ <- runDB $ getBy404 $ UniqueUser name
|
|
|
|
getUserFeedRssR uId
|
2014-12-14 19:21:23 +00:00
|
|
|
|
2015-09-26 09:15:20 +00:00
|
|
|
getNameFeedAtomR :: Text -> Handler RepAtom
|
|
|
|
getNameFeedAtomR name = do
|
|
|
|
Entity uId _ <- runDB $ getBy404 $ UniqueUser name
|
|
|
|
getUserFeedAtomR uId
|
2014-12-14 19:21:23 +00:00
|
|
|
|
2015-09-26 09:15:20 +00:00
|
|
|
getUserFeedRssR :: UserId -> Handler RepRss
|
|
|
|
getUserFeedRssR uId = do
|
|
|
|
feed <- userFeedBuilder uId
|
|
|
|
rssFeed feed
|
2014-12-19 21:36:15 +00:00
|
|
|
|
|
|
|
getUserFeedAtomR :: UserId -> Handler RepAtom
|
2015-09-26 09:15:20 +00:00
|
|
|
getUserFeedAtomR uId = do
|
|
|
|
feed <- userFeedBuilder uId
|
|
|
|
atomFeed feed
|
|
|
|
|
|
|
|
userFeedBuilder :: UserId -> Handler (Feed (Route App))
|
|
|
|
userFeedBuilder uId = do
|
|
|
|
user <- runDB $ get404 uId
|
|
|
|
ms <- runDB $ selectList [MediumOwner ==. uId] [Desc MediumTime, LimitTo 100]
|
|
|
|
time <- case ms of
|
|
|
|
x:_ -> return $ mediumTime $ entityVal x
|
|
|
|
[] -> liftIO getCurrentTime
|
|
|
|
es <- mapM mediumToEntry ms
|
|
|
|
route <- fromJust <$> getCurrentRoute
|
|
|
|
return Feed
|
|
|
|
{ feedTitle = "Eidolon :: Newest media of " `T.append` (userSlug user)
|
|
|
|
, feedLinkSelf = route
|
|
|
|
, feedLinkHome = ProfileR uId
|
|
|
|
, feedAuthor = userSlug user
|
|
|
|
, feedDescription = [shamlet|
|
|
|
|
These are the latest media uploaded by #{userSlug user} to Eidolon.
|
|
|
|
|]
|
|
|
|
, feedLanguage = "en"
|
|
|
|
, feedUpdated = time
|
2015-09-30 02:07:08 +00:00
|
|
|
, feedLogo = Nothing
|
2015-09-26 09:15:20 +00:00
|
|
|
, feedEntries = es
|
|
|
|
}
|
2014-12-19 21:36:15 +00:00
|
|
|
|
2015-09-26 09:15:20 +00:00
|
|
|
getRootFeedRssR :: Handler RepRss
|
|
|
|
getRootFeedRssR = do
|
|
|
|
feed <- rootFeedBuilder
|
|
|
|
rssFeed feed
|
2015-08-18 21:57:50 +00:00
|
|
|
|
2015-09-26 09:15:20 +00:00
|
|
|
getRootFeedAtomR :: Handler RepAtom
|
|
|
|
getRootFeedAtomR = do
|
|
|
|
feed <- rootFeedBuilder
|
|
|
|
atomFeed feed
|
|
|
|
|
|
|
|
rootFeedBuilder :: Handler (Feed (Route App))
|
|
|
|
rootFeedBuilder = do
|
|
|
|
ms <- runDB $ selectList [] [Desc MediumTime, LimitTo 100]
|
|
|
|
time <- case ms of
|
|
|
|
x:_ -> return $ mediumTime $ entityVal x
|
|
|
|
[] -> liftIO getCurrentTime
|
|
|
|
es <- mapM mediumToEntry ms
|
|
|
|
route <- fromJust <$> getCurrentRoute
|
|
|
|
return Feed
|
|
|
|
{ feedTitle = "Eidolon :: Latest media"
|
|
|
|
, feedLinkSelf = route
|
|
|
|
, feedLinkHome = HomeR
|
|
|
|
, feedAuthor = "Everyone"
|
|
|
|
, feedDescription = [shamlet|
|
|
|
|
These are the latest media uploaded to the Eidolon gallery service.
|
|
|
|
|]
|
|
|
|
, feedLanguage = "en"
|
|
|
|
, feedUpdated = time
|
2015-09-30 02:07:08 +00:00
|
|
|
, feedLogo = Nothing
|
2015-09-26 09:15:20 +00:00
|
|
|
, feedEntries = es
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:27:49 +00:00
|
|
|
mediumToEntry :: MonadIO m => Entity Medium -> m (FeedEntry (Route App))
|
|
|
|
mediumToEntry ent = do
|
|
|
|
size <- liftIO $ getSize $ L.tail $ mediumPreview $ entityVal ent
|
2015-09-26 09:15:20 +00:00
|
|
|
return FeedEntry
|
|
|
|
{ feedEntryLink = MediumR (entityKey ent)
|
|
|
|
, feedEntryUpdated = mediumTime (entityVal ent)
|
|
|
|
, feedEntryTitle = mediumTitle (entityVal ent)
|
2017-01-06 17:28:36 +00:00
|
|
|
, feedEntryContent = toHtml (fromMaybe (Markdown "") $ mediumDescription $ entityVal ent)
|
2016-03-02 08:51:42 +00:00
|
|
|
, feedEntryEnclosure = Just $ EntryEnclosure
|
|
|
|
(StaticR $ StaticRoute (drop 2 $ map T.pack $ splitDirectories $ mediumPreview $ entityVal ent) [])
|
|
|
|
size
|
2016-11-20 06:29:05 +00:00
|
|
|
"image/png"
|
2015-09-26 09:15:20 +00:00
|
|
|
}
|
2015-09-30 02:07:08 +00:00
|
|
|
|
|
|
|
getSize :: FilePath -> IO Int
|
|
|
|
getSize path = do
|
|
|
|
stat <- P.getFileStatus path
|
|
|
|
P.COff raw <- return $ P.fileSize stat
|
|
|
|
return $ (fromIntegral raw :: Int)
|
2017-01-06 17:28:36 +00:00
|
|
|
|
|
|
|
unMarkdown :: Markdown -> LT.Text
|
|
|
|
unMarkdown (Markdown t) = t
|