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
|
module Erebos.Chatroom (
Chatroom(..),
ChatroomData(..),
validateChatroom,
ChatroomState(..),
ChatroomStateData(..),
createChatroom,
updateChatroomByStateData,
listChatrooms,
findChatroomByRoomData,
findChatroomByStateData,
getMessagesSinceState,
ChatroomSetChange(..),
watchChatrooms,
ChatMessage, cmsgFrom, cmsgReplyTo, cmsgTime, cmsgText, cmsgLeave,
ChatMessageData(..),
chatroomMessageByStateData,
ChatroomService(..),
) where
import Control.Arrow
import Control.Monad
import Control.Monad.Except
import Control.Monad.IO.Class
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.Storage
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)
}
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
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 {..}
threadToList :: [Stored (Signed ChatMessageData)] -> [ChatMessage]
threadToList thread = helper S.empty $ 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 =
messageFromData msg : helper (S.insert msg seen) (msgs' ++ mdPrev (fromSigned msg))
| otherwise = []
cmpView msg = (zonedTimeToUTC $ mdTime $ fromSigned msg, msg)
messageFromData :: Stored (Signed ChatMessageData) -> ChatMessage
messageFromData sdata = ChatMessage { cmsgData = sdata }
chatroomMessageByStateData
:: (MonadStorage m, MonadHead LocalState m, MonadError String m)
=> Stored ChatroomStateData -> Text -> m ()
chatroomMessageByStateData lookupData msg = void $ findAndUpdateChatroomState $ \cstate -> do
guard $ any (lookupData `precedesOrEquals`) $ roomStateData cstate
Just $ do
self <- finalOwner . localIdentity . fromStored <$> getLocalHead
secret <- loadKey $ idKeyMessage self
time <- liftIO getZonedTime
mdata <- mstore =<< sign secret =<< mstore ChatMessageData
{ mdPrev = roomStateMessageData cstate
, mdRoom = []
, mdFrom = self
, mdReplyTo = Nothing
, mdTime = time
, mdText = Just msg
, mdLeave = False
}
mergeSorted . (:[]) <$> mstore ChatroomStateData
{ rsdPrev = roomStateData cstate
, rsdRoom = []
, rsdMessages = [ mdata ]
}
data ChatroomStateData = ChatroomStateData
{ rsdPrev :: [Stored ChatroomStateData]
, rsdRoom :: [Stored (Signed ChatroomData)]
, rsdMessages :: [Stored (Signed ChatMessageData)]
}
data ChatroomState = ChatroomState
{ roomStateData :: [Stored ChatroomStateData]
, roomStateRoom :: Maybe Chatroom
, roomStateMessageData :: [Stored (Signed ChatMessageData)]
, roomStateMessages :: [ChatMessage]
}
instance Storable ChatroomStateData where
store' ChatroomStateData {..} = storeRec $ do
forM_ rsdPrev $ storeRef "PREV"
forM_ rsdRoom $ storeRef "room"
forM_ rsdMessages $ storeRef "msg"
load' = loadRec $ do
rsdPrev <- loadRefs "PREV"
rsdRoom <- loadRefs "room"
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
roomStateMessages = threadToList $ 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 ]
, 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 ]
, 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
getMessagesSinceState :: ChatroomState -> ChatroomState -> [ChatMessage]
getMessagesSinceState cur old = takeWhile notOld (roomStateMessages cur)
where
notOld msg = cmsgData msg `notElem` roomStateMessageData old
-- TODO: parallel message threads
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)]
, chatRoomMessage :: [Stored (Signed ChatMessageData)]
}
emptyPacket :: ChatroomService
emptyPacket = ChatroomService
{ chatRoomQuery = False
, chatRoomInfo = []
, chatRoomMessage = []
}
instance Storable ChatroomService where
store' ChatroomService {..} = storeRec $ do
when chatRoomQuery $ storeEmpty "room-query"
forM_ chatRoomInfo $ storeRef "room-info"
forM_ chatRoomMessage $ storeRef "room-message"
load' = loadRec $ do
chatRoomQuery <- isJust <$> loadMbEmpty "room-query"
chatRoomInfo <- loadRefs "room-info"
chatRoomMessage <- loadRefs "room-message"
return ChatroomService {..}
data PeerState = PeerState
{ psSendRoomUpdates :: Bool
, psLastList :: [(Stored ChatroomStateData, ChatroomState)]
}
instance Service ChatroomService where
serviceID _ = mkServiceID "627657ae-3e39-468a-8381-353395ef4386"
type ServiceState ChatroomService = PeerState
emptyServiceState _ = PeerState
{ psSendRoomUpdates = False
, psLastList = []
}
serviceHandler spacket = do
let ChatroomService {..} = fromStored spacket
svcModify $ \s -> s { psSendRoomUpdates = True }
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) .
concatMap (rsdRoom . fromStored) . roomStateData
let prev = concatMap roomStateData $ filter isCurrentRoom rooms
prevRoom = concatMap (rsdRoom . fromStored) 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
, rsdMessages = []
}
storeSetAddComponent sdata set
else return set
foldM upd roomSet chatRoomInfo
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
updates <- fmap (concat . catMaybes) $
forM (makeChatroomDiff psLastList curList) $ return . \case
AddedChatroom room -> roomData <$> roomStateRoom room
RemovedChatroom {} -> Nothing
UpdatedChatroom _ room -> roomData <$> roomStateRoom room
when (not $ null updates) $ do
replyPacket $ emptyPacket { chatRoomInfo = updates }
svcSet $ ps { psLastList = curList }
|