2021-08-21 09:28:19 +02:00
|
|
|
module Handler.Announcements where
|
|
|
|
|
|
|
|
import Import
|
|
|
|
|
|
|
|
import Handler.Shared
|
2022-01-18 22:54:07 +01:00
|
|
|
import Web.Announcements (sendAnnouncement,
|
|
|
|
AnnouncementPiece(..),
|
|
|
|
AnnouncementMessage,
|
|
|
|
AnnouncementHook,
|
|
|
|
toAnnouncementHook)
|
|
|
|
|
|
|
|
import qualified Database.Esqueleto as E
|
|
|
|
import Database.Esqueleto ((^.))
|
|
|
|
|
|
|
|
sendChallengeAnnouncement :: ChallengeId -> AnnouncementMessage -> Handler ()
|
|
|
|
sendChallengeAnnouncement challengeId announcementMsg = do
|
|
|
|
hooks <- fetchChallengeHooks challengeId
|
|
|
|
liftIO $ mapM_ (\hook -> sendAnnouncement hook announcementMsg) hooks
|
|
|
|
|
|
|
|
fetchChallengeHooks :: ChallengeId -> Handler [AnnouncementHook]
|
|
|
|
fetchChallengeHooks challengeId = do
|
|
|
|
courses <- runDB $ E.select $ E.from $ \(course, course_challenge) -> do
|
|
|
|
E.where_ (course_challenge ^. CourseChallengeChallenge E.==. E.val challengeId
|
|
|
|
E.&&. course_challenge ^. CourseChallengeCourse E.==. course ^. CourseId)
|
|
|
|
return course
|
|
|
|
|
|
|
|
case catMaybes $ map (courseAnnouncementHook . entityVal) courses of
|
|
|
|
[] -> do
|
|
|
|
app <- getYesod
|
|
|
|
let mWebHook = appAnnouncementHook $ appSettings app
|
|
|
|
return $ maybeToList mWebHook
|
|
|
|
hooks -> return $ map toAnnouncementHook hooks
|
|
|
|
|
|
|
|
getTestChallengeAnnouncementsR :: Text -> Handler Html
|
|
|
|
getTestChallengeAnnouncementsR challengeName = do
|
|
|
|
(Entity challengeId _) <- runDB $ getBy404 $ UniqueName challengeName
|
|
|
|
|
|
|
|
app <- getYesod
|
|
|
|
sendChallengeAnnouncement challengeId (testMessage app)
|
|
|
|
|
|
|
|
defaultLayout $ do
|
|
|
|
setTitle "Test announcements"
|
|
|
|
$(widgetFile "test-challenge-announcements")
|
2021-08-21 09:28:19 +02:00
|
|
|
|
|
|
|
getTestAnnouncementsR :: Handler Html
|
|
|
|
getTestAnnouncementsR = do
|
|
|
|
app <- getYesod
|
|
|
|
|
2021-08-21 10:26:46 +02:00
|
|
|
let webHook = appAnnouncementHook $ appSettings app
|
2021-08-21 09:28:19 +02:00
|
|
|
|
|
|
|
case webHook of
|
2022-01-18 22:54:07 +01:00
|
|
|
Just hook -> liftIO $ sendAnnouncement hook (testMessage app)
|
2021-08-21 09:28:19 +02:00
|
|
|
Nothing -> return ()
|
|
|
|
|
|
|
|
defaultLayout $ do
|
|
|
|
setTitle "Test announcements"
|
|
|
|
$(widgetFile "test-announcements")
|
2022-01-18 22:54:07 +01:00
|
|
|
|
|
|
|
testMessage :: App -> AnnouncementMessage
|
|
|
|
testMessage app = [AnnouncementText "Test message from ",
|
|
|
|
(linkInAnnouncement app name "")]
|
|
|
|
where name = case appLocation $ appSettings app of
|
|
|
|
Just loc -> "Gonito@" ++ loc
|
|
|
|
Nothing -> "Gonito"
|