improve handling an anonymous user

This commit is contained in:
Filip Gralinski 2016-05-03 12:29:56 +02:00
parent e80f72d741
commit 92185a14a9
5 changed files with 25 additions and 25 deletions

View File

@ -115,6 +115,8 @@ instance Yesod App where
isAuthorized (ChallengeGraphDataR _) _ = return Authorized isAuthorized (ChallengeGraphDataR _) _ = return Authorized
isAuthorized (ChallengeDiscussionR _) _ = return Authorized isAuthorized (ChallengeDiscussionR _) _ = return Authorized
isAuthorized (AvatarR _) _ = return Authorized
-- Default to Authorized for now. -- Default to Authorized for now.
isAuthorized _ _ = isTrustedAuthorized isAuthorized _ _ = isTrustedAuthorized

View File

@ -49,6 +49,7 @@ instance ToTimelineItem (Entity Submission) where
getChallengeDiscussionR :: Text -> Handler Html getChallengeDiscussionR :: Text -> Handler Html
getChallengeDiscussionR name = do getChallengeDiscussionR name = do
(Entity challengeId challenge) <- runDB $ getBy404 $ UniqueName name (Entity challengeId challenge) <- runDB $ getBy404 $ UniqueName name
maybeUser <- maybeAuth
(formWidget, formEnctype) <- generateFormPost $ renderBootstrap3 BootstrapBasicForm (commentForm challengeId) (formWidget, formEnctype) <- generateFormPost $ renderBootstrap3 BootstrapBasicForm (commentForm challengeId)
comments <- runDB $ selectList [CommentChallenge ==. challengeId] [Desc CommentPosted] comments <- runDB $ selectList [CommentChallenge ==. challengeId] [Desc CommentPosted]
submissions <- runDB $ selectList [SubmissionChallenge ==. challengeId] [Desc SubmissionStamp] submissions <- runDB $ selectList [SubmissionChallenge ==. challengeId] [Desc SubmissionStamp]
@ -56,9 +57,9 @@ getChallengeDiscussionR name = do
timelineItems'' <- mapM toTimelineItem submissions timelineItems'' <- mapM toTimelineItem submissions
let sortedTimelineItems = sortBy (\item1 item2 -> (getTime item2 `compare` getTime item1)) ( let sortedTimelineItems = sortBy (\item1 item2 -> (getTime item2 `compare` getTime item1)) (
timelineItems' ++ timelineItems'') timelineItems' ++ timelineItems'')
challengeLayout True challenge (discussionWidget formWidget formEnctype name sortedTimelineItems) challengeLayout True challenge (discussionWidget maybeUser formWidget formEnctype name sortedTimelineItems)
discussionWidget formWidget formEnctype name sortedTimelineItems = $(widgetFile "challenge-discussion") discussionWidget maybeUser formWidget formEnctype name sortedTimelineItems = $(widgetFile "challenge-discussion")
timelineItemWidget item = $(widgetFile "timeline-item") timelineItemWidget item = $(widgetFile "timeline-item")
@ -66,28 +67,20 @@ postChallengeDiscussionR :: Text -> Handler TypedContent
postChallengeDiscussionR name = do postChallengeDiscussionR name = do
(Entity challengeId _) <- runDB $ getBy404 $ UniqueName name (Entity challengeId _) <- runDB $ getBy404 $ UniqueName name
((result, formWidget), formEnctype) <- runFormPost $ renderBootstrap3 BootstrapBasicForm (commentForm challengeId) ((result, formWidget), formEnctype) <- runFormPost $ renderBootstrap3 BootstrapBasicForm (commentForm challengeId)
case result of stamp <- liftIO getCurrentTime
FormSuccess comment -> do
userId <- requireAuthId userId <- requireAuthId
if commentAuthor comment == userId case result of
then FormSuccess (challengeId, commentContent) -> do
do
setMessage $ toHtml ("Comment submitted" :: Text) setMessage $ toHtml ("Comment submitted" :: Text)
_ <- runDB $ insert comment _ <- runDB $ insert $ Comment challengeId userId stamp commentContent
return ()
else
do
setMessage $ toHtml ("Wrong user ID" :: Text)
return () return ()
_ -> do _ -> do
setMessage $ toHtml ("Something went wrong" :: Text) setMessage $ toHtml ("Something went wrong" :: Text)
redirect $ ChallengeDiscussionR name redirect $ ChallengeDiscussionR name
commentForm :: Key Challenge -> AForm Handler Comment commentForm :: Key Challenge -> AForm Handler (ChallengeId, Textarea)
commentForm challengeId = Comment commentForm challengeId = (,)
<$> pure challengeId <$> pure challengeId
<*> lift requireAuthId
<*> lift (liftIO getCurrentTime)
<*> areq textareaField (bfs MsgCommentText) Nothing <*> areq textareaField (bfs MsgCommentText) Nothing

View File

@ -266,6 +266,7 @@ getChallengeSubmissions condition name = do
challengeAllSubmissionsWidget muserId challenge submissions tests = $(widgetFile "challenge-all-submissions") challengeAllSubmissionsWidget muserId challenge submissions tests = $(widgetFile "challenge-all-submissions")
challengeLayout withHeader challenge widget = do challengeLayout withHeader challenge widget = do
maybeUser <- maybeAuth
bc <- widgetToPageContent widget bc <- widgetToPageContent widget
defaultLayout $ do defaultLayout $ do
setTitle "Challenge" setTitle "Challenge"

View File

@ -1,9 +1,12 @@
<p> <p>
$maybe user <- maybeUser
<form method=post action=@{ChallengeDiscussionR name}#form enctype=#{formEnctype}> <form method=post action=@{ChallengeDiscussionR name}#form enctype=#{formEnctype}>
^{formWidget} ^{formWidget}
<button .btn .btn-primary type="submit"> <button .btn .btn-primary type="submit">
_{MsgSend} <span class="glyphicon glyphicon-upload"></span> _{MsgSend} <span class="glyphicon glyphicon-upload"></span>
$nothing
<b>Log in to write a comment
<div .timeline-box> <div .timeline-box>
$forall item <- sortedTimelineItems $forall item <- sortedTimelineItems

View File

@ -5,6 +5,7 @@
<li role="presentation" class="active"><a href="@{ShowChallengeR (challengeName challenge)}">Challenge</a> <li role="presentation" class="active"><a href="@{ShowChallengeR (challengeName challenge)}">Challenge</a>
<li role="presentation"><a href="@{ChallengeReadmeR (challengeName challenge)}">Readme</a> <li role="presentation"><a href="@{ChallengeReadmeR (challengeName challenge)}">Readme</a>
<li role="presentation"><a href="@{ChallengeHowToR (challengeName challenge)}">How To</a> <li role="presentation"><a href="@{ChallengeHowToR (challengeName challenge)}">How To</a>
$if isJust maybeUser
<li role="presentation"><a href="@{ChallengeSubmissionR (challengeName challenge)}">Submit</a> <li role="presentation"><a href="@{ChallengeSubmissionR (challengeName challenge)}">Submit</a>
<li role="presentation"><a href="@{ChallengeMySubmissionsR (challengeName challenge)}">My Submissions</a> <li role="presentation"><a href="@{ChallengeMySubmissionsR (challengeName challenge)}">My Submissions</a>
<li role="presentation"><a href="@{ChallengeAllSubmissionsR (challengeName challenge)}">All Submissions</a> <li role="presentation"><a href="@{ChallengeAllSubmissionsR (challengeName challenge)}">All Submissions</a>