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
|
module Erebos.Chatroom (
Chatroom(..),
ChatroomData(..),
validateChatroom,
ChatroomState(..),
ChatroomStateData(..),
createChatroom,
updateChatroomByStateData,
listChatrooms,
findChatroomByRoomData,
findChatroomByStateData,
chatroomSetSubscribe,
chatroomMembers,
joinChatroom, joinChatroomByStateData,
joinChatroomAs, joinChatroomAsByStateData,
leaveChatroom, leaveChatroomByStateData,
getMessagesSinceState,
ChatroomSetChange(..),
watchChatrooms,
ChatMessage,
cmsgFrom, cmsgReplyTo, cmsgTime, cmsgText, cmsgLeave,
cmsgRoom, cmsgRoomData,
ChatMessageData(..),
sendChatroomMessage,
sendChatroomMessageByStateData,
ChatroomService(..),
) where
import Control.Arrow
import Control.Monad
import Control.Monad.Except
import Control.Monad.IO.Class
import Data.Bool
import Data.Either
import Data.Foldable
import Data.Function
import Data.IORef
import Data.List
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.Set qualified as S
import Data.Text (Text)
import Data.Time
import Erebos.Identity
import Erebos.PubKey
import Erebos.Service
import Erebos.Set
import Erebos.State
import Erebos.Storable
import Erebos.Storage.Head
import Erebos.Storage.Merge
import Erebos.Util
data ChatroomData = ChatroomData
{ rdPrev :: [Stored (Signed ChatroomData)]
, rdName :: Maybe Text
, rdDescription :: Maybe Text
, rdKey :: Stored PublicKey
}
data Chatroom = Chatroom
{ roomData :: [Stored (Signed ChatroomData)]
, roomName :: Maybe Text
, roomDescription :: Maybe Text
, roomKey :: Stored PublicKey
}
instance Storable ChatroomData where
store' ChatroomData {..} = storeRec $ do
mapM_ (storeRef "SPREV") rdPrev
storeMbText "name" rdName
storeMbText "description" rdDescription
storeRef "key" rdKey
load' = loadRec $ do
rdPrev <- loadRefs "SPREV"
rdName <- loadMbText "name"
rdDescription <- loadMbText "description"
rdKey <- loadRef "key"
return ChatroomData {..}
validateChatroom :: [Stored (Signed ChatroomData)] -> Except String Chatroom
validateChatroom roomData = do
when (null roomData) $ throwError "null data"
when (not $ getAll $ walkAncestors verifySignatures roomData) $ do
throwError "signature verification failed"
let roomName = findPropertyFirst (rdName . fromStored . signedData) roomData
roomDescription = findPropertyFirst (rdDescription . fromStored . signedData) roomData
roomKey <- maybe (throwError "missing key") return $
findPropertyFirst (Just . rdKey . fromStored . signedData) roomData
return Chatroom {..}
where
verifySignatures sdata =
let rdata = fromSigned sdata
required = concat
[ [ rdKey rdata ]
, map (rdKey . fromSigned) $ rdPrev rdata
]
in All $ all (fromStored sdata `isSignedBy`) required
data ChatMessageData = ChatMessageData
{ mdPrev :: [Stored (Signed ChatMessageData)]
, mdRoom :: [Stored (Signed ChatroomData)]
, mdFrom :: ComposedIdentity
, mdReplyTo :: Maybe (Stored (Signed ChatMessageData))
, mdTime :: ZonedTime
, mdText :: Maybe Text
, mdLeave :: Bool
}
data ChatMessage = ChatMessage
{ cmsgData :: Stored (Signed ChatMessageData)
}
validateSingleMessage :: Stored (Signed ChatMessageData) -> Maybe ChatMessage
validateSingleMessage sdata = do
guard $ fromStored sdata `isSignedBy` idKeyMessage (mdFrom (fromSigned sdata))
return $ ChatMessage sdata
cmsgFrom :: ChatMessage -> ComposedIdentity
cmsgFrom = mdFrom . fromSigned . cmsgData
cmsgReplyTo :: ChatMessage -> Maybe ChatMessage
cmsgReplyTo = fmap ChatMessage . mdReplyTo . fromSigned . cmsgData
cmsgTime :: ChatMessage -> ZonedTime
cmsgTime = mdTime . fromSigned . cmsgData
cmsgText :: ChatMessage -> Maybe Text
cmsgText = mdText . fromSigned . cmsgData
cmsgLeave :: ChatMessage -> Bool
cmsgLeave = mdLeave . fromSigned . cmsgData
cmsgRoom :: ChatMessage -> Maybe Chatroom
cmsgRoom = either (const Nothing) Just . runExcept . validateChatroom . cmsgRoomData
cmsgRoomData :: ChatMessage -> [ Stored (Signed ChatroomData) ]
cmsgRoomData = concat . findProperty ((\case [] -> Nothing; xs -> Just xs) . mdRoom . fromStored . signedData) . (: []) . cmsgData
instance Storable ChatMessageData where
store' ChatMessageData {..} = storeRec $ do
mapM_ (storeRef "SPREV") mdPrev
mapM_ (storeRef "room") mdRoom
mapM_ (storeRef "from") $ idExtDataF mdFrom
storeMbRef "reply-to" mdReplyTo
storeDate "time" mdTime
storeMbText "text" mdText
when mdLeave $ storeEmpty "leave"
load' = loadRec $ do
mdPrev <- loadRefs "SPREV"
mdRoom <- loadRefs "room"
mdFrom <- loadIdentity "from"
mdReplyTo <- loadMbRef "reply-to"
mdTime <- loadDate "time"
mdText <- loadMbText "text"
mdLeave <- isJust <$> loadMbEmpty "leave"
return ChatMessageData {..}
threadToListSince :: [ Stored (Signed ChatMessageData) ] -> [ Stored (Signed ChatMessageData) ] -> [ ChatMessage ]
threadToListSince since thread = helper (S.fromList since) thread
where
helper :: S.Set (Stored (Signed ChatMessageData)) -> [Stored (Signed ChatMessageData)] -> [ChatMessage]
helper seen msgs
| msg : msgs' <- filter (`S.notMember` seen) $ reverse $ sortBy (comparing cmpView) msgs =
maybe id (:) (validateSingleMessage msg) $
helper (S.insert msg seen) (msgs' ++ mdPrev (fromSigned msg))
| otherwise = []
cmpView msg = (zonedTimeToUTC $ mdTime $ fromSigned msg, msg)
sendChatroomMessage
:: (MonadStorage m, MonadHead LocalState m, MonadError String m)
=> ChatroomState -> Text -> m ()
sendChatroomMessage rstate msg = sendChatroomMessageByStateData (head $ roomStateData rstate) msg
sendChatroomMessageByStateData
:: (MonadStorage m, MonadHead LocalState m, MonadError String m)
=> Stored ChatroomStateData -> Text -> m ()
sendChatroomMessageByStateData lookupData msg = sendRawChatroomMessageByStateData lookupData Nothing Nothing (Just msg) False
sendRawChatroomMessageByStateData
:: (MonadStorage m, MonadHead LocalState m, MonadError String m)
=> Stored ChatroomStateData -> Maybe UnifiedIdentity -> Maybe (Stored (Signed ChatMessageData)) -> Maybe Text -> Bool -> m ()
sendRawChatroomMessageByStateData lookupData mbIdentity mdReplyTo mdText mdLeave = void $ findAndUpdateChatroomState $ \cstate -> do
guard $ any (lookupData `precedesOrEquals`) $ roomStateData cstate
Just $ do
mdFrom <- finalOwner <$> if
| Just identity <- mbIdentity -> return identity
| Just identity <- roomStateIdentity cstate -> return identity
| otherwise -> localIdentity . fromStored <$> getLocalHead
secret <- loadKey $ idKeyMessage mdFrom
mdTime <- liftIO getZonedTime
let mdPrev = roomStateMessageData cstate
mdRoom = if null (roomStateMessageData cstate)
then maybe [] roomData (roomStateRoom cstate)
else []
mdata <- mstore =<< sign secret =<< mstore ChatMessageData {..}
mergeSorted . (:[]) <$> mstore ChatroomStateData
{ rsdPrev = roomStateData cstate
, rsdRoom = []
, rsdSubscribe = Just (not mdLeave)
, rsdIdentity = mbIdentity
, rsdMessages = [ mdata ]
}
data ChatroomStateData = ChatroomStateData
{ rsdPrev :: [Stored ChatroomStateData]
, rsdRoom :: [Stored (Signed ChatroomData)]
, rsdSubscribe :: Maybe Bool
, rsdIdentity :: Maybe UnifiedIdentity
, rsdMessages :: [Stored (Signed ChatMessageData)]
}
data ChatroomState = ChatroomState
{ roomStateData :: [Stored ChatroomStateData]
, roomStateRoom :: Maybe Chatroom
, roomStateMessageData :: [Stored (Signed ChatMessageData)]
, roomStateSubscribe :: Bool
, roomStateIdentity :: Maybe UnifiedIdentity
, roomStateMessages :: [ChatMessage]
}
instance Storable ChatroomStateData where
store' ChatroomStateData {..} = storeRec $ do
forM_ rsdPrev $ storeRef "PREV"
forM_ rsdRoom $ storeRef "room"
forM_ rsdSubscribe $ storeInt "subscribe" . bool @Int 0 1
forM_ rsdIdentity $ storeRef "id" . idExtData
forM_ rsdMessages $ storeRef "msg"
load' = loadRec $ do
rsdPrev <- loadRefs "PREV"
rsdRoom <- loadRefs "room"
rsdSubscribe <- fmap ((/=) @Int 0) <$> loadMbInt "subscribe"
rsdIdentity <- loadMbUnifiedIdentity "id"
rsdMessages <- loadRefs "msg"
return ChatroomStateData {..}
instance Mergeable ChatroomState where
type Component ChatroomState = ChatroomStateData
mergeSorted roomStateData =
let roomStateRoom = either (const Nothing) Just $ runExcept $
validateChatroom $ concat $ findProperty ((\case [] -> Nothing; xs -> Just xs) . rsdRoom) roomStateData
roomStateMessageData = filterAncestors $ concat $ flip findProperty roomStateData $ \case
ChatroomStateData {..} | null rsdMessages -> Nothing
| otherwise -> Just rsdMessages
roomStateSubscribe = fromMaybe False $ findPropertyFirst rsdSubscribe roomStateData
roomStateIdentity = findPropertyFirst rsdIdentity roomStateData
roomStateMessages = threadToListSince [] $ concatMap (rsdMessages . fromStored) roomStateData
in ChatroomState {..}
toComponents = roomStateData
instance SharedType (Set ChatroomState) where
sharedTypeID _ = mkSharedTypeID "7bc71cbf-bc43-42b1-b413-d3a2c9a2aae0"
createChatroom :: (MonadStorage m, MonadHead LocalState m, MonadIO m, MonadError String m) => Maybe Text -> Maybe Text -> m ChatroomState
createChatroom rdName rdDescription = do
(secret, rdKey) <- liftIO . generateKeys =<< getStorage
let rdPrev = []
rdata <- mstore =<< sign secret =<< mstore ChatroomData {..}
cstate <- mergeSorted . (:[]) <$> mstore ChatroomStateData
{ rsdPrev = []
, rsdRoom = [ rdata ]
, rsdSubscribe = Just True
, rsdIdentity = Nothing
, rsdMessages = []
}
updateLocalHead $ updateSharedState $ \rooms -> do
st <- getStorage
(, cstate) <$> storeSetAdd st cstate rooms
findAndUpdateChatroomState
:: (MonadStorage m, MonadHead LocalState m)
=> (ChatroomState -> Maybe (m ChatroomState))
-> m (Maybe ChatroomState)
findAndUpdateChatroomState f = do
updateLocalHead $ updateSharedState $ \roomSet -> do
let roomList = fromSetBy (comparing $ roomName <=< roomStateRoom) roomSet
case catMaybes $ map (\x -> (x,) <$> f x) roomList of
((orig, act) : _) -> do
upd <- act
if roomStateData orig /= roomStateData upd
then do
st <- getStorage
roomSet' <- storeSetAdd st upd roomSet
return (roomSet', Just upd)
else do
return (roomSet, Just upd)
[] -> return (roomSet, Nothing)
updateChatroomByStateData
:: (MonadStorage m, MonadHead LocalState m, MonadError String m)
=> Stored ChatroomStateData
-> Maybe Text
-> Maybe Text
-> m (Maybe ChatroomState)
updateChatroomByStateData lookupData newName newDesc = findAndUpdateChatroomState $ \cstate -> do
guard $ any (lookupData `precedesOrEquals`) $ roomStateData cstate
room <- roomStateRoom cstate
Just $ do
secret <- loadKey $ roomKey room
rdata <- mstore =<< sign secret =<< mstore ChatroomData
{ rdPrev = roomData room
, rdName = newName
, rdDescription = newDesc
, rdKey = roomKey room
}
mergeSorted . (:[]) <$> mstore ChatroomStateData
{ rsdPrev = roomStateData cstate
, rsdRoom = [ rdata ]
, rsdSubscribe = Just True
, rsdIdentity = Nothing
, rsdMessages = []
}
listChatrooms :: MonadHead LocalState m => m [ChatroomState]
listChatrooms = fromSetBy (comparing $ roomName <=< roomStateRoom) .
lookupSharedValue . lsShared . fromStored <$> getLocalHead
findChatroom :: MonadHead LocalState m => (ChatroomState -> Bool) -> m (Maybe ChatroomState)
findChatroom p = do
list <- map snd . chatroomSetToList . lookupSharedValue . lsShared . fromStored <$> getLocalHead
return $ find p list
findChatroomByRoomData :: MonadHead LocalState m => Stored (Signed ChatroomData) -> m (Maybe ChatroomState)
findChatroomByRoomData cdata = findChatroom $
maybe False (any (cdata `precedesOrEquals`) . roomData) . roomStateRoom
findChatroomByStateData :: MonadHead LocalState m => Stored ChatroomStateData -> m (Maybe ChatroomState)
findChatroomByStateData cdata = findChatroom $ any (cdata `precedesOrEquals`) . roomStateData
chatroomSetSubscribe
:: (MonadStorage m, MonadHead LocalState m, MonadError String m)
=> Stored ChatroomStateData -> Bool -> m ()
chatroomSetSubscribe lookupData subscribe = void $ findAndUpdateChatroomState $ \cstate -> do
guard $ any (lookupData `precedesOrEquals`) $ roomStateData cstate
Just $ do
mergeSorted . (:[]) <$> mstore ChatroomStateData
{ rsdPrev = roomStateData cstate
, rsdRoom = []
, rsdSubscribe = Just subscribe
, rsdIdentity = Nothing
, rsdMessages = []
}
chatroomMembers :: ChatroomState -> [ ComposedIdentity ]
chatroomMembers ChatroomState {..} =
map (mdFrom . fromSigned . head) $
filter (any $ not . mdLeave . fromSigned) $ -- keep only users that hasn't left
map (filterAncestors . map snd) $ -- gather message data per each identity and filter ancestors
groupBy ((==) `on` fst) $ -- group on identity root
sortBy (comparing fst) $ -- sort by first root of identity data
map (\x -> ( head . filterAncestors . concatMap storedRoots . idDataF . mdFrom . fromSigned $ x, x )) $
toList $ ancestors $ roomStateMessageData
joinChatroom
:: (MonadStorage m, MonadHead LocalState m, MonadError String m)
=> ChatroomState -> m ()
joinChatroom rstate = joinChatroomByStateData (head $ roomStateData rstate)
joinChatroomByStateData
:: (MonadStorage m, MonadHead LocalState m, MonadError String m)
=> Stored ChatroomStateData -> m ()
joinChatroomByStateData lookupData = sendRawChatroomMessageByStateData lookupData Nothing Nothing Nothing False
joinChatroomAs
:: (MonadStorage m, MonadHead LocalState m, MonadError String m)
=> UnifiedIdentity -> ChatroomState -> m ()
joinChatroomAs identity rstate = joinChatroomAsByStateData identity (head $ roomStateData rstate)
joinChatroomAsByStateData
:: (MonadStorage m, MonadHead LocalState m, MonadError String m)
=> UnifiedIdentity -> Stored ChatroomStateData -> m ()
joinChatroomAsByStateData identity lookupData = sendRawChatroomMessageByStateData lookupData (Just identity) Nothing Nothing False
leaveChatroom
:: (MonadStorage m, MonadHead LocalState m, MonadError String m)
=> ChatroomState -> m ()
leaveChatroom rstate = leaveChatroomByStateData (head $ roomStateData rstate)
leaveChatroomByStateData
:: (MonadStorage m, MonadHead LocalState m, MonadError String m)
=> Stored ChatroomStateData -> m ()
leaveChatroomByStateData lookupData = sendRawChatroomMessageByStateData lookupData Nothing Nothing Nothing True
getMessagesSinceState :: ChatroomState -> ChatroomState -> [ChatMessage]
getMessagesSinceState cur old = threadToListSince (roomStateMessageData old) (roomStateMessageData cur)
data ChatroomSetChange = AddedChatroom ChatroomState
| RemovedChatroom ChatroomState
| UpdatedChatroom ChatroomState ChatroomState
watchChatrooms :: MonadIO m => Head LocalState -> (Set ChatroomState -> Maybe [ChatroomSetChange] -> IO ()) -> m WatchedHead
watchChatrooms h f = liftIO $ do
lastVar <- newIORef Nothing
watchHeadWith h (lookupSharedValue . lsShared . headObject) $ \cur -> do
let curList = chatroomSetToList cur
mbLast <- readIORef lastVar
writeIORef lastVar $ Just curList
f cur $ do
lastList <- mbLast
return $ makeChatroomDiff lastList curList
chatroomSetToList :: Set ChatroomState -> [(Stored ChatroomStateData, ChatroomState)]
chatroomSetToList = map (cmp &&& id) . fromSetBy (comparing cmp)
where
cmp :: ChatroomState -> Stored ChatroomStateData
cmp = head . filterAncestors . concatMap storedRoots . toComponents
makeChatroomDiff
:: [(Stored ChatroomStateData, ChatroomState)]
-> [(Stored ChatroomStateData, ChatroomState)]
-> [ChatroomSetChange]
makeChatroomDiff (x@(cx, vx) : xs) (y@(cy, vy) : ys)
| cx < cy = RemovedChatroom vx : makeChatroomDiff xs (y : ys)
| cx > cy = AddedChatroom vy : makeChatroomDiff (x : xs) ys
| roomStateData vx /= roomStateData vy = UpdatedChatroom vx vy : makeChatroomDiff xs ys
| otherwise = makeChatroomDiff xs ys
makeChatroomDiff xs [] = map (RemovedChatroom . snd) xs
makeChatroomDiff [] ys = map (AddedChatroom . snd) ys
data ChatroomService = ChatroomService
{ chatRoomQuery :: Bool
, chatRoomInfo :: [Stored (Signed ChatroomData)]
, chatRoomSubscribe :: [Stored (Signed ChatroomData)]
, chatRoomUnsubscribe :: [Stored (Signed ChatroomData)]
, chatRoomMessage :: [Stored (Signed ChatMessageData)]
}
deriving (Eq)
emptyPacket :: ChatroomService
emptyPacket = ChatroomService
{ chatRoomQuery = False
, chatRoomInfo = []
, chatRoomSubscribe = []
, chatRoomUnsubscribe = []
, chatRoomMessage = []
}
instance Storable ChatroomService where
store' ChatroomService {..} = storeRec $ do
when chatRoomQuery $ storeEmpty "room-query"
forM_ chatRoomInfo $ storeRef "room-info"
forM_ chatRoomSubscribe $ storeRef "room-subscribe"
forM_ chatRoomUnsubscribe $ storeRef "room-unsubscribe"
forM_ chatRoomMessage $ storeRef "room-message"
load' = loadRec $ do
chatRoomQuery <- isJust <$> loadMbEmpty "room-query"
chatRoomInfo <- loadRefs "room-info"
chatRoomSubscribe <- loadRefs "room-subscribe"
chatRoomUnsubscribe <- loadRefs "room-unsubscribe"
chatRoomMessage <- loadRefs "room-message"
return ChatroomService {..}
data PeerState = PeerState
{ psSendRoomUpdates :: Bool
, psLastList :: [(Stored ChatroomStateData, ChatroomState)]
, psSubscribedTo :: [ Stored (Signed ChatroomData) ] -- least root for each room
}
instance Service ChatroomService where
serviceID _ = mkServiceID "627657ae-3e39-468a-8381-353395ef4386"
type ServiceState ChatroomService = PeerState
emptyServiceState _ = PeerState
{ psSendRoomUpdates = False
, psLastList = []
, psSubscribedTo = []
}
serviceHandler spacket = do
let ChatroomService {..} = fromStored spacket
previouslyUpdated <- psSendRoomUpdates <$> svcGet
svcModify $ \s -> s { psSendRoomUpdates = True }
when (not previouslyUpdated) $ do
syncChatroomsToPeer . lookupSharedValue . lsShared . fromStored =<< getLocalHead
when chatRoomQuery $ do
rooms <- listChatrooms
replyPacket emptyPacket
{ chatRoomInfo = concatMap roomData $ catMaybes $ map roomStateRoom rooms
}
when (not $ null chatRoomInfo) $ do
updateLocalHead_ $ updateSharedState_ $ \roomSet -> do
let rooms = fromSetBy (comparing $ roomName <=< roomStateRoom) roomSet
upd set (roomInfo :: Stored (Signed ChatroomData)) = do
let currentRoots = storedRoots roomInfo
isCurrentRoom = any ((`intersectsSorted` currentRoots) . storedRoots) .
maybe [] roomData . roomStateRoom
let prev = concatMap roomStateData $ filter isCurrentRoom rooms
prevRoom = filterAncestors $ concat $ findProperty ((\case [] -> Nothing; xs -> Just xs) . rsdRoom) prev
room = filterAncestors $ (roomInfo : ) prevRoom
-- update local state only if we got roomInfo not present there
if roomInfo `notElem` prevRoom && roomInfo `elem` room
then do
sdata <- mstore ChatroomStateData
{ rsdPrev = prev
, rsdRoom = room
, rsdSubscribe = Nothing
, rsdIdentity = Nothing
, rsdMessages = []
}
storeSetAddComponent sdata set
else return set
foldM upd roomSet chatRoomInfo
forM_ chatRoomSubscribe $ \subscribeData -> do
mbRoomState <- findChatroomByRoomData subscribeData
forM_ mbRoomState $ \roomState ->
forM (roomStateRoom roomState) $ \room -> do
let leastRoot = head . filterAncestors . concatMap storedRoots . roomData $ room
svcModify $ \ps -> ps { psSubscribedTo = leastRoot : psSubscribedTo ps }
replyPacket emptyPacket
{ chatRoomMessage = roomStateMessageData roomState
}
forM_ chatRoomUnsubscribe $ \unsubscribeData -> do
mbRoomState <- findChatroomByRoomData unsubscribeData
forM_ (mbRoomState >>= roomStateRoom) $ \room -> do
let leastRoot = head . filterAncestors . concatMap storedRoots . roomData $ room
svcModify $ \ps -> ps { psSubscribedTo = filter (/= leastRoot) (psSubscribedTo ps) }
when (not (null chatRoomMessage)) $ do
updateLocalHead_ $ updateSharedState_ $ \roomSet -> do
let rooms = fromSetBy (comparing $ roomName <=< roomStateRoom) roomSet
upd set (msgData :: Stored (Signed ChatMessageData))
| Just msg <- validateSingleMessage msgData = do
let roomInfo = cmsgRoomData msg
currentRoots = filterAncestors $ concatMap storedRoots roomInfo
isCurrentRoom = any ((`intersectsSorted` currentRoots) . storedRoots) .
maybe [] roomData . roomStateRoom
let prevData = concatMap roomStateData $ filter isCurrentRoom rooms
prev = mergeSorted prevData
prevMessages = roomStateMessageData prev
messages = filterAncestors $ msgData : prevMessages
-- update local state only if subscribed and we got some new messages
if roomStateSubscribe prev && messages /= prevMessages
then do
sdata <- mstore ChatroomStateData
{ rsdPrev = prevData
, rsdRoom = []
, rsdSubscribe = Nothing
, rsdIdentity = Nothing
, rsdMessages = messages
}
storeSetAddComponent sdata set
else return set
| otherwise = return set
foldM upd roomSet chatRoomMessage
serviceNewPeer = do
replyPacket emptyPacket { chatRoomQuery = True }
serviceStorageWatchers _ = (:[]) $
SomeStorageWatcher (lookupSharedValue . lsShared . fromStored) syncChatroomsToPeer
syncChatroomsToPeer :: Set ChatroomState -> ServiceHandler ChatroomService ()
syncChatroomsToPeer set = do
ps@PeerState {..} <- svcGet
when psSendRoomUpdates $ do
let curList = chatroomSetToList set
diff = makeChatroomDiff psLastList curList
roomUpdates <- fmap (concat . catMaybes) $
forM diff $ return . \case
AddedChatroom room -> roomData <$> roomStateRoom room
RemovedChatroom {} -> Nothing
UpdatedChatroom oldroom room
| roomStateData oldroom /= roomStateData room -> roomData <$> roomStateRoom room
| otherwise -> Nothing
(subscribe, unsubscribe) <- fmap (partitionEithers . concat . catMaybes) $
forM diff $ return . \case
AddedChatroom room
| roomStateSubscribe room
-> map Left . roomData <$> roomStateRoom room
RemovedChatroom oldroom
| roomStateSubscribe oldroom
-> map Right . roomData <$> roomStateRoom oldroom
UpdatedChatroom oldroom room
| roomStateSubscribe oldroom /= roomStateSubscribe room
-> map (if roomStateSubscribe room then Left else Right) . roomData <$> roomStateRoom room
_ -> Nothing
messages <- fmap concat $ do
let leastRootFor = head . filterAncestors . concatMap storedRoots . roomData
forM diff $ return . \case
AddedChatroom rstate
| Just room <- roomStateRoom rstate
, leastRootFor room `elem` psSubscribedTo
-> roomStateMessageData rstate
UpdatedChatroom oldstate rstate
| Just room <- roomStateRoom rstate
, leastRootFor room `elem` psSubscribedTo
, roomStateMessageData oldstate /= roomStateMessageData rstate
-> roomStateMessageData rstate
_ -> []
let packet = emptyPacket
{ chatRoomInfo = roomUpdates
, chatRoomSubscribe = subscribe
, chatRoomUnsubscribe = unsubscribe
, chatRoomMessage = messages
}
when (packet /= emptyPacket) $ do
replyPacket packet
svcSet $ ps { psLastList = curList }
|