103 lines
2.5 KiB
Haskell
103 lines
2.5 KiB
Haskell
{-# LANGUAGE FlexibleContexts #-}
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
module Util where
|
|
|
|
import Opaleye
|
|
|
|
import Data.ByteString.Lazy (fromStrict)
|
|
|
|
import Data.Maybe (fromMaybe)
|
|
|
|
import Data.Profunctor.Product.Default (Default)
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Text.Encoding as E
|
|
|
|
import Database.PostgreSQL.Simple
|
|
|
|
import Control.Monad (void)
|
|
import Control.Monad.Reader (asks)
|
|
import Control.Monad.IO.Class (liftIO)
|
|
|
|
import Network.Mail.Mime
|
|
|
|
import System.Directory (doesFileExist)
|
|
|
|
-- internal imports
|
|
|
|
import Model
|
|
|
|
import Types
|
|
|
|
printSql :: Default Unpackspec a a => Select a -> IO ()
|
|
printSql = putStrLn . fromMaybe "Empty query" . showSqlForPostgres
|
|
|
|
initDB :: Connection -> IO ()
|
|
initDB conn = do
|
|
execute_ conn initAvatar
|
|
execute_ conn initUser
|
|
execute_ conn initProduct
|
|
execute_ conn initToken
|
|
execute_ conn initAuthData
|
|
execute_ conn initAmount
|
|
execute_ conn initJournal
|
|
execute_ conn initRole
|
|
execute_ conn initUserToRole
|
|
void $ runInsertInitialRoles conn
|
|
|
|
-- This is only a dummy function.
|
|
-- TODO: Replace with proper translation function(s)!
|
|
__ = id
|
|
|
|
sendUserNotification
|
|
:: UserDetails -- ^ The recipient
|
|
-> T.Text -- ^ The mail subject
|
|
-> T.Text -- ^ The mail body
|
|
-> MateHandler ()
|
|
sendUserNotification recipient subject message =
|
|
case userDetailsEmail recipient of
|
|
Just email ->
|
|
sendNotification (Mail
|
|
{ mailFrom = Address Nothing "noreply"
|
|
, mailTo =
|
|
[ Address
|
|
(Just $ userDetailsIdent recipient)
|
|
email
|
|
]
|
|
, mailCc = []
|
|
, mailBcc = []
|
|
, mailHeaders = [("Subject", subject)]
|
|
, mailParts =
|
|
[[ Part
|
|
{ partType = "text/plain; charset=utf-8"
|
|
, partEncoding = None
|
|
, partDisposition = DefaultDisposition
|
|
, partHeaders = []
|
|
, partContent = PartContent (fromStrict $ E.encodeUtf8 message)
|
|
}
|
|
]]
|
|
}
|
|
)
|
|
Nothing ->
|
|
return ()
|
|
|
|
sendAdminNotification
|
|
:: T.Text -- The mail subject
|
|
-> T.Text -- The mail body
|
|
-> MateHandler ()
|
|
sendAdminNotification subject message =
|
|
-- TODO: Grab administrators from settings and actually send the mails to them
|
|
return ()
|
|
|
|
sendNotification :: Mail -> MateHandler ()
|
|
sendNotification mail = do
|
|
sendmailPath <- asks rsSendmailPath
|
|
liftIO $ do
|
|
existence <- doesFileExist sendmailPath
|
|
if existence
|
|
then
|
|
renderSendMailCustom sendmailPath [] mail
|
|
else
|
|
print ("Warning: sending notification failed: Sendmail not present!"
|
|
:: String)
|