eidolon/Handler/AlbumSettings.hs

224 lines
9.2 KiB
Haskell
Raw Permalink 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-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
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
2015-09-14 16:54:46 +00:00
let ownerId = albumOwner album
2017-04-26 19:43:22 +00:00
musername <- maybeAuthId
case musername of
Just username -> do
(Just (Entity uId u)) <- runDB $ getBy $ UniqueUser username
let ownerPresence = uId == ownerId
presence = uId `elem` (albumShares album)
if ownerPresence || presence
then do
entities <- runDB $ selectList [UserId !=. (albumOwner album)] [Desc UserName]
let users = map (\u -> (userName $ entityVal u, entityKey u)) entities
(albumSettingsWidget, enctype) <- generateFormPost $
renderBootstrap3 BootstrapBasicForm $
albumSettingsForm album albumId users
defaultLayout $ do
setTitle "Eidolon :: Album Settings"
$(widgetFile "albumSettings")
else do
setMessage "You must own this album to change its settings"
redirect $ AlbumR albumId
2014-08-28 16:08:56 +00:00
Nothing -> do
setMessage "You must be logged in to change settings"
2017-04-24 05:46:34 +00:00
redirect $ AuthR LoginR
2014-08-28 16:08:56 +00:00
Nothing -> do
setMessage "This album does not exist"
2015-09-14 16:54:46 +00:00
redirect HomeR
2014-08-28 16:08:56 +00:00
postAlbumSettingsR :: AlbumId -> Handler Html
postAlbumSettingsR albumId = do
tempAlbum <- runDB $ get albumId
case tempAlbum of
Just album -> do
2015-09-14 16:54:46 +00:00
let ownerId = albumOwner album
2014-08-28 16:08:56 +00:00
owner <- runDB $ getJust ownerId
2015-09-14 16:54:46 +00:00
let ownerName = userName owner
2017-04-26 19:43:22 +00:00
musername <- maybeAuthId
case musername of
Just username -> do
(Just (Entity uId u)) <- runDB $ getBy $ UniqueUser username
let ownerPresence = uId == ownerId
presence = uId `elem` albumShares album
if ownerPresence || presence
then do
entities <- runDB $ selectList [UserId !=. (albumOwner album)] [Desc UserName]
let users = map (\u -> (userName $ entityVal u, entityKey u)) entities
((result, _), _) <- runFormPost $
renderBootstrap3 BootstrapBasicForm $
albumSettingsForm album albumId users
case result of
FormSuccess temp -> do
let newShares = L.sort $ albumShares temp
let oldShares = L.sort $ albumShares album
_ <- if
newShares /= oldShares
then do
link <- ($ AlbumR albumId) <$> getUrlRender
let rcptIds = L.nub $ newShares L.\\ oldShares
mapM (\id -> do
-- update userAlbums
user <- runDB $ getJust id
let oldAlbs = userAlbums user
newAlbs = albumId : oldAlbs
_ <- runDB $ update id [UserAlbums =. newAlbs]
-- send notification
let addr = 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
.
|]
) rcptIds
else do
return [()]
-- nothing to do here
_ <- runDB $ update albumId
[ AlbumTitle =. albumTitle temp
, AlbumShares =. newShares
, AlbumSamplePic =. albumSamplePic temp
]
setMessage "Album settings changed succesfully"
redirect $ AlbumR albumId
_ -> do
setMessage "There was an error while changing the settings"
redirect $ AlbumSettingsR albumId
else do
setMessage "You must own this album to change its settings"
redirect $ AlbumR albumId
2014-08-28 16:08:56 +00:00
Nothing -> do
setMessage "You must be logged in to change settings"
2017-04-24 05:46:34 +00:00
redirect $ AuthR LoginR
2014-08-28 16:08:56 +00:00
Nothing -> do
setMessage "This album does not exist"
2015-09-14 16:54:46 +00:00
redirect HomeR
2014-08-28 16:08:56 +00:00
2015-10-19 06:32:47 +00:00
albumSettingsForm :: Album -> AlbumId -> [(Text, UserId)]-> AForm Handler Album
albumSettingsForm album albumId users = Album
<$> areq textField (bfs ("Title" :: T.Text)) (Just $ albumTitle album)
2014-08-28 16:08:56 +00:00
<*> pure (albumOwner album)
2015-10-19 06:32:47 +00:00
<*> areq (userField users) (bfs ("Share this album with" :: T.Text)) (Just $ albumShares album)
2014-08-28 16:08:56 +00:00
<*> pure (albumContent album)
2015-10-19 06:32:47 +00:00
<*> aopt (selectField media) (bfs ("Sample picture" :: T.Text)) (Just $ albumSamplePic album)
<* bootstrapSubmit ("Change settings" :: BootstrapSubmit Text)
2014-08-28 16:08:56 +00:00
where
media = do
entities <- runDB $ selectList [MediumAlbum ==. albumId] [Asc MediumTitle]
optionsPairs $ map (\med -> (mediumTitle $ entityVal med, mediumThumb (entityVal med))) 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
2015-09-14 16:54:46 +00:00
let ownerId = albumOwner album
2017-04-26 19:43:22 +00:00
musername <- maybeAuthId
case musername of
Just username -> do
(Just (Entity uId u)) <- runDB $ getBy $ UniqueUser username
if uId == ownerId
then do
defaultLayout $ do
setTitle $ toHtml ("Eidolon :: Delete album" `T.append` (albumTitle album))
$(widgetFile "albumDelete")
else do
setMessage "You must own this album to delete it"
redirect $ AlbumR albumId
2014-08-29 22:19:52 +00:00
Nothing -> do
setMessage "You must be logged in to delete albums"
2017-04-24 05:46:34 +00:00
redirect $ AuthR LoginR
2014-08-29 22:19:52 +00:00
Nothing -> do
setMessage "This album does not exist"
2015-09-14 16:54:46 +00:00
redirect HomeR
2014-08-29 22:19:52 +00:00
postAlbumDeleteR :: AlbumId -> Handler Html
postAlbumDeleteR albumId = do
tempAlbum <- runDB $ get albumId
case tempAlbum of
Just album -> do
2015-09-14 16:54:46 +00:00
let ownerId = albumOwner album
2014-08-29 22:19:52 +00:00
owner <- runDB $ getJust ownerId
2017-04-26 19:43:22 +00:00
musername <- maybeAuthId
case musername of
Just username -> do
(Just (Entity uId u)) <- runDB $ getBy $ UniqueUser username
if uId == ownerId
then do
confirm <- lookupPostParam "confirm"
case confirm of
Just "confirm" -> do
-- remove album reference from user
let albumList = userAlbums owner
let newAlbumList = removeItem albumId albumList
runDB $ update ownerId [UserAlbums =. newAlbumList]
-- delete album content and its comments
_ <- mapM (\a -> do
2014-09-07 07:23:12 +00:00
-- delete files
2017-04-26 19:43:22 +00:00
medium <- runDB $ getJust a
liftIO $ removeFile (normalise $ L.tail $ mediumPath medium)
liftIO $ removeFile (normalise $ L.tail $ mediumThumb medium)
liftIO $ removeFile (normalise $ L.tail $ mediumPreview medium)
-- delete comments
commEnts <- runDB $ selectList [CommentOrigin ==. a] []
_ <- mapM (\c -> do
children <- runDB $ selectList [CommentParent ==. (Just $ entityKey c)] []
_ <- mapM (\child -> do
-- delete comment children from elasticsearch and db
runDB $ delete $ entityKey child
) children
-- delete comment from elasticsearch
runDB $ delete $ entityKey c) commEnts
runDB $ delete a
) (albumContent album)
-- delete album
runDB $ delete albumId
-- delete files
liftIO $ removeDirectoryRecursive $ "static" </> "data" </> T.unpack (extractKey uId) </> T.unpack (extractKey albumId)
-- outro
setMessage "Album deleted succesfully"
redirect HomeR
_ -> do
setMessage "You must confirm the deletion"
redirect $ AlbumSettingsR albumId
else do
setMessage "You must own this album to delete it"
redirect $ AlbumR albumId
2014-08-29 22:19:52 +00:00
Nothing -> do
setMessage "You must be logged in to delete albums"
2017-04-24 05:46:34 +00:00
redirect $ AuthR LoginR
2014-08-29 22:19:52 +00:00
Nothing -> do
setMessage "This album does not exist"
2015-09-14 16:54:46 +00:00
redirect HomeR