Eventually-decentralized project hosting and management platform

[[ 🗃 ^WvWbo vervis ]] :: [📥 Inbox] [📤 Outbox] [🐤 Followers] [🤝 Collaborators] [🛠 Changes]

Clone

HTTPS: darcs clone https://vervis.peers.community/repos/WvWbo

SSH: darcs clone USERNAME@vervis.peers.community:WvWbo

Tags

TODO

src / Vervis /

Ticket.hs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
{- This file is part of Vervis.
 -
 - Written in 2016, 2018, 2019, 2020, 2021, 2022
 - by fr33domlover <fr33domlover@riseup.net>.
 -
 - ♡ 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/>.
 -}

module Vervis.Ticket
    (
      getTicketSummaries
    , getClothSummaries
    --, getTicketDepEdges

    , WorkflowFieldFilter (..)
    , WorkflowFieldSummary (..)
    , TicketTextParamValue (..)
    , TicketTextParam (..)
    , getTicketTextParams
    , WorkflowEnumSummary (..)
    , TicketEnumParamValue (..)
    , TicketEnumParam (..)
    , getTicketEnumParams
    , TicketClassParam (..)
    , getTicketClasses


    , getTicket
    , getTicket404

    --, getDependencyCollection
    --, getReverseDependencyCollection

    --, getWorkItem

    --, checkDepAndTarget
    )
where

import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Control.Monad.Trans.Except
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Reader
import Data.Bitraversable
import Data.Either
import Data.Foldable (for_)
import Data.Maybe
import Data.Text (Text)
import Data.Traversable
import Database.Persist
import Database.Persist.Sql
import Yesod.Core (notFound)
import Yesod.Core.Content
import Yesod.Persist.Core

import qualified Database.Esqueleto as E

import Network.FedURI
import Web.ActivityPub hiding (Ticket, Project)
import Yesod.ActivityPub
import Yesod.FedURI
import Yesod.Hashids
import Yesod.MonadSite

import Control.Monad.Trans.Except.Local
import Data.Either.Local
import Data.Paginate.Local
import Database.Persist.Local
import Yesod.Persist.Local

import Vervis.Data.Ticket
import Vervis.FedURI
import Vervis.Foundation
import Vervis.Model
import Vervis.Model.Ident
import Vervis.Model.Workflow
import Vervis.Paginate
import Vervis.Persist.Actor
import Vervis.Recipient
import Vervis.Widget.Ticket

-- | Get summaries of all the tickets in the given project.
getTicketSummaries
    :: Maybe (E.SqlExpr (Maybe (Entity TicketResolve)) -> E.SqlExpr (E.Value Bool))
    -> Maybe (E.SqlExpr (Entity Ticket) -> [E.SqlExpr E.OrderBy])
    -> Maybe (Int, Int)
    -> DeckId
    -> AppDB [TicketSummary]
getTicketSummaries mfilt morder offlim deckID = do
    tickets <- E.select $ E.from $
        \ ( t
            `E.InnerJoin` td
            `E.LeftOuterJoin` (tal `E.InnerJoin` p `E.InnerJoin` a)
            `E.LeftOuterJoin` (tar `E.InnerJoin` ra `E.InnerJoin` ro `E.InnerJoin` i)
            `E.LeftOuterJoin` tr
            `E.InnerJoin` d
            `E.LeftOuterJoin` m
          ) -> do
            E.on $ E.just (d E.^. DiscussionId) E.==. m E.?. MessageRoot
            E.on $ t E.^. TicketDiscuss E.==. d E.^. DiscussionId

            E.on $ E.just (t E.^. TicketId) E.==. tr E.?. TicketResolveTicket

            E.on $ ro E.?. RemoteObjectInstance E.==. i E.?. InstanceId
            E.on $ ra E.?. RemoteActorIdent E.==. ro E.?. RemoteObjectId
            E.on $ tar E.?. TicketAuthorRemoteAuthor E.==. ra E.?. RemoteActorId
            E.on $ E.just (t E.^. TicketId) E.==. tar E.?. TicketAuthorRemoteTicket

            E.on $ p E.?. PersonActor E.==. a E.?. ActorId
            E.on $ tal E.?. TicketAuthorLocalAuthor E.==. p E.?. PersonId
            E.on $ E.just (t E.^. TicketId) E.==. tal E.?. TicketAuthorLocalTicket

            E.on $ t E.^. TicketId E.==. td E.^. TicketDeckTicket

            E.where_ $ td E.^. TicketDeckDeck E.==. E.val deckID
            E.groupBy
                ( (t E.^. TicketId, td E.^. TicketDeckId)
                , tr E.?. TicketResolveId
                , tal E.?. TicketAuthorLocalId, p E.?. PersonId, a E.?. ActorId
                , ra E.?. RemoteActorId, ro E.?. RemoteObjectId, i E.?. InstanceId
                )
            for_ mfilt $ \ filt -> E.where_ $ filt tr
            for_ morder $ \ order -> E.orderBy $ order t
            for_ offlim $ \ (off, lim) -> do
                E.offset $ fromIntegral off
                E.limit $ fromIntegral lim

            return
                ( t E.^. TicketId
                , td E.^. TicketDeckId
                , p, a
                , i, ro, ra
                , t E.^. TicketCreated
                , t E.^. TicketTitle
                , tr E.?. TicketResolveId
                , E.count $ m E.?. MessageId
                )

    for tickets $
        \ (E.Value tid, E.Value tdid, mp, ma, mi, mro, mra, E.Value c, E.Value t, E.Value mc, E.Value r) -> do
            labels <- E.select $ E.from $ \ (tpc `E.InnerJoin` wf) -> do
                E.on $ tpc E.^. TicketParamClassField E.==. wf E.^. WorkflowFieldId
                E.where_ $ tpc E.^. TicketParamClassTicket E.==. E.val tid
                return wf
            return TicketSummary
                { tsId        = tdid
                , tsCreatedBy =
                    case (mp, ma, mi, mro, mra) of
                        (Just p, Just a, Nothing, Nothing, Nothing) ->
                            Left (p, entityVal a)
                        (Nothing, Nothing, Just i, Just ro, Just ra) ->
                            Right (entityVal i, entityVal ro, entityVal ra)
                        _ -> error "Ticket author DB invalid state"
                , tsCreatedAt = c
                , tsTitle     = t
                , tsLabels    = map entityVal labels
                , tsClosed    = isJust mc
                , tsComments  = r
                }

getClothSummaries
    :: Maybe (E.SqlExpr (Maybe (Entity TicketResolve)) -> E.SqlExpr (E.Value Bool))
    -> Maybe (E.SqlExpr (Entity Ticket) -> [E.SqlExpr E.OrderBy])
    -> Maybe (Int, Int)
    -> LoomId
    -> AppDB [ClothSummary]
getClothSummaries mfilt morder offlim loomID = do
    tickets <- E.select $ E.from $
        \ ( t
            `E.InnerJoin` tl
            `E.LeftOuterJoin` (tal `E.InnerJoin` p `E.InnerJoin` a)
            `E.LeftOuterJoin` (tar `E.InnerJoin` ra `E.InnerJoin` ro `E.InnerJoin` i)
            `E.LeftOuterJoin` tr
            `E.InnerJoin` d
            `E.LeftOuterJoin` m
          ) -> do
            E.on $ E.just (d E.^. DiscussionId) E.==. m E.?. MessageRoot
            E.on $ t E.^. TicketDiscuss E.==. d E.^. DiscussionId

            E.on $ E.just (t E.^. TicketId) E.==. tr E.?. TicketResolveTicket

            E.on $ ro E.?. RemoteObjectInstance E.==. i E.?. InstanceId
            E.on $ ra E.?. RemoteActorIdent E.==. ro E.?. RemoteObjectId
            E.on $ tar E.?. TicketAuthorRemoteAuthor E.==. ra E.?. RemoteActorId
            E.on $ E.just (t E.^. TicketId) E.==. tar E.?. TicketAuthorRemoteTicket

            E.on $ p E.?. PersonActor E.==. a E.?. ActorId
            E.on $ tal E.?. TicketAuthorLocalAuthor E.==. p E.?. PersonId
            E.on $ E.just (t E.^. TicketId) E.==. tal E.?. TicketAuthorLocalTicket

            E.on $ t E.^. TicketId E.==. tl E.^. TicketLoomTicket

            E.where_ $ tl E.^. TicketLoomLoom E.==. E.val loomID
            E.groupBy
                ( (t E.^. TicketId, tl E.^. TicketLoomId)
                , tr E.?. TicketResolveId
                , tal E.?. TicketAuthorLocalId, p E.?. PersonId, a E.?. ActorId
                , ra E.?. RemoteActorId, ro E.?. RemoteObjectId, i E.?. InstanceId
                )
            for_ mfilt $ \ filt -> E.where_ $ filt tr
            for_ morder $ \ order -> E.orderBy $ order t
            for_ offlim $ \ (off, lim) -> do
                E.offset $ fromIntegral off
                E.limit $ fromIntegral lim

            return
                ( t E.^. TicketId
                , tl E.^. TicketLoomId
                , p, a
                , i, ro, ra
                , t E.^. TicketCreated
                , t E.^. TicketTitle
                , tr E.?. TicketResolveId
                , E.count $ m E.?. MessageId
                )

    for tickets $
        \ (E.Value tid, E.Value tlid, mp, ma, mi, mro, mra, E.Value c, E.Value t, E.Value mc, E.Value r) -> do
            labels <- E.select $ E.from $ \ (tpc `E.InnerJoin` wf) -> do
                E.on $ tpc E.^. TicketParamClassField E.==. wf E.^. WorkflowFieldId
                E.where_ $ tpc E.^. TicketParamClassTicket E.==. E.val tid
                return wf
            return ClothSummary
                { csId        = tlid
                , csCreatedBy =
                    case (mp, ma, mi, mro, mra) of
                        (Just p, Just a, Nothing, Nothing, Nothing) ->
                            Left (p, entityVal a)
                        (Nothing, Nothing, Just i, Just ro, Just ra) ->
                            Right (entityVal i, entityVal ro, entityVal ra)
                        _ -> error "Ticket author DB invalid state"
                , csCreatedAt = c
                , csTitle     = t
                , csLabels    = map entityVal labels
                , csClosed    = isJust mc
                , csComments  = r
                }

-- | Get the child-parent ticket number pairs of all the ticket dependencies
-- in the given project, in ascending order by child, and then ascending order
-- by parent.
{-
getTicketDepEdges :: ProjectId -> AppDB [(Int64, Int64)]
getTicketDepEdges jid =
    fmap (map $ fromSqlKey . unValue *** fromSqlKey . unValue) $
    select $ from $
        \ (t1 `InnerJoin` tcl1 `InnerJoin` tpl1 `InnerJoin`
           td `InnerJoin`
           t2 `InnerJoin` tcl2 `InnerJoin` tpl2
          ) -> do
            on $ tcl2 ^. TicketContextLocalId ==. tpl2 ^. TicketProjectLocalContext
            on $ t2 ^. TicketId ==. tcl2 ^. TicketContextLocalTicket
            on $ t2 ^. TicketId ==. td ^. TicketDependencyParent
            on $ t1 ^. TicketId ==. td ^. TicketDependencyChild
            on $ tcl1 ^. TicketContextLocalId ==. tpl1 ^. TicketProjectLocalContext
            on $ t1 ^. TicketId ==. tcl1 ^. TicketContextLocalTicket
            where_ $
                tpl1 ^. TicketProjectLocalProject ==. val jid &&.
                tpl2 ^. TicketProjectLocalProject ==. val jid
            orderBy [asc $ t1 ^. TicketId, asc $ t2 ^. TicketId]
            return (t1 ^. TicketId, t2 ^. TicketId)
-}

data WorkflowFieldFilter = WorkflowFieldFilter
    { wffNew    :: Bool
    , wffTodo   :: Bool
    , wffClosed :: Bool
    }

data WorkflowFieldSummary = WorkflowFieldSummary
    { wfsId       :: WorkflowFieldId
    , wfsIdent    :: FldIdent
    , wfsName     :: Text
    , wfsRequired :: Bool
    , wfsConstant :: Bool
    , wfsFilter   :: WorkflowFieldFilter
    }

data TicketTextParamValue = TicketTextParamValue
    { ttpvId  :: TicketParamTextId
    , ttpvVal :: Text
    }

data TicketTextParam = TicketTextParam
    { ttpField :: WorkflowFieldSummary
    , ttpValue :: Maybe TicketTextParamValue
    }

toTParam
    :: ( E.Value WorkflowFieldId
       , E.Value FldIdent
       , E.Value Text
       , E.Value Bool
       , E.Value Bool
       , E.Value Bool
       , E.Value Bool
       , E.Value Bool
       , E.Value (Maybe TicketParamTextId)
       , E.Value (Maybe Text)
       )
    -> TicketTextParam
toTParam
    ( E.Value fid
    , E.Value fld
    , E.Value name
    , E.Value req
    , E.Value con
    , E.Value new
    , E.Value todo
    , E.Value closed
    , E.Value mp
    , E.Value mt
    ) =
        TicketTextParam
            { ttpField = WorkflowFieldSummary
                { wfsId       = fid
                , wfsIdent    = fld
                , wfsName     = name
                , wfsRequired = req
                , wfsConstant = con
                , wfsFilter   = WorkflowFieldFilter
                    { wffNew    = new
                    , wffTodo   = todo
                    , wffClosed = closed
                    }
                }
            , ttpValue =
                case (mp, mt) of
                    (Just p,  Just t)  ->
                        Just TicketTextParamValue
                            { ttpvId  = p
                            , ttpvVal = t
                            }
                    (Nothing, Nothing) -> Nothing
                    _                  -> error "Impossible"
            }

getTicketTextParams :: TicketId {--> WorkflowId-} -> AppDB [TicketTextParam]
getTicketTextParams tid {-wid-} = fmap (map toTParam) $
    E.select $ E.from $ \ (p `E.RightOuterJoin` f) -> do
        E.on $
            p E.?. TicketParamTextField  E.==. E.just (f E.^. WorkflowFieldId) E.&&.
            p E.?. TicketParamTextTicket E.==. E.just (E.val tid)
        E.where_ $
            --f E.^. WorkflowFieldWorkflow E.==. E.val wid     E.&&.
            f E.^. WorkflowFieldType     E.==. E.val WFTText E.&&.
            E.isNothing (f E.^. WorkflowFieldEnm)
        return
            ( f E.^. WorkflowFieldId
            , f E.^. WorkflowFieldIdent
            , f E.^. WorkflowFieldName
            , f E.^. WorkflowFieldRequired
            , f E.^. WorkflowFieldConstant
            , f E.^. WorkflowFieldFilterNew
            , f E.^. WorkflowFieldFilterTodo
            , f E.^. WorkflowFieldFilterClosed
            , p E.?. TicketParamTextId
            , p E.?. TicketParamTextValue
            )

data WorkflowEnumSummary = WorkflowEnumSummary
    { wesId    :: WorkflowEnumId
    , wesIdent :: EnmIdent
    }

data TicketEnumParamValue = TicketEnumParamValue
    { tepvId   :: TicketParamEnumId
    , tepvVal  :: WorkflowEnumCtorId
    , tepvName :: Text
    }

data TicketEnumParam = TicketEnumParam
    { tepField :: WorkflowFieldSummary
    , tepEnum  :: WorkflowEnumSummary
    , tepValue :: Maybe TicketEnumParamValue
    }

toEParam
    :: ( E.Value WorkflowFieldId
       , E.Value FldIdent
       , E.Value Text
       , E.Value Bool
       , E.Value Bool
       , E.Value Bool
       , E.Value Bool
       , E.Value Bool
       , E.Value WorkflowEnumId
       , E.Value EnmIdent
       , E.Value (Maybe TicketParamEnumId)
       , E.Value (Maybe WorkflowEnumCtorId)
       , E.Value (Maybe Text)
       )
    -> TicketEnumParam
toEParam
    ( E.Value fid
    , E.Value fld
    , E.Value name
    , E.Value req
    , E.Value con
    , E.Value new
    , E.Value todo
    , E.Value closed
    , E.Value i
    , E.Value e
    , E.Value mp
    , E.Value mc
    , E.Value mt
    ) =
        TicketEnumParam
            { tepField = WorkflowFieldSummary
                { wfsId       = fid
                , wfsIdent    = fld
                , wfsName     = name
                , wfsRequired = req
                , wfsConstant = con
                , wfsFilter   = WorkflowFieldFilter
                    { wffNew    = new
                    , wffTodo   = todo
                    , wffClosed = closed
                    }
                }
            , tepEnum = WorkflowEnumSummary
                { wesId    = i
                , wesIdent = e
                }
            , tepValue =
                case (mp, mc, mt) of
                    (Just p,  Just c,  Just t)  ->
                        Just TicketEnumParamValue
                            { tepvId   = p
                            , tepvVal  = c
                            , tepvName = t
                            }
                    (Nothing, Nothing, Nothing) -> Nothing
                    _                           -> error "Impossible"
            }

getTicketEnumParams :: TicketId {--> WorkflowId-} -> AppDB [TicketEnumParam]
getTicketEnumParams tid {-wid-} = fmap (map toEParam) $
    E.select $ E.from $ \ (p `E.InnerJoin` c `E.RightOuterJoin` f `E.InnerJoin` e) -> do
        E.on $
            --e E.^. WorkflowEnumWorkflow E.==. E.val wid E.&&.
            f E.^. WorkflowFieldEnm     E.==. E.just (e E.^. WorkflowEnumId)
        E.on $
            --f E.^. WorkflowFieldWorkflow E.==. E.val wid                       E.&&.
            f E.^. WorkflowFieldType     E.==. E.val WFTEnum                   E.&&.
            p E.?. TicketParamEnumField  E.==. E.just (f E.^. WorkflowFieldId) E.&&.
            c E.?. WorkflowEnumCtorEnum  E.==. f E.^. WorkflowFieldEnm
        E.on $
            p E.?. TicketParamEnumTicket E.==. E.just (E.val tid) E.&&.
            p E.?. TicketParamEnumValue  E.==. c E.?. WorkflowEnumCtorId
        return
            ( f E.^. WorkflowFieldId
            , f E.^. WorkflowFieldIdent
            , f E.^. WorkflowFieldName
            , f E.^. WorkflowFieldRequired
            , f E.^. WorkflowFieldConstant
            , f E.^. WorkflowFieldFilterNew
            , f E.^. WorkflowFieldFilterTodo
            , f E.^. WorkflowFieldFilterClosed
            , e E.^. WorkflowEnumId
            , e E.^. WorkflowEnumIdent
            , p E.?. TicketParamEnumId
            , c E.?. WorkflowEnumCtorId
            , c E.?. WorkflowEnumCtorName
            )

data TicketClassParam = TicketClassParam
    { tcpField :: WorkflowFieldSummary
    , tcpValue :: Maybe TicketParamClassId
    }

toCParam
    :: ( E.Value WorkflowFieldId
       , E.Value FldIdent
       , E.Value Text
       , E.Value Bool
       , E.Value Bool
       , E.Value Bool
       , E.Value Bool
       , E.Value Bool
       , E.Value (Maybe TicketParamClassId)
       )
    -> TicketClassParam
toCParam
    ( E.Value fid
    , E.Value fld
    , E.Value name
    , E.Value req
    , E.Value con
    , E.Value new
    , E.Value todo
    , E.Value closed
    , E.Value mp
    ) = TicketClassParam
            { tcpField = WorkflowFieldSummary
                { wfsId       = fid
                , wfsIdent    = fld
                , wfsName     = name
                , wfsRequired = req
                , wfsConstant = con
                , wfsFilter   = WorkflowFieldFilter
                    { wffNew    = new
                    , wffTodo   = todo
                    , wffClosed = closed
                    }
                }
            , tcpValue = mp
            }

getTicketClasses :: TicketId {--> WorkflowId-} -> AppDB [TicketClassParam]
getTicketClasses tid {-wid-} = fmap (map toCParam) $
    E.select $ E.from $ \ (p `E.RightOuterJoin` f) -> do
        E.on $
            p E.?. TicketParamClassField  E.==. E.just (f E.^. WorkflowFieldId) E.&&.
            p E.?. TicketParamClassTicket E.==. E.just (E.val tid)
        E.where_ $
            --f E.^. WorkflowFieldWorkflow E.==. E.val wid      E.&&.
            f E.^. WorkflowFieldType     E.==. E.val WFTClass E.&&.
            E.isNothing (f E.^. WorkflowFieldEnm)
        return
            ( f E.^. WorkflowFieldId
            , f E.^. WorkflowFieldIdent
            , f E.^. WorkflowFieldName
            , f E.^. WorkflowFieldRequired
            , f E.^. WorkflowFieldConstant
            , f E.^. WorkflowFieldFilterNew
            , f E.^. WorkflowFieldFilterTodo
            , f E.^. WorkflowFieldFilterClosed
            , p E.?. TicketParamClassId
            )

getTicket
    :: MonadIO m
    => DeckId
    -> TicketDeckId
    -> ReaderT SqlBackend m
        ( Maybe
            ( Entity Deck
            , Entity TicketDeck
            , Entity Ticket
            , Either (Entity TicketAuthorLocal) (Entity TicketAuthorRemote)
            , Maybe
                ( Entity TicketResolve
                , Either
                    (Entity TicketResolveLocal)
                    (Entity TicketResolveRemote)
                )
            )
        )
getTicket did tdid = runMaybeT $ do
    d <- MaybeT $ get did
    td <- MaybeT $ get tdid
    guard $ ticketDeckDeck td == did

    let tid = ticketDeckTicket td
    t <- lift $ getJust tid

    author <-
        lift $
            requireEitherAlt
                (getBy $ UniqueTicketAuthorLocal tid)
                (getBy $ UniqueTicketAuthorRemote tid)
                "Ticket doesn't have author"
                "Ticket has both local and remote author"

    mresolved <- lift $ getResolved tid

    return (Entity did d, Entity tdid td, Entity tid t, author, mresolved)

    where

    getResolved
        :: MonadIO m
        => TicketId
        -> ReaderT SqlBackend m
            (Maybe
                ( Entity TicketResolve
                , Either
                    (Entity TicketResolveLocal)
                    (Entity TicketResolveRemote)
                )
            )
    getResolved tid = do
        metr <- getBy $ UniqueTicketResolve tid
        for metr $ \ etr@(Entity trid _) ->
            (etr,) <$>
                requireEitherAlt
                    (getBy $ UniqueTicketResolveLocal trid)
                    (getBy $ UniqueTicketResolveRemote trid)
                    "No TRX"
                    "Both TRL and TRR"

getTicket404
    :: KeyHashid Deck
    -> KeyHashid TicketDeck
    -> AppDB
        ( Entity Deck
        , Entity TicketDeck
        , Entity Ticket
        , Either (Entity TicketAuthorLocal) (Entity TicketAuthorRemote)
        , Maybe
            ( Entity TicketResolve
            , Either
                (Entity TicketResolveLocal)
                (Entity TicketResolveRemote)
            )
        )
getTicket404 dkhid tdkhid = do
    did <- decodeKeyHashid404 dkhid
    tdid <- decodeKeyHashid404 tdkhid
    mticket <- getTicket did tdid
    case mticket of
        Nothing -> notFound
        Just ticket -> return ticket

{-
getDependencyCollection
    :: Route App
    -> (KeyHashid LocalTicket -> Route App)
    -> AppDB LocalTicketId
    -> Handler TypedContent
getDependencyCollection here depRoute getLocalTicketId404 = do
    tdids <- runDB $ do
        ltid <- getLocalTicketId404
        selectKeysList
            [LocalTicketDependencyParent ==. ltid]
            [Desc LocalTicketDependencyId]
    encodeRouteLocal <- getEncodeRouteLocal
    encodeRouteHome <- getEncodeRouteHome
    encodeHid <- getEncodeKeyHashid
    let deps = Collection
            { collectionId         = encodeRouteLocal here
            , collectionType       = CollectionTypeOrdered
            , collectionTotalItems = Just $ length tdids
            , collectionCurrent    = Nothing
            , collectionFirst      = Nothing
            , collectionLast       = Nothing
            , collectionItems      =
                map (encodeRouteHome . depRoute . encodeHid) tdids
            }
    provideHtmlAndAP deps $ redirectToPrettyJSON here

getReverseDependencyCollection
    :: Route App -> AppDB LocalTicketId -> Handler TypedContent
getReverseDependencyCollection here getLocalTicketId404 = do
    (locals, remotes) <- runDB $ do
        ltid <- getLocalTicketId404
        (,) <$> getLocals ltid <*> getRemotes ltid
    encodeRouteLocal <- getEncodeRouteLocal
    encodeRouteHome <- getEncodeRouteHome
    encodeHid <- getEncodeKeyHashid
    let deps = Collection
            { collectionId         = encodeRouteLocal here
            , collectionType       = CollectionTypeUnordered
            , collectionTotalItems = Just $ length locals + length remotes
            , collectionCurrent    = Nothing
            , collectionFirst      = Nothing
            , collectionLast       = Nothing
            , collectionItems      =
                map (encodeRouteHome . TicketDepR . encodeHid) locals ++
                map (\ (E.Value h, E.Value lu) -> ObjURI h lu) remotes
            }
    provideHtmlAndAP deps $ redirectToPrettyJSON here
    where
    getLocals ltid =
        map (ticketDependencyChildLocalDep . entityVal) <$>
            selectList [TicketDependencyChildLocalChild ==. ltid] []
    getRemotes ltid =
        E.select $ E.from $ \ (rtd `E.InnerJoin` ro `E.InnerJoin` i) -> do
            E.on $ ro E.^. RemoteObjectInstance E.==. i E.^. InstanceId
            E.on $ rtd E.^. RemoteTicketDependencyIdent E.==. ro E.^. RemoteObjectId
            E.where_ $ rtd E.^. RemoteTicketDependencyChild E.==. E.val ltid
            return (i E.^. InstanceHost, ro E.^. RemoteObjectIdent)
-}

{-
checkDepAndTarget
    :: (MonadSite m, SiteEnv m ~ App)
    => TicketDependency URIMode
    -> FedURI
    -> ExceptT Text m (Either WorkItem FedURI, Either WorkItem FedURI)
checkDepAndTarget
    (TicketDependency id_ uParent uChild _attrib published updated) uTarget = do
        verifyNothingE id_ "Dep with 'id'"
        parent <- parseWorkItem "Dep parent" uParent
        child <- parseWorkItem "Dep child" uChild
        when (parent == child) $
            throwE "Parent and child are the same work item"
        verifyNothingE published "Dep with 'published'"
        verifyNothingE updated "Dep with 'updated'"
        target <- parseTarget uTarget
        checkParentAndTarget parent target
        return (parent, child)
    where
    parseTarget u@(ObjURI h lu) = do
        hl <- hostIsLocal h
        if hl
            then Left <$> do
                route <-
                    fromMaybeE
                    (decodeRouteLocal lu)
                    "Offer local target isn't a valid route"
                fromMaybeE
                    (parseLocalActor route)
                    "Offer local target isn't an actor route"
            else return $ Right u
    checkParentAndTarget (Left wi) (Left la) = do
        la' <-
            case wi of
                WorkItemTicket did _ -> LocalActorDeck <$> encodeKeyHashid did
                WorkItemCloth lid _ -> LocalActorLoom <$> encodeKeyHashid lid
        unless (la' == la) $
            throwE "Parent and target mismatch"
    checkParentAndTarget (Left _) (Right _) = throwE "Local parent but remote target"
    checkParentAndTarget (Right _) (Left _) = throwE "Local target but remote parent"
    checkParentAndTarget (Right _) (Right _) = return ()
-}
[See repo JSON]