Fix bugs for MVP

This version now officially works. I have tested it and I will publish an example soon.
pull/24/head
Bram 2024-05-28 10:32:17 +02:00
parent 12c919b071
commit 2b9370f0c2
4 changed files with 61 additions and 35 deletions

View File

@ -287,7 +287,7 @@ fullBody value _ =
FullBody value
getBody : List ContextAttr -> Json.Value
getBody : List ContextAttr -> Maybe Json.Value
getBody attributes =
attributes
|> List.filterMap
@ -301,8 +301,14 @@ getBody attributes =
)
|> List.reverse
|> List.head
|> Maybe.withDefault
(List.filterMap
|> (\fb ->
case fb of
Just _ ->
fb
Nothing ->
case
List.filterMap
(\attr ->
case attr of
BodyParam key value ->
@ -312,7 +318,12 @@ getBody attributes =
Nothing
)
attributes
|> E.object
of
[] ->
Nothing
head :: tail ->
Just <| E.object (head :: tail)
)
@ -479,21 +490,21 @@ rawApiCallResolver decoder statusCodeErrors =
Http.BadUrl s
|> InternetException
|> Tuple.pair
|> (|>) []
|> (|>) [ log.error ("Encountered bad URL " ++ s) ]
|> Err
Http.Timeout_ ->
Http.Timeout
|> InternetException
|> Tuple.pair
|> (|>) []
|> (|>) [ log.error "Encountered timeout - maybe the server is down?" ]
|> Err
Http.NetworkError_ ->
Http.NetworkError
|> InternetException
|> Tuple.pair
|> (|>) []
|> (|>) [ log.error "Encountered a network error - the user might be offline" ]
|> Err
Http.BadStatus_ metadata body ->
@ -534,7 +545,10 @@ toChain data apiContext =
{ method = call.method
, headers = getHeaders call.attributes
, url = getUrl call
, body = Http.jsonBody (getBody call.attributes)
, body =
getBody call.attributes
|> Maybe.map Http.jsonBody
|> Maybe.withDefault Http.emptyBody
, resolver = rawApiCallResolver (Json.decode data.coder) (getStatusCodes call.attributes)
, timeout = getTimeout call.attributes
}
@ -544,7 +558,10 @@ toChain data apiContext =
{ method = call.method
, headers = getHeaders call.attributes
, url = getUrl call
, body = Http.jsonBody (getBody call.attributes)
, body =
getBody call.attributes
|> Maybe.map Http.jsonBody
|> Maybe.withDefault Http.emptyBody
, resolver =
rawApiCallResolver
(Json.decode data.coder

View File

@ -84,7 +84,7 @@ type alias SendMessageEventOutputV2 =
sendMessageEventV1 : SendMessageEventInputV1 i -> A.TaskChain (PhantomV1 a) (PhantomV1 a)
sendMessageEventV1 { content, eventType, roomId, transactionId } =
A.request
{ attributes = [ R.fullBody content ]
{ attributes = [ R.accessToken, R.fullBody content ]
, coder = coderV1
, contextChange = always identity
, method = "PUT"
@ -105,7 +105,7 @@ sendMessageEventV1 { content, eventType, roomId, transactionId } =
sendMessageEventV2 : SendMessageEventInputV1 i -> A.TaskChain (PhantomV1 a) (PhantomV1 a)
sendMessageEventV2 { content, eventType, roomId, transactionId } =
A.request
{ attributes = [ R.fullBody content ]
{ attributes = [ R.accessToken, R.fullBody content ]
, coder = coderV2
, contextChange = always identity
, method = "PUT"
@ -124,7 +124,7 @@ sendMessageEventV2 { content, eventType, roomId, transactionId } =
sendMessageEventV3 : SendMessageEventInputV1 i -> A.TaskChain (PhantomV1 a) (PhantomV1 a)
sendMessageEventV3 { content, eventType, roomId, transactionId } =
A.request
{ attributes = [ R.fullBody content ]
{ attributes = [ R.accessToken, R.fullBody content ]
, coder = coderV2
, contextChange = always identity
, method = "PUT"
@ -165,7 +165,7 @@ coderV1 =
(Json.field.optional.value
{ fieldName = "event_id"
, toField = .eventId
, description = Debug.todo "Needs docs"
, description = [ "A unique identifier for the event." ]
, coder = Json.string
}
)
@ -196,7 +196,7 @@ coderV2 =
(Json.field.required
{ fieldName = "event_id"
, toField = .eventId
, description = Debug.todo "Needs docs"
, description = [ "A unique identifier for the event." ]
, coder = Json.string
}
)

View File

@ -318,23 +318,28 @@ init sn =
-}
mostPopularToken : Context -> Maybe String
mostPopularToken c =
c.accessTokens
|> Hashdict.values
|> List.sortBy
(\token ->
case token.expiryMs of
Nothing ->
( 0, Timestamp.toMs token.created )
case c.suggestedAccessToken of
Just _ ->
c.suggestedAccessToken
Just e ->
( 1
, token.created
|> Timestamp.add e
|> Timestamp.toMs
)
)
|> List.head
|> Maybe.map .value
Nothing ->
c.accessTokens
|> Hashdict.values
|> List.sortBy
(\token ->
case token.expiryMs of
Nothing ->
( 0, Timestamp.toMs token.created )
Just e ->
( 1
, token.created
|> Timestamp.add e
|> Timestamp.toMs
)
)
|> List.head
|> Maybe.map .value
{-| Reset the phantom type of the Context, effectively forgetting all values.

View File

@ -1,7 +1,7 @@
module Matrix exposing
( Vault
( Vault, fromUserId
, VaultUpdate, update
, sendMessageEvent, fromUserId, addAccessToken
, addAccessToken, sendMessageEvent
)
{-|
@ -35,10 +35,10 @@ support a monolithic public registry. (:
import Internal.Api.Main as Api
import Internal.Values.Envelope as Envelope
import Internal.Values.User as User
import Internal.Values.Vault as Internal
import Json.Encode as E
import Types exposing (Vault(..), VaultUpdate(..))
import Internal.Values.User as User
{-| The Vault type stores all relevant information about the Matrix API.
@ -56,19 +56,22 @@ type alias Vault =
type alias VaultUpdate =
Types.VaultUpdate
addAccessToken : String -> Vault -> Vault
addAccessToken token (Vault vault) =
Envelope.mapContext (\c -> { c | suggestedAccessToken = Just token }) vault
|> Vault
{-| Use a fully-fledged Matrix ID to connect.
case Matrix.fromUserId "@alice:example.org" of
Just vault ->
"We got a vault!"
Nothing ->
"Invalid username"
-}
fromUserId : String -> Maybe Vault
fromUserId =
@ -76,12 +79,13 @@ fromUserId =
>> Maybe.map
(\u ->
Envelope.init
{ serverName = User.domain u
{ serverName = "https://" ++ User.domain u
, content = Internal.init u
}
)
>> Maybe.map Vault
{-| Send a message event to a room.
This function can be used in a scenario where the user does not want to sync