summaryrefslogtreecommitdiff
path: root/src/Message/Service.hs
blob: 0a8f18093f1dc972764a9a28561f7f45b3201e3f (plain)
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
module Message.Service (
    DirectMessageService,
    ServicePacket(DirectMessagePacket),
    formatMessage,
) where

import Control.Monad.Reader

import Data.List
import qualified Data.Text as T
import Data.Time.Format
import Data.Time.LocalTime

import Identity
import Message
import Service
import State
import Storage
import Storage.List

data DirectMessageService

instance Service DirectMessageService where
    serviceID _ = mkServiceID "c702076c-4928-4415-8b6b-3e839eafcb0d"

    data ServiceState DirectMessageService = DirectMessageService
    emptyServiceState = DirectMessageService

    newtype ServicePacket DirectMessageService = DirectMessagePacket (Stored DirectMessage)

    serviceHandler packet = do
        let DirectMessagePacket smsg = fromStored packet
            msg = fromStored smsg
        powner <- asks $ finalOwner . svcPeer
        tzone <- liftIO $ getCurrentTimeZone
        svcPrint $ formatMessage tzone msg
        if | powner `sameIdentity` msgFrom msg
           -> do erb <- svcGetLocal
                 let st = storedStorage erb
                 erb' <- liftIO $ do
                     threads <- storedFromSList $ lsMessages $ fromStored erb
                     slist <- case find (sameIdentity powner . msgPeer . fromStored) threads of
                                   Just thread -> do thread' <- wrappedStore st (fromStored thread) { msgHead = smsg : msgHead (fromStored thread) }
                                                     slistReplaceS thread thread' $ lsMessages $ fromStored erb
                                   Nothing -> slistAdd (emptyDirectThread powner) { msgHead = [smsg] } $ lsMessages $ fromStored erb
                     wrappedStore st (fromStored erb) { lsMessages = slist }
                 svcSetLocal erb'
                 return Nothing

           | otherwise -> do svcPrint "Owner mismatch"
                             return Nothing

instance Storable (ServicePacket DirectMessageService) where
    store' (DirectMessagePacket smsg) = store' smsg
    load' = DirectMessagePacket <$> load'

formatMessage :: TimeZone -> DirectMessage -> String
formatMessage tzone msg = concat
    [ formatTime defaultTimeLocale "[%H:%M] " $ utcToLocalTime tzone $ zonedTimeToUTC $ msgTime msg
    , maybe "<unnamed>" T.unpack $ idName $ msgFrom msg
    , ": "
    , T.unpack $ msgText msg
    ]