Skip to content

Commit 0c28c59

Browse files
Update the Tutorial with runMatrixIO
Change-Id: Iaa4110faa1737e0395de86ef5ebeb9f4af1a95c7
1 parent db06e15 commit 0c28c59

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

matrix-client/src/Network/Matrix/Internal.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ runMatrixM :: ClientSession -> MatrixM m a -> m (Either MatrixError a)
192192
runMatrixM session = flip runReaderT session . runExceptT . unMatrixM
193193

194194
-- | Run Matrix actions in 'IO'.
195-
runMatrixIO :: ClientSession -> MatrixM IO a -> IO (Either MatrixError a)
195+
runMatrixIO :: ClientSession -> MatrixIO a -> IO (Either MatrixError a)
196196
runMatrixIO = runMatrixM
197197

198198
-- | Retry a network action

matrix-client/src/Network/Matrix/Tutorial.hs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,37 +47,42 @@ where
4747
-- > Prelude Netowrk.Matrix.Client> :set prompt "> "
4848
-- > > :set -XOverloadedStrings
4949
-- > > :type getTokenOwner
50-
-- > getTokenOwner :: ClientSession -> MatrixIO WhoAmI
50+
-- > getTokenOwner :: MatrixIO UserID
5151

5252
-- $session
53-
-- Most functions require 'Network.Matrix.Client.ClientSession' which carries the
54-
-- endpoint url and the http client manager.
55-
--
56-
-- The only way to get the client is through the 'Network.Matrix.Client.createSession' function:
53+
-- Most functions operates in the 'MatrixIO' context, and to get their output you need to use
54+
-- the 'runMatrixIO' helper. This helper expects a 'ClientSession' that can be created with
55+
-- 'Network.Matrix.Client.createSession':
5756
--
5857
-- > > token <- getTokenFromEnv "MATRIX_TOKEN"
59-
-- > > sess <- createSession "https://matrix.org" token
60-
-- > > getTokenOwner sess
61-
-- > Right (WhoAmI "@tristanc_:matrix.org")
58+
-- > > session <- createSession "https://matrix.org" token
59+
-- > > runMatrixIO session getTokenOwner
60+
-- > Right (UserID "@tristanc_:matrix.org")
61+
--
62+
-- For the purpose of this tutorial, we can create a `withSession` wrapper:
63+
--
64+
-- > > let withSession = runMatrixIO session :: MatrixIO a -> IO (Either MatrixError a)
65+
-- > > withSession getTokenOwner
66+
-- > Right (UserID "@tristanc_:matrix.org")
6267

6368
-- $sync
6469
-- Create a filter to limit the sync result using the 'Network.Matrix.Client.createFilter' function.
6570
-- To keep room message only, use the 'Network.Matrix.Client.messageFilter' default filter:
6671
--
67-
-- > > Right userId <- getTokenOwner sess
68-
-- > > Right filterId <- createFilter sess userId messageFilter
69-
-- > > getFilter sess (UserID "@gerritbot:matrix.org") filterId
72+
-- > > Right userId <- withSession getTokenOwner
73+
-- > > Right filterId <- withSession (createFilter userId messageFilter)
74+
-- > > withSession (getFilter userId filterId)
7075
-- > Right (Filter {filterEventFields = ...})
7176
--
7277
-- Call the 'Network.Matrix.Client.sync' function to synchronize your client state:
7378
--
74-
-- > > Right syncResult <- sync sess (Just filterId) Nothing (Just Online) Nothing
79+
-- > > Right syncResult <- withSession (sync (Just filterId) Nothing (Just Online) Nothing)
7580
-- > > putStrLn $ take 512 $ show (getTimelines syncResult)
7681
-- > SyncResult {srNextBatch = ...}
7782
--
7883
-- Get next batch with a 300 second timeout using the @since@ argument:
7984
--
80-
-- > > Right syncResult' <- sync sess (Just filterId) (Just (srNextBatch syncResult)) (Just Online) (Just 300000)
85+
-- > > Right syncResult' <- withSession (sync (Just filterId) (Just (srNextBatch syncResult)) (Just Online) (Just 300000))
8186
--
8287
-- Here are some helpers function to format the messages from sync results, copy them in your REPL:
8388
--
@@ -96,7 +101,7 @@ where
96101
-- Use the 'Network.Matrix.Client.syncPoll' utility function to continuously get events,
97102
-- here is an example to print new messages, similar to a @tail -f@ process:
98103
--
99-
-- > > syncPoll sess (Just filterId) (Just (srNextBatch syncResult)) (Just Online) printTimelines
104+
-- > > withSession (syncPoll (Just filterId) (Just (srNextBatch syncResult)) (Just Online) printTimelines)
100105
-- > room1| test-user: Hello world!
101106
-- > ...
102107

@@ -112,5 +117,5 @@ where
112117
-- > > tokenId <- getTokenFromEnv "MATRIX_IDENTITY_TOKEN"
113118
-- > > sessId <- createIdentitySession "https://matrix.org" tokenId
114119
-- > > Right hd <- hashDetails sessId
115-
-- > > identityLookup sessId hd (Email "[email protected]")
120+
-- > > runMatrixIO sessionId (identityLookup hd (Email "[email protected]"))
116121
-- > Right (Just (UserID "@tristanc_:matrix.org"))

0 commit comments

Comments
 (0)