eidolon/Handler/Medium.hs

261 lines
10 KiB
Haskell
Raw Normal View History

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-08-18 04:06:16 +00:00
module Handler.Medium where
import Import
import Data.Time
import Data.Maybe
2014-09-24 21:03:18 +00:00
import qualified Data.Text as T
2015-09-25 21:31:37 +00:00
-- import System.Locale
import System.FilePath
import Yesod.Markdown
import Yesod.RssFeed
import Yesod.AtomFeed
2014-08-18 04:06:16 +00:00
getMediumR :: MediumId -> Handler Html
getMediumR mediumId = do
tempMedium <- runDB $ get mediumId
case tempMedium of
Just medium -> do
2015-09-14 16:54:46 +00:00
let ownerId = mediumOwner medium
2014-08-18 04:06:16 +00:00
owner <- runDB $ getJust ownerId
2015-09-14 16:54:46 +00:00
let ownerName = userName owner
let albumId = mediumAlbum medium
2014-09-05 19:00:03 +00:00
album <- runDB $ getJust albumId
2014-08-18 04:06:16 +00:00
msu <- lookupSession "userId"
userId <- case msu of
2015-09-14 16:54:46 +00:00
Just tempUserId ->
return $ Just $ getUserIdFromText tempUserId
2014-08-18 04:06:16 +00:00
Nothing ->
return Nothing
2014-12-08 05:26:45 +00:00
userSl <- case userId of
Just uId -> do
u <- runDB $ getJust uId
return $ Just $ userSlug u
Nothing ->
return Nothing
2015-09-14 16:54:46 +00:00
let presence = userId == (Just ownerId) || userId == Just (albumOwner album)
(commentWidget, enctype) <- generateFormPost $ commentForm (fromJust userId) (fromJust userSl) mediumId Nothing
2015-09-14 16:54:46 +00:00
comments <- runDB $ selectList
[ CommentOrigin ==. mediumId
, CommentParent ==. Nothing ]
[ Desc CommentTime ]
replies <- runDB $ selectList
[ CommentOrigin ==. mediumId
, CommentParent !=. Nothing ]
[ Desc CommentTime ]
2015-09-26 03:27:54 +00:00
let dataWidth = if mediumWidth medium >= 850 then 850 else mediumWidth medium
let tr = StaticR $ StaticRoute (drop 2 $ map T.pack $ splitDirectories $ mediumThumb medium) []
let pr = StaticR $ StaticRoute (drop 2 $ map T.pack $ splitDirectories $ mediumPreview medium) []
let ir = StaticR $ StaticRoute (drop 2 $ map T.pack $ splitDirectories $ mediumPath medium) []
2015-01-12 14:15:52 +00:00
formLayout $ do
2014-09-24 21:03:18 +00:00
setTitle $ toHtml ("Eidolon :: Medium " `T.append` (mediumTitle medium))
rssLink (CommentFeedRssR mediumId) $ "Comment feed of medium " `T.append` mediumTitle medium
atomLink (CommentFeedAtomR mediumId) $ "Comment feed of medium " `T.append` mediumTitle medium
2014-08-18 04:06:16 +00:00
$(widgetFile "medium")
Nothing -> do
setMessage "This image does not exist"
2015-09-14 16:54:46 +00:00
redirect HomeR
postMediumR :: MediumId -> Handler Html
postMediumR mediumId = do
tempMedium <- runDB $ get mediumId
case tempMedium of
Just medium -> do
msu <- lookupSession "userId"
case msu of
Just tempUserId -> do
2015-09-14 16:54:46 +00:00
let userId = getUserIdFromText tempUserId
u <- runDB $ getJust userId
let userSl = userSlug u
((res, _), _) <- runFormPost $ commentForm userId userSl mediumId Nothing
case res of
FormSuccess temp -> do
2014-12-28 06:12:25 +00:00
_ <- runDB $ insert temp
2014-12-08 05:13:39 +00:00
--send mail to medium owner
owner <- runDB $ getJust $ mediumOwner medium
link <- ($ MediumR (commentOrigin temp)) <$> getUrlRender
sendMail (userEmail owner) ((commentAuthorSlug temp) `T.append` " commented on your medium")
2014-12-08 05:13:39 +00:00
[shamlet|
<h1>Hello #{userSlug owner}
<p>#{commentAuthorSlug temp} commented on your medium:
2014-12-08 05:13:39 +00:00
<p>#{commentContent temp}
<p>To follow the comment thread follow
<a href=#{link}>
this link
2014-12-08 05:27:28 +00:00
.
2014-12-08 05:13:39 +00:00
|]
setMessage "Your Comment has been posted"
redirect $ MediumR mediumId
_ -> do
setMessage "There has been an error whith your comment"
redirect $ MediumR mediumId
Nothing -> do
setMessage "You need to be looged in to comment on media"
redirect LoginR
Nothing -> do
setMessage "This image does not exist"
2015-09-14 16:54:46 +00:00
redirect HomeR
commentForm :: UserId -> Text -> MediumId -> Maybe CommentId -> Form Comment
commentForm authorId authorSlug originId parentId = renderDivs $ Comment
<$> pure authorId
<*> pure authorSlug
<*> pure originId
<*> pure parentId
<*> lift (liftIO getCurrentTime)
<*> areq markdownField "Comment this medium" Nothing
getCommentReplyR :: CommentId -> Handler Html
getCommentReplyR commentId = do
tempComment <- runDB $ get commentId
case tempComment of
Just comment -> do
msu <- lookupSession "userId"
case msu of
Just tempUserId -> do
2015-09-14 16:54:46 +00:00
let userId = getUserIdFromText tempUserId
u <- runDB $ getJust userId
let userSl = userSlug u
2015-09-14 16:54:46 +00:00
let mediumId = commentOrigin comment
(replyWidget, enctype) <- generateFormPost $ commentForm userId userSl mediumId (Just commentId)
2015-01-12 20:31:29 +00:00
formLayout $ do
setTitle "Eidolon :: Reply to comment"
$(widgetFile "commentReply")
Nothing -> do
setMessage "You need to be logged in to comment on media"
2015-09-14 16:54:46 +00:00
redirect LoginR
Nothing -> do
setMessage "This comment does not Exist"
2015-09-14 16:54:46 +00:00
redirect HomeR
postCommentReplyR :: CommentId -> Handler Html
postCommentReplyR commentId = do
tempComment <- runDB $ get commentId
case tempComment of
Just comment -> do
msu <- lookupSession "userId"
case msu of
Just tempUserId -> do
2015-09-14 16:54:46 +00:00
let userId = getUserIdFromText tempUserId
u <- runDB $ getJust userId
let userSl = userSlug u
2015-09-14 16:54:46 +00:00
let mediumId = commentOrigin comment
((res, _), _) <- runFormPost $ commentForm userId userSl mediumId (Just commentId)
case res of
FormSuccess temp -> do
2014-12-28 06:12:25 +00:00
_ <- runDB $ insert temp
2014-12-08 05:13:39 +00:00
--send mail to parent author
parent <- runDB $ getJust $ fromJust $ commentParent temp
parAuth <- runDB $ getJust $ commentAuthor parent
2014-12-08 05:13:39 +00:00
link <- ($ MediumR (commentOrigin temp)) <$> getUrlRender
sendMail (userEmail parAuth) ((commentAuthorSlug temp) `T.append` " replied to your comment")
2014-12-08 05:13:39 +00:00
[shamlet|
<h1>Hello #{userSlug parAuth}
<p>#{commentAuthorSlug temp} replied to your comment:
2014-12-28 02:53:49 +00:00
#{commentContent temp}
2014-12-08 05:13:39 +00:00
<p>To see the comment thread follow
<a href=#{link}>
this link
2014-12-08 05:27:28 +00:00
.
2014-12-08 05:13:39 +00:00
|]
--send mail to medium owner
medium <- runDB $ getJust mediumId
owner <- runDB $ getJust $ mediumOwner medium
sendMail (userEmail owner) ((commentAuthorSlug temp) `T.append` " commented on your medium")
2014-12-08 05:13:39 +00:00
[shamlet|
<h1>Hello #{userSlug owner}
<p>#{commentAuthorSlug temp} commented your medium with:
2014-12-28 02:53:49 +00:00
#{commentContent temp}
2014-12-08 05:13:39 +00:00
<p>To see the comment thread follow
<a href=#{link}>
this link
2014-12-08 05:27:28 +00:00
.
2014-12-08 05:13:39 +00:00
|]
setMessage "Your reply has been posted"
redirect $ MediumR mediumId
_ -> do
setMessage "There has been an error with your reply"
redirect $ CommentReplyR commentId
Nothing -> do
setMessage "You need to be logged in to post replies"
2015-09-14 16:54:46 +00:00
redirect LoginR
Nothing -> do
setMessage "This comment does not exist!"
2015-09-14 16:54:46 +00:00
redirect HomeR
getCommentDeleteR :: CommentId -> Handler Html
2014-12-07 05:08:45 +00:00
getCommentDeleteR commentId = do
tempComment <- runDB $ get commentId
case tempComment of
Just comment -> do
msu <- lookupSession "userId"
case msu of
Just tempUserId -> do
2015-09-14 16:54:46 +00:00
let userId = getUserIdFromText tempUserId
if
Just userId == Just (commentAuthor comment)
2015-09-14 16:54:46 +00:00
then do
2015-01-12 20:31:29 +00:00
formLayout $ do
2014-12-07 05:08:45 +00:00
setTitle "Eidolon :: Delete comment"
$(widgetFile "commentDelete")
2015-09-14 16:54:46 +00:00
else do
2014-12-07 05:08:45 +00:00
setMessage "You must be the author of this comment to delete it"
redirect $ MediumR $ commentOrigin comment
Nothing -> do
setMessage "You must be logged in to delete comments"
2015-09-14 16:54:46 +00:00
redirect LoginR
2014-12-07 05:08:45 +00:00
Nothing -> do
setMessage "This comment does not exist"
2015-09-14 16:54:46 +00:00
redirect HomeR
postCommentDeleteR :: CommentId -> Handler Html
2014-12-07 05:08:45 +00:00
postCommentDeleteR commentId = do
tempComment <- runDB $ get commentId
case tempComment of
Just comment -> do
msu <- lookupSession "userId"
case msu of
Just tempUserId -> do
2015-09-14 16:54:46 +00:00
let userId = getUserIdFromText tempUserId
if
Just userId == Just (commentAuthor comment)
2015-09-14 16:54:46 +00:00
then do
2014-12-07 05:08:45 +00:00
confirm <- lookupPostParam "confirm"
case confirm of
Just "confirm" -> do
-- delete comment children
childEnts <- runDB $ selectList [CommentParent ==. (Just commentId)] []
2014-12-28 06:12:25 +00:00
_ <- mapM (\ent -> runDB $ delete $ entityKey ent) childEnts
2014-12-07 05:08:45 +00:00
-- delete comment itself
runDB $ delete commentId
-- outro
setMessage "Your comment has been deleted"
redirect $ MediumR $ commentOrigin comment
_ -> do
setMessage "You must confirm the deletion"
redirect $ MediumR $ commentOrigin comment
2015-09-14 16:54:46 +00:00
else do
2014-12-07 05:08:45 +00:00
setMessage "You must be the author of this comment to delete it"
redirect $ MediumR $ commentOrigin comment
Nothing -> do
setMessage "You must be logged in to delete comments"
2015-09-14 16:54:46 +00:00
redirect LoginR
2014-12-07 05:08:45 +00:00
Nothing -> do
setMessage "This comment does not exist"
2015-09-14 16:54:46 +00:00
redirect HomeR