2014-08-28 16:08:56 +00:00
|
|
|
module Handler.AlbumSettings where
|
|
|
|
|
|
|
|
import Import
|
2014-08-29 22:19:52 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import System.Directory
|
|
|
|
import System.FilePath
|
2014-12-06 13:02:19 +00:00
|
|
|
import qualified Data.List as L
|
2014-08-28 16:08:56 +00:00
|
|
|
|
|
|
|
getAlbumSettingsR :: AlbumId -> Handler Html
|
|
|
|
getAlbumSettingsR albumId = do
|
|
|
|
tempAlbum <- runDB $ get albumId
|
|
|
|
case tempAlbum of
|
|
|
|
Just album -> do
|
|
|
|
ownerId <- return $ albumOwner album
|
|
|
|
msu <- lookupSession "userId"
|
|
|
|
case msu of
|
|
|
|
Just tempUserId -> do
|
|
|
|
userId <- return $ getUserIdFromText tempUserId
|
2014-12-06 03:39:24 +00:00
|
|
|
ownerPresence <- return (userId == ownerId)
|
|
|
|
presence <- return $ userId `elem` (albumShares album)
|
|
|
|
case ownerPresence || presence of
|
2014-08-28 16:08:56 +00:00
|
|
|
True -> do
|
2014-12-06 03:39:24 +00:00
|
|
|
entities <- runDB $ selectList [UserId !=. (albumOwner album)] [Desc UserName]
|
|
|
|
users <- return $ map (\u -> (userName $ entityVal u, entityKey u)) entities
|
|
|
|
(albumSettingsWidget, enctype) <- generateFormPost $ albumSettingsForm album albumId users
|
2014-08-28 16:08:56 +00:00
|
|
|
defaultLayout $ do
|
2014-09-24 21:03:18 +00:00
|
|
|
setTitle "Eidolon :: Album Settings"
|
2014-08-28 16:08:56 +00:00
|
|
|
$(widgetFile "albumSettings")
|
|
|
|
False -> do
|
|
|
|
setMessage "You must own this album to change its settings"
|
|
|
|
redirect $ AlbumR albumId
|
|
|
|
Nothing -> do
|
|
|
|
setMessage "You must be logged in to change settings"
|
|
|
|
redirect $ LoginR
|
|
|
|
Nothing -> do
|
|
|
|
setMessage "This album does not exist"
|
|
|
|
redirect $ HomeR
|
|
|
|
|
|
|
|
postAlbumSettingsR :: AlbumId -> Handler Html
|
|
|
|
postAlbumSettingsR albumId = do
|
|
|
|
tempAlbum <- runDB $ get albumId
|
|
|
|
case tempAlbum of
|
|
|
|
Just album -> do
|
|
|
|
ownerId <- return $ albumOwner album
|
|
|
|
owner <- runDB $ getJust ownerId
|
|
|
|
ownerName <- return $ userName owner
|
|
|
|
msu <- lookupSession "userId"
|
|
|
|
case msu of
|
|
|
|
Just tempUserId -> do
|
|
|
|
userId <- return $ getUserIdFromText tempUserId
|
2014-12-06 03:39:24 +00:00
|
|
|
ownerPresence <- return (userId == ownerId)
|
|
|
|
presence <- return $ userId `elem` (albumShares album)
|
|
|
|
case ownerPresence || presence of
|
2014-08-28 16:08:56 +00:00
|
|
|
True -> do
|
2014-12-06 03:39:24 +00:00
|
|
|
entities <- runDB $ selectList [UserId !=. (albumOwner album)] [Desc UserName]
|
|
|
|
users <- return $ map (\u -> (userName $ entityVal u, entityKey u)) entities
|
2014-12-28 06:12:25 +00:00
|
|
|
((result, _), _) <- runFormPost $ albumSettingsForm album albumId users
|
2014-08-28 16:08:56 +00:00
|
|
|
case result of
|
|
|
|
FormSuccess temp -> do
|
2014-12-06 13:02:19 +00:00
|
|
|
newShares <- return (L.sort $ albumShares temp)
|
|
|
|
oldShares <- return (L.sort $ albumShares album)
|
2014-12-28 06:12:25 +00:00
|
|
|
_ <- case newShares /= oldShares of
|
2014-12-06 13:02:19 +00:00
|
|
|
True -> do
|
2014-12-08 05:26:11 +00:00
|
|
|
link <- ($ AlbumR albumId) <$> getUrlRender
|
2014-12-06 13:02:19 +00:00
|
|
|
rcptIds <- return $ L.nub $ newShares L.\\ oldShares
|
2014-12-08 05:26:11 +00:00
|
|
|
mapM (\uId -> do
|
2014-12-06 13:02:19 +00:00
|
|
|
user <- runDB $ getJust uId
|
2014-12-08 05:26:11 +00:00
|
|
|
addr <- return $ userEmail user
|
|
|
|
sendMail addr "A new album was shared with you" $
|
|
|
|
[shamlet|
|
|
|
|
<h1>Hello #{userSlug user}!
|
|
|
|
<p>#{ownerName} was so kind to share his album #{albumTitle album} with you.
|
|
|
|
<p>You can find it
|
|
|
|
<a href=#{link}>
|
|
|
|
here
|
|
|
|
.
|
|
|
|
|]
|
2014-12-06 13:02:19 +00:00
|
|
|
) rcptIds
|
|
|
|
False -> do
|
|
|
|
return [()]
|
|
|
|
-- nothing to do here
|
2014-12-28 06:12:25 +00:00
|
|
|
_ <- runDB $ update albumId
|
2014-08-28 16:08:56 +00:00
|
|
|
[ AlbumTitle =. albumTitle temp
|
2014-12-06 13:02:19 +00:00
|
|
|
, AlbumShares =. newShares
|
2014-08-28 16:08:56 +00:00
|
|
|
, AlbumSamplePic =. albumSamplePic temp
|
|
|
|
]
|
|
|
|
setMessage "Album settings changed succesfully"
|
|
|
|
redirect $ AlbumR albumId
|
|
|
|
_ -> do
|
2014-09-07 23:47:17 +00:00
|
|
|
setMessage "There was an error while changing the settings"
|
2014-08-28 16:08:56 +00:00
|
|
|
redirect $ AlbumSettingsR albumId
|
|
|
|
False -> do
|
|
|
|
setMessage "You must own this album to change its settings"
|
|
|
|
redirect $ AlbumR albumId
|
|
|
|
Nothing -> do
|
|
|
|
setMessage "You must be logged in to change settings"
|
|
|
|
redirect $ LoginR
|
|
|
|
Nothing -> do
|
|
|
|
setMessage "This album does not exist"
|
|
|
|
redirect $ HomeR
|
|
|
|
|
2014-12-06 03:39:24 +00:00
|
|
|
albumSettingsForm :: Album -> AlbumId -> [(Text, UserId)]-> Form Album
|
|
|
|
albumSettingsForm album albumId users = renderDivs $ Album
|
2014-08-28 16:08:56 +00:00
|
|
|
<$> areq textField "Title" (Just $ albumTitle album)
|
|
|
|
<*> pure (albumOwner album)
|
2014-12-06 03:39:24 +00:00
|
|
|
<*> areq (userField users) "Share this album with" (Just $ albumShares album)
|
2014-08-28 16:08:56 +00:00
|
|
|
<*> pure (albumContent album)
|
|
|
|
<*> aopt (selectField media) "Sample picture" (Just $ albumSamplePic album)
|
|
|
|
where
|
|
|
|
media = do
|
2014-12-23 20:40:17 +00:00
|
|
|
entities <- runDB $ selectList [MediumAlbum ==. albumId] [Asc MediumTitle]
|
2014-12-03 21:01:23 +00:00
|
|
|
optionsPairs $ map (\med -> (mediumTitle $ entityVal med, mediumThumb (entityVal med))) entities
|
2014-12-06 03:39:24 +00:00
|
|
|
-- users = do
|
|
|
|
-- entities <- runDB $ selectList [UserId !=. (albumOwner album)] [Desc UserName]
|
|
|
|
-- return $ map (\u -> (userName $ entityVal u, entityKey u)) entities
|
2014-08-29 22:19:52 +00:00
|
|
|
|
|
|
|
getAlbumDeleteR :: AlbumId -> Handler Html
|
|
|
|
getAlbumDeleteR albumId = do
|
|
|
|
tempAlbum <- runDB $ get albumId
|
|
|
|
case tempAlbum of
|
|
|
|
Just album -> do
|
|
|
|
ownerId <- return $ albumOwner album
|
|
|
|
msu <- lookupSession "userId"
|
|
|
|
case msu of
|
|
|
|
Just tempUserId -> do
|
|
|
|
userId <- return $ getUserIdFromText tempUserId
|
|
|
|
presence <- return (userId == ownerId)
|
|
|
|
case presence of
|
|
|
|
True -> do
|
|
|
|
defaultLayout $ do
|
2014-09-24 21:03:18 +00:00
|
|
|
setTitle $ toHtml ("Eidolon :: Delete album" `T.append` (albumTitle album))
|
2014-08-29 22:19:52 +00:00
|
|
|
$(widgetFile "albumDelete")
|
|
|
|
False -> do
|
|
|
|
setMessage "You must own this album to delete it"
|
|
|
|
redirect $ AlbumR albumId
|
|
|
|
Nothing -> do
|
|
|
|
setMessage "You must be logged in to delete albums"
|
|
|
|
redirect $ LoginR
|
|
|
|
Nothing -> do
|
|
|
|
setMessage "This album does not exist"
|
|
|
|
redirect $ HomeR
|
|
|
|
|
|
|
|
postAlbumDeleteR :: AlbumId -> Handler Html
|
|
|
|
postAlbumDeleteR albumId = do
|
|
|
|
tempAlbum <- runDB $ get albumId
|
|
|
|
case tempAlbum of
|
|
|
|
Just album -> do
|
|
|
|
ownerId <- return $ albumOwner album
|
|
|
|
owner <- runDB $ getJust ownerId
|
|
|
|
msu <- lookupSession "userId"
|
|
|
|
case msu of
|
|
|
|
Just tempUserId -> do
|
|
|
|
userId <- return $ getUserIdFromText tempUserId
|
|
|
|
presence <- return (userId == ownerId)
|
|
|
|
case presence of
|
|
|
|
True -> do
|
|
|
|
confirm <- lookupPostParam "confirm"
|
|
|
|
case confirm of
|
|
|
|
Just "confirm" -> do
|
2014-09-07 07:23:12 +00:00
|
|
|
-- remove album reference from user
|
|
|
|
albumList <- return $ userAlbums owner
|
|
|
|
newAlbumList <- return $ removeItem albumId albumList
|
|
|
|
runDB $ update ownerId [UserAlbums =. newAlbumList]
|
2014-12-07 06:55:03 +00:00
|
|
|
-- delete album content and its comments
|
2014-12-28 06:12:25 +00:00
|
|
|
_ <- mapM (\a -> do
|
2014-12-07 06:55:03 +00:00
|
|
|
-- delete files
|
|
|
|
medium <- runDB $ getJust a
|
|
|
|
liftIO $ removeFile (normalise $ L.tail $ mediumPath medium)
|
|
|
|
liftIO $ removeFile (normalise $ L.tail $ mediumThumb medium)
|
|
|
|
-- delete comments
|
|
|
|
commEnts <- runDB $ selectList [CommentOrigin ==. a] []
|
2014-12-28 06:12:25 +00:00
|
|
|
_ <- mapM (\ent -> runDB $ delete $ entityKey ent) commEnts
|
2014-12-07 06:55:03 +00:00
|
|
|
runDB $ delete a
|
|
|
|
) (albumContent album)
|
2014-09-07 07:23:12 +00:00
|
|
|
-- delete album
|
2014-08-29 22:19:52 +00:00
|
|
|
runDB $ delete albumId
|
2014-09-07 07:23:12 +00:00
|
|
|
-- delete files
|
2014-08-29 22:19:52 +00:00
|
|
|
liftIO $ removeDirectoryRecursive $ "static" </> "data" </> (T.unpack $ extractKey userId) </> (T.unpack $ extractKey albumId)
|
2014-09-07 07:23:12 +00:00
|
|
|
-- outro
|
2014-08-29 22:19:52 +00:00
|
|
|
setMessage "Album deleted succesfully"
|
|
|
|
redirect $ HomeR
|
2014-08-30 19:19:41 +00:00
|
|
|
_ -> do
|
|
|
|
setMessage "You must confirm the deletion"
|
|
|
|
redirect $ AlbumSettingsR albumId
|
|
|
|
_ -> do
|
2014-08-29 22:19:52 +00:00
|
|
|
setMessage "You must own this album to delete it"
|
|
|
|
redirect $ AlbumR albumId
|
|
|
|
Nothing -> do
|
|
|
|
setMessage "You must be logged in to delete albums"
|
|
|
|
redirect $ LoginR
|
|
|
|
Nothing -> do
|
|
|
|
setMessage "This album does not exist"
|
|
|
|
redirect $ HomeR
|