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 /

RemoteActorStore.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
{- This file is part of Vervis.
 -
 - Written in 2019, 2020, 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/>.
 -}

-- | This module collects functions for working with the storage of remote
-- instances, actors and their details in our local database.
module Vervis.RemoteActorStore
    ( InstanceMutex ()
    , newInstanceMutex
    , ActorFetchShare
    , YesodRemoteActorStore (..)
    , withHostLock
    , keyListedByActorShared
    , VerifKeyDetail (..)
    , addVerifKey
    , actorFetchShareAction
    , fetchRemoteActor
    , deleteUnusedURAs
    )
where

import Control.Applicative
import Control.Concurrent (forkIO)
import Control.Concurrent.MVar (MVar, newMVar)
import Control.Concurrent.ResultShare
import Control.Concurrent.STM.TVar
import Control.Exception
import Control.Monad
import Control.Monad.Logger.CallStack
import Control.Monad.STM
import Control.Monad.Trans.Except
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Reader
import Data.Foldable
import Data.HashMap.Strict (HashMap)
import Data.Maybe
import Data.Text (Text)
import Data.Time.Clock
import Data.Traversable
import Database.Persist
import Database.Persist.Sql
import Network.HTTP.Client
import UnliftIO.MVar (withMVar)
import Yesod.Core hiding (logWarn, logError)
import Yesod.Persist.Core

import qualified Data.HashMap.Strict as M
import qualified Data.Text as T
import qualified Database.Esqueleto as E

import Crypto.PublicVerifKey
import Database.Persist.Local
import Network.FedURI
import Web.ActivityPub
import Yesod.MonadSite

import Vervis.FedURI
import Vervis.Model hiding (Actor (..))

newtype InstanceMutex = InstanceMutex (TVar (HashMap Host (MVar ())))

newInstanceMutex :: IO InstanceMutex
newInstanceMutex = InstanceMutex <$> newTVarIO M.empty

data RoomModeDB
    = RoomModeNoLimit
    | RoomModeLimit Int

data RoomMode
    = RoomModeInstant
    | RoomModeCached RoomModeDB

type ActorFetchShare site = ResultShare FedURI (Either (Maybe APGetError) (Maybe (Entity RemoteActor))) (site, InstanceId)

class Yesod site => YesodRemoteActorStore site where
    siteInstanceMutex    :: site -> InstanceMutex
    siteInstanceRoomMode :: site -> Maybe Int
    siteActorRoomMode    :: site -> Maybe Int
    siteRejectOnMaxKeys  :: site -> Bool

    siteActorFetchShare  :: site -> ActorFetchShare site

withHostLock
    :: ( MonadHandler m
       , MonadUnliftIO m
       , HandlerSite m ~ site
       , YesodRemoteActorStore site
       )
    => Host
    -> m a
    -> m a
withHostLock host action = do
    InstanceMutex tvar <- getsYesod siteInstanceMutex
    mvar <- liftIO $ do
        existing <- M.lookup host <$> readTVarIO tvar
        case existing of
            Just v -> return v
            Nothing -> do
                v <- newMVar ()
                atomically $ stateTVar tvar $ \ m ->
                    case M.lookup host m of
                        Just v' -> (v', m)
                        Nothing -> (v, M.insert host v m)
    withMVar mvar $ const action

sumUpTo :: Int -> YesodDB site Int -> YesodDB site Int -> YesodDB site Bool
sumUpTo limit action1 action2 = do
    n <- action1
    if n <= limit
        then do
            m <- action2
            return $ n + m <= limit
        else return False

-- | Grab instance and remote sharer IDs from the DB, inserting new ones if
-- they can't be found in the DB. The @Maybe Bool@ indicates whether the IDs
-- are newly inserted record: 'Nothing' means they're both new. @Just True@
-- means the instance record existed but the remote sharer is new. @Just False@
-- means both the instance and remote sharer existed in the DB.
instanceAndActor
    :: ( PersistUniqueWrite (YesodPersistBackend site)
       , BaseBackend (YesodPersistBackend site) ~ SqlBackend
       )
    => Host
    -> LocalURI
    -> Maybe Text
    -> LocalURI
    -> Maybe LocalURI
    -> YesodDB site (InstanceId, RemoteActorId, Maybe Bool)
instanceAndActor host luActor mname luInbox mluFollowers = do
    (iid, inew) <- idAndNew <$> insertBy' (Instance host)
    (raid, ranew) <- do
        roid <- either entityKey id <$> insertBy' (RemoteObject iid luActor)
        idAndNew <$>
            insertBy' (RemoteActor roid mname luInbox mluFollowers Nothing)
    return $
        ( iid
        , raid
        , if inew
            then if ranew
                then Nothing
                else Just False
            else if ranew
                then Just True
                else Just False
        )

actorRoom
    :: ( PersistQueryRead (YesodPersistBackend site)
       , BaseBackend (YesodPersistBackend site) ~ SqlBackend
       )
    => Int
    -> RemoteActorId
    -> YesodDB site Bool
actorRoom limit rsid = do
    sumUpTo limit
        (count [VerifKeySharedUsageUser ==. rsid])
        (count [VerifKeySharer ==. Just rsid])

getOldUsageId
    :: ( PersistQueryRead (YesodPersistBackend site)
       , BaseBackend (YesodPersistBackend site) ~ SqlBackend
       )
    => RemoteActorId
    -> YesodDB site (Maybe VerifKeySharedUsageId)
getOldUsageId rsid = fmap entityKey . listToMaybe <$> selectList [VerifKeySharedUsageUser ==. rsid] [Asc VerifKeySharedUsageId, LimitTo 1]

getOldPersonalKeyId
    :: ( PersistQueryRead (YesodPersistBackend site)
       , BaseBackend (YesodPersistBackend site) ~ SqlBackend
       )
    => RemoteActorId
    -> YesodDB site (Maybe VerifKeyId)
getOldPersonalKeyId rsid = fmap entityKey . listToMaybe <$> selectList [VerifKeySharer ==. Just rsid] [Asc VerifKeyExpires, Asc VerifKeyId, LimitTo 1]

makeActorRoomByPersonal
    :: ( PersistQueryRead (YesodPersistBackend site)
       , PersistStoreWrite (YesodPersistBackend site)
       , BaseBackend (YesodPersistBackend site) ~ SqlBackend
       )
    => Int
    -> RemoteActorId
    -> VerifKeyId
    -> YesodDB site ()
makeActorRoomByPersonal limit rsid vkid = do
    room <-
        if limit <= 1
            then return False
            else (< limit-1) <$> count [VerifKeySharer ==. Just rsid, VerifKeyId !=. vkid]
    unless room $ delete vkid

makeActorRoomByUsage
    :: ( PersistQueryRead (YesodPersistBackend site)
       , PersistStoreWrite (YesodPersistBackend site)
       , BaseBackend (YesodPersistBackend site) ~ SqlBackend
       )
    => Int
    -> RemoteActorId
    -> VerifKeySharedUsageId
    -> YesodDB site ()
makeActorRoomByUsage limit rsid suid = do
    room <-
        if limit <= 1
            then return False
            else
                sumUpTo (limit-1)
                    (count [VerifKeySharedUsageUser ==. rsid, VerifKeySharedUsageId !=. suid])
                    (count [VerifKeySharer ==. Just rsid])
    unless room $ delete suid

-- | Checks whether the given actor has room left for a new shared key usage
-- record, and if not, deletes a record to make room for a new one. It prefers
-- to delete a usage record if any exist; otherwise it deletes a personal key.
--
-- The first parameter is the actor key storage limit, and it must be above
-- zero.
makeActorRoomForUsage
    :: ( PersistQueryRead (YesodPersistBackend site)
       , PersistStoreWrite (YesodPersistBackend site)
       , BaseBackend (YesodPersistBackend site) ~ SqlBackend
       )
    => Int
    -> RemoteActorId
    -> YesodDB site ()
makeActorRoomForUsage limit rsid = do
    msuid <- getOldUsageId rsid
    case msuid of
        Nothing -> do
            mvkid <- getOldPersonalKeyId rsid
            case mvkid of
                Nothing -> return ()
                Just vkid -> makeActorRoomByPersonal limit rsid vkid
        Just suid -> makeActorRoomByUsage limit rsid suid

-- | Checks whether the given actor has room left for a new personal key
-- record, and if not, deletes a record to make room for a new one. It prefers
-- to delete a personal key if any exist; otherwise it deletes a usage record.
--
-- The first parameter is the actor key storage limit, and it must be above
-- zero.
makeActorRoomForPersonalKey
    :: ( PersistQueryRead (YesodPersistBackend site)
       , PersistStoreWrite (YesodPersistBackend site)
       , BaseBackend (YesodPersistBackend site) ~ SqlBackend
       )
    => Int
    -> RemoteActorId
    -> YesodDB site ()
makeActorRoomForPersonalKey limit rsid = do
    mvkid <- getOldPersonalKeyId rsid
    case mvkid of
        Nothing -> do
            msuid <- getOldUsageId rsid
            case msuid of
                Nothing -> return ()
                Just suid -> makeActorRoomByUsage limit rsid suid
        Just vkid -> makeActorRoomByPersonal limit rsid vkid

-- | Checks whether the given instance has room left for a new shared key
-- record, and if not, deletes a record to make room for a new one.
--
-- The first parameter is the actor key storage limit, and it must be above
-- zero.
makeInstanceRoom
    :: ( PersistQueryRead (YesodPersistBackend site)
       , PersistStoreWrite (YesodPersistBackend site)
       , BaseBackend (YesodPersistBackend site) ~ SqlBackend
       )
    => Int
    -> InstanceId
    -> YesodDB site ()
makeInstanceRoom limit iid = do
    mvk <- listToMaybe <$> selectList [VerifKeyInstance ==. iid, VerifKeySharer ==. Nothing] [Asc VerifKeyExpires, Asc VerifKeyId, LimitTo 1]
    case mvk of
        Nothing -> return ()
        Just (Entity vkid _) -> do
            room <-
                if limit <= 1
                    then return False
                    else (< limit-1) <$> count [VerifKeyInstance ==. iid, VerifKeySharer ==. Nothing, VerifKeyId !=. vkid]
            unless room $ delete vkid

roomModeFromLimit :: Maybe Int -> RoomMode
roomModeFromLimit Nothing      = RoomModeCached $ RoomModeNoLimit
roomModeFromLimit (Just limit) =
    if limit <= 0
        then RoomModeInstant
        else RoomModeCached $ RoomModeLimit limit

-- | Given a shared key we have in our DB, verify that the given actor lists
-- this key, and update the DB accordingly.
--
--   * If the storage limit on actor keys is zero:
--       - If we're supposed to reject signatures when there's no room, raise
--         an error! We can't store anything with a limit of 0
--       - Otherwise, fetch the actor, store in DB if we don't have it, verify
--         usage via actor JSON. Usage isn't stored in the DB.
--   * If there's no storage limit, or it's above zero:
--     - If we know the actor and we have a record that it lists the key,
--       return success, no other action
--     - If we know the actor but we don't have a record of usage, fetch the
--       actor and verify usage. If the actor already has the maximal number of
--       keys: If we're supposed to reject signatures when there's no room,
--       raise an error. Otherwise, delete an old key/usage and store the new
--       usage in the DB.
--     - If we don't know the actor, fetch actor, verify usage, store actor and
--       usage in DB.
--
-- If we get success, that means the actor lists the key, and both the actor
-- and the usage exist in our DB now (if the storage limit isn't zero).
keyListedByActorShared
    :: ( HasHttpManager site
       , YesodPersist site
       , YesodRemoteActorStore site
       , BaseBackend (YesodPersistBackend site) ~ SqlBackend
       , PersistQueryRead (YesodPersistBackend site)
       , PersistUniqueWrite (YesodPersistBackend site)
       )
    => InstanceId
    -> VerifKeyId
    -> Host
    -> LocalRefURI
    -> LocalURI
    -> ExceptT String (HandlerFor site) RemoteActorId
keyListedByActorShared iid vkid host luKey luActor = do
    manager <- getsYesod getHttpManager
    reject <- getsYesod siteRejectOnMaxKeys
    roomMode <- getsYesod $ roomModeFromLimit . siteActorRoomMode
    case roomMode of
        RoomModeInstant -> do
            when reject $ throwE "Actor key storage limit is 0 and set to reject"
            Actor local detail <- ExceptT (keyListedByActor manager host luKey luActor)
            lift $ runDB $ do
                roid <- either entityKey id <$> insertBy' (RemoteObject iid luActor)
                either entityKey id <$> insertBy' (RemoteActor roid (actorName detail <|> actorUsername detail) (actorInbox local) (actorFollowers local) Nothing)
        RoomModeCached m -> do
            eresult <- do
                ments <- lift $ runDB $ do
                    mrs <- runMaybeT $ do
                        roid <- MaybeT $ getKeyBy $ UniqueRemoteObject iid luActor
                        MaybeT $ getBy $ UniqueRemoteActor roid
                    for mrs $ \ (Entity rsid _) ->
                        (rsid,) . isJust <$>
                            getBy (UniqueVerifKeySharedUsage vkid rsid)
                return $
                    case ments of
                        Nothing -> Right Nothing
                        Just (rsid, used) ->
                            if used
                                then Left rsid
                                else Right $ Just rsid
            case eresult of
                Left rsid -> return rsid
                Right mrsid -> do
                    Actor local detail <- ExceptT (keyListedByActor manager host luKey luActor)
                    ExceptT $ runDB $ do
                        vkExists <- isJust <$> get vkid
                        case mrsid of
                            Nothing -> do
                                rsid <- do
                                    roid <- either entityKey id <$> insertBy' (RemoteObject iid luActor)
                                    either entityKey id <$> insertBy' (RemoteActor roid (actorName detail <|> actorUsername detail) (actorInbox local) (actorFollowers local) Nothing)
                                when vkExists $ insert_ $ VerifKeySharedUsage vkid rsid
                                return $ Right rsid
                            Just rsid -> runExceptT $ do
                                when vkExists $ do
                                    case m of
                                        RoomModeNoLimit -> return ()
                                        RoomModeLimit limit -> do
                                            if reject
                                                then do
                                                    room <- lift $ actorRoom limit rsid
                                                    unless room $ throwE "Actor key storage limit reached"
                                                else lift $ makeActorRoomForUsage limit rsid
                                    lift $ insert_ $ VerifKeySharedUsage vkid rsid
                                return rsid

data VerifKeyDetail = VerifKeyDetail
    { vkdKeyId          :: LocalRefURI
    , vkdKey            :: PublicVerifKey
    , vkdExpires        :: Maybe UTCTime
    , vkdActorId        :: LocalURI
    , vkdActorFollowers :: Maybe LocalURI
    , vkdShared         :: Bool
    }

addVerifKey
    :: ( YesodRemoteActorStore site
       , BaseBackend (YesodPersistBackend site) ~ SqlBackend
       , PersistQueryRead (YesodPersistBackend site)
       , PersistUniqueWrite (YesodPersistBackend site)
       )
    => Host
    -> Maybe Text
    -> LocalURI
    -> VerifKeyDetail
    -> ExceptT String (YesodDB site) (InstanceId, RemoteActorId)
addVerifKey h mname uinb vkd =
    if vkdShared vkd
        then addSharedKey h uinb vkd
        else addPersonalKey h uinb vkd
    where
    addSharedKey host luInbox (VerifKeyDetail luKey key mexpires luActor mluFollowers _) = do
        reject <- getsYesod siteRejectOnMaxKeys
        roomModeA <- getsYesod $ roomModeFromLimit . siteActorRoomMode
        roomModeI <- getsYesod $ roomModeFromLimit . siteInstanceRoomMode
        (iid, rsid, inew) <- lift $ instanceAndActor host luActor mname luInbox mluFollowers
        case roomModeI of
            RoomModeInstant ->
                when reject $ throwE "Instance key storage limit is 0 and set to reject"
            RoomModeCached m -> do
                case m of
                    RoomModeNoLimit -> return ()
                    RoomModeLimit limit ->
                        if reject
                            then when (isJust inew) $ do
                                room <- lift $ instanceRoom limit iid
                                unless room $ throwE "Instance key storage limit reached"
                            else when (isJust inew) $ lift $ makeInstanceRoom limit iid
                vkid <- lift $ insert $ VerifKey luKey iid mexpires key Nothing
                case roomModeA of
                    RoomModeInstant ->
                        when reject $ throwE "Actor key storage limit is 0 and set to reject"
                    RoomModeCached m -> do
                        case m of
                            RoomModeNoLimit -> return ()
                            RoomModeLimit limit ->
                                if reject
                                    then when (inew == Just False) $ do
                                        room <- lift $ actorRoom limit rsid
                                        unless room $ throwE "Actor key storage limit reached"
                                    else when (inew == Just False) $ lift $ makeActorRoomForUsage limit rsid
                        lift $ insert_ $ VerifKeySharedUsage vkid rsid
        return (iid, rsid)
        where
        instanceRoom n iid =
            (< n) <$> count [VerifKeyInstance ==. iid, VerifKeySharer ==. Nothing]
    addPersonalKey host luInbox (VerifKeyDetail luKey key mexpires luActor mluFollowers _) = do
        reject <- getsYesod siteRejectOnMaxKeys
        roomMode <- getsYesod $ roomModeFromLimit . siteActorRoomMode
        (iid, rsid, inew) <- lift $ instanceAndActor host luActor mname luInbox mluFollowers
        case roomMode of
            RoomModeInstant ->
                when reject $ throwE "Actor key storage limit is 0 and set to reject"
            RoomModeCached m -> do
                case m of
                    RoomModeNoLimit -> return ()
                    RoomModeLimit limit ->
                        if reject
                            then when (inew == Just False) $ do
                                room <- lift $ actorRoom limit rsid
                                unless room $ throwE "Actor key storage limit reached"
                            else when (inew == Just False) $ lift $ makeActorRoomForPersonalKey limit rsid
                lift $ insert_ $ VerifKey luKey iid mexpires key (Just rsid)
        return (iid, rsid)

actorFetchShareAction
    :: ( Yesod site
       , YesodPersist site
       , PersistUniqueWrite (YesodPersistBackend site)
       , BaseBackend (YesodPersistBackend site) ~ SqlBackend
       , HasHttpManager site
       , Site site
       , PersistConfigPool (SitePersistConfig site) ~ ConnectionPool
       , PersistConfigBackend (SitePersistConfig site) ~ SqlPersistT
       )
    => FedURI
    -> (site, InstanceId)
    -> IO (Either (Maybe APGetError) (Maybe (Entity RemoteActor)))
actorFetchShareAction u (site, iid) = flip runWorkerT site $ do
    let ObjURI h lu = u
    mrecip <- runSiteDB $ runMaybeT $
        MaybeT (getKeyBy $ UniqueRemoteObject iid lu) >>= \ roid ->
                Left <$> MaybeT (getBy $ UniqueRemoteActor roid)
            <|> Right <$> MaybeT (getBy $ UniqueRemoteCollection roid)
    case mrecip of
        Just recip ->
            return $ Right $
                case recip of
                    Left ers -> Just ers
                    Right _ -> Nothing
        Nothing -> do
            manager <- asksSite getHttpManager
            erecip <- fetchRecipient manager h lu
            for erecip $ \ recip ->
                case recip of
                    RecipientActor (Actor local detail) -> runSiteDB $ do
                        roid <- either entityKey id <$> insertBy' (RemoteObject iid lu)
                        let ra = RemoteActor
                                { remoteActorIdent      = roid
                                , remoteActorName       =
                                    actorName detail <|> actorUsername detail
                                , remoteActorInbox      = actorInbox local
                                , remoteActorFollowers  = actorFollowers local
                                , remoteActorErrorSince = Nothing
                                }
                        Just . either id (flip Entity ra) <$> insertBy' ra
                    RecipientCollection _ -> runSiteDB $ do
                        mroid <- insertUnique $ RemoteObject iid lu
                        for_ mroid $ \ roid ->
                            insertUnique_ $ RemoteCollection roid
                        return Nothing
                    -- TODO see https://vervis.peers.community/decks/br6Go/tickets/r7dDo

fetchRemoteActor
    :: ( YesodPersist site
       , PersistUniqueRead (YesodPersistBackend site)
       , BaseBackend (YesodPersistBackend site) ~ SqlBackend
       , YesodRemoteActorStore site
       , MonadUnliftIO m
       , MonadSite m
       , SiteEnv m ~ site
       , Site site
       , PersistConfigPool (SitePersistConfig site) ~ ConnectionPool
       , PersistConfigBackend (SitePersistConfig site) ~ SqlPersistT
       )
    => InstanceId
    -> Host
    -> LocalURI
    -> m (Either
            SomeException
            (Either (Maybe APGetError) (Maybe (Entity RemoteActor)))
         )
fetchRemoteActor iid host luActor = do
    mrecip <- runSiteDB $ runMaybeT $
        MaybeT (getKeyBy $ UniqueRemoteObject iid luActor) >>= \ roid ->
                Left <$> MaybeT (getBy $ UniqueRemoteActor roid)
            <|> Right <$> MaybeT (getBy $ UniqueRemoteCollection roid)
    case mrecip of
        Just recip ->
            return $ Right $ Right $
                case recip of
                    Left ers -> Just ers
                    Right _ -> Nothing
        Nothing -> do
            site <- askSite
            liftIO $ runShared (siteActorFetchShare site) (ObjURI host luActor) (site, iid)

deleteUnusedURAs :: (MonadIO m, MonadLogger m) => ReaderT SqlBackend m ()
deleteUnusedURAs = do
    uraids <- E.select $ E.from $ \ ura -> do
        E.where_ $ E.notExists $ E.from $ \ udl ->
            E.where_ $ ura E.^. UnfetchedRemoteActorId E.==. udl E.^. UnlinkedDeliveryRecipient
        return $ ura E.^. UnfetchedRemoteActorId
    unless (null uraids) $ do
        deleteWhere [UnfetchedRemoteActorId <-. map E.unValue uraids]
        logWarn $ T.pack (show $ length uraids) <> " unused URAs deleted"
[See repo JSON]