Compare commits

..

No commits in common. "1aecb1116adb9bba0ecf89d36655a40e1d88e385" and "b239eecc6bf3529c12caae70c3dec144882be76c" have entirely different histories.

22 changed files with 626 additions and 958 deletions

View File

@ -18,9 +18,8 @@ supported for which spec versions.
- ✅ **One way to do things** instead of having multiple functions that are - ✅ **One way to do things** instead of having multiple functions that are
considered deprecated. considered deprecated.
Follow us on [Mastodon](https://social.noordstar.me/@elm_matrix_sdk) or join the Follow us on [Mastodon](https://social.noordstar.me/@elm_matrix_sdk) at
conversation on [Matrix](https://matrix.to/#/#elm-sdk:matrix.org) to stay @elm_matrix_sdk@social.noordstar.me to stay up-to-date on the latest changes.
up-to-date on the latest changes.
## How to install ## How to install

View File

@ -3,7 +3,7 @@
"name": "noordstar/elm-matrix-sdk-beta", "name": "noordstar/elm-matrix-sdk-beta",
"summary": "Matrix SDK for instant communication. Unstable beta version for testing only.", "summary": "Matrix SDK for instant communication. Unstable beta version for testing only.",
"license": "EUPL-1.1", "license": "EUPL-1.1",
"version": "3.3.1", "version": "3.2.0",
"exposed-modules": [ "exposed-modules": [
"Matrix", "Matrix",
"Matrix.Event", "Matrix.Event",
@ -20,8 +20,7 @@
"elm/time": "1.0.0 <= v < 2.0.0", "elm/time": "1.0.0 <= v < 2.0.0",
"elm/url": "1.0.0 <= v < 2.0.0", "elm/url": "1.0.0 <= v < 2.0.0",
"micahhahn/elm-safe-recursion": "2.0.0 <= v < 3.0.0", "micahhahn/elm-safe-recursion": "2.0.0 <= v < 3.0.0",
"miniBill/elm-fast-dict": "1.0.0 <= v < 2.0.0", "miniBill/elm-fast-dict": "1.0.0 <= v < 2.0.0"
"noordstar/elm-iddict": "1.0.1 <= v < 2.0.0"
}, },
"test-dependencies": { "test-dependencies": {
"elm-explorations/test": "2.1.2 <= v < 3.0.0" "elm-explorations/test": "2.1.2 <= v < 3.0.0"

View File

@ -1,6 +1,6 @@
module Internal.Api.Main exposing module Internal.Api.Main exposing
( Msg ( Msg
, sendMessageEvent, sync , sendMessageEvent
) )
{-| {-|
@ -18,7 +18,7 @@ This module is used as reference for getting
## Actions ## Actions
@docs sendMessageEvent, sync @docs sendMessageEvent
-} -}
@ -57,22 +57,3 @@ sendMessageEvent env data =
} }
) )
(Context.apiFormat env.context) (Context.apiFormat env.context)
{-| Sync with the Matrix API to stay up-to-date.
-}
sync :
E.Envelope a
-> { toMsg : Msg -> msg }
-> Cmd msg
sync env data =
ITask.run
data.toMsg
(ITask.sync
{ fullState = Nothing
, presence = env.settings.presence
, since = env.context.nextBatch
, timeout = Just env.settings.syncTime
}
)
(Context.apiFormat env.context)

View File

@ -1,4 +1,4 @@
module Internal.Api.Sync.Api exposing (sync, Phantom) module Internal.Api.Sync.Api exposing (..)
{-| {-|
@ -9,37 +9,16 @@ The sync module might be one of the most crucial parts of the Elm SDK. It offers
users the guarantee that the `Vault` type remains up-to-date, and it helps users the guarantee that the `Vault` type remains up-to-date, and it helps
communicate with the Matrix server about the Vault's needs. communicate with the Matrix server about the Vault's needs.
@docs sync, Phantom @docs Phantom
-} -}
import Internal.Api.Api as A import Internal.Api.Api as A
import Internal.Api.Request as R import Internal.Api.Request as R
import Internal.Api.Sync.V1 as V1 import Internal.Api.Sync.V1 as V1
import Internal.Api.Sync.V2 as V2
import Internal.Api.Sync.V3 as V3
import Internal.Api.Sync.V4 as V4
import Internal.Filter.Timeline as Filter import Internal.Filter.Timeline as Filter
{-| Sync with the Matrix API.
-}
sync : SyncInput -> A.TaskChain (Phantom a) (Phantom a)
sync =
A.startWithVersion "v1.1" syncV1
|> A.forVersion "v1.2" syncV2
|> A.sameForVersion "v1.3"
|> A.forVersion "v1.4" syncV3
|> A.sameForVersion "v1.5"
|> A.sameForVersion "v1.6"
|> A.sameForVersion "v1.7"
|> A.sameForVersion "v1.8"
|> A.sameForVersion "v1.9"
|> A.sameForVersion "v1.10"
|> A.forVersion "v1.11" syncV4
|> A.versionChain
-- For simplicity, we will not use a filter for now -- For simplicity, we will not use a filter for now
-- and assume that every client always wants to receive all events. -- and assume that every client always wants to receive all events.
@ -57,10 +36,14 @@ type alias PhantomV1 a =
{ a | accessToken : (), baseUrl : () } { a | accessToken : (), baseUrl : () }
type PresenceV1
= OfflineV1
type alias SyncInput = type alias SyncInput =
{ -- filter : FilterV1, { -- filter : FilterV1,
fullState : Maybe Bool fullState : Maybe Bool
, presence : Maybe String , presenceV1 : Maybe PresenceV1
, since : Maybe String , since : Maybe String
, timeout : Maybe Int , timeout : Maybe Int
} }
@ -71,21 +54,15 @@ type alias SyncInputV1 a =
| -- filter : FilterV1 , | -- filter : FilterV1 ,
since : Maybe String since : Maybe String
, fullState : Maybe Bool , fullState : Maybe Bool
, presence : Maybe String , presenceV1 : Maybe PresenceV1
, timeout : Maybe Int , timeout : Maybe Int
} }
presenceFromOptions : List String -> Maybe String -> Maybe String sync : SyncInput -> A.TaskChain (Phantom a) (Phantom a)
presenceFromOptions options = sync =
Maybe.andThen A.startWithVersion "r0.0.0" syncV1
(\v -> |> A.versionChain
if List.member v options then
Just v
else
Nothing
)
syncV1 : SyncInputV1 i -> A.TaskChain (PhantomV1 a) (PhantomV1 a) syncV1 : SyncInputV1 i -> A.TaskChain (PhantomV1 a) (PhantomV1 a)
@ -94,83 +71,24 @@ syncV1 data =
{ attributes = { attributes =
[ R.accessToken [ R.accessToken
, R.queryOpString "filter" Nothing -- FILTER HERE , R.queryOpString "filter" Nothing -- FILTER HERE
, R.queryOpBool "full_state" data.fullState
, data.presence
|> presenceFromOptions [ "offline", "online", "unavailable" ]
|> R.queryOpString "set_presence"
, R.queryOpString "since" data.since , R.queryOpString "since" data.since
, R.queryOpBool "full_state" data.fullState
, data.presenceV1
|> Maybe.map (always "offline")
|> R.queryOpString "set_presence"
, R.queryOpInt "timeout" data.timeout , R.queryOpInt "timeout" data.timeout
] ]
, coder = V1.coderSyncResponse , coder = V1.syncResponseCoder
, contextChange = always identity , contextChange = always identity
, method = "GET" , method = "GET"
, path = [ "_matrix", "client", "r0", "sync" ] , path = [ "_matrix", "client", "r0", "sync" ]
, toUpdate = , toUpdate =
V1.updateSyncResponse { filter = Filter.pass, since = data.since } \out ->
} ( V1.syncResponseToUpdate
{ filter = Filter.pass -- FILTER HERE
, since = data.since
syncV2 : SyncInputV1 i -> A.TaskChain (PhantomV1 a) (PhantomV1 a) }
syncV2 data = out
A.request , []
{ attributes = )
[ R.accessToken
, R.queryOpString "filter" Nothing
, R.queryOpBool "full_state" data.fullState
, data.presence
|> presenceFromOptions [ "offline", "online", "unavailable" ]
|> R.queryOpString "set_presence"
, R.queryOpString "since" data.since
, R.queryOpInt "timeout" data.timeout
]
, coder = V2.coderSyncResponse
, contextChange = always identity
, method = "GET"
, path = [ "_matrix", "client", "r0", "sync" ]
, toUpdate =
V2.updateSyncResponse { filter = Filter.pass, since = data.since }
}
syncV3 : SyncInputV1 i -> A.TaskChain (PhantomV1 a) (PhantomV1 a)
syncV3 data =
A.request
{ attributes =
[ R.accessToken
, R.queryOpString "filter" Nothing
, R.queryOpBool "full_state" data.fullState
, data.presence
|> presenceFromOptions [ "offline", "online", "unavailable" ]
|> R.queryOpString "set_presence"
, R.queryOpString "since" data.since
, R.queryOpInt "timeout" data.timeout
]
, coder = V3.coderSyncResponse
, contextChange = always identity
, method = "GET"
, path = [ "_matrix", "client", "r0", "sync" ]
, toUpdate =
V3.updateSyncResponse { filter = Filter.pass, since = data.since }
}
syncV4 : SyncInputV1 i -> A.TaskChain (PhantomV1 a) (PhantomV1 a)
syncV4 data =
A.request
{ attributes =
[ R.accessToken
, R.queryOpString "filter" Nothing
, R.queryOpBool "full_state" data.fullState
, data.presence
|> presenceFromOptions [ "offline", "online", "unavailable" ]
|> R.queryOpString "set_presence"
, R.queryOpString "since" data.since
, R.queryOpInt "timeout" data.timeout
]
, coder = V4.coderSyncResponse
, contextChange = always identity
, method = "GET"
, path = [ "_matrix", "client", "r0", "sync" ]
, toUpdate =
V4.updateSyncResponse { filter = Filter.pass, since = data.since }
} }

View File

@ -12,16 +12,12 @@ This API module represents the /sync endpoint on Matrix spec version v1.1.
-} -}
import FastDict as Dict exposing (Dict) import FastDict as Dict exposing (Dict)
import Internal.Config.Log exposing (Log, log) import Internal.Config.Log exposing (Log)
import Internal.Config.Text as Text
import Internal.Filter.Timeline exposing (Filter)
import Internal.Tools.Json as Json import Internal.Tools.Json as Json
import Internal.Tools.StrippedEvent as StrippedEvent import Internal.Tools.StrippedEvent as StrippedEvent exposing (StrippedEvent)
import Internal.Tools.Timestamp as Timestamp exposing (Timestamp) import Internal.Tools.Timestamp as Timestamp exposing (Timestamp)
import Internal.Values.Envelope as E import Internal.Values.Envelope as E
import Internal.Values.Event as Event
import Internal.Values.Room as R import Internal.Values.Room as R
import Internal.Values.User as User exposing (User)
import Internal.Values.Vault as V import Internal.Values.Vault as V
@ -68,7 +64,7 @@ type alias InviteState =
type alias StrippedState = type alias StrippedState =
{ content : Json.Value { content : Json.Value
, sender : User , sender : String
, stateKey : String , stateKey : String
, eventType : String , eventType : String
} }
@ -97,7 +93,7 @@ type alias SyncStateEvent =
, eventId : String , eventId : String
, originServerTs : Timestamp , originServerTs : Timestamp
, prevContent : Maybe Json.Value , prevContent : Maybe Json.Value
, sender : User , sender : String
, stateKey : String , stateKey : String
, eventType : String , eventType : String
, unsigned : Maybe UnsignedData , unsigned : Maybe UnsignedData
@ -129,7 +125,7 @@ type alias SyncRoomEvent =
{ content : Json.Value { content : Json.Value
, eventId : String , eventId : String
, originServerTs : Timestamp , originServerTs : Timestamp
, sender : User , sender : String
, eventType : String , eventType : String
, unsigned : Maybe UnsignedData , unsigned : Maybe UnsignedData
} }
@ -168,7 +164,7 @@ type alias ToDevice =
type alias ToDeviceEvent = type alias ToDeviceEvent =
{ content : Maybe Json.Value { content : Maybe Json.Value
, sender : Maybe User , sender : Maybe String
, eventType : Maybe String , eventType : Maybe String
} }
@ -355,7 +351,7 @@ coderStrippedState =
{ fieldName = "sender" { fieldName = "sender"
, toField = .sender , toField = .sender
, description = [ "The sender for the event." ] , description = [ "The sender for the event." ]
, coder = User.coder , coder = Json.string
} }
) )
(Json.field.required (Json.field.required
@ -496,7 +492,7 @@ coderSyncStateEvent =
{ fieldName = "sender" { fieldName = "sender"
, toField = .sender , toField = .sender
, description = [ "Contains the fully-qualified ID of the user who sent this event." ] , description = [ "Contains the fully-qualified ID of the user who sent this event." ]
, coder = User.coder , coder = Json.string
} }
) )
(Json.field.required (Json.field.required
@ -644,7 +640,7 @@ coderSyncRoomEvent =
{ fieldName = "sender" { fieldName = "sender"
, toField = .sender , toField = .sender
, description = [ "Contains the fully-qualified ID of the user who sent this event." ] , description = [ "Contains the fully-qualified ID of the user who sent this event." ]
, coder = User.coder , coder = Json.string
} }
) )
(Json.field.required (Json.field.required
@ -805,7 +801,7 @@ coderToDeviceEvent =
{ fieldName = "sender" { fieldName = "sender"
, toField = .sender , toField = .sender
, description = [ "The Matrix user ID of the user who sent this event." ] , description = [ "The Matrix user ID of the user who sent this event." ]
, coder = User.coder , coder = Json.string
} }
) )
(Json.field.optional.value (Json.field.optional.value
@ -817,37 +813,16 @@ coderToDeviceEvent =
) )
updateSyncResponse : { filter : Filter, since : Maybe String } -> SyncResponse -> ( E.EnvelopeUpdate V.VaultUpdate, List Log ) updateSyncResponse : SyncResponse -> ( E.EnvelopeUpdate V.VaultUpdate, List Log )
updateSyncResponse { filter, since } response = updateSyncResponse response =
-- Account data -- TODO: Add account data
[ response.accountData
|> Maybe.andThen .events
|> Maybe.map (List.map (\e -> V.SetAccountData e.eventType e.content))
|> Maybe.map
(\x ->
( E.ContentUpdate <| V.More x
, if List.length x > 0 then
List.length x
|> Text.logs.syncAccountDataFound
|> log.debug
|> List.singleton
else
[]
)
)
-- TODO: Add device lists -- TODO: Add device lists
-- Next batch -- Next batch
, Just ( E.SetNextBatch response.nextBatch, [] ) [ Just ( E.SetNextBatch response.nextBatch, [] )
-- TODO: Add presence -- TODO: Add presence
-- Rooms -- Rooms
, Maybe.map , Maybe.map (updateRooms >> Tuple.mapFirst E.ContentUpdate) response.rooms
(updateRooms { filter = filter, nextBatch = response.nextBatch, since = since }
>> Tuple.mapFirst E.ContentUpdate
)
response.rooms
-- TODO: Add to_device -- TODO: Add to_device
] ]
@ -857,8 +832,8 @@ updateSyncResponse { filter, since } response =
|> Tuple.mapSecond List.concat |> Tuple.mapSecond List.concat
updateRooms : { filter : Filter, nextBatch : String, since : Maybe String } -> Rooms -> ( V.VaultUpdate, List Log ) updateRooms : Rooms -> ( V.VaultUpdate, List Log )
updateRooms { filter, nextBatch, since } rooms = updateRooms rooms =
let let
( roomUpdate, roomLogs ) = ( roomUpdate, roomLogs ) =
rooms.join rooms.join
@ -868,13 +843,7 @@ updateRooms { filter, nextBatch, since } rooms =
(\( roomId, room ) -> (\( roomId, room ) ->
let let
( u, l ) = ( u, l ) =
updateJoinedRoom updateJoinedRoom room
{ filter = filter
, nextBatch = nextBatch
, roomId = roomId
, since = since
}
room
in in
( V.MapRoom roomId u, l ) ( V.MapRoom roomId u, l )
) )
@ -900,8 +869,8 @@ updateRooms { filter, nextBatch, since } rooms =
) )
updateJoinedRoom : { filter : Filter, nextBatch : String, roomId : String, since : Maybe String } -> JoinedRoom -> ( R.RoomUpdate, List Log ) updateJoinedRoom : JoinedRoom -> ( R.RoomUpdate, List Log )
updateJoinedRoom data room = updateJoinedRoom room =
( R.More ( R.More
[ room.accountData [ room.accountData
|> Maybe.andThen .events |> Maybe.andThen .events
@ -919,56 +888,8 @@ updateJoinedRoom data room =
-- TODO: Add state -- TODO: Add state
-- TODO: Add RoomSummary -- TODO: Add RoomSummary
, room.timeline -- TODO: Add timeline
|> Maybe.andThen
(updateTimeline data)
|> R.Optional
-- TODO: Add unread notifications -- TODO: Add unread notifications
] ]
, [] , []
) )
updateTimeline : { filter : Filter, nextBatch : String, roomId : String, since : Maybe String } -> Timeline -> Maybe R.RoomUpdate
updateTimeline { filter, nextBatch, roomId, since } timeline =
timeline.events
|> Maybe.map
(\events ->
R.AddSync
{ events = List.map (toEvent roomId) events
, filter = filter
, start =
case timeline.prevBatch of
Just _ ->
timeline.prevBatch
Nothing ->
since
, end = nextBatch
}
)
toEvent : String -> SyncRoomEvent -> Event.Event
toEvent roomId event =
{ content = event.content
, eventId = event.eventId
, originServerTs = event.originServerTs
, roomId = roomId
, sender = event.sender
, stateKey = Nothing
, eventType = event.eventType
, unsigned = Maybe.map toUnsigned event.unsigned
}
toUnsigned : UnsignedData -> Event.UnsignedData
toUnsigned u =
Event.UnsignedData
{ age = u.age
, membership = Nothing
, prevContent = Nothing
, redactedBecause = Nothing
, transactionId = u.transactionId
}

View File

@ -13,19 +13,9 @@ v1.3.
-} -}
import FastDict as Dict exposing (Dict) import FastDict exposing (Dict)
import Internal.Api.Sync.V1 as PV import Internal.Api.Sync.V1 as PV
import Internal.Config.Log exposing (Log, log)
import Internal.Config.Text as Text
import Internal.Filter.Timeline exposing (Filter)
import Internal.Tools.Json as Json import Internal.Tools.Json as Json
import Internal.Tools.Timestamp as Timestamp exposing (Timestamp)
import Internal.Values.Envelope as E
import Internal.Values.Event as Event
import Internal.Values.Room as R
import Internal.Values.User as User exposing (User)
import Internal.Values.Vault as V
import Recursion
type alias SyncResponse = type alias SyncResponse =
@ -72,7 +62,7 @@ type alias InviteState =
type alias StrippedStateEvent = type alias StrippedStateEvent =
{ content : Json.Value { content : Json.Value
, sender : User , sender : String
, stateKey : String , stateKey : String
, eventType : String , eventType : String
} }
@ -99,8 +89,8 @@ type alias State =
type alias ClientEventWithoutRoomID = type alias ClientEventWithoutRoomID =
{ content : Json.Value { content : Json.Value
, eventId : String , eventId : String
, originServerTs : Timestamp , originServerTs : Int
, sender : User , sender : String
, stateKey : Maybe String , stateKey : Maybe String
, eventType : String , eventType : String
, unsigned : Maybe UnsignedData , unsigned : Maybe UnsignedData
@ -163,7 +153,7 @@ type alias ToDevice =
type alias ToDeviceEvent = type alias ToDeviceEvent =
{ content : Maybe Json.Value { content : Maybe Json.Value
, sender : Maybe User , sender : Maybe String
, eventType : Maybe String , eventType : Maybe String
} }
@ -397,14 +387,14 @@ coderClientEventWithoutRoomID =
{ fieldName = "origin_server_ts" { fieldName = "origin_server_ts"
, toField = .originServerTs , toField = .originServerTs
, description = [ "Required: Timestamp (in milliseconds since the unix epoch) on originating homeserver when this event was sent." ] , description = [ "Required: Timestamp (in milliseconds since the unix epoch) on originating homeserver when this event was sent." ]
, coder = Timestamp.coder , coder = Json.int
} }
) )
(Json.field.required (Json.field.required
{ fieldName = "sender" { fieldName = "sender"
, toField = .sender , toField = .sender
, description = [ "Required: Contains the fully-qualified ID of the user who sent this event." ] , description = [ "Required: Contains the fully-qualified ID of the user who sent this event." ]
, coder = User.coder , coder = Json.string
} }
) )
(Json.field.optional.value (Json.field.optional.value
@ -567,193 +557,3 @@ coderToDevice =
coderToDeviceEvent : Json.Coder ToDeviceEvent coderToDeviceEvent : Json.Coder ToDeviceEvent
coderToDeviceEvent = coderToDeviceEvent =
PV.coderToDeviceEvent PV.coderToDeviceEvent
updateSyncResponse : { filter : Filter, since : Maybe String } -> SyncResponse -> ( E.EnvelopeUpdate V.VaultUpdate, List Log )
updateSyncResponse { filter, since } response =
-- Account data
[ response.accountData
|> Maybe.andThen .events
|> Maybe.map (List.map (\e -> V.SetAccountData e.eventType e.content))
|> Maybe.map
(\x ->
( E.ContentUpdate <| V.More x
, if List.length x > 0 then
List.length x
|> Text.logs.syncAccountDataFound
|> log.debug
|> List.singleton
else
[]
)
)
-- TODO: Add device lists
-- Next batch
, Just ( E.SetNextBatch response.nextBatch, [] )
-- TODO: Add presence
-- Rooms
, Maybe.map
(updateRooms { filter = filter, nextBatch = response.nextBatch, since = since }
>> Tuple.mapFirst E.ContentUpdate
)
response.rooms
-- TODO: Add to_device
]
|> List.filterMap identity
|> List.unzip
|> Tuple.mapFirst E.More
|> Tuple.mapSecond List.concat
updateRooms : { filter : Filter, nextBatch : String, since : Maybe String } -> Rooms -> ( V.VaultUpdate, List Log )
updateRooms { filter, nextBatch, since } rooms =
let
( roomUpdate, roomLogs ) =
rooms.join
|> Maybe.withDefault Dict.empty
|> Dict.toList
|> List.map
(\( roomId, room ) ->
let
( u, l ) =
updateJoinedRoom
{ filter = filter
, nextBatch = nextBatch
, roomId = roomId
, since = since
}
room
in
( V.MapRoom roomId u, l )
)
|> List.unzip
|> Tuple.mapBoth V.More List.concat
in
( V.More
-- Add rooms
[ rooms.join
|> Maybe.withDefault Dict.empty
|> Dict.keys
|> List.map V.CreateRoomIfNotExists
|> V.More
-- Update rooms
, roomUpdate
-- TODO: Add invited rooms
-- TODO: Add knocked rooms
-- TODO: Add left rooms
]
, roomLogs
)
updateJoinedRoom : { filter : Filter, nextBatch : String, roomId : String, since : Maybe String } -> JoinedRoom -> ( R.RoomUpdate, List Log )
updateJoinedRoom data room =
( R.More
[ room.accountData
|> Maybe.andThen .events
|> Maybe.map
(\events ->
events
|> List.map (\e -> R.SetAccountData e.eventType e.content)
|> R.More
)
|> R.Optional
, room.ephemeral
|> Maybe.andThen .events
|> Maybe.map R.SetEphemeral
|> R.Optional
-- TODO: Add state
-- TODO: Add RoomSummary
, room.timeline
|> Maybe.map (updateTimeline data)
|> R.Optional
-- TODO: Add unread notifications
]
, []
)
updateTimeline : { filter : Filter, nextBatch : String, roomId : String, since : Maybe String } -> Timeline -> R.RoomUpdate
updateTimeline { filter, nextBatch, roomId, since } timeline =
R.AddSync
{ events = List.map (toEvent roomId) timeline.events
, filter = filter
, start =
case timeline.prevBatch of
Just _ ->
timeline.prevBatch
Nothing ->
since
, end = nextBatch
}
toEvent : String -> ClientEventWithoutRoomID -> Event.Event
toEvent roomId event =
Recursion.runRecursion
(\ev ->
case Maybe.andThen (\(UnsignedData u) -> u.redactedBecause) ev.unsigned of
Just e ->
Recursion.recurseThen e
(\eo ->
Recursion.base
{ content = ev.content
, eventId = ev.eventId
, originServerTs = ev.originServerTs
, roomId = roomId
, sender = ev.sender
, stateKey = ev.stateKey
, eventType = ev.eventType
, unsigned = toUnsigned (Just eo) ev.unsigned
}
)
Nothing ->
Recursion.base
{ content = ev.content
, eventId = ev.eventId
, originServerTs = ev.originServerTs
, roomId = roomId
, sender = ev.sender
, stateKey = ev.stateKey
, eventType = ev.eventType
, unsigned = toUnsigned Nothing ev.unsigned
}
)
event
toUnsigned : Maybe Event.Event -> Maybe UnsignedData -> Maybe Event.UnsignedData
toUnsigned ev unsigned =
case ( ev, unsigned ) of
( Nothing, Nothing ) ->
Nothing
( Just e, Nothing ) ->
{ age = Nothing
, membership = Nothing
, prevContent = Nothing
, redactedBecause = Just e
, transactionId = Nothing
}
|> Event.UnsignedData
|> Just
( _, Just (UnsignedData u) ) ->
{ age = u.age
, membership = Nothing
, prevContent = u.prevContent
, redactedBecause = ev
, transactionId = u.transactionId
}
|> Event.UnsignedData
|> Just

View File

@ -18,18 +18,9 @@ versions:
-} -}
import FastDict as Dict exposing (Dict) import FastDict exposing (Dict)
import Internal.Api.Sync.V2 as PV import Internal.Api.Sync.V2 as PV
import Internal.Config.Log exposing (Log, log)
import Internal.Config.Text as Text
import Internal.Filter.Timeline exposing (Filter)
import Internal.Tools.Json as Json import Internal.Tools.Json as Json
import Internal.Tools.Timestamp exposing (Timestamp)
import Internal.Values.Envelope as E
import Internal.Values.Event as Event
import Internal.Values.Room as R
import Internal.Values.User exposing (User)
import Internal.Values.Vault as V
type alias SyncResponse = type alias SyncResponse =
@ -76,7 +67,7 @@ type alias InviteState =
type alias StrippedStateEvent = type alias StrippedStateEvent =
{ content : Json.Value { content : Json.Value
, sender : User , sender : String
, stateKey : String , stateKey : String
, eventType : String , eventType : String
} }
@ -104,8 +95,8 @@ type alias State =
type alias ClientEventWithoutRoomID = type alias ClientEventWithoutRoomID =
{ content : Json.Value { content : Json.Value
, eventId : String , eventId : String
, originServerTs : Timestamp , originServerTs : Int
, sender : User , sender : String
, stateKey : Maybe String , stateKey : Maybe String
, eventType : String , eventType : String
, unsigned : Maybe UnsignedData , unsigned : Maybe UnsignedData
@ -169,7 +160,7 @@ type alias ToDevice =
type alias ToDeviceEvent = type alias ToDeviceEvent =
{ content : Maybe Json.Value { content : Maybe Json.Value
, sender : Maybe User , sender : Maybe String
, eventType : Maybe String , eventType : Maybe String
} }
@ -450,131 +441,3 @@ coderToDevice =
coderToDeviceEvent : Json.Coder ToDeviceEvent coderToDeviceEvent : Json.Coder ToDeviceEvent
coderToDeviceEvent = coderToDeviceEvent =
PV.coderToDeviceEvent PV.coderToDeviceEvent
updateSyncResponse : { filter : Filter, since : Maybe String } -> SyncResponse -> ( E.EnvelopeUpdate V.VaultUpdate, List Log )
updateSyncResponse { filter, since } response =
-- Account data
[ response.accountData
|> Maybe.andThen .events
|> Maybe.map (List.map (\e -> V.SetAccountData e.eventType e.content))
|> Maybe.map
(\x ->
( E.ContentUpdate <| V.More x
, if List.length x > 0 then
List.length x
|> Text.logs.syncAccountDataFound
|> log.debug
|> List.singleton
else
[]
)
)
-- TODO: Add device lists
-- Next batch
, Just ( E.SetNextBatch response.nextBatch, [] )
-- TODO: Add presence
-- Rooms
, Maybe.map
(updateRooms { filter = filter, nextBatch = response.nextBatch, since = since }
>> Tuple.mapFirst E.ContentUpdate
)
response.rooms
-- TODO: Add to_device
]
|> List.filterMap identity
|> List.unzip
|> Tuple.mapFirst E.More
|> Tuple.mapSecond List.concat
updateRooms : { filter : Filter, nextBatch : String, since : Maybe String } -> Rooms -> ( V.VaultUpdate, List Log )
updateRooms { filter, nextBatch, since } rooms =
let
( roomUpdate, roomLogs ) =
rooms.join
|> Maybe.withDefault Dict.empty
|> Dict.toList
|> List.map
(\( roomId, room ) ->
let
( u, l ) =
updateJoinedRoom
{ filter = filter
, nextBatch = nextBatch
, roomId = roomId
, since = since
}
room
in
( V.MapRoom roomId u, l )
)
|> List.unzip
|> Tuple.mapBoth V.More List.concat
in
( V.More
-- Add rooms
[ rooms.join
|> Maybe.withDefault Dict.empty
|> Dict.keys
|> List.map V.CreateRoomIfNotExists
|> V.More
-- Update rooms
, roomUpdate
-- TODO: Add invited rooms
-- TODO: Add knocked rooms
-- TODO: Add left rooms
]
, roomLogs
)
updateJoinedRoom : { filter : Filter, nextBatch : String, roomId : String, since : Maybe String } -> JoinedRoom -> ( R.RoomUpdate, List Log )
updateJoinedRoom data room =
( R.More
[ room.accountData
|> Maybe.andThen .events
|> Maybe.map
(\events ->
events
|> List.map (\e -> R.SetAccountData e.eventType e.content)
|> R.More
)
|> R.Optional
, room.ephemeral
|> Maybe.andThen .events
|> Maybe.map R.SetEphemeral
|> R.Optional
-- TODO: Add state
-- TODO: Add RoomSummary
, room.timeline
|> Maybe.map (updateTimeline data)
|> R.Optional
-- TODO: Add unread notifications
-- TODO: Add unread thread notifications
]
, []
)
updateTimeline : { filter : Filter, nextBatch : String, roomId : String, since : Maybe String } -> Timeline -> R.RoomUpdate
updateTimeline =
PV.updateTimeline
toEvent : String -> ClientEventWithoutRoomID -> Event.Event
toEvent =
PV.toEvent
toUnsigned : Maybe Event.Event -> Maybe UnsignedData -> Maybe Event.UnsignedData
toUnsigned =
PV.toUnsigned

View File

@ -11,19 +11,9 @@ This API module represents the /sync endpoint on Matrix spec version v1.11.
-} -}
import FastDict as Dict exposing (Dict) import FastDict exposing (Dict)
import Internal.Api.Sync.V3 as PV import Internal.Api.Sync.V3 as PV
import Internal.Config.Log exposing (Log, log)
import Internal.Config.Text as Text
import Internal.Filter.Timeline exposing (Filter)
import Internal.Tools.Json as Json import Internal.Tools.Json as Json
import Internal.Tools.Timestamp as Timestamp exposing (Timestamp)
import Internal.Values.Envelope as E
import Internal.Values.Event as Event
import Internal.Values.Room as R
import Internal.Values.User as User exposing (User)
import Internal.Values.Vault as V
import Recursion
type alias SyncResponse = type alias SyncResponse =
@ -70,7 +60,7 @@ type alias InviteState =
type alias StrippedStateEvent = type alias StrippedStateEvent =
{ content : Json.Value { content : Json.Value
, sender : User , sender : String
, stateKey : String , stateKey : String
, eventType : String , eventType : String
} }
@ -98,8 +88,8 @@ type alias State =
type alias ClientEventWithoutRoomID = type alias ClientEventWithoutRoomID =
{ content : Json.Value { content : Json.Value
, eventId : String , eventId : String
, originServerTs : Timestamp , originServerTs : Int
, sender : User , sender : String
, stateKey : Maybe String , stateKey : Maybe String
, eventType : String , eventType : String
, unsigned : Maybe UnsignedData , unsigned : Maybe UnsignedData
@ -169,7 +159,7 @@ type alias ToDevice =
type alias ToDeviceEvent = type alias ToDeviceEvent =
{ content : Maybe Json.Value { content : Maybe Json.Value
, sender : Maybe User , sender : Maybe String
, eventType : Maybe String , eventType : Maybe String
} }
@ -381,7 +371,7 @@ coderStrippedStateEvent =
{ fieldName = "sender" { fieldName = "sender"
, toField = .sender , toField = .sender
, description = [ "The sender for the event." ] , description = [ "The sender for the event." ]
, coder = User.coder , coder = Json.string
} }
) )
(Json.field.required (Json.field.required
@ -515,14 +505,14 @@ coderClientEventWithoutRoomID =
{ fieldName = "origin_server_ts" { fieldName = "origin_server_ts"
, toField = .originServerTs , toField = .originServerTs
, description = [ "Timestamp (in milliseconds since the unix epoch) on originating homeserver when this event was sent." ] , description = [ "Timestamp (in milliseconds since the unix epoch) on originating homeserver when this event was sent." ]
, coder = Timestamp.coder , coder = Json.int
} }
) )
(Json.field.required (Json.field.required
{ fieldName = "sender" { fieldName = "sender"
, toField = .sender , toField = .sender
, description = [ "Contains the fully-qualified ID of the user who sent this event." ] , description = [ "Contains the fully-qualified ID of the user who sent this event." ]
, coder = User.coder , coder = Json.string
} }
) )
(Json.field.optional.value (Json.field.optional.value
@ -825,7 +815,7 @@ coderToDeviceEvent =
{ fieldName = "sender" { fieldName = "sender"
, toField = .sender , toField = .sender
, description = [ "The Matrix user ID of the user who sent this event." ] , description = [ "The Matrix user ID of the user who sent this event." ]
, coder = User.coder , coder = Json.string
} }
) )
(Json.field.optional.value (Json.field.optional.value
@ -835,194 +825,3 @@ coderToDeviceEvent =
, coder = Json.string , coder = Json.string
} }
) )
updateSyncResponse : { filter : Filter, since : Maybe String } -> SyncResponse -> ( E.EnvelopeUpdate V.VaultUpdate, List Log )
updateSyncResponse { filter, since } response =
-- Account data
[ response.accountData
|> Maybe.andThen .events
|> Maybe.map (List.map (\e -> V.SetAccountData e.eventType e.content))
|> Maybe.map
(\x ->
( E.ContentUpdate <| V.More x
, if List.length x > 0 then
List.length x
|> Text.logs.syncAccountDataFound
|> log.debug
|> List.singleton
else
[]
)
)
-- TODO: Add device lists
-- Next batch
, Just ( E.SetNextBatch response.nextBatch, [] )
-- TODO: Add presence
-- Rooms
, Maybe.map
(updateRooms { filter = filter, nextBatch = response.nextBatch, since = since }
>> Tuple.mapFirst E.ContentUpdate
)
response.rooms
-- TODO: Add to_device
]
|> List.filterMap identity
|> List.unzip
|> Tuple.mapFirst E.More
|> Tuple.mapSecond List.concat
updateRooms : { filter : Filter, nextBatch : String, since : Maybe String } -> Rooms -> ( V.VaultUpdate, List Log )
updateRooms { filter, nextBatch, since } rooms =
let
( roomUpdate, roomLogs ) =
rooms.join
|> Maybe.withDefault Dict.empty
|> Dict.toList
|> List.map
(\( roomId, room ) ->
let
( u, l ) =
updateJoinedRoom
{ filter = filter
, nextBatch = nextBatch
, roomId = roomId
, since = since
}
room
in
( V.MapRoom roomId u, l )
)
|> List.unzip
|> Tuple.mapBoth V.More List.concat
in
( V.More
-- Add rooms
[ rooms.join
|> Maybe.withDefault Dict.empty
|> Dict.keys
|> List.map V.CreateRoomIfNotExists
|> V.More
-- Update rooms
, roomUpdate
-- TODO: Add invited rooms
-- TODO: Add knocked rooms
-- TODO: Add left rooms
]
, roomLogs
)
updateJoinedRoom : { filter : Filter, nextBatch : String, roomId : String, since : Maybe String } -> JoinedRoom -> ( R.RoomUpdate, List Log )
updateJoinedRoom data room =
( R.More
[ room.accountData
|> Maybe.andThen .events
|> Maybe.map
(\events ->
events
|> List.map (\e -> R.SetAccountData e.eventType e.content)
|> R.More
)
|> R.Optional
, room.ephemeral
|> Maybe.andThen .events
|> Maybe.map R.SetEphemeral
|> R.Optional
-- TODO: Add state
-- TODO: Add RoomSummary
, room.timeline
|> Maybe.map (updateTimeline data)
|> R.Optional
-- TODO: Add unread notifications
-- TODO: Add unread thread notifications
]
, []
)
updateTimeline : { filter : Filter, nextBatch : String, roomId : String, since : Maybe String } -> Timeline -> R.RoomUpdate
updateTimeline { filter, nextBatch, roomId, since } timeline =
R.AddSync
{ events = List.map (toEvent roomId) timeline.events
, filter = filter
, start =
case timeline.prevBatch of
Just _ ->
timeline.prevBatch
Nothing ->
since
, end = nextBatch
}
toEvent : String -> ClientEventWithoutRoomID -> Event.Event
toEvent roomId event =
Recursion.runRecursion
(\ev ->
case Maybe.andThen (\(UnsignedData u) -> u.redactedBecause) ev.unsigned of
Just e ->
Recursion.recurseThen e
(\eo ->
Recursion.base
{ content = ev.content
, eventId = ev.eventId
, originServerTs = ev.originServerTs
, roomId = roomId
, sender = ev.sender
, stateKey = ev.stateKey
, eventType = ev.eventType
, unsigned = toUnsigned (Just eo) ev.unsigned
}
)
Nothing ->
Recursion.base
{ content = ev.content
, eventId = ev.eventId
, originServerTs = ev.originServerTs
, roomId = roomId
, sender = ev.sender
, stateKey = ev.stateKey
, eventType = ev.eventType
, unsigned = toUnsigned Nothing ev.unsigned
}
)
event
toUnsigned : Maybe Event.Event -> Maybe UnsignedData -> Maybe Event.UnsignedData
toUnsigned ev unsigned =
case ( ev, unsigned ) of
( Nothing, Nothing ) ->
Nothing
( Just e, Nothing ) ->
{ age = Nothing
, membership = Nothing
, prevContent = Nothing
, redactedBecause = Just e
, transactionId = Nothing
}
|> Event.UnsignedData
|> Just
( _, Just (UnsignedData u) ) ->
{ age = u.age
, membership = Nothing
, prevContent = u.prevContent
, redactedBecause = ev
, transactionId = u.transactionId
}
|> Event.UnsignedData
|> Just

View File

@ -1,6 +1,6 @@
module Internal.Api.Task exposing module Internal.Api.Task exposing
( Task, run, Backpack ( Task, run, Backpack
, sendMessageEvent, sync , sendMessageEvent
) )
{-| {-|
@ -23,7 +23,7 @@ up-to-date.
## Tasks ## Tasks
@docs sendMessageEvent, sync @docs sendMessageEvent
-} -}
@ -33,13 +33,11 @@ import Internal.Api.LoginWithUsernameAndPassword.Api
import Internal.Api.Now.Api import Internal.Api.Now.Api
import Internal.Api.Request as Request import Internal.Api.Request as Request
import Internal.Api.SendMessageEvent.Api import Internal.Api.SendMessageEvent.Api
import Internal.Api.Sync.Api
import Internal.Api.Versions.Api import Internal.Api.Versions.Api
import Internal.Config.Log exposing (Log, log) import Internal.Config.Log exposing (Log, log)
import Internal.Config.Text as Text
import Internal.Tools.Json as Json import Internal.Tools.Json as Json
import Internal.Values.Context as Context exposing (APIContext) import Internal.Values.Context as Context exposing (APIContext)
import Internal.Values.Envelope as E exposing (EnvelopeUpdate(..)) import Internal.Values.Envelope exposing (EnvelopeUpdate(..))
import Internal.Values.Room exposing (RoomUpdate(..)) import Internal.Values.Room exposing (RoomUpdate(..))
import Internal.Values.Vault exposing (VaultUpdate(..)) import Internal.Values.Vault exposing (VaultUpdate(..))
import Task import Task
@ -113,20 +111,7 @@ getBaseUrl c =
Nothing -> Nothing ->
Internal.Api.BaseUrl.Api.baseUrl Internal.Api.BaseUrl.Api.baseUrl
{ url = Context.fromApiFormat c |> .serverName } { url = Context.fromApiFormat c |> .serverName }
|> C.catchWith c
(\_ ->
let
url : String
url =
Context.fromApiFormat c
|> .serverName
in
{ contextChange = Context.setBaseUrl url
, logs = [ log.warn (Text.logs.baseUrlFailed url) ]
, messages = [ E.SetBaseUrl url ]
}
)
|> (|>) c
{-| Get the current timestamp {-| Get the current timestamp
@ -232,15 +217,6 @@ sendMessageEvent input =
|> finishTask |> finishTask
{-| Sync with the Matrix API to stay up-to-date.
-}
sync : { fullState : Maybe Bool, presence : Maybe String, since : Maybe String, timeout : Maybe Int } -> Task
sync input =
makeVBA
|> C.andThen (Internal.Api.Sync.Api.sync input)
|> finishTask
{-| Transform a completed task into a Cmd. {-| Transform a completed task into a Cmd.
-} -}
run : (Backpack -> msg) -> Task -> APIContext {} -> Cmd msg run : (Backpack -> msg) -> Task -> APIContext {} -> Cmd msg

View File

@ -29,7 +29,7 @@ will assume until overriden by the user.
-} -}
currentVersion : String currentVersion : String
currentVersion = currentVersion =
"beta 3.3.1" "beta 3.2.0"
{-| The default device name that is being communicated with the Matrix API. {-| The default device name that is being communicated with the Matrix API.

View File

@ -118,6 +118,7 @@ docs :
, event : TypeDocs , event : TypeDocs
, hashdict : TypeDocs , hashdict : TypeDocs
, ibatch : TypeDocs , ibatch : TypeDocs
, iddict : TypeDocs
, itoken : TypeDocs , itoken : TypeDocs
, mashdict : TypeDocs , mashdict : TypeDocs
, room : TypeDocs , room : TypeDocs
@ -169,6 +170,12 @@ docs =
[ "The internal batch tracks a patch of events on the Matrix timeline." [ "The internal batch tracks a patch of events on the Matrix timeline."
] ]
} }
, iddict =
{ name = "Iddict"
, description =
[ "An iddict automatically handles creating appropriate keys by incrementally assiging a new key to new values."
]
}
, itoken = , itoken =
{ name = "IToken" { name = "IToken"
, description = , description =
@ -323,7 +330,6 @@ fields :
, settings : , settings :
{ currentVersion : Desc { currentVersion : Desc
, deviceName : Desc , deviceName : Desc
, presence : Desc
, removePasswordOnLogin : Desc , removePasswordOnLogin : Desc
, syncTime : Desc , syncTime : Desc
} }
@ -342,7 +348,6 @@ fields :
} }
, unsigned : , unsigned :
{ age : Desc { age : Desc
, membership : Desc
, prevContent : Desc , prevContent : Desc
, redactedBecause : Desc , redactedBecause : Desc
, transactionId : Desc , transactionId : Desc
@ -513,9 +518,6 @@ fields =
, deviceName = , deviceName =
[ "Indicates the device name that is communicated to the Matrix API." [ "Indicates the device name that is communicated to the Matrix API."
] ]
, presence =
[ "Controls whether the client is automatically marked as online. The value is passed on to the Matrix API."
]
, removePasswordOnLogin = , removePasswordOnLogin =
[ "Remove the password as soon as a valid access token has been received." [ "Remove the password as soon as a valid access token has been received."
] ]
@ -561,9 +563,6 @@ fields =
{ age = { age =
[ "The time in milliseconds that has elapsed since the event was sent. This field is generated by the local homeserver, and may be incorrect if the local time on at least one of the two servers is out of sync, which can cause the age to either be negative or greater than it actually is." [ "The time in milliseconds that has elapsed since the event was sent. This field is generated by the local homeserver, and may be incorrect if the local time on at least one of the two servers is out of sync, which can cause the age to either be negative or greater than it actually is."
] ]
, membership =
[ "The room membership of the user making the request, at the time of the event."
]
, prevContent = , prevContent =
[ "The previous content for this event. This field is generated by the local homeserver, and is only returned if the event is a state event, and the client has permission to see the previous content." [ "The previous content for this event. This field is generated by the local homeserver, and is only returned if the event is a state event, and the client has permission to see the previous content."
] ]
@ -629,8 +628,7 @@ happened. Most of these unexpected results, are taken account of by the Elm SDK,
but logged so that the programmer can do something about it. but logged so that the programmer can do something about it.
-} -}
logs : logs :
{ baseUrlFailed : String -> String { baseUrlFound : String -> String -> String
, baseUrlFound : String -> String -> String
, getEventId : String -> String , getEventId : String -> String
, getNow : Int -> String , getNow : Int -> String
, httpRequest : String -> String -> String , httpRequest : String -> String -> String
@ -640,12 +638,9 @@ logs :
, sendEvent : Maybe String -> String , sendEvent : Maybe String -> String
, serverReturnedInvalidJSON : String -> String , serverReturnedInvalidJSON : String -> String
, serverReturnedUnknownJSON : String -> String , serverReturnedUnknownJSON : String -> String
, syncAccountDataFound : Int -> String
} }
logs = logs =
{ baseUrlFailed = { baseUrlFound =
(++) "Failed to find .well-known, using default server address: "
, baseUrlFound =
\url baseUrl -> \url baseUrl ->
String.concat [ "Found baseURL of ", url, " at address ", baseUrl ] String.concat [ "Found baseURL of ", url, " at address ", baseUrl ]
, getEventId = (++) "Received event with id = " , getEventId = (++) "Received event with id = "
@ -680,8 +675,6 @@ logs =
"Sent event, event id not known - make sure to check transaction id" "Sent event, event id not known - make sure to check transaction id"
, serverReturnedInvalidJSON = (++) "The server returned invalid JSON: " , serverReturnedInvalidJSON = (++) "The server returned invalid JSON: "
, serverReturnedUnknownJSON = (++) "The server returned JSON that doesn't seem to live up to spec rules: " , serverReturnedUnknownJSON = (++) "The server returned JSON that doesn't seem to live up to spec rules: "
, syncAccountDataFound =
\n -> String.concat [ "Found ", String.fromInt n, " account data updates" ]
} }

View File

@ -0,0 +1,197 @@
module Internal.Tools.Iddict exposing
( Iddict
, empty, singleton, insert, map, remove
, isEmpty, member, get, size
, keys, values
, coder, encode, decoder
)
{-| The id-dict is a data type that lets us store values in a dictionary using
unique identifiers. This can be used as a dictionary where the keys do not
matter.
The benefit of the iddict is that it generates the keys FOR you. This way, you
do not need to generate identifiers yourself.
## Id-dict
@docs Iddict
## Build
@docs empty, singleton, insert, map, remove
## Query
@docs isEmpty, member, get, size
## Lists
@docs keys, values
## JSON coders
@docs coder, encode, decoder
-}
import FastDict as Dict exposing (Dict)
import Internal.Config.Text as Text
import Internal.Tools.Json as Json
{-| The Iddict data type.
-}
type Iddict a
= Iddict
{ cursor : Int
, dict : Dict Int a
}
{-| Define how an Iddict can be encoded and decoded to and from a JSON value.
-}
coder : Json.Coder a -> Json.Coder (Iddict a)
coder x =
Json.object2
{ name = Text.docs.iddict.name
, description = Text.docs.iddict.description
, init =
\c d ->
Iddict
{ cursor =
Dict.keys d
|> List.maximum
|> Maybe.map ((+) 1)
|> Maybe.withDefault 0
|> max (Dict.size d)
|> max c
, dict = d
}
}
(Json.field.optional.withDefault
{ fieldName = "cursor"
, toField = \(Iddict i) -> i.cursor
, description = Text.fields.iddict.cursor
, coder = Json.int
, default = ( 0, [] )
}
)
(Json.field.required
{ fieldName = "dict"
, toField = \(Iddict i) -> i.dict
, description = Text.fields.iddict.dict
, coder = Json.fastIntDict x
}
)
{-| Decode an id-dict from a JSON value.
-}
decoder : Json.Coder a -> Json.Decoder (Iddict a)
decoder x =
Json.decode (coder x)
{-| Create an empty id-dict.
-}
empty : Iddict a
empty =
Iddict
{ cursor = 0
, dict = Dict.empty
}
{-| Encode an id-dict to a JSON value.
-}
encode : Json.Coder a -> Json.Encoder (Iddict a)
encode x =
Json.encode (coder x)
{-| Get a value from the id-dict using its key.
-}
get : Int -> Iddict a -> Maybe a
get k (Iddict { dict }) =
Dict.get k dict
{-| Insert a new value into the id-dict. Given that the id-dict generates its
key, the function returns both the updated id-dict as the newly generated key.
x = empty |> insert "hello" -- ( 0, <Iddict with value "hello"> )
case x of
( _, iddict ) ->
get 0 iddict -- Just "hello"
-}
insert : a -> Iddict a -> ( Int, Iddict a )
insert v (Iddict d) =
( d.cursor
, Iddict { cursor = d.cursor + 1, dict = Dict.insert d.cursor v d.dict }
)
{-| Determine if an id-dict is empty.
-}
isEmpty : Iddict a -> Bool
isEmpty (Iddict d) =
Dict.isEmpty d.dict
{-| Get all of the keys from the id-dict, sorted from lowest to highest.
-}
keys : Iddict a -> List Int
keys (Iddict { dict }) =
Dict.keys dict
{-| Map an existing value at a given key, if it exists. If it does not exist,
the operation does nothing.
-}
map : Int -> (a -> a) -> Iddict a -> Iddict a
map k f (Iddict d) =
Iddict { d | dict = Dict.update k (Maybe.map f) d.dict }
{-| Determine if a key is in an id-dict.
-}
member : Int -> Iddict a -> Bool
member k (Iddict d) =
k < d.cursor && Dict.member k d.dict
{-| Remove a key-value pair from the id-dict. If the key is not found, no
changes are made.
-}
remove : Int -> Iddict a -> Iddict a
remove k (Iddict d) =
Iddict { d | dict = Dict.remove k d.dict }
{-| Create an id-dict with a single value.
-}
singleton : a -> ( Int, Iddict a )
singleton v =
insert v empty
{-| Determine the number of key-value pairs in the id-dict.
-}
size : Iddict a -> Int
size (Iddict d) =
Dict.size d.dict
{-| Get all of the values from an id-dict, in the order of their keys.
-}
values : Iddict a -> List a
values (Iddict { dict }) =
Dict.values dict

View File

@ -3,7 +3,7 @@ module Internal.Tools.Json exposing
, Encoder, encode, Decoder, decode, Value , Encoder, encode, Decoder, decode, Value
, succeed, fail, andThen, lazy, map , succeed, fail, andThen, lazy, map
, Docs(..), RequiredField(..), toDocs , Docs(..), RequiredField(..), toDocs
, list, listWithOne, slowDict, fastDict, fastIntDict, set, iddict, maybe , list, listWithOne, slowDict, fastDict, fastIntDict, set, maybe
, Field, field, parser , Field, field, parser
, object1, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11, object12 , object1, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11, object12
) )
@ -49,7 +49,7 @@ module to build its encoders and decoders.
## Data types ## Data types
@docs list, listWithOne, slowDict, fastDict, fastIntDict, set, iddict, maybe @docs list, listWithOne, slowDict, fastDict, fastIntDict, set, maybe
## Objects ## Objects
@ -68,7 +68,6 @@ Once all fields are constructed, the user can create JSON objects.
import Dict as SlowDict import Dict as SlowDict
import FastDict import FastDict
import Iddict exposing (Iddict)
import Internal.Config.Log as Log exposing (Log) import Internal.Config.Log as Log exposing (Log)
import Internal.Config.Text as Text import Internal.Config.Text as Text
import Internal.Tools.DecodeExtra as D import Internal.Tools.DecodeExtra as D
@ -142,7 +141,6 @@ type Docs
= DocsBool = DocsBool
| DocsDict Docs | DocsDict Docs
| DocsFloat | DocsFloat
| DocsIddict Docs
| DocsInt | DocsInt
| DocsIntDict Docs | DocsIntDict Docs
| DocsLazy (() -> Docs) | DocsLazy (() -> Docs)
@ -469,25 +467,6 @@ float =
} }
{-| Define an Iddict as defined in
[noordstar/elm-iddict](https://package.elm-lang.org/packages/noordstar/elm-iddict/latest/).
-}
iddict : Coder a -> Coder (Iddict a)
iddict (Coder old) =
Coder
{ encoder = Iddict.encode old.encoder
, decoder =
D.andThen
(\( out, logs ) ->
D.succeed out
|> Iddict.decoder
|> D.map (\o -> ( o, logs ))
)
old.decoder
, docs = DocsIddict old.docs
}
{-| Define an int value. {-| Define an int value.
-} -}
int : Coder Int int : Coder Int

View File

@ -59,7 +59,6 @@ helper functions.
type UnsignedData type UnsignedData
= UnsignedData = UnsignedData
{ age : Maybe Int { age : Maybe Int
, membership : Maybe String
, prevContent : Maybe Json.Value , prevContent : Maybe Json.Value
, redactedBecause : Maybe Event , redactedBecause : Maybe Event
, transactionId : Maybe String , transactionId : Maybe String
@ -243,10 +242,10 @@ transactionId event =
unsignedCoder : Json.Coder UnsignedData unsignedCoder : Json.Coder UnsignedData
unsignedCoder = unsignedCoder =
Json.object5 Json.object4
{ name = Text.docs.unsigned.name { name = Text.docs.unsigned.name
, description = Text.docs.unsigned.description , description = Text.docs.unsigned.description
, init = \a b c d e -> UnsignedData { age = a, membership = b, prevContent = c, redactedBecause = d, transactionId = e } , init = \a b c d -> UnsignedData { age = a, prevContent = b, redactedBecause = c, transactionId = d }
} }
(Json.field.optional.value (Json.field.optional.value
{ fieldName = "age" { fieldName = "age"
@ -255,13 +254,6 @@ unsignedCoder =
, coder = Json.int , coder = Json.int
} }
) )
(Json.field.optional.value
{ fieldName = "membership"
, toField = \(UnsignedData data) -> data.membership
, description = Text.fields.unsigned.membership
, coder = Json.string
}
)
(Json.field.optional.value (Json.field.optional.value
{ fieldName = "prevContent" { fieldName = "prevContent"
, toField = \(UnsignedData data) -> data.prevContent , toField = \(UnsignedData data) -> data.prevContent

View File

@ -35,7 +35,6 @@ behave under the user's preferred settings.
type alias Settings = type alias Settings =
{ currentVersion : String { currentVersion : String
, deviceName : String , deviceName : String
, presence : Maybe String
, removePasswordOnLogin : Bool , removePasswordOnLogin : Bool
, syncTime : Int , syncTime : Int
} }
@ -45,7 +44,7 @@ type alias Settings =
-} -}
coder : Json.Coder Settings coder : Json.Coder Settings
coder = coder =
Json.object5 Json.object4
{ name = Text.docs.settings.name { name = Text.docs.settings.name
, description = Text.docs.settings.description , description = Text.docs.settings.description
, init = Settings , init = Settings
@ -66,13 +65,6 @@ coder =
, default = Tuple.pair Default.deviceName [] , default = Tuple.pair Default.deviceName []
} }
) )
(Json.field.optional.value
{ fieldName = "presence"
, toField = .presence
, description = Text.fields.settings.presence
, coder = Json.string
}
)
(Json.field.optional.withDefault (Json.field.optional.withDefault
{ fieldName = "removePasswordOnLogin" { fieldName = "removePasswordOnLogin"
, toField = .removePasswordOnLogin , toField = .removePasswordOnLogin
@ -111,7 +103,6 @@ init : Settings
init = init =
{ currentVersion = Default.currentVersion { currentVersion = Default.currentVersion
, deviceName = Default.deviceName , deviceName = Default.deviceName
, presence = Nothing
, removePasswordOnLogin = Default.removePasswordOnLogin , removePasswordOnLogin = Default.removePasswordOnLogin
, syncTime = Default.syncTime , syncTime = Default.syncTime
} }

View File

@ -67,10 +67,10 @@ events!
-} -}
import FastDict as Dict exposing (Dict) import FastDict as Dict exposing (Dict)
import Iddict exposing (Iddict)
import Internal.Config.Text as Text import Internal.Config.Text as Text
import Internal.Filter.Timeline as Filter exposing (Filter) import Internal.Filter.Timeline as Filter exposing (Filter)
import Internal.Tools.Hashdict as Hashdict exposing (Hashdict) import Internal.Tools.Hashdict as Hashdict exposing (Hashdict)
import Internal.Tools.Iddict as Iddict exposing (Iddict)
import Internal.Tools.Json as Json import Internal.Tools.Json as Json
import Recursion import Recursion
import Recursion.Traverse import Recursion.Traverse
@ -210,7 +210,7 @@ coder =
{ fieldName = "batches" { fieldName = "batches"
, toField = \(Timeline t) -> t.batches , toField = \(Timeline t) -> t.batches
, description = Text.fields.timeline.batches , description = Text.fields.timeline.batches
, coder = Json.iddict coderIBatch , coder = Iddict.coder coderIBatch
} }
) )
(Json.field.required (Json.field.required
@ -406,8 +406,8 @@ connectIBatchToIToken (IBatchPTR bptr) pointer (Timeline tl) =
Timeline Timeline
{ tl { tl
| batches = | batches =
Iddict.update bptr Iddict.map bptr
(Maybe.map (\batch -> { batch | end = pointer })) (\batch -> { batch | end = pointer })
tl.batches tl.batches
, tokens = , tokens =
Hashdict.map tptr Hashdict.map tptr
@ -432,8 +432,8 @@ connectITokenToIBatch pointer (IBatchPTR bptr) (Timeline tl) =
(\token -> { token | starts = Set.insert bptr token.starts }) (\token -> { token | starts = Set.insert bptr token.starts })
tl.tokens tl.tokens
, batches = , batches =
Iddict.update bptr Iddict.map bptr
(Maybe.map (\batch -> { batch | start = pointer })) (\batch -> { batch | start = pointer })
tl.batches tl.batches
} }

View File

@ -1,6 +1,6 @@
module Matrix exposing module Matrix exposing
( Vault, fromUserId, fromUsername ( Vault, fromUserId, fromUsername
, VaultUpdate, update, sync , VaultUpdate, update
, addAccessToken, sendMessageEvent , addAccessToken, sendMessageEvent
) )
@ -24,7 +24,7 @@ support a monolithic public registry. (:
## Keeping the Vault up-to-date ## Keeping the Vault up-to-date
@docs VaultUpdate, update, sync @docs VaultUpdate, update
## Debugging ## Debugging
@ -141,17 +141,6 @@ sendMessageEvent data =
} }
{-| Synchronize the Vault with the Matrix API.
Effectively, this task asks the Matrix API to provide the latest information,
which will be returned as your VaultUpdate.
-}
sync : (VaultUpdate -> msg) -> Vault -> Cmd msg
sync toMsg (Vault vault) =
Api.sync vault { toMsg = Types.VaultUpdate >> toMsg }
{-| Using new VaultUpdate information, update the Vault accordingly. {-| Using new VaultUpdate information, update the Vault accordingly.
This allows us to change our perception of the Matrix environment: has anyone This allows us to change our perception of the Matrix environment: has anyone

280
tests/Test/Tools/Iddict.elm Normal file
View File

@ -0,0 +1,280 @@
module Test.Tools.Iddict exposing (..)
import Expect
import Fuzz exposing (Fuzzer)
import Internal.Tools.Iddict as Iddict exposing (Iddict)
import Internal.Tools.Json as Json
import Json.Decode as D
import Json.Encode as E
import Test exposing (..)
fuzzer : Fuzzer a -> Fuzzer (Iddict a)
fuzzer fuz =
fuz
|> Fuzz.pair Fuzz.bool
|> Fuzz.list
|> Fuzz.map
(\items ->
List.foldl
(\( rm, item ) dict ->
case Iddict.insert item dict of
( key, d ) ->
if rm then
Iddict.remove key d
else
d
)
Iddict.empty
items
)
empty : Test
empty =
describe "empty"
[ test "isEmpty"
(Iddict.empty
|> Iddict.isEmpty
|> Expect.equal True
|> always
)
, fuzz Fuzz.int
"No members"
(\i ->
Iddict.empty
|> Iddict.member i
|> Expect.equal False
)
, fuzz Fuzz.int
"Get gets Nothing"
(\i ->
Iddict.empty
|> Iddict.get i
|> Expect.equal Nothing
)
, test "Size = 0"
(Iddict.empty
|> Iddict.size
|> Expect.equal 0
|> always
)
, test "No keys"
(Iddict.empty
|> Iddict.keys
|> Expect.equal []
|> always
)
, test "No values"
(Iddict.empty
|> Iddict.values
|> Expect.equal []
|> always
)
, test "JSON encode -> decode -> empty"
(Iddict.empty
|> Iddict.encode Json.value
|> D.decodeValue (Iddict.decoder Json.value)
|> Result.map Tuple.first
|> Expect.equal (Ok Iddict.empty)
|> always
)
, test "JSON encode"
(Iddict.empty
|> Iddict.encode Json.value
|> E.encode 0
|> Expect.equal "{\"dict\":{}}"
|> always
)
, test "JSON decode"
("{\"dict\":{}}"
|> D.decodeString (Iddict.decoder Json.value)
|> Result.map Tuple.first
|> Expect.equal (Ok Iddict.empty)
|> always
)
]
singleton : Test
singleton =
let
singleFuzzer : Fuzzer (Iddict Int)
singleFuzzer =
Fuzz.map
(\i ->
Iddict.singleton i
|> Tuple.second
)
Fuzz.int
in
describe "singleton"
[ fuzz singleFuzzer
"not isEmpty"
(\single ->
single
|> Iddict.isEmpty
|> Expect.equal False
)
, fuzz Fuzz.int
"singleton == insert empty"
(\i ->
Iddict.empty
|> Iddict.insert i
|> Expect.equal (Iddict.singleton i)
)
, fuzz Fuzz.int
"First item is key 0"
(\i ->
Iddict.singleton i
|> Tuple.first
|> Expect.equal 0
)
, fuzz singleFuzzer
"Key 0 is member"
(\single ->
single
|> Iddict.member 0
|> Expect.equal True
)
, fuzz Fuzz.int
"Key 0 get returns Just value"
(\i ->
Iddict.singleton i
|> Tuple.second
|> Iddict.get 0
|> Expect.equal (Just i)
)
, fuzz singleFuzzer
"Size == 1"
(\single ->
single
|> Iddict.size
|> Expect.equal 1
)
, fuzz Fuzz.int
"Only key 0"
(\i ->
Iddict.singleton i
|> Tuple.second
|> Iddict.keys
|> Expect.equal [ 0 ]
)
, fuzz Fuzz.int
"Only value value"
(\i ->
Iddict.singleton i
|> Tuple.second
|> Iddict.values
|> Expect.equal [ i ]
)
, fuzz singleFuzzer
"JSON encode -> decode -> singleton"
(\single ->
single
|> Iddict.encode Json.int
|> D.decodeValue (Iddict.decoder Json.int)
|> Result.map Tuple.first
|> Expect.equal (Ok single)
)
, fuzz Fuzz.int
"JSON encode"
(\i ->
Iddict.singleton i
|> Tuple.second
|> Iddict.encode Json.int
|> E.encode 0
|> Expect.equal ("{\"cursor\":1,\"dict\":{\"0\":" ++ String.fromInt i ++ "}}")
)
, fuzz Fuzz.int
"JSON decode"
(\i ->
("{\"cursor\":1,\"dict\":{\"0\":" ++ String.fromInt i ++ "}}")
|> D.decodeString (Iddict.decoder Json.int)
|> Result.map Tuple.first
|> Tuple.pair 0
|> Expect.equal (Iddict.singleton i |> Tuple.mapSecond Ok)
)
]
insert : Test
insert =
describe "insert"
[ fuzz2 (fuzzer Fuzz.int)
Fuzz.int
"Add something"
(\d i ->
case Iddict.insert i d of
( key, dict ) ->
dict
|> Iddict.get key
|> Expect.equal (Just i)
)
, fuzz2 (fuzzer Fuzz.int)
Fuzz.int
"Never isEmpty"
(\d i ->
Iddict.insert i d
|> Tuple.second
|> Iddict.isEmpty
|> Expect.equal False
)
, fuzz2 (fuzzer Fuzz.int)
Fuzz.int
"New key"
(\d i ->
case Iddict.insert i d of
( key, dict ) ->
dict
|> Iddict.remove key
|> Iddict.insert i
|> (\( newKey, _ ) ->
Expect.notEqual key newKey
)
)
, fuzz2 (fuzzer Fuzz.int)
Fuzz.int
"New dict"
(\d i ->
case Iddict.insert i d of
( key, dict ) ->
dict
|> Iddict.remove key
|> Iddict.insert i
|> (\( _, newDict ) ->
Expect.notEqual dict newDict
)
)
, fuzz2 (fuzzer Fuzz.int)
Fuzz.int
"Inserted value is member"
(\d i ->
case Iddict.insert i d of
( key, dict ) ->
dict
|> Iddict.member key
|> Expect.equal True
)
, fuzz2 (fuzzer Fuzz.int)
Fuzz.int
"Get inserted value"
(\d i ->
case Iddict.insert i d of
( key, dict ) ->
dict
|> Iddict.get key
|> Expect.equal (Just i)
)
, fuzz2 (fuzzer Fuzz.int)
Fuzz.int
"size = size + 1"
(\d i ->
case Iddict.insert i d of
( _, dict ) ->
Expect.equal
(Iddict.size dict)
(Iddict.size d + 1)
)
]

View File

@ -142,16 +142,22 @@ apiContext =
] ]
json : Test
-- json : Test json =
-- json = describe "JSON encode + JSON decode"
-- describe "JSON encode + JSON decode" [ test "Empty is {}"
-- [ fuzz fuzzer (Context.init ""
-- "JSON recode" |> Context.encode
-- (\context -> |> E.encode 0
-- context |> Expect.equal "{}"
-- |> Context.encode |> always
-- |> D.decodeValue Context.decoder )
-- |> Expect.equal (Ok ( context, [] )) , fuzz fuzzer
-- ) "JSON recode"
-- ] (\context ->
context
|> Context.encode
|> D.decodeValue Context.decoder
|> Expect.equal (Ok ( context, [] ))
)
]

View File

@ -51,17 +51,16 @@ suite =
) )
] ]
] ]
, describe "JSON"
-- , describe "JSON" [ fuzz2 (fuzzer Fuzz.string)
-- [ fuzz2 (fuzzer Fuzz.string) Fuzz.int
-- Fuzz.int "JSON encode -> JSON decode"
-- "JSON encode -> JSON decode" (\envelope indent ->
-- (\envelope indent -> envelope
-- envelope |> Envelope.encode Json.string
-- |> Envelope.encode Json.string |> E.encode indent
-- |> E.encode indent |> D.decodeString (Envelope.decoder Json.string)
-- |> D.decodeString (Envelope.decoder Json.string) |> Expect.equal (Ok ( envelope, [] ))
-- |> Expect.equal (Ok ( envelope, [] )) )
-- ) ]
-- ]
] ]

View File

@ -18,26 +18,23 @@ fuzzer =
Fuzz.string Fuzz.string
|> Fuzz.map Room.init |> Fuzz.map Room.init
|> addAFewTimes Fuzz.string (\key -> Room.setAccountData key placeholderValue) |> addAFewTimes Fuzz.string (\key -> Room.setAccountData key placeholderValue)
|> addAFewTimes (Fuzz.list TestEvent.fuzzer) Room.addEvents
|> add4AFewTimes (Fuzz.list TestEvent.fuzzer)
TestFilter.fuzzer
-- |> addAFewTimes (Fuzz.list TestEvent.fuzzer) Room.addEvents (Fuzz.maybe Fuzz.string)
-- |> add4AFewTimes (Fuzz.list TestEvent.fuzzer) Fuzz.string
-- TestFilter.fuzzer (\a b c d ->
-- (Fuzz.maybe Fuzz.string) Room.Batch a b c d
-- Fuzz.string |> Room.addBatch
-- (\a b c d -> )
-- Room.Batch a b c d |> add4AFewTimes (Fuzz.list TestEvent.fuzzer)
-- |> Room.addBatch TestFilter.fuzzer
-- ) (Fuzz.maybe Fuzz.string)
-- |> add4AFewTimes (Fuzz.list TestEvent.fuzzer) Fuzz.string
-- TestFilter.fuzzer (\a b c d ->
-- (Fuzz.maybe Fuzz.string) Room.Batch a b c d
-- Fuzz.string |> Room.addSync
-- (\a b c d -> )
-- Room.Batch a b c d
-- |> Room.addSync
-- )
addAFewTimes : Fuzzer a -> (a -> Room -> Room) -> Fuzzer Room -> Fuzzer Room addAFewTimes : Fuzzer a -> (a -> Room -> Room) -> Fuzzer Room -> Fuzzer Room

View File

@ -11,7 +11,7 @@ import Test exposing (..)
fuzzer : Fuzzer Settings fuzzer : Fuzzer Settings
fuzzer = fuzzer =
Fuzz.map4 Settings Fuzz.map3 Settings
(Fuzz.oneOf (Fuzz.oneOf
[ Fuzz.constant Default.currentVersion [ Fuzz.constant Default.currentVersion
, Fuzz.string , Fuzz.string
@ -22,11 +22,6 @@ fuzzer =
, Fuzz.string , Fuzz.string
] ]
) )
(Fuzz.oneOf
[ Fuzz.constant Default.removePasswordOnLogin
, Fuzz.bool
]
)
(Fuzz.oneOf (Fuzz.oneOf
[ Fuzz.constant Default.syncTime [ Fuzz.constant Default.syncTime
, Fuzz.int , Fuzz.int
@ -50,12 +45,6 @@ suite =
|> Expect.equal Default.deviceName |> Expect.equal Default.deviceName
|> always |> always
) )
, test "Remove password on login"
(Settings.init
|> .removePasswordOnLogin
|> Expect.equal Default.removePasswordOnLogin
|> always
)
, test "Sync time" , test "Sync time"
(Settings.init (Settings.init
|> .syncTime |> .syncTime