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
|
2015-10-17 23:28:25 +00:00
|
|
|
import Handler.Commons
|
2014-08-29 22:19:52 +00:00
|
|
|
import qualified Data.Text as T
|
2015-01-11 07:57:38 +00:00
|
|
|
import Data.Maybe
|
2014-08-29 22:19:52 +00:00
|
|
|
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
|
2015-09-14 16:54:46 +00:00
|
|
|
let ownerId = albumOwner album
|
2014-08-28 16:08:56 +00:00
|
|
|
msu <- lookupSession "userId"
|
|
|
|
case msu of
|
|
|
|
Just tempUserId -> do
|
2015-09-14 16:54:46 +00:00
|
|
|
let userId = getUserIdFromText tempUserId
|
|
|
|
let ownerPresence = userId == ownerId
|
|
|
|
let presence = userId `elem` (albumShares album)
|
2014-12-06 03:39:24 +00:00
|
|
|
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]
|
2015-09-14 16:54:46 +00:00
|
|
|
let users = map (\u -> (userName $ entityVal u, entityKey u)) entities
|
2015-10-19 06:32:47 +00:00
|
|
|
(albumSettingsWidget, enctype) <- generateFormPost $
|
|
|
|
renderBootstrap3 BootstrapBasicForm $
|
|
|
|
albumSettingsForm album albumId users
|
2015-01-11 07:06:48 +00:00
|
|
|
formLayout $ 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"
|
2015-09-14 16:54:46 +00:00
|
|
|
redirect 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
|
2014-08-28 16:08:56 +00:00
|
|
|
msu <- lookupSession "userId"
|
|
|
|
case msu of
|
|
|
|
Just tempUserId -> do
|
2015-09-14 16:54:46 +00:00
|
|
|
let userId = getUserIdFromText tempUserId
|
|
|
|
let ownerPresence = userId == ownerId
|
|
|
|
let presence = userId `elem` albumShares album
|
|
|
|
if
|
|
|
|
ownerPresence || presence
|
|
|
|
then do
|
2014-12-06 03:39:24 +00:00
|
|
|
entities <- runDB $ selectList [UserId !=. (albumOwner album)] [Desc UserName]
|
2015-09-14 16:54:46 +00:00
|
|
|
let users = map (\u -> (userName $ entityVal u, entityKey u)) entities
|
2015-10-19 06:32:47 +00:00
|
|
|
((result, _), _) <- runFormPost $
|
|
|
|
renderBootstrap3 BootstrapBasicForm $
|
|
|
|
albumSettingsForm album albumId users
|
2014-08-28 16:08:56 +00:00
|
|
|
case result of
|
|
|
|
FormSuccess temp -> do
|
2015-09-14 16:54:46 +00:00
|
|
|
let newShares = L.sort $ albumShares temp
|
|
|
|
let oldShares = L.sort $ albumShares album
|
|
|
|
_ <- if
|
|
|
|
newShares /= oldShares
|
|
|
|
then do
|
2014-12-08 05:26:11 +00:00
|
|
|
link <- ($ AlbumR albumId) <$> getUrlRender
|
2015-09-14 16:54:46 +00:00
|
|
|
let rcptIds = L.nub $ newShares L.\\ oldShares
|
2014-12-08 05:26:11 +00:00
|
|
|
mapM (\uId -> do
|
2015-01-11 07:06:48 +00:00
|
|
|
-- update userAlbums
|
2014-12-06 13:02:19 +00:00
|
|
|
user <- runDB $ getJust uId
|
2015-09-14 16:54:46 +00:00
|
|
|
let oldAlbs = userAlbums user
|
|
|
|
let newAlbs = albumId : oldAlbs
|
2015-01-11 07:06:48 +00:00
|
|
|
_ <- runDB $ update uId [UserAlbums =. newAlbs]
|
|
|
|
-- send notification
|
2015-09-14 16:54:46 +00:00
|
|
|
let addr = userEmail user
|
2014-12-08 05:26:11 +00:00
|
|
|
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
|
2015-09-14 16:54:46 +00:00
|
|
|
else do
|
2014-12-06 13:02:19 +00:00
|
|
|
return [()]
|
|
|
|
-- nothing to do here
|
2015-01-11 07:57:38 +00:00
|
|
|
width <- getThumbWidth $ Just $ L.tail $ fromMaybe ['a'] $ albumSamplePic temp
|
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
|
2015-01-11 07:06:48 +00:00
|
|
|
, AlbumSampleWidth =. width
|
2014-08-28 16:08:56 +00:00
|
|
|
]
|
2015-10-17 23:28:25 +00:00
|
|
|
putIndexES (ESAlbum albumId temp)
|
2014-08-28 16:08:56 +00:00
|
|
|
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
|
2015-09-14 16:54:46 +00:00
|
|
|
else do
|
2014-08-28 16:08:56 +00:00
|
|
|
setMessage "You must own this album to change its settings"
|
|
|
|
redirect $ AlbumR albumId
|
|
|
|
Nothing -> do
|
|
|
|
setMessage "You must be logged in to change settings"
|
2015-09-14 16:54:46 +00:00
|
|
|
redirect 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)
|
2015-01-11 07:06:48 +00:00
|
|
|
<*> pure 230
|
2015-10-19 06:32:47 +00:00
|
|
|
<* bootstrapSubmit ("Change settings" :: BootstrapSubmit Text)
|
2014-08-28 16:08:56 +00:00
|
|
|
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-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
|
2014-08-29 22:19:52 +00:00
|
|
|
msu <- lookupSession "userId"
|
|
|
|
case msu of
|
|
|
|
Just tempUserId -> do
|
2015-09-14 16:54:46 +00:00
|
|
|
let userId = getUserIdFromText tempUserId
|
|
|
|
if
|
|
|
|
userId == ownerId
|
|
|
|
then do
|
2015-01-11 07:06:48 +00:00
|
|
|
formLayout $ 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")
|
2015-09-14 16:54:46 +00:00
|
|
|
else 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"
|
2015-09-14 16:54:46 +00:00
|
|
|
redirect 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
|
|
|
|
msu <- lookupSession "userId"
|
|
|
|
case msu of
|
|
|
|
Just tempUserId -> do
|
2015-09-14 16:54:46 +00:00
|
|
|
let userId = getUserIdFromText tempUserId
|
|
|
|
if
|
|
|
|
userId == ownerId
|
|
|
|
then do
|
2014-08-29 22:19:52 +00:00
|
|
|
confirm <- lookupPostParam "confirm"
|
|
|
|
case confirm of
|
|
|
|
Just "confirm" -> do
|
2014-09-07 07:23:12 +00:00
|
|
|
-- remove album reference from user
|
2015-09-14 16:54:46 +00:00
|
|
|
let albumList = userAlbums owner
|
|
|
|
let newAlbumList = removeItem albumId albumList
|
2014-09-07 07:23:12 +00:00
|
|
|
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)
|
2016-05-04 00:26:48 +00:00
|
|
|
liftIO $ removeFile (normalise $ L.tail $ mediumPreview medium)
|
2015-10-16 19:57:18 +00:00
|
|
|
-- delete medium from elasticsearch
|
2015-10-17 23:28:25 +00:00
|
|
|
deleteIndexES (ESMedium a medium)
|
2014-12-07 06:55:03 +00:00
|
|
|
-- delete comments
|
|
|
|
commEnts <- runDB $ selectList [CommentOrigin ==. a] []
|
2015-10-16 19:57:18 +00:00
|
|
|
_ <- mapM (\c -> do
|
|
|
|
children <- runDB $ selectList [CommentParent ==. (Just $ entityKey c)] []
|
|
|
|
_ <- mapM (\child -> do
|
|
|
|
-- delete comment children from elasticsearch and db
|
2015-10-17 23:28:25 +00:00
|
|
|
deleteIndexES (ESComment (entityKey child) (entityVal child))
|
2015-10-16 19:57:18 +00:00
|
|
|
runDB $ delete $ entityKey child
|
|
|
|
) children
|
|
|
|
-- delete comment from elasticsearch
|
2015-10-17 23:28:25 +00:00
|
|
|
deleteIndexES (ESComment (entityKey c) (entityVal c))
|
2015-10-16 19:57:18 +00:00
|
|
|
runDB $ delete $ entityKey c) 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
|
2015-09-14 16:54:46 +00:00
|
|
|
liftIO $ removeDirectoryRecursive $ "static" </> "data" </> T.unpack (extractKey userId) </> T.unpack (extractKey albumId)
|
2015-10-16 13:32:47 +00:00
|
|
|
-- delete from elasticsearch
|
2015-10-17 23:28:25 +00:00
|
|
|
deleteIndexES (ESAlbum albumId album)
|
2014-09-07 07:23:12 +00:00
|
|
|
-- outro
|
2014-08-29 22:19:52 +00:00
|
|
|
setMessage "Album deleted succesfully"
|
2015-09-14 16:54:46 +00:00
|
|
|
redirect HomeR
|
2014-08-30 19:19:41 +00:00
|
|
|
_ -> do
|
|
|
|
setMessage "You must confirm the deletion"
|
|
|
|
redirect $ AlbumSettingsR albumId
|
2015-09-14 16:54:46 +00:00
|
|
|
else 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"
|
2015-09-14 16:54:46 +00:00
|
|
|
redirect 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
|