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

View File

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

View File

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

View File

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