2016-02-18 06:53:53 +09:00
|
|
|
{- This file is part of Vervis.
|
|
|
|
-
|
2019-01-28 23:43:07 +09:00
|
|
|
- Written in 2016, 2019 by fr33domlover <fr33domlover@riseup.net>.
|
2016-02-18 06:53:53 +09:00
|
|
|
-
|
|
|
|
- ♡ Copying is an act of love. Please copy, reuse and share.
|
|
|
|
-
|
|
|
|
- The author(s) have dedicated all copyright and related and neighboring
|
|
|
|
- rights to this software to the public domain worldwide. This software is
|
|
|
|
- distributed without any warranty.
|
|
|
|
-
|
|
|
|
- You should have received a copy of the CC0 Public Domain Dedication along
|
|
|
|
- with this software. If not, see
|
|
|
|
- <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
-}
|
|
|
|
|
2016-02-23 17:45:03 +09:00
|
|
|
module Vervis.Handler.Project
|
2016-02-18 06:53:53 +09:00
|
|
|
( getProjectsR
|
2016-02-25 12:10:30 +09:00
|
|
|
, postProjectsR
|
|
|
|
, getProjectNewR
|
2016-02-18 06:53:53 +09:00
|
|
|
, getProjectR
|
2016-06-05 19:43:28 +09:00
|
|
|
, putProjectR
|
|
|
|
, postProjectR
|
|
|
|
, getProjectEditR
|
2016-06-01 17:52:14 +09:00
|
|
|
, getProjectDevsR
|
|
|
|
, postProjectDevsR
|
|
|
|
, getProjectDevNewR
|
|
|
|
, getProjectDevR
|
|
|
|
, deleteProjectDevR
|
|
|
|
, postProjectDevR
|
2016-02-18 06:53:53 +09:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2016-05-14 20:36:45 +09:00
|
|
|
import Prelude
|
|
|
|
|
|
|
|
import Data.Maybe (fromMaybe)
|
|
|
|
import Data.Text (Text)
|
|
|
|
import Database.Persist
|
2016-06-01 17:52:14 +09:00
|
|
|
import Database.Esqueleto hiding (delete, (%), (==.))
|
2016-05-14 20:36:45 +09:00
|
|
|
import Text.Blaze.Html (Html)
|
2016-06-06 15:48:59 +09:00
|
|
|
import Yesod.Auth (requireAuthId)
|
2019-03-20 21:01:10 +09:00
|
|
|
import Yesod.Core
|
2016-06-01 17:52:14 +09:00
|
|
|
import Yesod.Core.Handler (redirect, setMessage, lookupPostParam, notFound)
|
2016-05-14 20:36:45 +09:00
|
|
|
import Yesod.Form.Functions (runFormPost)
|
|
|
|
import Yesod.Form.Types (FormResult (..))
|
2016-08-09 04:05:22 +09:00
|
|
|
import Yesod.Persist.Core (runDB, get404, getBy404)
|
2016-05-14 20:36:45 +09:00
|
|
|
|
|
|
|
import qualified Database.Esqueleto as E
|
2016-02-18 06:53:53 +09:00
|
|
|
|
2019-03-20 21:01:10 +09:00
|
|
|
import Network.FedURI
|
|
|
|
import Web.ActivityPub
|
|
|
|
import Yesod.FedURI
|
|
|
|
|
2016-02-25 12:10:30 +09:00
|
|
|
import Vervis.Form.Project
|
2016-05-14 20:36:45 +09:00
|
|
|
import Vervis.Foundation
|
|
|
|
import Vervis.Model
|
2016-05-24 05:46:54 +09:00
|
|
|
import Vervis.Model.Ident
|
2016-05-14 20:36:45 +09:00
|
|
|
import Vervis.Model.Repo
|
|
|
|
import Vervis.Settings
|
2016-06-01 17:52:14 +09:00
|
|
|
import Vervis.Widget.Sharer
|
2016-08-09 04:05:22 +09:00
|
|
|
import Vervis.Widget.Workflow
|
2016-02-18 06:53:53 +09:00
|
|
|
|
2016-05-23 21:24:14 +09:00
|
|
|
getProjectsR :: ShrIdent -> Handler Html
|
2016-02-18 06:53:53 +09:00
|
|
|
getProjectsR ident = do
|
2016-06-01 17:52:14 +09:00
|
|
|
projects <- runDB $ select $ from $ \ (sharer, project) -> do
|
|
|
|
where_ $
|
|
|
|
sharer ^. SharerIdent E.==. val ident &&.
|
|
|
|
sharer ^. SharerId E.==. project ^. ProjectSharer
|
|
|
|
orderBy [asc $ project ^. ProjectIdent]
|
|
|
|
return $ project ^. ProjectIdent
|
2016-05-14 07:11:46 +09:00
|
|
|
defaultLayout $(widgetFile "project/list")
|
2016-02-18 06:53:53 +09:00
|
|
|
|
2016-05-23 21:24:14 +09:00
|
|
|
postProjectsR :: ShrIdent -> Handler Html
|
2016-06-06 15:48:59 +09:00
|
|
|
postProjectsR shr = do
|
|
|
|
Entity sid _ <- runDB $ getBy404 $ UniqueSharer shr
|
2016-06-07 02:29:54 +09:00
|
|
|
((result, widget), enctype) <- runFormPost $ newProjectForm sid
|
2016-02-25 12:10:30 +09:00
|
|
|
case result of
|
2016-06-06 15:48:59 +09:00
|
|
|
FormSuccess np -> do
|
2016-06-07 02:29:54 +09:00
|
|
|
pid <- requireAuthId
|
2016-06-06 15:48:59 +09:00
|
|
|
runDB $ do
|
|
|
|
let project = Project
|
|
|
|
{ projectIdent = npIdent np
|
|
|
|
, projectSharer = sid
|
|
|
|
, projectName = npName np
|
|
|
|
, projectDesc = npDesc np
|
2016-08-09 04:05:22 +09:00
|
|
|
, projectWorkflow = npWflow np
|
2016-06-06 15:48:59 +09:00
|
|
|
, projectNextTicket = 1
|
|
|
|
, projectWiki = Nothing
|
2019-01-30 07:24:32 +09:00
|
|
|
, projectCollabAnon = Nothing
|
|
|
|
, projectCollabUser = Nothing
|
2016-06-06 15:48:59 +09:00
|
|
|
}
|
|
|
|
jid <- insert project
|
|
|
|
let collab = ProjectCollab
|
|
|
|
{ projectCollabProject = jid
|
|
|
|
, projectCollabPerson = pid
|
|
|
|
, projectCollabRole = npRole np
|
|
|
|
}
|
|
|
|
insert_ collab
|
2016-02-25 12:10:30 +09:00
|
|
|
setMessage "Project added."
|
2016-06-06 15:48:59 +09:00
|
|
|
redirect $ ProjectR shr (npIdent np)
|
2016-02-25 12:10:30 +09:00
|
|
|
FormMissing -> do
|
|
|
|
setMessage "Field(s) missing"
|
2016-05-14 07:11:46 +09:00
|
|
|
defaultLayout $(widgetFile "project/new")
|
2016-05-14 07:07:56 +09:00
|
|
|
FormFailure _l -> do
|
2016-05-14 07:06:23 +09:00
|
|
|
setMessage "Project creation failed, see below"
|
2016-05-14 07:11:46 +09:00
|
|
|
defaultLayout $(widgetFile "project/new")
|
2016-02-25 12:10:30 +09:00
|
|
|
|
2016-05-23 21:24:14 +09:00
|
|
|
getProjectNewR :: ShrIdent -> Handler Html
|
2016-06-06 15:48:59 +09:00
|
|
|
getProjectNewR shr = do
|
|
|
|
Entity sid _ <- runDB $ getBy404 $ UniqueSharer shr
|
2016-06-07 02:29:54 +09:00
|
|
|
((_result, widget), enctype) <- runFormPost $ newProjectForm sid
|
2016-05-14 07:11:46 +09:00
|
|
|
defaultLayout $(widgetFile "project/new")
|
2016-02-25 12:10:30 +09:00
|
|
|
|
2019-03-20 21:01:10 +09:00
|
|
|
getProjectR :: ShrIdent -> PrjIdent -> Handler TypedContent
|
|
|
|
getProjectR shar proj = selectRep $ do
|
|
|
|
provideRep $ do
|
|
|
|
(project, workflow, wsharer, repos) <- runDB $ do
|
|
|
|
Entity sid s <- getBy404 $ UniqueSharer shar
|
|
|
|
Entity pid p <- getBy404 $ UniqueProject proj sid
|
|
|
|
w <- get404 $ projectWorkflow p
|
|
|
|
sw <-
|
|
|
|
if workflowSharer w == sid
|
|
|
|
then return s
|
|
|
|
else get404 $ workflowSharer w
|
|
|
|
rs <- selectList [RepoProject ==. Just pid] [Asc RepoIdent]
|
|
|
|
return (p, w, sw, rs)
|
|
|
|
defaultLayout $(widgetFile "project/one")
|
|
|
|
provideAP $ do
|
|
|
|
project <- runDB $ do
|
|
|
|
Entity sid _s <- getBy404 $ UniqueSharer shar
|
|
|
|
Entity _pid p <- getBy404 $ UniqueProject proj sid
|
|
|
|
return p
|
2019-05-21 08:51:06 +09:00
|
|
|
route2fed <- getEncodeRouteHome
|
2019-03-20 21:01:10 +09:00
|
|
|
route2local <- getEncodeRouteLocal
|
|
|
|
let (host, me) = f2l $ route2fed $ ProjectR shar proj
|
|
|
|
return $ Doc host Actor
|
|
|
|
{ actorId = me
|
|
|
|
, actorType = ActorTypeProject
|
|
|
|
, actorUsername = Nothing
|
|
|
|
, actorName =
|
|
|
|
Just $ fromMaybe (prj2text proj) $ projectName project
|
|
|
|
, actorSummary = projectDesc project
|
2019-04-21 19:58:57 +09:00
|
|
|
, actorInbox = route2local $ ProjectInboxR shar proj
|
2019-04-26 22:04:00 +09:00
|
|
|
, actorPublicKeys =
|
|
|
|
[ Left $ route2local ActorKey1R
|
|
|
|
, Left $ route2local ActorKey2R
|
|
|
|
]
|
2019-03-20 21:01:10 +09:00
|
|
|
}
|
2016-06-01 17:52:14 +09:00
|
|
|
|
2016-06-05 19:43:28 +09:00
|
|
|
putProjectR :: ShrIdent -> PrjIdent -> Handler Html
|
|
|
|
putProjectR shr prj = do
|
2019-01-30 07:24:32 +09:00
|
|
|
(sid, ep@(Entity jid _)) <- runDB $ do
|
2016-06-05 19:43:28 +09:00
|
|
|
Entity sid _sharer <- getBy404 $ UniqueSharer shr
|
2019-01-30 07:24:32 +09:00
|
|
|
eproj <- getBy404 $ UniqueProject prj sid
|
|
|
|
return (sid, eproj)
|
|
|
|
((result, widget), enctype) <- runFormPost $ editProjectForm sid ep
|
2016-06-05 19:43:28 +09:00
|
|
|
case result of
|
|
|
|
FormSuccess project' -> do
|
|
|
|
runDB $ replace jid project'
|
|
|
|
setMessage "Project updated."
|
|
|
|
redirect $ ProjectR shr prj
|
|
|
|
FormMissing -> do
|
|
|
|
setMessage "Field(s) missing."
|
|
|
|
defaultLayout $(widgetFile "project/edit")
|
|
|
|
FormFailure _l -> do
|
|
|
|
setMessage "Project update failed, see errors below."
|
|
|
|
defaultLayout $(widgetFile "project/edit")
|
|
|
|
|
|
|
|
postProjectR :: ShrIdent -> PrjIdent -> Handler Html
|
|
|
|
postProjectR shr prj = do
|
|
|
|
mmethod <- lookupPostParam "_method"
|
|
|
|
case mmethod of
|
|
|
|
Just "PUT" -> putProjectR shr prj
|
|
|
|
_ -> notFound
|
|
|
|
|
|
|
|
getProjectEditR :: ShrIdent -> PrjIdent -> Handler Html
|
|
|
|
getProjectEditR shr prj = do
|
2019-01-30 07:24:32 +09:00
|
|
|
(sid, ep) <- runDB $ do
|
2016-06-05 19:43:28 +09:00
|
|
|
Entity sid _sharer <- getBy404 $ UniqueSharer shr
|
2019-01-30 07:24:32 +09:00
|
|
|
ep <- getBy404 $ UniqueProject prj sid
|
|
|
|
return (sid, ep)
|
|
|
|
((_result, widget), enctype) <- runFormPost $ editProjectForm sid ep
|
2016-06-05 19:43:28 +09:00
|
|
|
defaultLayout $(widgetFile "project/edit")
|
|
|
|
|
2016-06-01 17:52:14 +09:00
|
|
|
getProjectDevsR :: ShrIdent -> PrjIdent -> Handler Html
|
2019-01-28 23:43:07 +09:00
|
|
|
getProjectDevsR shr prj = do
|
2016-06-01 17:52:14 +09:00
|
|
|
devs <- runDB $ do
|
2019-01-28 23:43:07 +09:00
|
|
|
jid <- do
|
|
|
|
Entity sid _ <- getBy404 $ UniqueSharer shr
|
|
|
|
Entity jid _ <- getBy404 $ UniqueProject prj sid
|
|
|
|
return jid
|
|
|
|
select $ from $ \ (collab `InnerJoin`
|
|
|
|
person `InnerJoin`
|
|
|
|
sharer `LeftOuterJoin`
|
|
|
|
role) -> do
|
|
|
|
on $ collab ^. ProjectCollabRole E.==. role ?. ProjectRoleId
|
|
|
|
on $ person ^. PersonIdent E.==. sharer ^. SharerId
|
|
|
|
on $ collab ^. ProjectCollabPerson E.==. person ^. PersonId
|
|
|
|
where_ $ collab ^. ProjectCollabProject E.==. val jid
|
|
|
|
return (sharer, role ?. ProjectRoleIdent)
|
2016-06-01 17:52:14 +09:00
|
|
|
defaultLayout $(widgetFile "project/collab/list")
|
|
|
|
|
|
|
|
postProjectDevsR :: ShrIdent -> PrjIdent -> Handler Html
|
|
|
|
postProjectDevsR shr rp = do
|
2016-06-07 02:29:54 +09:00
|
|
|
(sid, jid) <- runDB $ do
|
2016-06-01 17:52:14 +09:00
|
|
|
Entity s _ <- getBy404 $ UniqueSharer shr
|
2016-06-07 02:29:54 +09:00
|
|
|
Entity j _ <- getBy404 $ UniqueProject rp s
|
|
|
|
return (s, j)
|
|
|
|
((result, widget), enctype) <- runFormPost $ newProjectCollabForm sid jid
|
2016-06-01 17:52:14 +09:00
|
|
|
case result of
|
|
|
|
FormSuccess nc -> do
|
|
|
|
runDB $ do
|
|
|
|
let collab = ProjectCollab
|
2016-06-07 02:29:54 +09:00
|
|
|
{ projectCollabProject = jid
|
2016-06-01 17:52:14 +09:00
|
|
|
, projectCollabPerson = ncPerson nc
|
|
|
|
, projectCollabRole = ncRole nc
|
|
|
|
}
|
|
|
|
insert_ collab
|
|
|
|
setMessage "Collaborator added."
|
|
|
|
redirect $ ProjectDevsR shr rp
|
|
|
|
FormMissing -> do
|
|
|
|
setMessage "Field(s) missing"
|
|
|
|
defaultLayout $(widgetFile "project/collab/new")
|
|
|
|
FormFailure _l -> do
|
|
|
|
setMessage "Operation failed, see errors below"
|
|
|
|
defaultLayout $(widgetFile "project/collab/new")
|
|
|
|
|
|
|
|
getProjectDevNewR :: ShrIdent -> PrjIdent -> Handler Html
|
|
|
|
getProjectDevNewR shr rp = do
|
2016-06-07 02:29:54 +09:00
|
|
|
(sid, jid) <- runDB $ do
|
2016-06-01 17:52:14 +09:00
|
|
|
Entity s _ <- getBy404 $ UniqueSharer shr
|
2016-06-07 02:29:54 +09:00
|
|
|
Entity j _ <- getBy404 $ UniqueProject rp s
|
|
|
|
return (s, j)
|
|
|
|
((_result, widget), enctype) <- runFormPost $ newProjectCollabForm sid jid
|
2016-06-01 17:52:14 +09:00
|
|
|
defaultLayout $(widgetFile "project/collab/new")
|
|
|
|
|
|
|
|
getProjectDevR :: ShrIdent -> PrjIdent -> ShrIdent -> Handler Html
|
2019-01-28 23:43:07 +09:00
|
|
|
getProjectDevR shr prj dev = do
|
|
|
|
mrl <- runDB $ do
|
2016-06-07 02:29:54 +09:00
|
|
|
jid <- do
|
2016-06-01 17:52:14 +09:00
|
|
|
Entity s _ <- getBy404 $ UniqueSharer shr
|
2019-01-28 23:43:07 +09:00
|
|
|
Entity j _ <- getBy404 $ UniqueProject prj s
|
2016-06-07 02:29:54 +09:00
|
|
|
return j
|
2016-06-01 17:52:14 +09:00
|
|
|
pid <- do
|
|
|
|
Entity s _ <- getBy404 $ UniqueSharer dev
|
|
|
|
Entity p _ <- getBy404 $ UniquePersonIdent s
|
|
|
|
return p
|
2016-06-07 02:29:54 +09:00
|
|
|
Entity _cid collab <- getBy404 $ UniqueProjectCollab jid pid
|
2019-01-28 23:43:07 +09:00
|
|
|
fmap projectRoleIdent <$> traverse getJust (projectCollabRole collab)
|
2016-06-01 17:52:14 +09:00
|
|
|
defaultLayout $(widgetFile "project/collab/one")
|
|
|
|
|
|
|
|
deleteProjectDevR :: ShrIdent -> PrjIdent -> ShrIdent -> Handler Html
|
|
|
|
deleteProjectDevR shr rp dev = do
|
|
|
|
runDB $ do
|
2016-06-07 02:29:54 +09:00
|
|
|
jid <- do
|
2016-06-01 17:52:14 +09:00
|
|
|
Entity s _ <- getBy404 $ UniqueSharer shr
|
2016-06-07 02:29:54 +09:00
|
|
|
Entity j _ <- getBy404 $ UniqueProject rp s
|
|
|
|
return j
|
2016-06-01 17:52:14 +09:00
|
|
|
pid <- do
|
|
|
|
Entity s _ <- getBy404 $ UniqueSharer dev
|
|
|
|
Entity p _ <- getBy404 $ UniquePersonIdent s
|
|
|
|
return p
|
2016-06-07 02:29:54 +09:00
|
|
|
Entity cid _collab <- getBy404 $ UniqueProjectCollab jid pid
|
2016-06-01 17:52:14 +09:00
|
|
|
delete cid
|
|
|
|
setMessage "Collaborator removed."
|
|
|
|
redirect $ ProjectDevsR shr rp
|
|
|
|
|
|
|
|
postProjectDevR :: ShrIdent -> PrjIdent -> ShrIdent -> Handler Html
|
|
|
|
postProjectDevR shr rp dev = do
|
|
|
|
mmethod <- lookupPostParam "_method"
|
|
|
|
case mmethod of
|
|
|
|
Just "DELETE" -> deleteProjectDevR shr rp dev
|
|
|
|
_ -> notFound
|