From d360c561f9d3390745bdee9c49e5a7089c7e1829 Mon Sep 17 00:00:00 2001 From: Bram Date: Thu, 30 May 2024 16:46:44 +0200 Subject: [PATCH 01/20] Hide custom Elm configurations --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index ec38022..016b8d9 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ repl-temp-* # Elm output index.html elm.js + +# Elm configurations +elm-*.json From 7b00a46ffa83e672669bab30a62c37e6ca9a15c0 Mon Sep 17 00:00:00 2001 From: Bram Date: Mon, 3 Jun 2024 15:02:35 +0200 Subject: [PATCH 02/20] Add object1 --- src/Internal/Api/BaseUrl/Api.elm | 30 +++---------------- .../Api/LoginWithUsernameAndPassword/Api.elm | 30 +++---------------- src/Internal/Api/SendMessageEvent/Api.elm | 30 +++---------------- src/Internal/Tools/Json.elm | 21 +++++++++++-- 4 files changed, 31 insertions(+), 80 deletions(-) diff --git a/src/Internal/Api/BaseUrl/Api.elm b/src/Internal/Api/BaseUrl/Api.elm index 0e9a930..5e98ed3 100644 --- a/src/Internal/Api/BaseUrl/Api.elm +++ b/src/Internal/Api/BaseUrl/Api.elm @@ -87,12 +87,12 @@ coder = { fieldName = "m.homeserver" , toField = .homeserver , coder = - Json.object2 + Json.object1 { name = "Homeserver Information" , description = [ "Used by clients to discover homeserver information." ] - , init = \a _ -> { baseUrl = a } + , init = HomeserverInformation } (Json.field.required { fieldName = "base_url" @@ -103,17 +103,6 @@ coder = , coder = Json.string } ) - (Json.field.optional.value - { fieldName = L.field - , toField = always Nothing - , description = - [ "The Elm SDK always expects objects to have at least two fields." - , "Otherwise, what's the point of hiding the value in an object?" - , "For this reason, this empty placeholder key will always be ignored." - ] - , coder = Json.value - } - ) , description = [ "Used by clients to discover homeserver information." ] @@ -123,12 +112,12 @@ coder = { fieldName = "m.identity_server" , toField = .identityServer , coder = - Json.object2 + Json.object1 { name = "Homeserver Information" , description = [ "Used by clients to discover homeserver information." ] - , init = \a _ -> { baseUrl = a } + , init = IdentityServerInformation } (Json.field.required { fieldName = "base_url" @@ -139,17 +128,6 @@ coder = , coder = Json.string } ) - (Json.field.optional.value - { fieldName = L.field - , toField = always Nothing - , description = - [ "The Elm SDK always expects objects to have at least two fields." - , "Otherwise, what's the point of hiding the value in an object?" - , "For this reason, this empty placeholder key will always be ignored." - ] - , coder = Json.value - } - ) , description = [ "Used by clients to discover identity server information." ] diff --git a/src/Internal/Api/LoginWithUsernameAndPassword/Api.elm b/src/Internal/Api/LoginWithUsernameAndPassword/Api.elm index 17d121d..9242ba2 100644 --- a/src/Internal/Api/LoginWithUsernameAndPassword/Api.elm +++ b/src/Internal/Api/LoginWithUsernameAndPassword/Api.elm @@ -888,12 +888,12 @@ disoveryInformationCoderV1 = { fieldName = "m.homeserver" , toField = .homeserver , coder = - Json.object2 + Json.object1 { name = "Homeserver Information" , description = [ "Used by clients to discover homeserver information." ] - , init = \a _ -> { baseUrl = a } + , init = HomeserverInformation } (Json.field.required { fieldName = "base_url" @@ -904,17 +904,6 @@ disoveryInformationCoderV1 = , coder = Json.string } ) - (Json.field.optional.value - { fieldName = L.field - , toField = always Nothing - , description = - [ "The Elm SDK always expects objects to have at least two fields." - , "Otherwise, what's the point of hiding the value in an object?" - , "For this reason, this empty placeholder key will always be ignored." - ] - , coder = Json.value - } - ) , description = [ "Used by clients to discover homeserver information." ] @@ -924,12 +913,12 @@ disoveryInformationCoderV1 = { fieldName = "m.identity_server" , toField = .identityServer , coder = - Json.object2 + Json.object1 { name = "Homeserver Information" , description = [ "Used by clients to discover homeserver information." ] - , init = \a _ -> { baseUrl = a } + , init = HomeserverInformation } (Json.field.required { fieldName = "base_url" @@ -940,17 +929,6 @@ disoveryInformationCoderV1 = , coder = Json.string } ) - (Json.field.optional.value - { fieldName = L.field - , toField = always Nothing - , description = - [ "The Elm SDK always expects objects to have at least two fields." - , "Otherwise, what's the point of hiding the value in an object?" - , "For this reason, this empty placeholder key will always be ignored." - ] - , coder = Json.value - } - ) , description = [ "Used by clients to discover identity server information." ] diff --git a/src/Internal/Api/SendMessageEvent/Api.elm b/src/Internal/Api/SendMessageEvent/Api.elm index a16491f..abccf41 100644 --- a/src/Internal/Api/SendMessageEvent/Api.elm +++ b/src/Internal/Api/SendMessageEvent/Api.elm @@ -143,26 +143,15 @@ sendMessageEventV3 { content, eventType, roomId, transactionId } = coderV1 : Json.Coder SendMessageEventOutputV1 coderV1 = - Json.object2 + Json.object1 { name = "EventResponse" , description = [ "This endpoint is used to send a message event to a room. Message events allow access to historical events and pagination, making them suited for \"once-off\" activity in a room." , "The body of the request should be the content object of the event; the fields in this object will vary depending on the type of event." , "https://spec.matrix.org/legacy/r0.0.0/client_server.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid" ] - , init = always SendMessageEventOutputV1 + , init = SendMessageEventOutputV1 } - (Json.field.optional.value - { fieldName = L.field - , toField = always Nothing - , description = - [ "The Elm SDK always expects objects to have at least two fields." - , "Otherwise, what's the point of hiding the value in an object?" - , "For this reason, this empty placeholder key will always be ignored." - ] - , coder = Json.value - } - ) (Json.field.optional.value { fieldName = "event_id" , toField = .eventId @@ -174,26 +163,15 @@ coderV1 = coderV2 : Json.Coder SendMessageEventOutputV2 coderV2 = - Json.object2 + Json.object1 { name = "EventResponse" , description = [ "This endpoint is used to send a message event to a room. Message events allow access to historical events and pagination, making them suited for \"once-off\" activity in a room." , "The body of the request should be the content object of the event; the fields in this object will vary depending on the type of event." , "https://spec.matrix.org/legacy/client_server/r0.6.1.html#put-matrix-client-r0-rooms-roomid-send-eventtype-txnid" ] - , init = always SendMessageEventOutputV2 + , init = SendMessageEventOutputV2 } - (Json.field.optional.value - { fieldName = L.field - , toField = always Nothing - , description = - [ "The Elm SDK always expects objects to have at least two fields." - , "Otherwise, what's the point of hiding the value in an object?" - , "For this reason, this empty placeholder key will always be ignored." - ] - , coder = Json.value - } - ) (Json.field.required { fieldName = "event_id" , toField = .eventId diff --git a/src/Internal/Tools/Json.elm b/src/Internal/Tools/Json.elm index 28ccd8c..4962d45 100644 --- a/src/Internal/Tools/Json.elm +++ b/src/Internal/Tools/Json.elm @@ -5,7 +5,7 @@ module Internal.Tools.Json exposing , Docs(..), RequiredField(..), toDocs , list, listWithOne, slowDict, fastDict, fastIntDict, set, maybe , Field, field, parser - , object2, object3, object4, object5, object6, object7, object8, object9, object10, object11 + , object1, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11 ) {-| @@ -62,7 +62,7 @@ first. Once all fields are constructed, the user can create JSON objects. -@docs object2, object3, object4, object5, object6, object7, object8, object9, object10, object11 +@docs object1, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11 -} @@ -596,6 +596,23 @@ objectEncoder items object = |> E.maybeObject +object1 : + Descriptive { init : a -> object } + -> Field a object + -> Coder object +object1 { name, description, init } fa = + Coder + { encoder = objectEncoder [ toEncodeField fa ] + , decoder = D.map (Tuple.mapFirst init) (toDecoderField fa) + , docs = + DocsObject + { name = name + , description = description + , keys = [ toDocsField fa ] + } + } + + {-| Define an object with 2 keys type alias Human = From f6a6bb535e57835b828076413f62994f8280112e Mon Sep 17 00:00:00 2001 From: Bram Date: Sat, 8 Jun 2024 15:10:58 +0200 Subject: [PATCH 03/20] Add StrippedEvent module --- src/Internal/Tools/StrippedEvent.elm | 50 ++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/Internal/Tools/StrippedEvent.elm diff --git a/src/Internal/Tools/StrippedEvent.elm b/src/Internal/Tools/StrippedEvent.elm new file mode 100644 index 0000000..f5112ea --- /dev/null +++ b/src/Internal/Tools/StrippedEvent.elm @@ -0,0 +1,50 @@ +module Internal.Tools.StrippedEvent exposing (StrippedEvent, coder, strip) + +{-| + + +# Stripped event + +The stripped event is a simple Matrix event that does not contain any metadata. + +@docs StrippedEvent, coder, strip + +-} + +import Internal.Tools.Json as Json + + +type alias StrippedEvent = + { content : Json.Value, eventType : String } + + +coder : Json.Coder StrippedEvent +coder = + Json.object2 + { name = Debug.todo "Needs name" + , description = Debug.todo "Needs description" + , init = StrippedEvent + } + (Json.field.required + { fieldName = "content" + , toField = .content + , description = + [ "Event content" + ] + , coder = Json.value + } + ) + (Json.field.required + { fieldName = "eventType" + , toField = .eventType + , description = + [ "Event type, generally namespaced using the Java package naming convention." + ] + , coder = Json.string + } + ) + + +strip : { a | content : Json.Value, eventType : String } -> StrippedEvent +strip { content, eventType } = + { content = content, eventType = eventType } From 0ded7ab6bd4a6df457464b06aecb8eb94c6e0732 Mon Sep 17 00:00:00 2001 From: Bram Date: Sat, 8 Jun 2024 15:19:55 +0200 Subject: [PATCH 04/20] Add ephemeral events to Room --- src/Internal/Config/Text.elm | 4 ++++ src/Internal/Values/Room.elm | 25 ++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Internal/Config/Text.elm b/src/Internal/Config/Text.elm index a63e936..0c0d4ad 100644 --- a/src/Internal/Config/Text.elm +++ b/src/Internal/Config/Text.elm @@ -313,6 +313,7 @@ fields : } , room : { accountData : Desc + , ephemeral : Desc , events : Desc , roomId : Desc , state : Desc @@ -486,6 +487,9 @@ fields = , room = { accountData = [ "Room account data tracking the user's private storage about this room." ] + , ephemeral = + [ "Ephemeral events that were sent recently in this room." + ] , events = [ "Database containing events that were sent in this room." ] , roomId = diff --git a/src/Internal/Values/Room.elm b/src/Internal/Values/Room.elm index 1db7bf1..6f5c198 100644 --- a/src/Internal/Values/Room.elm +++ b/src/Internal/Values/Room.elm @@ -53,6 +53,7 @@ import Internal.Config.Text as Text import Internal.Filter.Timeline as Filter exposing (Filter) import Internal.Tools.Hashdict as Hashdict exposing (Hashdict) import Internal.Tools.Json as Json +import Internal.Tools.StrippedEvent as StrippedEvent exposing (StrippedEvent) import Internal.Values.Event as Event exposing (Event) import Internal.Values.StateManager as StateManager exposing (StateManager) import Internal.Values.Timeline as Timeline exposing (Timeline) @@ -71,6 +72,7 @@ homeserver. -} type alias Room = { accountData : Dict String Json.Value + , ephemeral : List StrippedEvent , events : Hashdict Event , roomId : String , state : StateManager @@ -86,7 +88,9 @@ type RoomUpdate | AddSync Batch | Invite User | More (List RoomUpdate) + | Optional (Maybe RoomUpdate) | SetAccountData String Json.Value + | SetEphemeral (List { eventType : String, content : Json.Value }) {-| Add new events to the Room's event directory + Room's timeline. @@ -140,7 +144,7 @@ addSync = -} coder : Json.Coder Room coder = - Json.object5 + Json.object6 { name = Text.docs.room.name , description = Text.docs.room.description , init = Room @@ -154,6 +158,15 @@ coder = , defaultToString = Json.encode (Json.fastDict Json.value) >> E.encode 0 } ) + (Json.field.optional.withDefault + { fieldName = "ephemeral" + , toField = .ephemeral + , description = Text.fields.room.ephemeral + , coder = Json.list StrippedEvent.coder + , default = ( [], [] ) + , defaultToString = Json.encode (Json.list StrippedEvent.coder) >> E.encode 0 + } + ) (Json.field.optional.withDefault { fieldName = "events" , toField = .events @@ -216,6 +229,7 @@ getAccountData key room = init : String -> Room init roomId = { accountData = Dict.empty + , ephemeral = [] , events = Hashdict.empty .eventId , roomId = roomId , state = StateManager.empty @@ -262,5 +276,14 @@ update ru room = More items -> List.foldl update room items + Optional (Just u) -> + update u room + + Optional Nothing -> + room + SetAccountData key value -> setAccountData key value room + + SetEphemeral eph -> + { room | ephemeral = eph } From 6783186c18d68b012bf28318e9e1f406facd5795 Mon Sep 17 00:00:00 2001 From: Bram Date: Sat, 8 Jun 2024 15:25:49 +0200 Subject: [PATCH 05/20] Add nextBatch value in Vault --- src/Internal/Config/Text.elm | 4 ++++ src/Internal/Values/Vault.elm | 22 +++++++++++++++++++++- tests/Test/Values/Vault.elm | 3 ++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Internal/Config/Text.elm b/src/Internal/Config/Text.elm index 0c0d4ad..c1c2060 100644 --- a/src/Internal/Config/Text.elm +++ b/src/Internal/Config/Text.elm @@ -346,6 +346,7 @@ fields : } , vault : { accountData : Desc + , nextBatch : Desc , rooms : Desc , user : Desc } @@ -565,6 +566,9 @@ fields = { accountData = [ "The account's global private data." ] + , nextBatch = + [ "The next batch that can be used to sync with the Matrix API." + ] , rooms = [ "Directory of joined rooms that the user is a member of." ] diff --git a/src/Internal/Values/Vault.elm b/src/Internal/Values/Vault.elm index 2725744..1c4112f 100644 --- a/src/Internal/Values/Vault.elm +++ b/src/Internal/Values/Vault.elm @@ -44,6 +44,7 @@ import Internal.Values.User as User exposing (User) -} type alias Vault = { accountData : Dict String Json.Value + , nextBatch : Maybe String , rooms : Hashdict Room , user : Maybe User } @@ -56,13 +57,15 @@ type VaultUpdate = CreateRoomIfNotExists String | MapRoom String Room.RoomUpdate | More (List VaultUpdate) + | Optional (Maybe VaultUpdate) | SetAccountData String Json.Value + | SetNextBatch String | SetUser User coder : Json.Coder Vault coder = - Json.object3 + Json.object4 { name = Text.docs.vault.name , description = Text.docs.vault.description , init = Vault @@ -74,6 +77,13 @@ coder = , coder = Json.fastDict Json.value } ) + (Json.field.optional.value + { fieldName = "nextBatch" + , toField = .nextBatch + , description = Text.fields.vault.nextBatch + , coder = Json.string + } + ) (Json.field.required { fieldName = "rooms" , toField = .rooms @@ -109,6 +119,7 @@ getAccountData key vault = init : Maybe User -> Vault init mUser = { accountData = Dict.empty + , nextBatch = Nothing , rooms = Hashdict.empty .roomId , user = mUser } @@ -152,8 +163,17 @@ update vu vault = More items -> List.foldl update vault items + Optional (Just u) -> + update u vault + + Optional Nothing -> + vault + SetAccountData key value -> setAccountData key value vault + SetNextBatch nb -> + { vault | nextBatch = Just nb } + SetUser user -> { vault | user = Just user } diff --git a/tests/Test/Values/Vault.elm b/tests/Test/Values/Vault.elm index 3982791..f1c90a0 100644 --- a/tests/Test/Values/Vault.elm +++ b/tests/Test/Values/Vault.elm @@ -12,11 +12,12 @@ import Test.Values.User as TestUser vault : Fuzzer Vault vault = - Fuzz.map3 Vault + Fuzz.map4 Vault (Fuzz.string |> Fuzz.map (\k -> ( k, Json.encode Json.int 0 )) |> Fuzz.list |> Fuzz.map Dict.fromList ) + (Fuzz.maybe Fuzz.string) (TestHashdict.fuzzer .roomId TestRoom.fuzzer) (Fuzz.maybe TestUser.fuzzer) From 693124aa15e5e3e91459c57288d9094c707e73f7 Mon Sep 17 00:00:00 2001 From: Bram Date: Sat, 8 Jun 2024 15:32:17 +0200 Subject: [PATCH 06/20] Add documentation for StrippedEvent --- src/Internal/Config/Text.elm | 7 +++++++ src/Internal/Tools/StrippedEvent.elm | 5 +++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Internal/Config/Text.elm b/src/Internal/Config/Text.elm index c1c2060..c40dd9a 100644 --- a/src/Internal/Config/Text.elm +++ b/src/Internal/Config/Text.elm @@ -124,6 +124,7 @@ docs : , room : TypeDocs , settings : TypeDocs , stateManager : TypeDocs + , strippedEvent : TypeDocs , timeline : TypeDocs , timelineFilter : TypeDocs , unsigned : TypeDocs @@ -206,6 +207,12 @@ docs = , "Instead of making the user loop through the room's timeline of events, the StateManager offers the user a dictionary-like experience to navigate through the Matrix room state." ] } + , strippedEvent = + { name = "StrippedEvent" + , description = + [ "The StrippedEvent is a simplified Matrix event that contains no metadata." + ] + } , timeline = { name = "Timeline" , description = diff --git a/src/Internal/Tools/StrippedEvent.elm b/src/Internal/Tools/StrippedEvent.elm index f5112ea..8465cba 100644 --- a/src/Internal/Tools/StrippedEvent.elm +++ b/src/Internal/Tools/StrippedEvent.elm @@ -11,6 +11,7 @@ The stripped event is a simple Matrix event that does not contain any metadata. -} +import Internal.Config.Text as Text import Internal.Tools.Json as Json @@ -21,8 +22,8 @@ type alias StrippedEvent = coder : Json.Coder StrippedEvent coder = Json.object2 - { name = Debug.todo "Needs name" - , description = Debug.todo "Needs description" + { name = Text.docs.strippedEvent.name + , description = Text.docs.strippedEvent.description , init = StrippedEvent } (Json.field.required From 0092f96a255e8aaae665a318632f815f7afc3c21 Mon Sep 17 00:00:00 2001 From: Bram Date: Thu, 13 Jun 2024 22:27:12 +0200 Subject: [PATCH 07/20] Add /sync at r0.3.0 --- src/Internal/Api/Sync/Api.elm | 94 ++++ src/Internal/Api/Sync/V1.elm | 801 ++++++++++++++++++++++++++++++++++ 2 files changed, 895 insertions(+) create mode 100644 src/Internal/Api/Sync/Api.elm create mode 100644 src/Internal/Api/Sync/V1.elm diff --git a/src/Internal/Api/Sync/Api.elm b/src/Internal/Api/Sync/Api.elm new file mode 100644 index 0000000..e51d35f --- /dev/null +++ b/src/Internal/Api/Sync/Api.elm @@ -0,0 +1,94 @@ +module Internal.Api.Sync.Api exposing (..) + +{-| + + +# Sync + +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 +communicate with the Matrix server about the Vault's needs. + +@docs Phantom + +-} + +import Internal.Api.Api as A +import Internal.Api.Request as R +import Internal.Api.Sync.V1 as V1 +import Internal.Filter.Timeline as Filter + + + +-- For simplicity, we will not use a filter for now +-- and assume that every client always wants to receive all events. +-- type FilterV1 +-- = FilterV1 Filter +-- | FilterIdV1 String Filter +-- | NoFilter + + +type alias Phantom a = + { a | accessToken : (), baseUrl : (), versions : () } + + +type alias PhantomV1 a = + { a | accessToken : (), baseUrl : () } + + +type PresenceV1 + = OfflineV1 + + +type alias SyncInput = + { -- filter : FilterV1, + fullState : Maybe Bool + , presenceV1 : Maybe PresenceV1 + , since : Maybe String + , timeout : Maybe Int + } + + +type alias SyncInputV1 a = + { a + | -- filter : FilterV1 , + since : Maybe String + , fullState : Maybe Bool + , presenceV1 : Maybe PresenceV1 + , timeout : Maybe Int + } + + +sync : SyncInput -> A.TaskChain (Phantom a) (Phantom a) +sync = + A.startWithVersion "r0.0.0" syncV1 + |> A.versionChain + + +syncV1 : SyncInputV1 i -> A.TaskChain (PhantomV1 a) (PhantomV1 a) +syncV1 data = + A.request + { attributes = + [ R.accessToken + , R.queryOpString "filter" Nothing -- FILTER HERE + , 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 + ] + , coder = V1.syncResponseCoder + , contextChange = always identity + , method = "GET" + , path = [ "_matrix", "client", "r0", "sync" ] + , toUpdate = + \out -> + ( V1.syncResponseToUpdate + { filter = Filter.pass -- FILTER HERE + , since = data.since + } + out + , [] + ) + } diff --git a/src/Internal/Api/Sync/V1.elm b/src/Internal/Api/Sync/V1.elm new file mode 100644 index 0000000..3685c63 --- /dev/null +++ b/src/Internal/Api/Sync/V1.elm @@ -0,0 +1,801 @@ +module Internal.Api.Sync.V1 exposing (..) + +{-| + + +# Sync V1 + +Given the complexity of the /sync endpoint, the JSON coders have been placed +in separate modules. Version 1 provides a valid JSON coder for the following +spec versions: + + - r0.3.0 : + +-- @docs coder + +-} + +import FastDict as Dict exposing (Dict) +import Internal.Config.Log exposing (Log) +import Internal.Filter.Timeline exposing (Filter) +import Internal.Tools.Json as Json +import Internal.Tools.StrippedEvent as StrippedEvent +import Internal.Tools.Timestamp as Timestamp exposing (Timestamp) +import Internal.Values.Envelope as E exposing (EnvelopeUpdate(..)) +import Internal.Values.Event as Event +import Internal.Values.Room as R exposing (RoomUpdate(..)) +import Internal.Values.User as User exposing (User) +import Internal.Values.Vault as V exposing (VaultUpdate(..)) + + +type alias SyncResponse = + { accountData : Maybe AccountData + , deviceLists : Maybe DeviceLists + , nextBatch : Maybe String + , presence : Maybe Presence + , rooms : Maybe Rooms + , toDevice : Maybe ToDevice + } + + +syncResponseCoder : Json.Coder SyncResponse +syncResponseCoder = + Json.object6 + { name = "SyncResponse" + , description = + [ "Response from the /sync endpoint" + ] + , init = SyncResponse + } + (Json.field.optional.value + { fieldName = "account_data" + , toField = .accountData + , description = + [ "The global private data created by this user." + ] + , coder = accountDataCoder + } + ) + (Json.field.optional.value + { fieldName = "device_lists" + , toField = .deviceLists + , description = + [ "Information on end-to-end device updates, as specified in End-to-end encryption." + ] + , coder = deviceListsCoder + } + ) + (Json.field.optional.value + { fieldName = "next_batch" + , toField = .nextBatch + , description = + [ "The batch token to supply in the since param of the next /sync request." + ] + , coder = Json.string + } + ) + (Json.field.optional.value + { fieldName = "presence" + , toField = .presence + , description = + [ "The updates to the presence status of other users." + ] + , coder = presenceCoder + } + ) + (Json.field.optional.value + { fieldName = "rooms" + , toField = .rooms + , description = + [ "Updates to rooms." + ] + , coder = roomsCoder + } + ) + (Json.field.optional.value + { fieldName = "to_device" + , toField = .toDevice + , description = + [ "Information on the send-to-device messages for the client device, as defined in Send-to-Device messaging." + ] + , coder = toDeviceCoder + } + ) + + +syncResponseToUpdate : { filter : Filter, since : Maybe String } -> SyncResponse -> EnvelopeUpdate VaultUpdate +syncResponseToUpdate data response = + E.More + [ E.ContentUpdate + (V.More + -- global account data + [ response.accountData + |> Maybe.map + (\acd -> + acd.events + |> List.map (\e -> V.SetAccountData e.eventType e.content) + |> V.More + ) + |> V.Optional + + -- rooms to update + , Maybe.map2 + (\rooms nextBatch -> + roomsToUpdate + { filter = data.filter + , nextBatch = nextBatch + , since = data.since + } + rooms + ) + response.rooms + response.nextBatch + |> V.Optional + + -- next batch + , response.nextBatch + |> Maybe.map V.SetNextBatch + |> V.Optional + ] + ) + + -- Add more updates here + ] + + +type alias ToDevice = + { events : List DeviceEvent } + + +toDeviceCoder : Json.Coder ToDevice +toDeviceCoder = + Json.object1 + { name = "ToDevice" + , description = + [ "Events indicating users' to_device changes, usually involving cryptography" + ] + , init = ToDevice + } + (Json.field.required + { fieldName = "events" + , toField = .events + , description = + [ "List of events" + ] + , coder = Json.list deviceEventCoder + } + ) + + +type alias DeviceEvent = + { content : EventContent, sender : User, eventType : String } + + +deviceEventCoder : Json.Coder DeviceEvent +deviceEventCoder = + Json.object3 + { name = "DeviceEvent" + , description = + [ "Partially stripped event used for sending to_device events." + ] + , init = DeviceEvent + } + (Json.field.required + { fieldName = "content" + , toField = .content + , description = + [ "The content of this event. The fields in this object will vary depending on the type of event." + ] + , coder = eventContentCoder + } + ) + (Json.field.required + { fieldName = "sender" + , toField = .sender + , description = + [ "The Matrix user ID of the user who sent this event." + ] + , coder = User.coder + } + ) + (Json.field.required + { fieldName = "type" + , toField = .eventType + , description = + [ "The type of event." + ] + , coder = Json.string + } + ) + + +type alias DeviceLists = + { changed : List String } + + +deviceListsCoder : Json.Coder DeviceLists +deviceListsCoder = + Json.object1 + { name = "DeviceLists" + , description = + [ "This module adds an optional device_lists property to the /sync response, as specified below. The server need only populate this property for an incremental /sync (ie, one where the since parameter was specified). The client is expected to use /keys/query or /keys/changes for the equivalent functionality after an initial sync, as documented in \"Tracking the device list for a user.\"" + ] + , init = DeviceLists + } + (Json.field.required + { fieldName = "changed" + , toField = .changed + , description = + [ "List of users who have updated their device identity keys since the previous sync response." + ] + , coder = Json.list Json.string + } + ) + + +type alias Rooms = + { invite : Dict String InvitedRoom + , join : Dict String JoinedRoom + , leave : Dict String LeftRoom + } + + +roomsCoder : Json.Coder Rooms +roomsCoder = + Json.object3 + { name = "Rooms" + , description = + [ "Summary of all relevant updates to all rooms." + ] + , init = Rooms + } + (Json.field.optional.withDefault + { fieldName = "invite" + , toField = .invite + , description = + [ "The rooms that the user has been invited to." + ] + , coder = Json.fastDict invitedRoomCoder + , default = ( Dict.empty, [] ) + , defaultToString = always "{}" + } + ) + (Json.field.optional.withDefault + { fieldName = "join" + , toField = .join + , description = + [ "The rooms that the user is a member of." + ] + , coder = Json.fastDict joinedRoomCoder + , default = ( Dict.empty, [] ) + , defaultToString = always "{}" + } + ) + (Json.field.optional.withDefault + { fieldName = "leave" + , toField = .leave + , description = + [ "The rooms that the user has left." + ] + , coder = Json.fastDict leftRoomCoder + , default = ( Dict.empty, [] ) + , defaultToString = always "{}" + } + ) + + +roomsToUpdate : { filter : Filter, nextBatch : String, since : Maybe String } -> Rooms -> VaultUpdate +roomsToUpdate data rooms = + rooms.join + |> Dict.foldl + (\roomId joinedRoom items -> + List.append items + [ V.CreateRoomIfNotExists roomId + , V.MapRoom roomId + (joinedRoomToUpdate + { filter = data.filter + , nextBatch = data.nextBatch + , roomId = roomId + , since = data.since + } + joinedRoom + ) + ] + ) + [] + |> V.More + + +type alias InvitedRoom = + { inviteState : Maybe InviteState } + + +invitedRoomCoder : Json.Coder InvitedRoom +invitedRoomCoder = + Json.object1 + { name = "InvitedRoom" + , description = + [ "Room that the user was invited to." + ] + , init = InvitedRoom + } + (Json.field.optional.value + { fieldName = "invite_state" + , toField = .inviteState + , description = + [ "The state of a room that the user has been invited to. These state events may only have the sender, type, state_key and content keys present. These events do not replace any state that the client already has for the room, for example if the client has archived the room. Instead the client should keep two separate copies of the state: the one from the invite_state and one from the archived state. If the client joins the room then the current state will be given as a delta against the archived state not the invite_state." + ] + , coder = inviteStateCoder + } + ) + + +type alias InviteState = + { events : List Event } + + +inviteStateCoder : Json.Coder InviteState +inviteStateCoder = + Json.object1 + { name = "InviteState" + , description = + [ "Invite (state) events describing the state of an invited room." + ] + , init = Ephemeral + } + (Json.field.required + { fieldName = "events" + , toField = .events + , description = + [ "List of events" + ] + , coder = Json.list eventCoder + } + ) + + +type alias JoinedRoom = + { accountData : Maybe AccountData + , ephemeral : Maybe Ephemeral + , state : Maybe State + , timeline : Maybe Timeline + } + + +joinedRoomCoder : Json.Coder JoinedRoom +joinedRoomCoder = + Json.object4 + { name = "JoinedRoom" + , description = + [ "Matrix room that the user is a member of." + ] + , init = JoinedRoom + } + (Json.field.optional.value + { fieldName = "account_data" + , toField = .accountData + , description = + [ "The private data that this user has attached to this room." + ] + , coder = accountDataCoder + } + ) + (Json.field.optional.value + { fieldName = "ephemeral" + , toField = .ephemeral + , description = + [ "The ephemeral events in the room that aren't recorded in the timeline or state of the room. e.g. typing." + ] + , coder = ephemeralCoder + } + ) + (Json.field.optional.value + { fieldName = "state" + , toField = .state + , description = + [ "Updates to the state, between the time indicated by the since parameter, and the start of the timeline (or all state up to the start of the timeline, if since is not given, or full_state is true)." + ] + , coder = stateCoder + } + ) + (Json.field.optional.value + { fieldName = "timeline" + , toField = .timeline + , description = + [ "The timeline of messages and state changes in the room." + ] + , coder = timelineCoder + } + ) + + +joinedRoomToUpdate : { filter : Filter, nextBatch : String, roomId : String, since : Maybe String } -> JoinedRoom -> RoomUpdate +joinedRoomToUpdate data room = + R.More + [ room.accountData + |> Maybe.map + (\acd -> + acd.events + |> List.map (\event -> R.SetAccountData event.eventType event.content) + |> R.More + ) + |> R.Optional + , room.ephemeral + |> Maybe.map ephemeralToUpdate + |> R.Optional + , room.timeline + |> Maybe.map (timelineToUpdate data) + |> R.Optional + ] + + +type alias Ephemeral = + { events : List Event } + + +ephemeralCoder : Json.Coder Ephemeral +ephemeralCoder = + Json.object1 + { name = "Ephemeral" + , description = + [ "Ephemeral events sent to the user from a room." + ] + , init = Ephemeral + } + (Json.field.required + { fieldName = "events" + , toField = .events + , description = + [ "List of events" + ] + , coder = Json.list eventCoder + } + ) + + +ephemeralToUpdate : Ephemeral -> RoomUpdate +ephemeralToUpdate { events } = + events + |> List.map StrippedEvent.strip + |> R.SetEphemeral + + +type alias LeftRoom = + { state : Maybe State, timeline : Maybe Timeline } + + +leftRoomCoder : Json.Coder LeftRoom +leftRoomCoder = + Json.object2 + { name = "LeftRoom" + , description = + [ "Room that the user is no longer a member of." + ] + , init = LeftRoom + } + (Json.field.optional.value + { fieldName = "state" + , toField = .state + , description = + [ "The state updates for the room up to the start of the timeline." + ] + , coder = stateCoder + } + ) + (Json.field.optional.value + { fieldName = "timeline" + , toField = .timeline + , description = + [ "The timeline of messages and state changes in the room up to the point when the user left." + ] + , coder = timelineCoder + } + ) + + +type alias State = + { events : List Event } + + +stateCoder : Json.Coder State +stateCoder = + Json.object1 + { name = "State" + , description = + [ "List of (state) events describing the room's state." + ] + , init = State + } + (Json.field.required + { fieldName = "events" + , toField = .events + , description = [ "List of events" ] + , coder = Json.list eventCoder + } + ) + + +type alias Timeline = + { events : List Event + , limited : Bool + , prevBatch : Maybe String + } + + +timelineCoder : Json.Coder Timeline +timelineCoder = + Json.object3 + { name = "Timeline" + , description = + [ "Timeline type describing a timeline in a room." + ] + , init = Timeline + } + (Json.field.required + { fieldName = "events" + , toField = .events + , description = + [ "List of events" + ] + , coder = Json.list eventCoder + } + ) + (Json.field.optional.withDefault + { fieldName = "limited" + , toField = .limited + , description = + [ "True if the number of events returned was limited by the limit on the filter" + ] + , coder = Json.bool + , default = ( False, [] ) + , defaultToString = + \b -> + if b then + "true" + + else + "false" + } + ) + (Json.field.optional.value + { fieldName = "prev_batch" + , toField = .prevBatch + , description = + [ "A token that can be supplied to to the from parameter of the rooms/{roomId}/messages endpoint" + , "If the batch was limited then this is a token that can be supplied to the server to retrieve earlier events" + ] + , coder = Json.string + } + ) + + +timelineToUpdate : { filter : Filter, nextBatch : String, roomId : String, since : Maybe String } -> Timeline -> RoomUpdate +timelineToUpdate { filter, nextBatch, roomId, since } t = + R.AddSync + { events = List.map (eventToUpdate roomId) t.events + , filter = filter + , start = + case t.prevBatch of + Just _ -> + t.prevBatch + + Nothing -> + since + , end = nextBatch + } + + +type alias Presence = + { events : List Event } + + +presenceCoder : Json.Coder Presence +presenceCoder = + Json.object1 + { name = "Presence" + , description = + [ "Events indicating users' presence" + ] + , init = Ephemeral + } + (Json.field.required + { fieldName = "events" + , toField = .events + , description = + [ "List of events" + ] + , coder = Json.list eventCoder + } + ) + + +type alias AccountData = + { events : List Event } + + +accountDataCoder : Json.Coder AccountData +accountDataCoder = + Json.object1 + { name = "AccountData" + , description = + [ "Account data events sent by the user in a given room using a different client." + ] + , init = Ephemeral + } + (Json.field.required + { fieldName = "events" + , toField = .events + , description = + [ "List of events" + ] + , coder = Json.list eventCoder + } + ) + + +type alias Event = + { content : EventContent + , eventId : String + , originServerTs : Timestamp + , sender : User + , stateKey : Maybe String + , eventType : String + , unsigned : Maybe Unsigned + } + + +eventCoder : Json.Coder Event +eventCoder = + Json.object7 + { name = "Event" + , description = + [ "Event describing a JSON object sent in a room." + ] + , init = Event + } + (Json.field.required + { fieldName = "content" + , toField = .content + , description = + [ "The content of this event. The fields in this object will vary depending on the type of event." + ] + , coder = eventContentCoder + } + ) + (Json.field.required + { fieldName = "event_id" + , toField = .eventId + , description = + [ "The ID of this event, if applicable." + ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "origin_server_ts" + , toField = .originServerTs + , description = + [ "Timestamp in milliseconds on originating homeserver when this event was sent." + ] + , coder = Timestamp.coder + } + ) + (Json.field.required + { fieldName = "sender" + , toField = .sender + , description = + [ "The MXID of the user who sent this event." + ] + , coder = User.coder + } + ) + (Json.field.optional.value + { fieldName = "state_key" + , toField = .stateKey + , description = + [ "This key will only be present for state events. A unique key which defines the overwriting semantics for this piece of room state." + ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "type" + , toField = .eventType + , description = + [ "The type of event." + ] + , coder = Json.string + } + ) + (Json.field.optional.value + { fieldName = "unsigned" + , toField = .unsigned + , description = + [ "Information about this event which was not sent by the originating homeserver" + ] + , coder = unsignedCoder + } + ) + + +eventToUpdate : String -> Event -> Event.Event +eventToUpdate roomId e = + -- TODO: Use Recursion module for call stack safety + { content = e.content + , eventId = e.eventId + , originServerTs = e.originServerTs + , roomId = roomId + , sender = e.sender + , stateKey = e.stateKey + , eventType = e.eventType + , unsigned = + Maybe.map + (\(Unsigned u) -> + Event.UnsignedData + { age = Just u.age + , transactionId = u.transactionId + , redactedBecause = Maybe.map (eventToUpdate roomId) u.redactedBecause + , prevContent = u.prevContent + } + ) + e.unsigned + } + + +type Unsigned + = Unsigned { age : Int, prevContent : Maybe EventContent, redactedBecause : Maybe Event, transactionId : Maybe String } + + +unsignedCoder : Json.Coder Unsigned +unsignedCoder = + Json.object4 + { name = "Unsigned" + , description = + [ "Information about the event that doesn't originate from the original homeserver." + ] + , init = \a b c d -> Unsigned { age = a, prevContent = b, redactedBecause = c, transactionId = d } + } + (Json.field.required + { fieldName = "age" + , toField = \(Unsigned u) -> u.age + , description = + [ "Time in milliseconds since the event was sent." ] + , coder = Json.int + } + ) + (Json.field.optional.value + { fieldName = "prev_content" + , toField = \(Unsigned u) -> u.prevContent + , description = + [ "The previous content for this state. This will be present only for state events appearing in the timeline. If this is not a state event, or there is no previous content, this key will be missing." + ] + , coder = eventContentCoder + } + ) + (Json.field.optional.value + { fieldName = "redacted_because" + , toField = \(Unsigned u) -> u.redactedBecause + , description = + [ "The event that redacted this event, if any." + ] + , coder = Json.lazy (\() -> eventCoder) + } + ) + (Json.field.optional.value + { fieldName = "transaction_id" + , toField = \(Unsigned u) -> u.transactionId + , description = + [ "The transaction ID set when this message was sent. This key will only be present for message events sent by the device calling this API." + ] + , coder = Json.string + } + ) + + +type alias EventContent = + Json.Value + + +eventContentCoder : Json.Coder EventContent +eventContentCoder = + Json.value From 7ab21b43144d80d71e49b8198c5744918e8ef660 Mon Sep 17 00:00:00 2001 From: Bram Date: Thu, 13 Jun 2024 22:42:36 +0200 Subject: [PATCH 08/20] Add /sync at r0.4.0 (partially) --- src/Internal/Api/Sync/V1.elm | 4 +- src/Internal/Api/Sync/V2.elm | 226 +++++++++++++++++++++++++++++++++++ 2 files changed, 228 insertions(+), 2 deletions(-) create mode 100644 src/Internal/Api/Sync/V2.elm diff --git a/src/Internal/Api/Sync/V1.elm b/src/Internal/Api/Sync/V1.elm index 3685c63..4f26038 100644 --- a/src/Internal/Api/Sync/V1.elm +++ b/src/Internal/Api/Sync/V1.elm @@ -596,7 +596,7 @@ presenceCoder = , description = [ "Events indicating users' presence" ] - , init = Ephemeral + , init = Presence } (Json.field.required { fieldName = "events" @@ -620,7 +620,7 @@ accountDataCoder = , description = [ "Account data events sent by the user in a given room using a different client." ] - , init = Ephemeral + , init = AccountData } (Json.field.required { fieldName = "events" diff --git a/src/Internal/Api/Sync/V2.elm b/src/Internal/Api/Sync/V2.elm new file mode 100644 index 0000000..6c55a29 --- /dev/null +++ b/src/Internal/Api/Sync/V2.elm @@ -0,0 +1,226 @@ +module Internal.Api.Sync.V2 exposing (..) + +{-| + + +# Sync V1 + +Given the complexity of the /sync endpoint, the JSON coders have been placed +in separate modules. Version 2 provides a valid JSON coder for the following +spec versions: + + - r0.4.0 : + +-- @docs coder + +-} +import Internal.Tools.StrippedEvent as StrippedEvent exposing (StrippedEvent) +import Internal.Api.Sync.V1 as V1 +import Internal.Tools.Json as Json +import Internal.Tools.Timestamp as Timestamp exposing (Timestamp) +import Internal.Values.User as User exposing (User) +import Internal.Values.Event as Event + +type alias RoomEvent = + { content : Json.Value + , eventId : String + , originServerTs : Timestamp + , sender : User + , stateKey : Maybe String + , eventType : String + , unsigned : Maybe Unsigned + } + + +roomEventCoder : Json.Coder RoomEvent +roomEventCoder = + Json.object7 + { name = "Event" + , description = + [ "Event describing a JSON object sent in a room." + ] + , init = RoomEvent + } + (Json.field.required + { fieldName = "content" + , toField = .content + , description = + [ "The content of this event. The fields in this object will vary depending on the type of event." + ] + , coder = Json.value + } + ) + (Json.field.required + { fieldName = "event_id" + , toField = .eventId + , description = + [ "The ID of this event, if applicable." + ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "origin_server_ts" + , toField = .originServerTs + , description = + [ "Timestamp in milliseconds on originating homeserver when this event was sent." + ] + , coder = Timestamp.coder + } + ) + (Json.field.required + { fieldName = "sender" + , toField = .sender + , description = + [ "The MXID of the user who sent this event." + ] + , coder = User.coder + } + ) + (Json.field.optional.value + { fieldName = "state_key" + , toField = .stateKey + , description = + [ "This key will only be present for state events. A unique key which defines the overwriting semantics for this piece of room state." + ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "type" + , toField = .eventType + , description = + [ "The type of event." + ] + , coder = Json.string + } + ) + (Json.field.optional.value + { fieldName = "unsigned" + , toField = .unsigned + , description = + [ "Information about this event which was not sent by the originating homeserver" + ] + , coder = unsignedCoder + } + ) + + +roomEventToUpdate : String -> RoomEvent -> Event.Event +roomEventToUpdate roomId e = + -- TODO: Use Recursion module for call stack safety + { content = e.content + , eventId = e.eventId + , originServerTs = e.originServerTs + , roomId = roomId + , sender = e.sender + , stateKey = e.stateKey + , eventType = e.eventType + , unsigned = + Maybe.map + (\(Unsigned u) -> + Event.UnsignedData + { age = Just u.age + , transactionId = u.transactionId + , redactedBecause = Maybe.map (roomEventToUpdate roomId) u.redactedBecause + , prevContent = u.prevContent + } + ) + e.unsigned + } + +type Unsigned + = Unsigned { age : Int, prevContent : Maybe Json.Value, redactedBecause : Maybe RoomEvent, transactionId : Maybe String } + + +unsignedCoder : Json.Coder Unsigned +unsignedCoder = + Json.object4 + { name = "Unsigned" + , description = + [ "Information about the event that doesn't originate from the original homeserver." + ] + , init = \a b c d -> Unsigned { age = a, prevContent = b, redactedBecause = c, transactionId = d } + } + (Json.field.required + { fieldName = "age" + , toField = \(Unsigned u) -> u.age + , description = + [ "Time in milliseconds since the event was sent." ] + , coder = Json.int + } + ) + (Json.field.optional.value -- Officially not supported in this spec version, but hey, what gives to try and look for it? + { fieldName = "prev_content" + , toField = \(Unsigned u) -> u.prevContent + , description = + [ "The previous content for this state. This will be present only for state events appearing in the timeline. If this is not a state event, or there is no previous content, this key will be missing." + ] + , coder = Json.value + } + ) + (Json.field.optional.value + { fieldName = "redacted_because" + , toField = \(Unsigned u) -> u.redactedBecause + , description = + [ "The event that redacted this event, if any." + ] + , coder = Json.lazy (\() -> roomEventCoder) + } + ) + (Json.field.optional.value + { fieldName = "transaction_id" + , toField = \(Unsigned u) -> u.transactionId + , description = + [ "The transaction ID set when this message was sent. This key will only be present for message events sent by the device calling this API." + ] + , coder = Json.string + } + ) + +type alias Presence = { events : List Event } + +presenceCoder : Json.Coder Presence +presenceCoder = + Json.object1 + { name = "Presence" + , description = + [ "Events indicating users' presence" + ] + , init = Presence + } + (Json.field.required + { fieldName = "events" + , toField = .events + , description = + [ "List of events" + ] + , coder = Json.list eventCoder + } + ) + +type alias AccountData = { events : List Event } + +accountDataCoder : Json.Coder AccountData +accountDataCoder = + Json.object1 + { name = "AccountData" + , description = + [ "Account data events sent by the user in a given room using a different client." + ] + , init = AccountData + } + (Json.field.required + { fieldName = "events" + , toField = .events + , description = + [ "List of events" + ] + , coder = Json.list eventCoder + } + ) + +type alias Event = StrippedEvent + +eventCoder : Json.Coder Event +eventCoder = StrippedEvent.coder From 95f0aa2934c788338b887d1777072f06e5710bd4 Mon Sep 17 00:00:00 2001 From: Bram Date: Thu, 13 Jun 2024 22:43:25 +0200 Subject: [PATCH 09/20] Remove deprecated spec version To improve backwards compatibility, we'll start with v1.1 and go from there. Then, if the spec versions seem fit, we can work backwards. --- src/Internal/Api/Sync/V1.elm | 801 ----------------------------------- src/Internal/Api/Sync/V2.elm | 226 ---------- 2 files changed, 1027 deletions(-) delete mode 100644 src/Internal/Api/Sync/V1.elm delete mode 100644 src/Internal/Api/Sync/V2.elm diff --git a/src/Internal/Api/Sync/V1.elm b/src/Internal/Api/Sync/V1.elm deleted file mode 100644 index 4f26038..0000000 --- a/src/Internal/Api/Sync/V1.elm +++ /dev/null @@ -1,801 +0,0 @@ -module Internal.Api.Sync.V1 exposing (..) - -{-| - - -# Sync V1 - -Given the complexity of the /sync endpoint, the JSON coders have been placed -in separate modules. Version 1 provides a valid JSON coder for the following -spec versions: - - - r0.3.0 : - --- @docs coder - --} - -import FastDict as Dict exposing (Dict) -import Internal.Config.Log exposing (Log) -import Internal.Filter.Timeline exposing (Filter) -import Internal.Tools.Json as Json -import Internal.Tools.StrippedEvent as StrippedEvent -import Internal.Tools.Timestamp as Timestamp exposing (Timestamp) -import Internal.Values.Envelope as E exposing (EnvelopeUpdate(..)) -import Internal.Values.Event as Event -import Internal.Values.Room as R exposing (RoomUpdate(..)) -import Internal.Values.User as User exposing (User) -import Internal.Values.Vault as V exposing (VaultUpdate(..)) - - -type alias SyncResponse = - { accountData : Maybe AccountData - , deviceLists : Maybe DeviceLists - , nextBatch : Maybe String - , presence : Maybe Presence - , rooms : Maybe Rooms - , toDevice : Maybe ToDevice - } - - -syncResponseCoder : Json.Coder SyncResponse -syncResponseCoder = - Json.object6 - { name = "SyncResponse" - , description = - [ "Response from the /sync endpoint" - ] - , init = SyncResponse - } - (Json.field.optional.value - { fieldName = "account_data" - , toField = .accountData - , description = - [ "The global private data created by this user." - ] - , coder = accountDataCoder - } - ) - (Json.field.optional.value - { fieldName = "device_lists" - , toField = .deviceLists - , description = - [ "Information on end-to-end device updates, as specified in End-to-end encryption." - ] - , coder = deviceListsCoder - } - ) - (Json.field.optional.value - { fieldName = "next_batch" - , toField = .nextBatch - , description = - [ "The batch token to supply in the since param of the next /sync request." - ] - , coder = Json.string - } - ) - (Json.field.optional.value - { fieldName = "presence" - , toField = .presence - , description = - [ "The updates to the presence status of other users." - ] - , coder = presenceCoder - } - ) - (Json.field.optional.value - { fieldName = "rooms" - , toField = .rooms - , description = - [ "Updates to rooms." - ] - , coder = roomsCoder - } - ) - (Json.field.optional.value - { fieldName = "to_device" - , toField = .toDevice - , description = - [ "Information on the send-to-device messages for the client device, as defined in Send-to-Device messaging." - ] - , coder = toDeviceCoder - } - ) - - -syncResponseToUpdate : { filter : Filter, since : Maybe String } -> SyncResponse -> EnvelopeUpdate VaultUpdate -syncResponseToUpdate data response = - E.More - [ E.ContentUpdate - (V.More - -- global account data - [ response.accountData - |> Maybe.map - (\acd -> - acd.events - |> List.map (\e -> V.SetAccountData e.eventType e.content) - |> V.More - ) - |> V.Optional - - -- rooms to update - , Maybe.map2 - (\rooms nextBatch -> - roomsToUpdate - { filter = data.filter - , nextBatch = nextBatch - , since = data.since - } - rooms - ) - response.rooms - response.nextBatch - |> V.Optional - - -- next batch - , response.nextBatch - |> Maybe.map V.SetNextBatch - |> V.Optional - ] - ) - - -- Add more updates here - ] - - -type alias ToDevice = - { events : List DeviceEvent } - - -toDeviceCoder : Json.Coder ToDevice -toDeviceCoder = - Json.object1 - { name = "ToDevice" - , description = - [ "Events indicating users' to_device changes, usually involving cryptography" - ] - , init = ToDevice - } - (Json.field.required - { fieldName = "events" - , toField = .events - , description = - [ "List of events" - ] - , coder = Json.list deviceEventCoder - } - ) - - -type alias DeviceEvent = - { content : EventContent, sender : User, eventType : String } - - -deviceEventCoder : Json.Coder DeviceEvent -deviceEventCoder = - Json.object3 - { name = "DeviceEvent" - , description = - [ "Partially stripped event used for sending to_device events." - ] - , init = DeviceEvent - } - (Json.field.required - { fieldName = "content" - , toField = .content - , description = - [ "The content of this event. The fields in this object will vary depending on the type of event." - ] - , coder = eventContentCoder - } - ) - (Json.field.required - { fieldName = "sender" - , toField = .sender - , description = - [ "The Matrix user ID of the user who sent this event." - ] - , coder = User.coder - } - ) - (Json.field.required - { fieldName = "type" - , toField = .eventType - , description = - [ "The type of event." - ] - , coder = Json.string - } - ) - - -type alias DeviceLists = - { changed : List String } - - -deviceListsCoder : Json.Coder DeviceLists -deviceListsCoder = - Json.object1 - { name = "DeviceLists" - , description = - [ "This module adds an optional device_lists property to the /sync response, as specified below. The server need only populate this property for an incremental /sync (ie, one where the since parameter was specified). The client is expected to use /keys/query or /keys/changes for the equivalent functionality after an initial sync, as documented in \"Tracking the device list for a user.\"" - ] - , init = DeviceLists - } - (Json.field.required - { fieldName = "changed" - , toField = .changed - , description = - [ "List of users who have updated their device identity keys since the previous sync response." - ] - , coder = Json.list Json.string - } - ) - - -type alias Rooms = - { invite : Dict String InvitedRoom - , join : Dict String JoinedRoom - , leave : Dict String LeftRoom - } - - -roomsCoder : Json.Coder Rooms -roomsCoder = - Json.object3 - { name = "Rooms" - , description = - [ "Summary of all relevant updates to all rooms." - ] - , init = Rooms - } - (Json.field.optional.withDefault - { fieldName = "invite" - , toField = .invite - , description = - [ "The rooms that the user has been invited to." - ] - , coder = Json.fastDict invitedRoomCoder - , default = ( Dict.empty, [] ) - , defaultToString = always "{}" - } - ) - (Json.field.optional.withDefault - { fieldName = "join" - , toField = .join - , description = - [ "The rooms that the user is a member of." - ] - , coder = Json.fastDict joinedRoomCoder - , default = ( Dict.empty, [] ) - , defaultToString = always "{}" - } - ) - (Json.field.optional.withDefault - { fieldName = "leave" - , toField = .leave - , description = - [ "The rooms that the user has left." - ] - , coder = Json.fastDict leftRoomCoder - , default = ( Dict.empty, [] ) - , defaultToString = always "{}" - } - ) - - -roomsToUpdate : { filter : Filter, nextBatch : String, since : Maybe String } -> Rooms -> VaultUpdate -roomsToUpdate data rooms = - rooms.join - |> Dict.foldl - (\roomId joinedRoom items -> - List.append items - [ V.CreateRoomIfNotExists roomId - , V.MapRoom roomId - (joinedRoomToUpdate - { filter = data.filter - , nextBatch = data.nextBatch - , roomId = roomId - , since = data.since - } - joinedRoom - ) - ] - ) - [] - |> V.More - - -type alias InvitedRoom = - { inviteState : Maybe InviteState } - - -invitedRoomCoder : Json.Coder InvitedRoom -invitedRoomCoder = - Json.object1 - { name = "InvitedRoom" - , description = - [ "Room that the user was invited to." - ] - , init = InvitedRoom - } - (Json.field.optional.value - { fieldName = "invite_state" - , toField = .inviteState - , description = - [ "The state of a room that the user has been invited to. These state events may only have the sender, type, state_key and content keys present. These events do not replace any state that the client already has for the room, for example if the client has archived the room. Instead the client should keep two separate copies of the state: the one from the invite_state and one from the archived state. If the client joins the room then the current state will be given as a delta against the archived state not the invite_state." - ] - , coder = inviteStateCoder - } - ) - - -type alias InviteState = - { events : List Event } - - -inviteStateCoder : Json.Coder InviteState -inviteStateCoder = - Json.object1 - { name = "InviteState" - , description = - [ "Invite (state) events describing the state of an invited room." - ] - , init = Ephemeral - } - (Json.field.required - { fieldName = "events" - , toField = .events - , description = - [ "List of events" - ] - , coder = Json.list eventCoder - } - ) - - -type alias JoinedRoom = - { accountData : Maybe AccountData - , ephemeral : Maybe Ephemeral - , state : Maybe State - , timeline : Maybe Timeline - } - - -joinedRoomCoder : Json.Coder JoinedRoom -joinedRoomCoder = - Json.object4 - { name = "JoinedRoom" - , description = - [ "Matrix room that the user is a member of." - ] - , init = JoinedRoom - } - (Json.field.optional.value - { fieldName = "account_data" - , toField = .accountData - , description = - [ "The private data that this user has attached to this room." - ] - , coder = accountDataCoder - } - ) - (Json.field.optional.value - { fieldName = "ephemeral" - , toField = .ephemeral - , description = - [ "The ephemeral events in the room that aren't recorded in the timeline or state of the room. e.g. typing." - ] - , coder = ephemeralCoder - } - ) - (Json.field.optional.value - { fieldName = "state" - , toField = .state - , description = - [ "Updates to the state, between the time indicated by the since parameter, and the start of the timeline (or all state up to the start of the timeline, if since is not given, or full_state is true)." - ] - , coder = stateCoder - } - ) - (Json.field.optional.value - { fieldName = "timeline" - , toField = .timeline - , description = - [ "The timeline of messages and state changes in the room." - ] - , coder = timelineCoder - } - ) - - -joinedRoomToUpdate : { filter : Filter, nextBatch : String, roomId : String, since : Maybe String } -> JoinedRoom -> RoomUpdate -joinedRoomToUpdate data room = - R.More - [ room.accountData - |> Maybe.map - (\acd -> - acd.events - |> List.map (\event -> R.SetAccountData event.eventType event.content) - |> R.More - ) - |> R.Optional - , room.ephemeral - |> Maybe.map ephemeralToUpdate - |> R.Optional - , room.timeline - |> Maybe.map (timelineToUpdate data) - |> R.Optional - ] - - -type alias Ephemeral = - { events : List Event } - - -ephemeralCoder : Json.Coder Ephemeral -ephemeralCoder = - Json.object1 - { name = "Ephemeral" - , description = - [ "Ephemeral events sent to the user from a room." - ] - , init = Ephemeral - } - (Json.field.required - { fieldName = "events" - , toField = .events - , description = - [ "List of events" - ] - , coder = Json.list eventCoder - } - ) - - -ephemeralToUpdate : Ephemeral -> RoomUpdate -ephemeralToUpdate { events } = - events - |> List.map StrippedEvent.strip - |> R.SetEphemeral - - -type alias LeftRoom = - { state : Maybe State, timeline : Maybe Timeline } - - -leftRoomCoder : Json.Coder LeftRoom -leftRoomCoder = - Json.object2 - { name = "LeftRoom" - , description = - [ "Room that the user is no longer a member of." - ] - , init = LeftRoom - } - (Json.field.optional.value - { fieldName = "state" - , toField = .state - , description = - [ "The state updates for the room up to the start of the timeline." - ] - , coder = stateCoder - } - ) - (Json.field.optional.value - { fieldName = "timeline" - , toField = .timeline - , description = - [ "The timeline of messages and state changes in the room up to the point when the user left." - ] - , coder = timelineCoder - } - ) - - -type alias State = - { events : List Event } - - -stateCoder : Json.Coder State -stateCoder = - Json.object1 - { name = "State" - , description = - [ "List of (state) events describing the room's state." - ] - , init = State - } - (Json.field.required - { fieldName = "events" - , toField = .events - , description = [ "List of events" ] - , coder = Json.list eventCoder - } - ) - - -type alias Timeline = - { events : List Event - , limited : Bool - , prevBatch : Maybe String - } - - -timelineCoder : Json.Coder Timeline -timelineCoder = - Json.object3 - { name = "Timeline" - , description = - [ "Timeline type describing a timeline in a room." - ] - , init = Timeline - } - (Json.field.required - { fieldName = "events" - , toField = .events - , description = - [ "List of events" - ] - , coder = Json.list eventCoder - } - ) - (Json.field.optional.withDefault - { fieldName = "limited" - , toField = .limited - , description = - [ "True if the number of events returned was limited by the limit on the filter" - ] - , coder = Json.bool - , default = ( False, [] ) - , defaultToString = - \b -> - if b then - "true" - - else - "false" - } - ) - (Json.field.optional.value - { fieldName = "prev_batch" - , toField = .prevBatch - , description = - [ "A token that can be supplied to to the from parameter of the rooms/{roomId}/messages endpoint" - , "If the batch was limited then this is a token that can be supplied to the server to retrieve earlier events" - ] - , coder = Json.string - } - ) - - -timelineToUpdate : { filter : Filter, nextBatch : String, roomId : String, since : Maybe String } -> Timeline -> RoomUpdate -timelineToUpdate { filter, nextBatch, roomId, since } t = - R.AddSync - { events = List.map (eventToUpdate roomId) t.events - , filter = filter - , start = - case t.prevBatch of - Just _ -> - t.prevBatch - - Nothing -> - since - , end = nextBatch - } - - -type alias Presence = - { events : List Event } - - -presenceCoder : Json.Coder Presence -presenceCoder = - Json.object1 - { name = "Presence" - , description = - [ "Events indicating users' presence" - ] - , init = Presence - } - (Json.field.required - { fieldName = "events" - , toField = .events - , description = - [ "List of events" - ] - , coder = Json.list eventCoder - } - ) - - -type alias AccountData = - { events : List Event } - - -accountDataCoder : Json.Coder AccountData -accountDataCoder = - Json.object1 - { name = "AccountData" - , description = - [ "Account data events sent by the user in a given room using a different client." - ] - , init = AccountData - } - (Json.field.required - { fieldName = "events" - , toField = .events - , description = - [ "List of events" - ] - , coder = Json.list eventCoder - } - ) - - -type alias Event = - { content : EventContent - , eventId : String - , originServerTs : Timestamp - , sender : User - , stateKey : Maybe String - , eventType : String - , unsigned : Maybe Unsigned - } - - -eventCoder : Json.Coder Event -eventCoder = - Json.object7 - { name = "Event" - , description = - [ "Event describing a JSON object sent in a room." - ] - , init = Event - } - (Json.field.required - { fieldName = "content" - , toField = .content - , description = - [ "The content of this event. The fields in this object will vary depending on the type of event." - ] - , coder = eventContentCoder - } - ) - (Json.field.required - { fieldName = "event_id" - , toField = .eventId - , description = - [ "The ID of this event, if applicable." - ] - , coder = Json.string - } - ) - (Json.field.required - { fieldName = "origin_server_ts" - , toField = .originServerTs - , description = - [ "Timestamp in milliseconds on originating homeserver when this event was sent." - ] - , coder = Timestamp.coder - } - ) - (Json.field.required - { fieldName = "sender" - , toField = .sender - , description = - [ "The MXID of the user who sent this event." - ] - , coder = User.coder - } - ) - (Json.field.optional.value - { fieldName = "state_key" - , toField = .stateKey - , description = - [ "This key will only be present for state events. A unique key which defines the overwriting semantics for this piece of room state." - ] - , coder = Json.string - } - ) - (Json.field.required - { fieldName = "type" - , toField = .eventType - , description = - [ "The type of event." - ] - , coder = Json.string - } - ) - (Json.field.optional.value - { fieldName = "unsigned" - , toField = .unsigned - , description = - [ "Information about this event which was not sent by the originating homeserver" - ] - , coder = unsignedCoder - } - ) - - -eventToUpdate : String -> Event -> Event.Event -eventToUpdate roomId e = - -- TODO: Use Recursion module for call stack safety - { content = e.content - , eventId = e.eventId - , originServerTs = e.originServerTs - , roomId = roomId - , sender = e.sender - , stateKey = e.stateKey - , eventType = e.eventType - , unsigned = - Maybe.map - (\(Unsigned u) -> - Event.UnsignedData - { age = Just u.age - , transactionId = u.transactionId - , redactedBecause = Maybe.map (eventToUpdate roomId) u.redactedBecause - , prevContent = u.prevContent - } - ) - e.unsigned - } - - -type Unsigned - = Unsigned { age : Int, prevContent : Maybe EventContent, redactedBecause : Maybe Event, transactionId : Maybe String } - - -unsignedCoder : Json.Coder Unsigned -unsignedCoder = - Json.object4 - { name = "Unsigned" - , description = - [ "Information about the event that doesn't originate from the original homeserver." - ] - , init = \a b c d -> Unsigned { age = a, prevContent = b, redactedBecause = c, transactionId = d } - } - (Json.field.required - { fieldName = "age" - , toField = \(Unsigned u) -> u.age - , description = - [ "Time in milliseconds since the event was sent." ] - , coder = Json.int - } - ) - (Json.field.optional.value - { fieldName = "prev_content" - , toField = \(Unsigned u) -> u.prevContent - , description = - [ "The previous content for this state. This will be present only for state events appearing in the timeline. If this is not a state event, or there is no previous content, this key will be missing." - ] - , coder = eventContentCoder - } - ) - (Json.field.optional.value - { fieldName = "redacted_because" - , toField = \(Unsigned u) -> u.redactedBecause - , description = - [ "The event that redacted this event, if any." - ] - , coder = Json.lazy (\() -> eventCoder) - } - ) - (Json.field.optional.value - { fieldName = "transaction_id" - , toField = \(Unsigned u) -> u.transactionId - , description = - [ "The transaction ID set when this message was sent. This key will only be present for message events sent by the device calling this API." - ] - , coder = Json.string - } - ) - - -type alias EventContent = - Json.Value - - -eventContentCoder : Json.Coder EventContent -eventContentCoder = - Json.value diff --git a/src/Internal/Api/Sync/V2.elm b/src/Internal/Api/Sync/V2.elm deleted file mode 100644 index 6c55a29..0000000 --- a/src/Internal/Api/Sync/V2.elm +++ /dev/null @@ -1,226 +0,0 @@ -module Internal.Api.Sync.V2 exposing (..) - -{-| - - -# Sync V1 - -Given the complexity of the /sync endpoint, the JSON coders have been placed -in separate modules. Version 2 provides a valid JSON coder for the following -spec versions: - - - r0.4.0 : - --- @docs coder - --} -import Internal.Tools.StrippedEvent as StrippedEvent exposing (StrippedEvent) -import Internal.Api.Sync.V1 as V1 -import Internal.Tools.Json as Json -import Internal.Tools.Timestamp as Timestamp exposing (Timestamp) -import Internal.Values.User as User exposing (User) -import Internal.Values.Event as Event - -type alias RoomEvent = - { content : Json.Value - , eventId : String - , originServerTs : Timestamp - , sender : User - , stateKey : Maybe String - , eventType : String - , unsigned : Maybe Unsigned - } - - -roomEventCoder : Json.Coder RoomEvent -roomEventCoder = - Json.object7 - { name = "Event" - , description = - [ "Event describing a JSON object sent in a room." - ] - , init = RoomEvent - } - (Json.field.required - { fieldName = "content" - , toField = .content - , description = - [ "The content of this event. The fields in this object will vary depending on the type of event." - ] - , coder = Json.value - } - ) - (Json.field.required - { fieldName = "event_id" - , toField = .eventId - , description = - [ "The ID of this event, if applicable." - ] - , coder = Json.string - } - ) - (Json.field.required - { fieldName = "origin_server_ts" - , toField = .originServerTs - , description = - [ "Timestamp in milliseconds on originating homeserver when this event was sent." - ] - , coder = Timestamp.coder - } - ) - (Json.field.required - { fieldName = "sender" - , toField = .sender - , description = - [ "The MXID of the user who sent this event." - ] - , coder = User.coder - } - ) - (Json.field.optional.value - { fieldName = "state_key" - , toField = .stateKey - , description = - [ "This key will only be present for state events. A unique key which defines the overwriting semantics for this piece of room state." - ] - , coder = Json.string - } - ) - (Json.field.required - { fieldName = "type" - , toField = .eventType - , description = - [ "The type of event." - ] - , coder = Json.string - } - ) - (Json.field.optional.value - { fieldName = "unsigned" - , toField = .unsigned - , description = - [ "Information about this event which was not sent by the originating homeserver" - ] - , coder = unsignedCoder - } - ) - - -roomEventToUpdate : String -> RoomEvent -> Event.Event -roomEventToUpdate roomId e = - -- TODO: Use Recursion module for call stack safety - { content = e.content - , eventId = e.eventId - , originServerTs = e.originServerTs - , roomId = roomId - , sender = e.sender - , stateKey = e.stateKey - , eventType = e.eventType - , unsigned = - Maybe.map - (\(Unsigned u) -> - Event.UnsignedData - { age = Just u.age - , transactionId = u.transactionId - , redactedBecause = Maybe.map (roomEventToUpdate roomId) u.redactedBecause - , prevContent = u.prevContent - } - ) - e.unsigned - } - -type Unsigned - = Unsigned { age : Int, prevContent : Maybe Json.Value, redactedBecause : Maybe RoomEvent, transactionId : Maybe String } - - -unsignedCoder : Json.Coder Unsigned -unsignedCoder = - Json.object4 - { name = "Unsigned" - , description = - [ "Information about the event that doesn't originate from the original homeserver." - ] - , init = \a b c d -> Unsigned { age = a, prevContent = b, redactedBecause = c, transactionId = d } - } - (Json.field.required - { fieldName = "age" - , toField = \(Unsigned u) -> u.age - , description = - [ "Time in milliseconds since the event was sent." ] - , coder = Json.int - } - ) - (Json.field.optional.value -- Officially not supported in this spec version, but hey, what gives to try and look for it? - { fieldName = "prev_content" - , toField = \(Unsigned u) -> u.prevContent - , description = - [ "The previous content for this state. This will be present only for state events appearing in the timeline. If this is not a state event, or there is no previous content, this key will be missing." - ] - , coder = Json.value - } - ) - (Json.field.optional.value - { fieldName = "redacted_because" - , toField = \(Unsigned u) -> u.redactedBecause - , description = - [ "The event that redacted this event, if any." - ] - , coder = Json.lazy (\() -> roomEventCoder) - } - ) - (Json.field.optional.value - { fieldName = "transaction_id" - , toField = \(Unsigned u) -> u.transactionId - , description = - [ "The transaction ID set when this message was sent. This key will only be present for message events sent by the device calling this API." - ] - , coder = Json.string - } - ) - -type alias Presence = { events : List Event } - -presenceCoder : Json.Coder Presence -presenceCoder = - Json.object1 - { name = "Presence" - , description = - [ "Events indicating users' presence" - ] - , init = Presence - } - (Json.field.required - { fieldName = "events" - , toField = .events - , description = - [ "List of events" - ] - , coder = Json.list eventCoder - } - ) - -type alias AccountData = { events : List Event } - -accountDataCoder : Json.Coder AccountData -accountDataCoder = - Json.object1 - { name = "AccountData" - , description = - [ "Account data events sent by the user in a given room using a different client." - ] - , init = AccountData - } - (Json.field.required - { fieldName = "events" - , toField = .events - , description = - [ "List of events" - ] - , coder = Json.list eventCoder - } - ) - -type alias Event = StrippedEvent - -eventCoder : Json.Coder Event -eventCoder = StrippedEvent.coder From 1d0a9de7da98bd1614c727be1de4227e11b774ec Mon Sep 17 00:00:00 2001 From: Bram Date: Mon, 8 Jul 2024 18:24:41 +0200 Subject: [PATCH 10/20] Automate defaultToString behaviour --- src/Internal/Filter/Timeline.elm | 2 -- src/Internal/Tools/Iddict.elm | 1 - src/Internal/Tools/Json.elm | 12 ++++++------ src/Internal/Values/Context.elm | 1 - src/Internal/Values/Envelope.elm | 1 - src/Internal/Values/Room.elm | 5 ----- src/Internal/Values/Settings.elm | 10 ---------- src/Internal/Values/Timeline.elm | 5 ----- tests/Test/Tools/Json.elm | 9 --------- 9 files changed, 6 insertions(+), 40 deletions(-) diff --git a/src/Internal/Filter/Timeline.elm b/src/Internal/Filter/Timeline.elm index 71e59d8..9b9d6c5 100644 --- a/src/Internal/Filter/Timeline.elm +++ b/src/Internal/Filter/Timeline.elm @@ -183,7 +183,6 @@ coder = , description = Text.fields.timelineFilter.senders , coder = Json.set Json.string , default = ( Set.empty, [] ) - , defaultToString = always "[]" } ) (Json.field.required @@ -199,7 +198,6 @@ coder = , description = Text.fields.timelineFilter.types , coder = Json.set Json.string , default = ( Set.empty, [] ) - , defaultToString = always "[]" } ) (Json.field.required diff --git a/src/Internal/Tools/Iddict.elm b/src/Internal/Tools/Iddict.elm index da718f2..6c99363 100644 --- a/src/Internal/Tools/Iddict.elm +++ b/src/Internal/Tools/Iddict.elm @@ -80,7 +80,6 @@ coder x = , description = Text.fields.iddict.cursor , coder = Json.int , default = ( 0, [] ) - , defaultToString = String.fromInt } ) (Json.field.required diff --git a/src/Internal/Tools/Json.elm b/src/Internal/Tools/Json.elm index 4962d45..3ab93b3 100644 --- a/src/Internal/Tools/Json.elm +++ b/src/Internal/Tools/Json.elm @@ -362,7 +362,7 @@ then the following field type would be used: , coder = string } -Suppose the JSO isn't obligated to provide a list of hobbies, and the list would +Suppose the JSON isn't obligated to provide a list of hobbies, and the list would by default be overriden with an empty list, then we would use the following field type: @@ -373,8 +373,7 @@ field type: [ "The hobbies of the person. Can be omitted." ] , coder = list string - , default = ( [], [] ) -- The `List Log` can be inserted in case you wish to insert a message when relying on a default - , defaultToString = always "[]" -- Default converted to a string + , default = ( [ "football" ], [] ) -- The `List Log` can be inserted in case you wish to insert a message when relying on a default } -} @@ -382,7 +381,7 @@ field : { required : { fieldName : String, toField : object -> a, description : List String, coder : Coder a } -> Field a object , optional : { value : { fieldName : String, toField : object -> Maybe a, description : List String, coder : Coder a } -> Field (Maybe a) object - , withDefault : { fieldName : String, toField : object -> a, description : List String, coder : Coder a, default : ( a, List Log ), defaultToString : a -> String } -> Field a object + , withDefault : { fieldName : String, toField : object -> a, description : List String, coder : Coder a, default : ( a, List Log ) } -> Field a object } } field = @@ -425,7 +424,7 @@ field = , requiredness = OptionalField } , withDefault = - \{ fieldName, toField, description, coder, default, defaultToString } -> + \{ fieldName, toField, description, coder, default } -> case coder of Coder { encoder, decoder, docs } -> Field @@ -449,7 +448,8 @@ field = , requiredness = default |> Tuple.first - |> defaultToString + |> encoder + |> E.encode 0 |> OptionalFieldWithDefault } } diff --git a/src/Internal/Values/Context.elm b/src/Internal/Values/Context.elm index 0449e7c..ca6a8a1 100644 --- a/src/Internal/Values/Context.elm +++ b/src/Internal/Values/Context.elm @@ -439,6 +439,5 @@ versionsCoder = , description = Text.fields.versions.unstableFeatures , coder = Json.set Json.string , default = ( Set.empty, [] ) - , defaultToString = Json.encode (Json.set Json.string) >> E.encode 0 } ) diff --git a/src/Internal/Values/Envelope.elm b/src/Internal/Values/Envelope.elm index 7823e62..7ae8ceb 100644 --- a/src/Internal/Values/Envelope.elm +++ b/src/Internal/Values/Envelope.elm @@ -124,7 +124,6 @@ coder c1 = , description = Text.fields.envelope.settings , coder = Settings.coder , default = Tuple.pair Settings.init [] - , defaultToString = always "" } ) diff --git a/src/Internal/Values/Room.elm b/src/Internal/Values/Room.elm index 6f5c198..2dd93b9 100644 --- a/src/Internal/Values/Room.elm +++ b/src/Internal/Values/Room.elm @@ -155,7 +155,6 @@ coder = , description = Text.fields.room.accountData , coder = Json.fastDict Json.value , default = ( Dict.empty, [] ) - , defaultToString = Json.encode (Json.fastDict Json.value) >> E.encode 0 } ) (Json.field.optional.withDefault @@ -164,7 +163,6 @@ coder = , description = Text.fields.room.ephemeral , coder = Json.list StrippedEvent.coder , default = ( [], [] ) - , defaultToString = Json.encode (Json.list StrippedEvent.coder) >> E.encode 0 } ) (Json.field.optional.withDefault @@ -173,7 +171,6 @@ coder = , description = Text.fields.room.events , coder = Hashdict.coder .eventId Event.coder , default = ( Hashdict.empty .eventId, [ log.warn "Found a room with no known events! Is it empty?" ] ) - , defaultToString = Json.encode (Hashdict.coder .eventId Event.coder) >> E.encode 0 } ) (Json.field.required @@ -189,7 +186,6 @@ coder = , description = Text.fields.room.state , coder = StateManager.coder , default = ( StateManager.empty, [] ) - , defaultToString = Json.encode StateManager.coder >> E.encode 0 } ) (Json.field.optional.withDefault @@ -198,7 +194,6 @@ coder = , description = Text.fields.room.timeline , coder = Timeline.coder , default = ( Timeline.empty, [] ) - , defaultToString = Json.encode Timeline.coder >> E.encode 0 } ) diff --git a/src/Internal/Values/Settings.elm b/src/Internal/Values/Settings.elm index eed039a..d0f40c4 100644 --- a/src/Internal/Values/Settings.elm +++ b/src/Internal/Values/Settings.elm @@ -55,7 +55,6 @@ coder = , description = Text.fields.settings.currentVersion , coder = Json.string , default = Tuple.pair Default.currentVersion [] - , defaultToString = identity } ) (Json.field.optional.withDefault @@ -64,7 +63,6 @@ coder = , description = Text.fields.settings.deviceName , coder = Json.string , default = Tuple.pair Default.deviceName [] - , defaultToString = identity } ) (Json.field.optional.withDefault @@ -73,13 +71,6 @@ coder = , description = Text.fields.settings.removePasswordOnLogin , coder = Json.bool , default = Tuple.pair Default.removePasswordOnLogin [] - , defaultToString = - \b -> - if b then - "true" - - else - "false" } ) (Json.field.optional.withDefault @@ -88,7 +79,6 @@ coder = , description = Text.fields.settings.syncTime , coder = Json.int , default = Tuple.pair Default.syncTime [] - , defaultToString = String.fromInt } ) diff --git a/src/Internal/Values/Timeline.elm b/src/Internal/Values/Timeline.elm index 9bded76..548b1a1 100644 --- a/src/Internal/Values/Timeline.elm +++ b/src/Internal/Values/Timeline.elm @@ -226,7 +226,6 @@ coder = , description = Text.fields.timeline.filledBatches , coder = Json.int , default = ( 0, [] ) - , defaultToString = String.fromInt } ) (Json.field.required @@ -326,7 +325,6 @@ coderIToken = , description = Text.fields.itoken.starts , coder = Json.set coderIBatchPTRValue , default = ( Set.empty, [] ) - , defaultToString = always "[]" } ) (Json.field.optional.withDefault @@ -335,7 +333,6 @@ coderIToken = , description = Text.fields.itoken.ends , coder = Json.set coderIBatchPTRValue , default = ( Set.empty, [] ) - , defaultToString = always "[]" } ) (Json.field.optional.withDefault @@ -344,7 +341,6 @@ coderIToken = , description = Text.fields.itoken.inFrontOf , coder = Json.set coderITokenPTRValue , default = ( Set.empty, [] ) - , defaultToString = always "[]" } ) (Json.field.optional.withDefault @@ -353,7 +349,6 @@ coderIToken = , description = Text.fields.itoken.behind , coder = Json.set coderITokenPTRValue , default = ( Set.empty, [] ) - , defaultToString = always "[]" } ) diff --git a/tests/Test/Tools/Json.elm b/tests/Test/Tools/Json.elm index 6124615..13d4c3a 100644 --- a/tests/Test/Tools/Json.elm +++ b/tests/Test/Tools/Json.elm @@ -100,7 +100,6 @@ gridField = , description = [] , coder = Json.list (Json.list Json.int) , default = ( [], [] ) - , defaultToString = always "[]" } @@ -132,7 +131,6 @@ hobbiesField = , description = [] , coder = Json.list Json.string , default = ( [], [] ) - , defaultToString = always "[]" } @@ -149,13 +147,6 @@ invitedToPartyField = , description = [] , coder = Json.bool , default = ( False, [] ) - , defaultToString = - \b -> - if b then - "True" - - else - "False" } From 80bb05fd30a7bf49f9b5e5244bf7512db51e7f42 Mon Sep 17 00:00:00 2001 From: Bram Date: Mon, 8 Jul 2024 20:08:32 +0200 Subject: [PATCH 11/20] Add /sync from v1.1 to v1.5 --- src/Internal/Api/Sync/V1.elm | 809 +++++++++++++++++++++++++++++++++++ src/Internal/Api/Sync/V2.elm | 557 ++++++++++++++++++++++++ src/Internal/Api/Sync/V3.elm | 261 +++++++++++ src/Internal/Api/Sync/V4.elm | 436 +++++++++++++++++++ src/Internal/Api/Sync/V5.elm | 249 +++++++++++ 5 files changed, 2312 insertions(+) create mode 100644 src/Internal/Api/Sync/V1.elm create mode 100644 src/Internal/Api/Sync/V2.elm create mode 100644 src/Internal/Api/Sync/V3.elm create mode 100644 src/Internal/Api/Sync/V4.elm create mode 100644 src/Internal/Api/Sync/V5.elm diff --git a/src/Internal/Api/Sync/V1.elm b/src/Internal/Api/Sync/V1.elm new file mode 100644 index 0000000..8260d66 --- /dev/null +++ b/src/Internal/Api/Sync/V1.elm @@ -0,0 +1,809 @@ +module Internal.Api.Sync.V1 exposing (..) + +{-| + + +# Sync response + +This API module represents the /sync endpoint on Matrix spec version v1.1. + + + +-} + +import FastDict exposing (Dict) +import Internal.Tools.Json as Json +import Internal.Tools.StrippedEvent as StrippedEvent exposing (StrippedEvent) +import Internal.Tools.Timestamp as Timestamp exposing (Timestamp) + + +type alias SyncResponse = + { accountData : Maybe AccountData + , deviceLists : Maybe DeviceLists + , deviceOneTimeKeysCount : Maybe (Dict String Int) + , nextBatch : String + , presence : Maybe Presence + , rooms : Maybe Rooms + , toDevice : Maybe ToDevice + } + + +type alias AccountData = + { events : Maybe (List Event) } + + +type alias Event = + { content : Json.Value + , eventType : String + } + + +type alias Presence = + { events : Maybe (List Event) } + + +type alias Rooms = + { invite : Maybe (Dict String InvitedRoom) + , join : Maybe (Dict String JoinedRoom) + , knock : Maybe (Dict String KnockedRoom) + , leave : Maybe (Dict String LeftRoom) + } + + +type alias InvitedRoom = + { inviteState : Maybe InviteState } + + +type alias InviteState = + { events : Maybe (List StrippedState) } + + +type alias StrippedState = + { content : Json.Value + , sender : String + , stateKey : String + , eventType : String + } + + +type alias JoinedRoom = + { accountData : Maybe AccountData + , ephemeral : Maybe Ephemeral + , state : Maybe State + , summary : Maybe RoomSummary + , timeline : Maybe Timeline + , unreadNotifications : Maybe UnreadNotificationCounts + } + + +type alias Ephemeral = + { events : Maybe (List Event) } + + +type alias State = + { events : Maybe (List SyncStateEvent) } + + +type alias SyncStateEvent = + { content : Json.Value + , eventId : String + , originServerTs : Timestamp + , prevContent : Maybe Json.Value + , sender : String + , stateKey : String + , eventType : String + , unsigned : Maybe UnsignedData + } + + +type alias UnsignedData = + { age : Maybe Int + , redactedBecause : Maybe Event + , transactionId : Maybe String + } + + +type alias RoomSummary = + { mHeroes : Maybe (List String) + , mInvitedMemberCount : Maybe Int + , mJoinedMemberCount : Maybe Int + } + + +type alias Timeline = + { events : Maybe (List SyncRoomEvent) + , limited : Maybe Bool + , prevBatch : Maybe String + } + + +type alias SyncRoomEvent = + { content : Json.Value + , eventId : String + , originServerTs : Timestamp + , sender : String + , eventType : String + , unsigned : Maybe UnsignedData + } + + +type alias UnreadNotificationCounts = + { highlightCount : Maybe Int + , notificationCount : Maybe Int + } + + +type alias KnockedRoom = + { knockState : Maybe KnockState } + + +type alias KnockState = + { events : Maybe (List StrippedState) } + + +type alias LeftRoom = + { accountData : Maybe AccountData + , state : Maybe State + , timeline : Maybe Timeline + } + + +type alias DeviceLists = + { changed : Maybe (List String) + , left : Maybe (List String) + } + + +type alias ToDevice = + { events : Maybe (List ToDeviceEvent) } + + +type alias ToDeviceEvent = + { content : Maybe Json.Value + , sender : Maybe String + , eventType : Maybe String + } + + +coderSyncResponse : Json.Coder SyncResponse +coderSyncResponse = + Json.object7 + { name = "SyncResponse" + , description = [ "The event that is returned on a 200 response." ] + , init = SyncResponse + } + (Json.field.optional.value + { fieldName = "account_data" + , toField = .accountData + , description = [ "The global private data created by this user." ] + , coder = coderAccountData + } + ) + (Json.field.optional.value + { fieldName = "device_lists" + , toField = .deviceLists + , description = [ "Information on end-to-end device updates, as specified in End-to-end encryption." ] + , coder = coderDeviceLists + } + ) + (Json.field.optional.value + { fieldName = "device_one_time_keys_count" + , toField = .deviceOneTimeKeysCount + , description = [ "Information on end-to-end encryption keys, as specified in End-to-end encryption." ] + , coder = Json.fastDict Json.int + } + ) + (Json.field.required + { fieldName = "next_batch" + , toField = .nextBatch + , description = [ "Required: The batch token to supply in the since param of the next /sync request." ] + , coder = Json.string + } + ) + (Json.field.optional.value + { fieldName = "presence" + , toField = .presence + , description = [ "The updates to the presence status of other users." ] + , coder = coderPresence + } + ) + (Json.field.optional.value + { fieldName = "rooms" + , toField = .rooms + , description = [ "Updates to rooms." ] + , coder = coderRooms + } + ) + (Json.field.optional.value + { fieldName = "to_device" + , toField = .toDevice + , description = [ "Information on the send-to-device messages for the client device, as defined in Send-to-Device messaging." ] + , coder = coderToDevice + } + ) + + +coderAccountData : Json.Coder AccountData +coderAccountData = + Json.object1 + { name = "AccountData" + , description = [ "The global private data created by this user." ] + , init = AccountData + } + (Json.field.optional.value + { fieldName = "events" + , toField = .events + , description = [ "List of events." ] + , coder = Json.list coderEvent + } + ) + + +coderEvent : Json.Coder Event +coderEvent = + StrippedEvent.coder + + +coderPresence : Json.Coder Presence +coderPresence = + Json.object1 + { name = "Presence" + , description = [ "The updates to the presence status of other users." ] + , init = Presence + } + (Json.field.optional.value + { fieldName = "events" + , toField = .events + , description = [ "List of events." ] + , coder = Json.list coderEvent + } + ) + + +coderRooms : Json.Coder Rooms +coderRooms = + Json.object4 + { name = "Rooms" + , description = [ "Updates to rooms." ] + , init = Rooms + } + (Json.field.optional.value + { fieldName = "invite" + , toField = .invite + , description = [ "The rooms that the user has been invited to, mapped as room ID to room information." ] + , coder = Json.fastDict coderInvitedRoom + } + ) + (Json.field.optional.value + { fieldName = "join" + , toField = .join + , description = [ "The rooms that the user has joined, mapped as room ID to room information." ] + , coder = Json.fastDict coderJoinedRoom + } + ) + (Json.field.optional.value + { fieldName = "knock" + , toField = .knock + , description = [ "The rooms that the user has knocked upon, mapped as room ID to room information." ] + , coder = Json.fastDict coderKnockedRoom + } + ) + (Json.field.optional.value + { fieldName = "leave" + , toField = .leave + , description = [ "The rooms that the user has left or been banned from, mapped as room ID to room information." ] + , coder = Json.fastDict coderLeftRoom + } + ) + + +coderInvitedRoom : Json.Coder InvitedRoom +coderInvitedRoom = + Json.object1 + { name = "InvitedRoom" + , description = [ "The rooms that the user has been invited to, mapped as room ID to room information." ] + , init = InvitedRoom + } + (Json.field.optional.value + { fieldName = "invite_state" + , toField = .inviteState + , description = [ "The state of a room that the user has been invited to.", "These state events may only have the sender, type, state_key and content keys present.", "These events do not replace any state that the client already has for the room, for example if the client has archived the room.", "Instead the client should keep two separate copies of the state: the one from the invite_state and one from the archived state.", "If the client joins the room then the current state will be given as a delta against the archived state not the invite_state." ] + , coder = coderInviteState + } + ) + + +coderInviteState : Json.Coder InviteState +coderInviteState = + Json.object1 + { name = "InviteState" + , description = [ "The state of a room that the user has been invited to." ] + , init = InviteState + } + (Json.field.optional.value + { fieldName = "events" + , toField = .events + , description = [ "The StrippedState events that form the invite state." ] + , coder = Json.list coderStrippedState + } + ) + + +coderStrippedState : Json.Coder StrippedState +coderStrippedState = + Json.object4 + { name = "StrippedState" + , description = [ "The StrippedState events that form the invite state." ] + , init = StrippedState + } + (Json.field.required + { fieldName = "content" + , toField = .content + , description = [ "The content for the event." ] + , coder = Json.value + } + ) + (Json.field.required + { fieldName = "sender" + , toField = .sender + , description = [ "The sender for the event." ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "state_key" + , toField = .stateKey + , description = [ "The state_key for the event." ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "type" + , toField = .eventType + , description = [ "The type for the event." ] + , coder = Json.string + } + ) + + +coderJoinedRoom : Json.Coder JoinedRoom +coderJoinedRoom = + Json.object6 + { name = "JoinedRoom" + , description = [ "The rooms that the user has joined, mapped as room ID to room information." ] + , init = JoinedRoom + } + (Json.field.optional.value + { fieldName = "account_data" + , toField = .accountData + , description = [ "The private data that this user has attached to this room." ] + , coder = coderAccountData + } + ) + (Json.field.optional.value + { fieldName = "ephemeral" + , toField = .ephemeral + , description = [ "The ephemeral events in the room that aren’t recorded in the timeline or state of the room. e.g. typing." ] + , coder = coderEphemeral + } + ) + (Json.field.optional.value + { fieldName = "state" + , toField = .state + , description = [ "Updates to the state, between the time indicated by the since parameter, and the start of the timeline (or all state up to the start of the timeline, if since is not given, or full_state is true).", "N.B. state updates for m.room.member events will be incomplete if lazy_load_members is enabled in the /sync filter, and only return the member events required to display the senders of the timeline events in this response." ] + , coder = coderState + } + ) + (Json.field.optional.value + { fieldName = "summary" + , toField = .summary + , description = [ "Information about the room which clients may need to correctly render it to users." ] + , coder = coderRoomSummary + } + ) + (Json.field.optional.value + { fieldName = "timeline" + , toField = .timeline + , description = [ "The timeline of messages and state changes in the room." ] + , coder = coderTimeline + } + ) + (Json.field.optional.value + { fieldName = "unread_notifications" + , toField = .unreadNotifications + , description = [ "Counts of unread notifications for this room. See the Receiving notifications section for more information on how these are calculated." ] + , coder = coderUnreadNotificationCounts + } + ) + + +coderEphemeral : Json.Coder Ephemeral +coderEphemeral = + Json.object1 + { name = "Ephemeral" + , description = [ "The ephemeral events in the room that aren’t recorded in the timeline or state of the room. e.g. typing." ] + , init = Ephemeral + } + (Json.field.optional.value + { fieldName = "events" + , toField = .events + , description = [ "List of events." ] + , coder = Json.list coderEvent + } + ) + + +coderState : Json.Coder State +coderState = + Json.object1 + { name = "State" + , description = [ "Updates to the state, between the time indicated by the since parameter, and the start of the timeline (or all state up to the start of the timeline, if since is not given, or full_state is true)." ] + , init = State + } + (Json.field.optional.value + { fieldName = "events" + , toField = .events + , description = [ "List of events." ] + , coder = Json.list coderSyncStateEvent + } + ) + + +coderSyncStateEvent : Json.Coder SyncStateEvent +coderSyncStateEvent = + Json.object8 + { name = "SyncStateEvent" + , description = [ "Represents a state event within a sync response." ] + , init = SyncStateEvent + } + (Json.field.required + { fieldName = "content" + , toField = .content + , description = [ "The fields in this object will vary depending on the type of event. When interacting with the REST API, this is the HTTP body." ] + , coder = Json.value + } + ) + (Json.field.required + { fieldName = "event_id" + , toField = .eventId + , description = [ "The globally unique event identifier." ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "origin_server_ts" + , toField = .originServerTs + , description = [ "Timestamp in milliseconds on originating homeserver when this event was sent." ] + , coder = Timestamp.coder + } + ) + (Json.field.optional.value + { fieldName = "prev_content" + , toField = .prevContent + , description = [ "Optional. The previous content for this event. If there is no previous content, this key will be missing." ] + , coder = Json.value + } + ) + (Json.field.required + { fieldName = "sender" + , toField = .sender + , description = [ "Contains the fully-qualified ID of the user who sent this event." ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "state_key" + , toField = .stateKey + , description = [ "A unique key which defines the overwriting semantics for this piece of room state.", "This value is often a zero-length string. The presence of this key makes this event a State Event.", "State keys starting with an @ are reserved for referencing user IDs, such as room members.", "With the exception of a few events, state events set with a given user’s ID as the state key MUST only be set by that user." ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "type" + , toField = .eventType + , description = [ "The type of event. This SHOULD be namespaced similar to Java package naming conventions e.g. ‘com.example.subdomain.event.type’" ] + , coder = Json.string + } + ) + (Json.field.optional.value + { fieldName = "unsigned" + , toField = .unsigned + , description = [ "Contains optional extra information about the event." ] + , coder = coderUnsignedData + } + ) + + +coderUnsignedData : Json.Coder UnsignedData +coderUnsignedData = + Json.object3 + { name = "UnsignedData" + , description = [ "Contains optional extra information about the event." ] + , init = UnsignedData + } + (Json.field.optional.value + { fieldName = "age" + , toField = .age + , description = [ "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." ] + , coder = Json.int + } + ) + (Json.field.optional.value + { fieldName = "redacted_because" + , toField = .redactedBecause + , description = [ "The event that redacted this event, if any." ] + , coder = coderEvent + } + ) + (Json.field.optional.value + { fieldName = "transaction_id" + , toField = .transactionId + , description = [ "The client-supplied transaction ID, for example, provided via PUT /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId}, if the client being given the event is the same one which sent it." ] + , coder = Json.string + } + ) + + +coderRoomSummary : Json.Coder RoomSummary +coderRoomSummary = + Json.object3 + { name = "RoomSummary" + , description = [ "Information about the room which clients may need to correctly render it to users." ] + , init = RoomSummary + } + (Json.field.optional.value + { fieldName = "m.heroes" + , toField = .mHeroes + , description = [ "The users which can be used to generate a room name if the room does not have one. Required if the room’s m.room.name or m.room.canonical_alias state events are unset or empty.", "This should be the first 5 members of the room, ordered by stream ordering, which are joined or invited.", "The list must never include the client’s own user ID.", "When no joined or invited members are available, this should consist of the banned and left users.", "More than 5 members may be provided, however less than 5 should only be provided when there are less than 5 members to represent.", "When lazy-loading room members is enabled, the membership events for the heroes MUST be included in the state, unless they are redundant.", "When the list of users changes, the server notifies the client by sending a fresh list of heroes.", "If there are no changes since the last sync, this field may be omitted." ] + , coder = Json.list Json.string + } + ) + (Json.field.optional.value + { fieldName = "m.invited_member_count" + , toField = .mInvitedMemberCount + , description = [ "The number of users with membership of invite.", "If this field has not changed since the last sync, it may be omitted. Required otherwise." ] + , coder = Json.int + } + ) + (Json.field.optional.value + { fieldName = "m.joined_member_count" + , toField = .mJoinedMemberCount + , description = [ "The number of users with membership of join, including the client’s own user ID.", "If this field has not changed since the last sync, it may be omitted. Required otherwise." ] + , coder = Json.int + } + ) + + +coderTimeline : Json.Coder Timeline +coderTimeline = + Json.object3 + { name = "Timeline" + , description = [ "The timeline of messages and state changes in the room." ] + , init = Timeline + } + (Json.field.optional.value + { fieldName = "events" + , toField = .events + , description = [ "List of events." ] + , coder = Json.list coderSyncRoomEvent + } + ) + (Json.field.optional.value + { fieldName = "limited" + , toField = .limited + , description = [ "True if the number of events returned was limited by the limit on the filter." ] + , coder = Json.bool + } + ) + (Json.field.optional.value + { fieldName = "prev_batch" + , toField = .prevBatch + , description = [ "A token that can be supplied to the from parameter of the /rooms//messages endpoint in order to retrieve earlier events.", "If no earlier events are available, this property may be omitted from the response." ] + , coder = Json.string + } + ) + + +coderSyncRoomEvent : Json.Coder SyncRoomEvent +coderSyncRoomEvent = + Json.object6 + { name = "SyncRoomEvent" + , description = [ "Represents a room event within a sync response." ] + , init = SyncRoomEvent + } + (Json.field.required + { fieldName = "content" + , toField = .content + , description = [ "The fields in this object will vary depending on the type of event. When interacting with the REST API, this is the HTTP body." ] + , coder = Json.value + } + ) + (Json.field.required + { fieldName = "event_id" + , toField = .eventId + , description = [ "The globally unique event identifier." ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "origin_server_ts" + , toField = .originServerTs + , description = [ "Timestamp in milliseconds on originating homeserver when this event was sent." ] + , coder = Timestamp.coder + } + ) + (Json.field.required + { fieldName = "sender" + , toField = .sender + , description = [ "Contains the fully-qualified ID of the user who sent this event." ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "type" + , toField = .eventType + , description = [ "The type of event. This SHOULD be namespaced similar to Java package naming conventions e.g. ‘com.example.subdomain.event.type’" ] + , coder = Json.string + } + ) + (Json.field.optional.value + { fieldName = "unsigned" + , toField = .unsigned + , description = [ "Contains optional extra information about the event." ] + , coder = coderUnsignedData + } + ) + + +coderUnreadNotificationCounts : Json.Coder UnreadNotificationCounts +coderUnreadNotificationCounts = + Json.object2 + { name = "UnreadNotificationCounts" + , description = [ "Counts of unread notifications for this room." ] + , init = UnreadNotificationCounts + } + (Json.field.optional.value + { fieldName = "highlight_count" + , toField = .highlightCount + , description = [ "The number of unread notifications for this room with the highlight flag set." ] + , coder = Json.int + } + ) + (Json.field.optional.value + { fieldName = "notification_count" + , toField = .notificationCount + , description = [ "The total number of unread notifications for this room." ] + , coder = Json.int + } + ) + + +coderKnockedRoom : Json.Coder KnockedRoom +coderKnockedRoom = + Json.object1 + { name = "KnockedRoom" + , description = [ "The rooms that the user has knocked upon, mapped as room ID to room information." ] + , init = KnockedRoom + } + (Json.field.optional.value + { fieldName = "knock_state" + , toField = .knockState + , description = [ "The state of a room that the user has knocked upon.", "The state events contained here have the same restrictions as InviteState above." ] + , coder = coderKnockState + } + ) + + +coderKnockState : Json.Coder KnockState +coderKnockState = + Json.object1 + { name = "KnockState" + , description = [ "The state of a room that the user has knocked upon." ] + , init = KnockState + } + (Json.field.optional.value + { fieldName = "events" + , toField = .events + , description = [ "The StrippedState events that form the knock state." ] + , coder = Json.list coderStrippedState + } + ) + + +coderLeftRoom : Json.Coder LeftRoom +coderLeftRoom = + Json.object3 + { name = "LeftRoom" + , description = [ "The rooms that the user has left or been banned from, mapped as room ID to room information." ] + , init = LeftRoom + } + (Json.field.optional.value + { fieldName = "account_data" + , toField = .accountData + , description = [ "The private data that this user has attached to this room." ] + , coder = coderAccountData + } + ) + (Json.field.optional.value + { fieldName = "state" + , toField = .state + , description = [ "The state updates for the room up to the start of the timeline." ] + , coder = coderState + } + ) + (Json.field.optional.value + { fieldName = "timeline" + , toField = .timeline + , description = [ "The timeline of messages and state changes in the room up to the point when the user left." ] + , coder = coderTimeline + } + ) + + +coderDeviceLists : Json.Coder DeviceLists +coderDeviceLists = + Json.object2 + { name = "DeviceLists" + , description = [ "Information on end-to-end device updates, as specified in End-to-end encryption." ] + , init = DeviceLists + } + (Json.field.optional.value + { fieldName = "changed" + , toField = .changed + , description = [ "List of users who have updated their device identity or cross-signing keys, or who now share an encrypted room with the client since the previous sync response." ] + , coder = Json.list Json.string + } + ) + (Json.field.optional.value + { fieldName = "left" + , toField = .left + , description = [ "List of users with whom we do not share any encrypted rooms anymore since the previous sync response." ] + , coder = Json.list Json.string + } + ) + + +coderToDevice : Json.Coder ToDevice +coderToDevice = + Json.object1 + { name = "ToDevice" + , description = [ "Information on the send-to-device messages for the client device, as defined in Send-to-Device messaging." ] + , init = ToDevice + } + (Json.field.optional.value + { fieldName = "events" + , toField = .events + , description = [ "List of send-to-device messages." ] + , coder = Json.list coderToDeviceEvent + } + ) + + +coderToDeviceEvent : Json.Coder ToDeviceEvent +coderToDeviceEvent = + Json.object3 + { name = "ToDeviceEvent" + , description = [ "An event." ] + , init = ToDeviceEvent + } + (Json.field.optional.value + { fieldName = "content" + , toField = .content + , description = [ "The content of this event. The fields in this object will vary depending on the type of event." ] + , coder = Json.value + } + ) + (Json.field.optional.value + { fieldName = "sender" + , toField = .sender + , description = [ "The Matrix user ID of the user who sent this event." ] + , coder = Json.string + } + ) + (Json.field.optional.value + { fieldName = "type" + , toField = .eventType + , description = [ "The type of event." ] + , coder = Json.string + } + ) diff --git a/src/Internal/Api/Sync/V2.elm b/src/Internal/Api/Sync/V2.elm new file mode 100644 index 0000000..ee34599 --- /dev/null +++ b/src/Internal/Api/Sync/V2.elm @@ -0,0 +1,557 @@ +module Internal.Api.Sync.V2 exposing (..) + +{-| + + +# Sync response + +This API module represents the /sync endpoint on Matrix spec version v1.1. + + + +-} + +import FastDict exposing (Dict) +import Internal.Api.Sync.V1 as PV +import Internal.Tools.Json as Json + + +type alias SyncResponse = + { accountData : Maybe AccountData + , deviceLists : Maybe DeviceLists + , deviceOneTimeKeysCount : Maybe (Dict String Int) + , deviceUnusedFallbackKeyTypes : List String + , nextBatch : String + , presence : Maybe Presence + , rooms : Maybe Rooms + , toDevice : Maybe ToDevice + } + + +type alias AccountData = + { events : Maybe (List Event) } + + +type alias Event = + { content : Json.Value + , eventType : String + } + + +type alias Presence = + { events : Maybe (List Event) } + + +type alias Rooms = + { invite : Maybe (Dict String InvitedRoom) + , join : Maybe (Dict String JoinedRoom) + , knock : Maybe (Dict String KnockedRoom) + , leave : Maybe (Dict String LeftRoom) + } + + +type alias InvitedRoom = + { inviteState : Maybe InviteState } + + +type alias InviteState = + { events : Maybe (List StrippedStateEvent) } + + +type alias StrippedStateEvent = + { content : Json.Value + , sender : String + , stateKey : String + , eventType : String + } + + +type alias JoinedRoom = + { accountData : Maybe AccountData + , ephemeral : Maybe Ephemeral + , state : Maybe State + , summary : Maybe RoomSummary + , timeline : Maybe Timeline + , unreadNotifications : Maybe UnreadNotificationCounts + } + + +type alias Ephemeral = + { events : Maybe (List Event) } + + +type alias State = + { events : Maybe (List ClientEventWithoutRoomID) } + + +type alias ClientEventWithoutRoomID = + { content : Json.Value + , eventId : String + , originServerTs : Int + , sender : String + , stateKey : Maybe String + , eventType : String + , unsigned : Maybe UnsignedData + } + + +type UnsignedData + = UnsignedData + { age : Maybe Int + , prevContent : Maybe Json.Value + , redactedBecause : Maybe ClientEventWithoutRoomID + , transactionId : Maybe String + } + + +type alias RoomSummary = + { mHeroes : Maybe (List String) + , mInvitedMemberCount : Maybe Int + , mJoinedMemberCount : Maybe Int + } + + +type alias Timeline = + { events : List ClientEventWithoutRoomID + , limited : Maybe Bool + , prevBatch : Maybe String + } + + +type alias UnreadNotificationCounts = + { highlightCount : Maybe Int + , notificationCount : Maybe Int + } + + +type alias KnockedRoom = + { knockState : Maybe KnockState } + + +type alias KnockState = + { events : Maybe (List StrippedStateEvent) } + + +type alias LeftRoom = + { accountData : Maybe AccountData + , state : Maybe State + , timeline : Maybe Timeline + } + + +type alias DeviceLists = + { changed : Maybe (List String) + , left : Maybe (List String) + } + + +type alias ToDevice = + { events : Maybe (List ToDeviceEvent) } + + +type alias ToDeviceEvent = + { content : Maybe Json.Value + , sender : Maybe String + , eventType : Maybe String + } + + +coderSyncResponse : Json.Coder SyncResponse +coderSyncResponse = + Json.object8 + { name = "SyncResponse" + , description = [ "An event that is part of a response." ] + , init = SyncResponse + } + (Json.field.optional.value + { fieldName = "account_data" + , toField = .accountData + , description = [ "The global private data created by this user." ] + , coder = coderAccountData + } + ) + (Json.field.optional.value + { fieldName = "device_lists" + , toField = .deviceLists + , description = [ "Information on end-to-end device updates, as specified in End-to-end encryption." ] + , coder = coderDeviceLists + } + ) + (Json.field.optional.value + { fieldName = "device_one_time_keys_count" + , toField = .deviceOneTimeKeysCount + , description = [ "Information on end-to-end encryption keys, as specified in End-to-end encryption." ] + , coder = Json.fastDict Json.int + } + ) + (Json.field.required + { fieldName = "device_unused_fallback_key_types" + , toField = .deviceUnusedFallbackKeyTypes + , description = [ "The unused fallback key algorithms." ] + , coder = Json.list Json.string + } + ) + (Json.field.required + { fieldName = "next_batch" + , toField = .nextBatch + , description = [ "Required: The batch token to supply in the since param of the next /sync request." ] + , coder = Json.string + } + ) + (Json.field.optional.value + { fieldName = "presence" + , toField = .presence + , description = [ "The updates to the presence status of other users." ] + , coder = coderPresence + } + ) + (Json.field.optional.value + { fieldName = "rooms" + , toField = .rooms + , description = [ "Updates to rooms." ] + , coder = coderRooms + } + ) + (Json.field.optional.value + { fieldName = "to_device" + , toField = .toDevice + , description = [ "Information on the send-to-device messages for the client device, as defined in Send-to-Device messaging." ] + , coder = coderToDevice + } + ) + + +coderAccountData : Json.Coder AccountData +coderAccountData = + PV.coderAccountData + + +coderEvent : Json.Coder Event +coderEvent = + PV.coderEvent + + +coderPresence : Json.Coder Presence +coderPresence = + PV.coderPresence + + +coderRooms : Json.Coder Rooms +coderRooms = + Json.object4 + { name = "Rooms" + , description = [ "Updates to rooms." ] + , init = Rooms + } + (Json.field.optional.value + { fieldName = "invite" + , toField = .invite + , description = [ "The rooms that the user has been invited to, mapped as room ID to room information." ] + , coder = Json.fastDict coderInvitedRoom + } + ) + (Json.field.optional.value + { fieldName = "join" + , toField = .join + , description = [ "The rooms that the user has joined, mapped as room ID to room information." ] + , coder = Json.fastDict coderJoinedRoom + } + ) + (Json.field.optional.value + { fieldName = "knock" + , toField = .knock + , description = [ "The rooms that the user has knocked upon, mapped as room ID to room information." ] + , coder = Json.fastDict coderKnockedRoom + } + ) + (Json.field.optional.value + { fieldName = "leave" + , toField = .leave + , description = [ "The rooms that the user has left or been banned from, mapped as room ID to room information." ] + , coder = Json.fastDict coderLeftRoom + } + ) + + +coderInvitedRoom : Json.Coder InvitedRoom +coderInvitedRoom = + PV.coderInvitedRoom + + +coderInviteState : Json.Coder InviteState +coderInviteState = + PV.coderInviteState + + +coderStrippedStateEvent : Json.Coder StrippedStateEvent +coderStrippedStateEvent = + PV.coderStrippedState + + +coderJoinedRoom : Json.Coder JoinedRoom +coderJoinedRoom = + Json.object6 + { name = "JoinedRoom" + , description = [ "The rooms that the user has joined." ] + , init = JoinedRoom + } + (Json.field.optional.value + { fieldName = "account_data" + , toField = .accountData + , description = [ "The private data that this user has attached to this room." ] + , coder = coderAccountData + } + ) + (Json.field.optional.value + { fieldName = "ephemeral" + , toField = .ephemeral + , description = [ "The ephemeral events in the room that aren’t recorded in the timeline or state of the room. e.g. typing." ] + , coder = coderEphemeral + } + ) + (Json.field.optional.value + { fieldName = "state" + , toField = .state + , description = [ "Updates to the state, between the time indicated by the since parameter, and the start of the timeline (or all state up to the start of the timeline, if since is not given, or full_state is true).", "N.B. state updates for m.room.member events will be incomplete if lazy_load_members is enabled in the /sync filter, and only return the member events required to display the senders of the timeline events in this response." ] + , coder = coderState + } + ) + (Json.field.optional.value + { fieldName = "summary" + , toField = .summary + , description = [ "Information about the room which clients may need to correctly render it to users." ] + , coder = coderRoomSummary + } + ) + (Json.field.optional.value + { fieldName = "timeline" + , toField = .timeline + , description = [ "The timeline of messages and state changes in the room." ] + , coder = coderTimeline + } + ) + (Json.field.optional.value + { fieldName = "unread_notifications" + , toField = .unreadNotifications + , description = [ "Counts of unread notifications for this room. See the Receiving notifications section for more information on how these are calculated." ] + , coder = coderUnreadNotificationCounts + } + ) + + +coderEphemeral : Json.Coder Ephemeral +coderEphemeral = + PV.coderEphemeral + + +coderState : Json.Coder State +coderState = + Json.object1 + { name = "State" + , description = [ "Updates to the state." ] + , init = State + } + (Json.field.optional.value + { fieldName = "events" + , toField = .events + , description = [ "List of events." ] + , coder = Json.list coderClientEventWithoutRoomID + } + ) + + +coderClientEventWithoutRoomID : Json.Coder ClientEventWithoutRoomID +coderClientEventWithoutRoomID = + Json.object7 + { name = "ClientEventWithoutRoomID" + , description = [ "An event without a room ID." ] + , init = ClientEventWithoutRoomID + } + (Json.field.required + { fieldName = "content" + , toField = .content + , description = [ "Required: The body of this event, as created by the client which sent it." ] + , coder = Json.value + } + ) + (Json.field.required + { fieldName = "event_id" + , toField = .eventId + , description = [ "Required: The globally unique identifier for this event." ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "origin_server_ts" + , toField = .originServerTs + , description = [ "Required: Timestamp (in milliseconds since the unix epoch) on originating homeserver when this event was sent." ] + , coder = Json.int + } + ) + (Json.field.required + { fieldName = "sender" + , toField = .sender + , description = [ "Required: Contains the fully-qualified ID of the user who sent this event." ] + , coder = Json.string + } + ) + (Json.field.optional.value + { fieldName = "state_key" + , toField = .stateKey + , description = [ "Present if, and only if, this event is a state event. The key making this piece of state unique in the room. Note that it is often an empty string." ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "type" + , toField = .eventType + , description = [ "Required: The type of the event." ] + , coder = Json.string + } + ) + (Json.field.optional.value + { fieldName = "unsigned" + , toField = .unsigned + , description = [ "Contains optional extra information about the event." ] + , coder = coderUnsignedData + } + ) + + +coderUnsignedData : Json.Coder UnsignedData +coderUnsignedData = + Json.object4 + { name = "UnsignedData" + , description = [ "Contains optional extra information about the event." ] + , init = + \a b c d -> + UnsignedData + { age = a + , prevContent = b + , redactedBecause = c + , transactionId = d + } + } + (Json.field.optional.value + { fieldName = "age" + , toField = \(UnsignedData u) -> u.age + , description = [ "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." ] + , coder = Json.int + } + ) + (Json.field.optional.value + { fieldName = "prev_content" + , toField = \(UnsignedData u) -> u.prevContent + , description = [ "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.", "Changed in v1.2: Previously, this field was specified at the top level of returned events rather than in unsigned (with the exception of the GET .../notifications endpoint), though in practice no known server implementations honoured this." ] + , coder = Json.value + } + ) + (Json.field.optional.value + { fieldName = "redacted_because" + , toField = \(UnsignedData u) -> u.redactedBecause + , description = [ "The event that redacted this event, if any." ] + , coder = Json.lazy (\_ -> coderClientEventWithoutRoomID) + } + ) + (Json.field.optional.value + { fieldName = "transaction_id" + , toField = \(UnsignedData u) -> u.transactionId + , description = [ "The client-supplied transaction ID, for example, provided via PUT /_matrix/client/v3/rooms/{roomId}/send/{eventType}/{txnId}, if the client being given the event is the same one which sent it." ] + , coder = Json.string + } + ) + + +coderRoomSummary : Json.Coder RoomSummary +coderRoomSummary = + PV.coderRoomSummary + + +coderTimeline : Json.Coder Timeline +coderTimeline = + Json.object3 + { name = "Timeline" + , description = [ "The timeline of messages and state changes in the room." ] + , init = Timeline + } + (Json.field.required + { fieldName = "events" + , toField = .events + , description = [ "Required: List of events." ] + , coder = Json.list coderClientEventWithoutRoomID + } + ) + (Json.field.optional.value + { fieldName = "limited" + , toField = .limited + , description = [ "True if the number of events returned was limited by the limit on the filter." ] + , coder = Json.bool + } + ) + (Json.field.optional.value + { fieldName = "prev_batch" + , toField = .prevBatch + , description = [ "A token that can be supplied to the from parameter of the /rooms//messages endpoint in order to retrieve earlier events. If no earlier events are available, this property may be omitted from the response." ] + , coder = Json.string + } + ) + + +coderUnreadNotificationCounts : Json.Coder UnreadNotificationCounts +coderUnreadNotificationCounts = + PV.coderUnreadNotificationCounts + + +coderKnockedRoom : Json.Coder KnockedRoom +coderKnockedRoom = + PV.coderKnockedRoom + + +coderKnockState : Json.Coder KnockState +coderKnockState = + PV.coderKnockState + + +coderLeftRoom : Json.Coder LeftRoom +coderLeftRoom = + Json.object3 + { name = "LeftRoom" + , description = [ "The rooms that the user has left or been banned from." ] + , init = LeftRoom + } + (Json.field.optional.value + { fieldName = "account_data" + , toField = .accountData + , description = [ "The private data that this user has attached to this room." ] + , coder = coderAccountData + } + ) + (Json.field.optional.value + { fieldName = "state" + , toField = .state + , description = [ "The state updates for the room up to the start of the timeline." ] + , coder = coderState + } + ) + (Json.field.optional.value + { fieldName = "timeline" + , toField = .timeline + , description = [ "The timeline of messages and state changes in the room up to the point when the user left." ] + , coder = coderTimeline + } + ) + + +coderDeviceLists : Json.Coder DeviceLists +coderDeviceLists = + PV.coderDeviceLists + + +coderToDevice : Json.Coder ToDevice +coderToDevice = + PV.coderToDevice + + +coderToDeviceEvent : Json.Coder ToDeviceEvent +coderToDeviceEvent = + PV.coderToDeviceEvent diff --git a/src/Internal/Api/Sync/V3.elm b/src/Internal/Api/Sync/V3.elm new file mode 100644 index 0000000..2f7bafb --- /dev/null +++ b/src/Internal/Api/Sync/V3.elm @@ -0,0 +1,261 @@ +module Internal.Api.Sync.V3 exposing (..) + +{-| + + +# Sync response + +This API module represents the /sync endpoint on Matrix spec version v1.1. + + + +-} + +import FastDict exposing (Dict) +import Internal.Api.Sync.V2 as PV +import Internal.Tools.Json as Json + + +type alias SyncResponse = + { accountData : Maybe AccountData + , deviceLists : Maybe DeviceLists + , deviceOneTimeKeysCount : Maybe (Dict String Int) + , deviceUnusedFallbackKeyTypes : List String + , nextBatch : String + , presence : Maybe Presence + , rooms : Maybe Rooms + , toDevice : Maybe ToDevice + } + + +type alias AccountData = + { events : Maybe (List Event) } + + +type alias Event = + { content : Json.Value + , eventType : String + } + + +type alias Presence = + { events : Maybe (List Event) } + + +type alias Rooms = + { invite : Maybe (Dict String InvitedRoom) + , join : Maybe (Dict String JoinedRoom) + , knock : Maybe (Dict String KnockedRoom) + , leave : Maybe (Dict String LeftRoom) + } + + +type alias InvitedRoom = + { inviteState : Maybe InviteState } + + +type alias InviteState = + { events : Maybe (List StrippedStateEvent) } + + +type alias StrippedStateEvent = + { content : Json.Value + , sender : String + , stateKey : String + , eventType : String + } + + +type alias JoinedRoom = + { accountData : Maybe AccountData + , ephemeral : Maybe Ephemeral + , state : Maybe State + , summary : Maybe RoomSummary + , timeline : Maybe Timeline + , unreadNotifications : Maybe UnreadNotificationCounts + } + + +type alias Ephemeral = + { events : Maybe (List Event) } + + +type alias State = + { events : Maybe (List ClientEventWithoutRoomID) } + + +type alias ClientEventWithoutRoomID = + { content : Json.Value + , eventId : String + , originServerTs : Int + , sender : String + , stateKey : Maybe String + , eventType : String + , unsigned : Maybe UnsignedData + } + + +type alias UnsignedData = + PV.UnsignedData + + +type alias RoomSummary = + { mHeroes : Maybe (List String) + , mInvitedMemberCount : Maybe Int + , mJoinedMemberCount : Maybe Int + } + + +type alias Timeline = + { events : List ClientEventWithoutRoomID + , limited : Maybe Bool + , prevBatch : Maybe String + } + + +type alias UnreadNotificationCounts = + { highlightCount : Maybe Int + , notificationCount : Maybe Int + } + + +type alias KnockedRoom = + { knockState : Maybe KnockState } + + +type alias KnockState = + { events : Maybe (List StrippedStateEvent) } + + +type alias LeftRoom = + { accountData : Maybe AccountData + , state : Maybe State + , timeline : Maybe Timeline + } + + +type alias DeviceLists = + { changed : Maybe (List String) + , left : Maybe (List String) + } + + +type alias ToDevice = + { events : Maybe (List ToDeviceEvent) } + + +type alias ToDeviceEvent = + { content : Maybe Json.Value + , sender : Maybe String + , eventType : Maybe String + } + + +coderSyncResponse : Json.Coder SyncResponse +coderSyncResponse = + PV.coderSyncResponse + + +coderAccountData : Json.Coder AccountData +coderAccountData = + PV.coderAccountData + + +coderEvent : Json.Coder Event +coderEvent = + PV.coderEvent + + +coderPresence : Json.Coder Presence +coderPresence = + PV.coderPresence + + +coderRooms : Json.Coder Rooms +coderRooms = + PV.coderRooms + + +coderInvitedRoom : Json.Coder InvitedRoom +coderInvitedRoom = + PV.coderInvitedRoom + + +coderInviteState : Json.Coder InviteState +coderInviteState = + PV.coderInviteState + + +coderStrippedStateEvent : Json.Coder StrippedStateEvent +coderStrippedStateEvent = + PV.coderStrippedStateEvent + + +coderJoinedRoom : Json.Coder JoinedRoom +coderJoinedRoom = + PV.coderJoinedRoom + + +coderEphemeral : Json.Coder Ephemeral +coderEphemeral = + PV.coderEphemeral + + +coderState : Json.Coder State +coderState = + PV.coderState + + +coderClientEventWithoutRoomID : Json.Coder ClientEventWithoutRoomID +coderClientEventWithoutRoomID = + PV.coderClientEventWithoutRoomID + + +coderUnsignedData : Json.Coder UnsignedData +coderUnsignedData = + PV.coderUnsignedData + + +coderRoomSummary : Json.Coder RoomSummary +coderRoomSummary = + PV.coderRoomSummary + + +coderTimeline : Json.Coder Timeline +coderTimeline = + PV.coderTimeline + + +coderUnreadNotificationCounts : Json.Coder UnreadNotificationCounts +coderUnreadNotificationCounts = + PV.coderUnreadNotificationCounts + + +coderKnockedRoom : Json.Coder KnockedRoom +coderKnockedRoom = + PV.coderKnockedRoom + + +coderKnockState : Json.Coder KnockState +coderKnockState = + PV.coderKnockState + + +coderLeftRoom : Json.Coder LeftRoom +coderLeftRoom = + PV.coderLeftRoom + + +coderDeviceLists : Json.Coder DeviceLists +coderDeviceLists = + PV.coderDeviceLists + + +coderToDevice : Json.Coder ToDevice +coderToDevice = + PV.coderToDevice + + +coderToDeviceEvent : Json.Coder ToDeviceEvent +coderToDeviceEvent = + PV.coderToDeviceEvent diff --git a/src/Internal/Api/Sync/V4.elm b/src/Internal/Api/Sync/V4.elm new file mode 100644 index 0000000..d65b5a5 --- /dev/null +++ b/src/Internal/Api/Sync/V4.elm @@ -0,0 +1,436 @@ +module Internal.Api.Sync.V4 exposing (..) + +{-| + + +# Sync response + +This API module represents the /sync endpoint on Matrix spec version v1.1. + + + +-} + +import FastDict exposing (Dict) +import Internal.Api.Sync.V3 as PV +import Internal.Tools.Json as Json + + +type alias SyncResponse = + { accountData : Maybe AccountData + , deviceLists : Maybe DeviceLists + , deviceOneTimeKeysCount : Maybe (Dict String Int) + , deviceUnusedFallbackKeyTypes : List String + , nextBatch : String + , presence : Maybe Presence + , rooms : Maybe Rooms + , toDevice : Maybe ToDevice + } + + +type alias AccountData = + { events : Maybe (List Event) } + + +type alias Event = + { content : Json.Value + , eventType : String + } + + +type alias Presence = + { events : Maybe (List Event) } + + +type alias Rooms = + { invite : Maybe (Dict String InvitedRoom) + , join : Maybe (Dict String JoinedRoom) + , knock : Maybe (Dict String KnockedRoom) + , leave : Maybe (Dict String LeftRoom) + } + + +type alias InvitedRoom = + { inviteState : Maybe InviteState } + + +type alias InviteState = + { events : Maybe (List StrippedStateEvent) } + + +type alias StrippedStateEvent = + { content : Json.Value + , sender : String + , stateKey : String + , eventType : String + } + + +type alias JoinedRoom = + { accountData : Maybe AccountData + , ephemeral : Maybe Ephemeral + , state : Maybe State + , summary : Maybe RoomSummary + , timeline : Maybe Timeline + , unreadNotifications : Maybe UnreadNotificationCounts + , unreadThreadNotifications : Maybe (Dict String ThreadNotificationCounts) + } + + +type alias Ephemeral = + { events : Maybe (List Event) } + + +type alias State = + { events : Maybe (List ClientEventWithoutRoomID) } + + +type alias ClientEventWithoutRoomID = + { content : Json.Value + , eventId : String + , originServerTs : Int + , sender : String + , stateKey : Maybe String + , eventType : String + , unsigned : Maybe UnsignedData + } + + +type alias UnsignedData = + PV.UnsignedData + + +type alias RoomSummary = + { mHeroes : Maybe (List String) + , mInvitedMemberCount : Maybe Int + , mJoinedMemberCount : Maybe Int + } + + +type alias Timeline = + { events : List ClientEventWithoutRoomID + , limited : Maybe Bool + , prevBatch : Maybe String + } + + +type alias UnreadNotificationCounts = + { highlightCount : Maybe Int + , notificationCount : Maybe Int + } + + +type alias ThreadNotificationCounts = + { highlightCount : Maybe Int + , notificationCount : Maybe Int + } + + +type alias KnockedRoom = + { knockState : Maybe KnockState } + + +type alias KnockState = + { events : Maybe (List StrippedStateEvent) } + + +type alias LeftRoom = + { accountData : Maybe AccountData + , state : Maybe State + , timeline : Maybe Timeline + } + + +type alias DeviceLists = + { changed : Maybe (List String) + , left : Maybe (List String) + } + + +type alias ToDevice = + { events : Maybe (List ToDeviceEvent) } + + +type alias ToDeviceEvent = + { content : Maybe Json.Value + , sender : Maybe String + , eventType : Maybe String + } + + +coderSyncResponse : Json.Coder SyncResponse +coderSyncResponse = + Json.object8 + { name = "SyncResponse" + , description = [ "The response for a sync request." ] + , init = SyncResponse + } + (Json.field.optional.value + { fieldName = "account_data" + , toField = .accountData + , description = [ "The global private data created by this user." ] + , coder = coderAccountData + } + ) + (Json.field.optional.value + { fieldName = "device_lists" + , toField = .deviceLists + , description = [ "Information on end-to-end device updates, as specified in End-to-end encryption." ] + , coder = coderDeviceLists + } + ) + (Json.field.optional.value + { fieldName = "device_one_time_keys_count" + , toField = .deviceOneTimeKeysCount + , description = [ "Information on end-to-end encryption keys, as specified in End-to-end encryption." ] + , coder = Json.fastDict Json.int + } + ) + (Json.field.required + { fieldName = "device_unused_fallback_key_types" + , toField = .deviceUnusedFallbackKeyTypes + , description = [ "The unused fallback key algorithms." ] + , coder = Json.list Json.string + } + ) + (Json.field.required + { fieldName = "next_batch" + , toField = .nextBatch + , description = [ "The batch token to supply in the since param of the next /sync request." ] + , coder = Json.string + } + ) + (Json.field.optional.value + { fieldName = "presence" + , toField = .presence + , description = [ "The updates to the presence status of other users." ] + , coder = coderPresence + } + ) + (Json.field.optional.value + { fieldName = "rooms" + , toField = .rooms + , description = [ "Updates to rooms." ] + , coder = coderRooms + } + ) + (Json.field.optional.value + { fieldName = "to_device" + , toField = .toDevice + , description = [ "Information on the send-to-device messages for the client device, as defined in Send-to-Device messaging." ] + , coder = coderToDevice + } + ) + + +coderAccountData : Json.Coder AccountData +coderAccountData = + PV.coderAccountData + + +coderEvent : Json.Coder Event +coderEvent = + PV.coderEvent + + +coderPresence : Json.Coder Presence +coderPresence = + PV.coderPresence + + +coderRooms : Json.Coder Rooms +coderRooms = + Json.object4 + { name = "Rooms" + , description = [ "Updates to rooms." ] + , init = Rooms + } + (Json.field.optional.value + { fieldName = "invite" + , toField = .invite + , description = [ "The rooms that the user has been invited to, mapped as room ID to room information." ] + , coder = Json.fastDict coderInvitedRoom + } + ) + (Json.field.optional.value + { fieldName = "join" + , toField = .join + , description = [ "The rooms that the user has joined, mapped as room ID to room information." ] + , coder = Json.fastDict coderJoinedRoom + } + ) + (Json.field.optional.value + { fieldName = "knock" + , toField = .knock + , description = [ "The rooms that the user has knocked upon, mapped as room ID to room information." ] + , coder = Json.fastDict coderKnockedRoom + } + ) + (Json.field.optional.value + { fieldName = "leave" + , toField = .leave + , description = [ "The rooms that the user has left or been banned from, mapped as room ID to room information." ] + , coder = Json.fastDict coderLeftRoom + } + ) + + +coderInvitedRoom : Json.Coder InvitedRoom +coderInvitedRoom = + PV.coderInvitedRoom + + +coderInviteState : Json.Coder InviteState +coderInviteState = + PV.coderInviteState + + +coderStrippedStateEvent : Json.Coder StrippedStateEvent +coderStrippedStateEvent = + PV.coderStrippedStateEvent + + +coderJoinedRoom : Json.Coder JoinedRoom +coderJoinedRoom = + Json.object7 + { name = "JoinedRoom" + , description = [ "Information about a room the user has joined." ] + , init = JoinedRoom + } + (Json.field.optional.value + { fieldName = "account_data" + , toField = .accountData + , description = [ "The private data that this user has attached to this room." ] + , coder = coderAccountData + } + ) + (Json.field.optional.value + { fieldName = "ephemeral" + , toField = .ephemeral + , description = [ "The ephemeral events in the room that aren’t recorded in the timeline or state of the room. e.g. typing." ] + , coder = coderEphemeral + } + ) + (Json.field.optional.value + { fieldName = "state" + , toField = .state + , description = [ "Updates to the state, between the time indicated by the since parameter, and the start of the timeline (or all state up to the start of the timeline, if since is not given, or full_state is true).", "N.B. state updates for m.room.member events will be incomplete if lazy_load_members is enabled in the /sync filter, and only return the member events required to display the senders of the timeline events in this response." ] + , coder = coderState + } + ) + (Json.field.optional.value + { fieldName = "summary" + , toField = .summary + , description = [ "Information about the room which clients may need to correctly render it to users." ] + , coder = coderRoomSummary + } + ) + (Json.field.optional.value + { fieldName = "timeline" + , toField = .timeline + , description = [ "The timeline of messages and state changes in the room." ] + , coder = coderTimeline + } + ) + (Json.field.optional.value + { fieldName = "unread_notifications" + , toField = .unreadNotifications + , description = [ "Counts of unread notifications for this room. See the Receiving notifications section for more information on how these are calculated.", "If unread_thread_notifications was specified as true on the RoomEventFilter, these counts will only be for the main timeline rather than all events in the room. See the threading module for more information.", "Changed in v1.4: Updated to reflect behaviour of having unread_thread_notifications as true in the RoomEventFilter for /sync." ] + , coder = coderUnreadNotificationCounts + } + ) + (Json.field.optional.value + { fieldName = "unread_thread_notifications" + , toField = .unreadThreadNotifications + , description = [ "If unread_thread_notifications was specified as true on the RoomEventFilter, the notification counts for each thread in this room. The object is keyed by thread root ID, with values matching unread_notifications.", "If a thread does not have any notifications it can be omitted from this object. If no threads have notification counts, this whole object can be omitted.", "Added in v1.4" ] + , coder = Json.fastDict coderThreadNotificationCounts + } + ) + + +coderEphemeral : Json.Coder Ephemeral +coderEphemeral = + PV.coderEphemeral + + +coderState : Json.Coder State +coderState = + PV.coderState + + +coderClientEventWithoutRoomID : Json.Coder ClientEventWithoutRoomID +coderClientEventWithoutRoomID = + PV.coderClientEventWithoutRoomID + + +coderUnsignedData : Json.Coder UnsignedData +coderUnsignedData = + PV.coderUnsignedData + + +coderRoomSummary : Json.Coder RoomSummary +coderRoomSummary = + PV.coderRoomSummary + + +coderTimeline : Json.Coder Timeline +coderTimeline = + PV.coderTimeline + + +coderUnreadNotificationCounts : Json.Coder UnreadNotificationCounts +coderUnreadNotificationCounts = + PV.coderUnreadNotificationCounts + + +coderThreadNotificationCounts : Json.Coder ThreadNotificationCounts +coderThreadNotificationCounts = + Json.object2 + { name = "ThreadNotificationCounts" + , description = [ "The notification counts for each thread in this room." ] + , init = ThreadNotificationCounts + } + (Json.field.optional.value + { fieldName = "highlight_count" + , toField = .highlightCount + , description = [ "The number of unread notifications for this thread with the highlight flag set." ] + , coder = Json.int + } + ) + (Json.field.optional.value + { fieldName = "notification_count" + , toField = .notificationCount + , description = [ "The total number of unread notifications for this thread." ] + , coder = Json.int + } + ) + + +coderKnockedRoom : Json.Coder KnockedRoom +coderKnockedRoom = + PV.coderKnockedRoom + + +coderKnockState : Json.Coder KnockState +coderKnockState = + PV.coderKnockState + + +coderLeftRoom : Json.Coder LeftRoom +coderLeftRoom = + PV.coderLeftRoom + + +coderDeviceLists : Json.Coder DeviceLists +coderDeviceLists = + PV.coderDeviceLists + + +coderToDevice : Json.Coder ToDevice +coderToDevice = + PV.coderToDevice + + +coderToDeviceEvent : Json.Coder ToDeviceEvent +coderToDeviceEvent = + PV.coderToDeviceEvent diff --git a/src/Internal/Api/Sync/V5.elm b/src/Internal/Api/Sync/V5.elm new file mode 100644 index 0000000..0e85d1c --- /dev/null +++ b/src/Internal/Api/Sync/V5.elm @@ -0,0 +1,249 @@ +module Internal.Api.Sync.V5 exposing (..) + +{-| + + +# Sync response + +This API module represents the /sync endpoint on Matrix spec version v1.1. + + + +-} + +import FastDict exposing (Dict) +import Internal.Api.Sync.V4 as PV +import Internal.Tools.Json as Json + + +type alias SyncResponse = + { accountData : Maybe AccountData + , deviceLists : Maybe DeviceLists + , deviceOneTimeKeysCount : Maybe (Dict String Int) + , deviceUnusedFallbackKeyTypes : List String + , nextBatch : String + , presence : Maybe Presence + , rooms : Maybe Rooms + , toDevice : Maybe ToDevice + } + + +type alias AccountData = + { events : Maybe (List Event) } + + +type alias Event = + { content : Json.Value + , eventType : String + } + + +type alias Presence = + { events : Maybe (List Event) } + + +type alias Rooms = + { invite : Maybe (Dict String InvitedRoom) + , join : Maybe (Dict String JoinedRoom) + , knock : Maybe (Dict String KnockedRoom) + , leave : Maybe (Dict String LeftRoom) + } + + +type alias InvitedRoom = + { inviteState : Maybe InviteState } + + +type alias InviteState = + { events : Maybe (List StrippedStateEvent) } + + +type alias StrippedStateEvent = + { content : Json.Value + , sender : String + , stateKey : String + , eventType : String + } + + +type alias JoinedRoom = + { accountData : Maybe AccountData + , ephemeral : Maybe Ephemeral + , state : Maybe State + , summary : Maybe RoomSummary + , timeline : Maybe Timeline + , unreadNotifications : Maybe UnreadNotificationCounts + , unreadThreadNotifications : Maybe (Dict String ThreadNotificationCounts) + } + + +type alias Ephemeral = + { events : Maybe (List Event) } + + +type alias State = + { events : Maybe (List ClientEventWithoutRoomID) } + + +type alias ClientEventWithoutRoomID = + { content : Json.Value + , eventId : String + , originServerTs : Int + , sender : String + , stateKey : Maybe String + , eventType : String + , unsigned : Maybe UnsignedData + } + + +type alias UnsignedData = PV.UnsignedData + + +type alias RoomSummary = + { mHeroes : Maybe (List String) + , mInvitedMemberCount : Maybe Int + , mJoinedMemberCount : Maybe Int + } + + +type alias Timeline = + { events : List ClientEventWithoutRoomID + , limited : Maybe Bool + , prevBatch : Maybe String + } + + +type alias UnreadNotificationCounts = + { highlightCount : Maybe Int + , notificationCount : Maybe Int + } + + +type alias ThreadNotificationCounts = + { highlightCount : Maybe Int + , notificationCount : Maybe Int + } + + +type alias KnockedRoom = + { knockState : Maybe KnockState } + + +type alias KnockState = + { events : Maybe (List StrippedStateEvent) } + + +type alias LeftRoom = + { accountData : Maybe AccountData + , state : Maybe State + , timeline : Maybe Timeline + } + + +type alias DeviceLists = + { changed : Maybe (List String) + , left : Maybe (List String) + } + + +type alias ToDevice = + { events : Maybe (List ToDeviceEvent) } + + +type alias ToDeviceEvent = + { content : Maybe Json.Value + , sender : Maybe String + , eventType : Maybe String + } + + +coderSyncResponse : Json.Coder SyncResponse +coderSyncResponse = PV.coderSyncResponse + + +coderAccountData : Json.Coder AccountData +coderAccountData = PV.coderAccountData + + +coderEvent : Json.Coder Event +coderEvent = PV.coderEvent + + +coderPresence : Json.Coder Presence +coderPresence = PV.coderPresence + + +coderRooms : Json.Coder Rooms +coderRooms = PV.coderRooms + + +coderInvitedRoom : Json.Coder InvitedRoom +coderInvitedRoom = PV.coderInvitedRoom + + +coderInviteState : Json.Coder InviteState +coderInviteState = PV.coderInviteState + + +coderStrippedStateEvent : Json.Coder StrippedStateEvent +coderStrippedStateEvent = PV.coderStrippedStateEvent + + +coderJoinedRoom : Json.Coder JoinedRoom +coderJoinedRoom = PV.coderJoinedRoom + + +coderEphemeral : Json.Coder Ephemeral +coderEphemeral = PV.coderEphemeral + + +coderState : Json.Coder State +coderState = PV.coderState + + +coderClientEventWithoutRoomID : Json.Coder ClientEventWithoutRoomID +coderClientEventWithoutRoomID = PV.coderClientEventWithoutRoomID + + +coderUnsignedData : Json.Coder UnsignedData +coderUnsignedData = PV.coderUnsignedData + + +coderRoomSummary : Json.Coder RoomSummary +coderRoomSummary = PV.coderRoomSummary + + +coderTimeline : Json.Coder Timeline +coderTimeline = PV.coderTimeline + + +coderUnreadNotificationCounts : Json.Coder UnreadNotificationCounts +coderUnreadNotificationCounts = PV.coderUnreadNotificationCounts + + +coderThreadNotificationCounts : Json.Coder ThreadNotificationCounts +coderThreadNotificationCounts = PV.coderThreadNotificationCounts + + +coderKnockedRoom : Json.Coder KnockedRoom +coderKnockedRoom = PV.coderKnockedRoom + + +coderKnockState : Json.Coder KnockState +coderKnockState = PV.coderKnockState + + +coderLeftRoom : Json.Coder LeftRoom +coderLeftRoom = PV.coderLeftRoom + + +coderDeviceLists : Json.Coder DeviceLists +coderDeviceLists = PV.coderDeviceLists + + +coderToDevice : Json.Coder ToDevice +coderToDevice = PV.coderToDevice + + +coderToDeviceEvent : Json.Coder ToDeviceEvent +coderToDeviceEvent = PV.coderToDeviceEvent From 29906ff976655bba78b10153fcaba8a390eaf655 Mon Sep 17 00:00:00 2001 From: Bram Date: Mon, 8 Jul 2024 22:10:29 +0200 Subject: [PATCH 12/20] Remove redundant /sync versions --- src/Internal/Api/Sync/V2.elm | 1 + src/Internal/Api/Sync/V3.elm | 261 ------------------------------ src/Internal/Api/Sync/V4.elm | 4 +- src/Internal/Api/Sync/V5.elm | 249 ---------------------------- src/Internal/Api/Versions/Api.elm | 1 - 5 files changed, 4 insertions(+), 512 deletions(-) delete mode 100644 src/Internal/Api/Sync/V3.elm delete mode 100644 src/Internal/Api/Sync/V5.elm diff --git a/src/Internal/Api/Sync/V2.elm b/src/Internal/Api/Sync/V2.elm index ee34599..d47491d 100644 --- a/src/Internal/Api/Sync/V2.elm +++ b/src/Internal/Api/Sync/V2.elm @@ -8,6 +8,7 @@ module Internal.Api.Sync.V2 exposing (..) This API module represents the /sync endpoint on Matrix spec version v1.1. + -} diff --git a/src/Internal/Api/Sync/V3.elm b/src/Internal/Api/Sync/V3.elm deleted file mode 100644 index 2f7bafb..0000000 --- a/src/Internal/Api/Sync/V3.elm +++ /dev/null @@ -1,261 +0,0 @@ -module Internal.Api.Sync.V3 exposing (..) - -{-| - - -# Sync response - -This API module represents the /sync endpoint on Matrix spec version v1.1. - - - --} - -import FastDict exposing (Dict) -import Internal.Api.Sync.V2 as PV -import Internal.Tools.Json as Json - - -type alias SyncResponse = - { accountData : Maybe AccountData - , deviceLists : Maybe DeviceLists - , deviceOneTimeKeysCount : Maybe (Dict String Int) - , deviceUnusedFallbackKeyTypes : List String - , nextBatch : String - , presence : Maybe Presence - , rooms : Maybe Rooms - , toDevice : Maybe ToDevice - } - - -type alias AccountData = - { events : Maybe (List Event) } - - -type alias Event = - { content : Json.Value - , eventType : String - } - - -type alias Presence = - { events : Maybe (List Event) } - - -type alias Rooms = - { invite : Maybe (Dict String InvitedRoom) - , join : Maybe (Dict String JoinedRoom) - , knock : Maybe (Dict String KnockedRoom) - , leave : Maybe (Dict String LeftRoom) - } - - -type alias InvitedRoom = - { inviteState : Maybe InviteState } - - -type alias InviteState = - { events : Maybe (List StrippedStateEvent) } - - -type alias StrippedStateEvent = - { content : Json.Value - , sender : String - , stateKey : String - , eventType : String - } - - -type alias JoinedRoom = - { accountData : Maybe AccountData - , ephemeral : Maybe Ephemeral - , state : Maybe State - , summary : Maybe RoomSummary - , timeline : Maybe Timeline - , unreadNotifications : Maybe UnreadNotificationCounts - } - - -type alias Ephemeral = - { events : Maybe (List Event) } - - -type alias State = - { events : Maybe (List ClientEventWithoutRoomID) } - - -type alias ClientEventWithoutRoomID = - { content : Json.Value - , eventId : String - , originServerTs : Int - , sender : String - , stateKey : Maybe String - , eventType : String - , unsigned : Maybe UnsignedData - } - - -type alias UnsignedData = - PV.UnsignedData - - -type alias RoomSummary = - { mHeroes : Maybe (List String) - , mInvitedMemberCount : Maybe Int - , mJoinedMemberCount : Maybe Int - } - - -type alias Timeline = - { events : List ClientEventWithoutRoomID - , limited : Maybe Bool - , prevBatch : Maybe String - } - - -type alias UnreadNotificationCounts = - { highlightCount : Maybe Int - , notificationCount : Maybe Int - } - - -type alias KnockedRoom = - { knockState : Maybe KnockState } - - -type alias KnockState = - { events : Maybe (List StrippedStateEvent) } - - -type alias LeftRoom = - { accountData : Maybe AccountData - , state : Maybe State - , timeline : Maybe Timeline - } - - -type alias DeviceLists = - { changed : Maybe (List String) - , left : Maybe (List String) - } - - -type alias ToDevice = - { events : Maybe (List ToDeviceEvent) } - - -type alias ToDeviceEvent = - { content : Maybe Json.Value - , sender : Maybe String - , eventType : Maybe String - } - - -coderSyncResponse : Json.Coder SyncResponse -coderSyncResponse = - PV.coderSyncResponse - - -coderAccountData : Json.Coder AccountData -coderAccountData = - PV.coderAccountData - - -coderEvent : Json.Coder Event -coderEvent = - PV.coderEvent - - -coderPresence : Json.Coder Presence -coderPresence = - PV.coderPresence - - -coderRooms : Json.Coder Rooms -coderRooms = - PV.coderRooms - - -coderInvitedRoom : Json.Coder InvitedRoom -coderInvitedRoom = - PV.coderInvitedRoom - - -coderInviteState : Json.Coder InviteState -coderInviteState = - PV.coderInviteState - - -coderStrippedStateEvent : Json.Coder StrippedStateEvent -coderStrippedStateEvent = - PV.coderStrippedStateEvent - - -coderJoinedRoom : Json.Coder JoinedRoom -coderJoinedRoom = - PV.coderJoinedRoom - - -coderEphemeral : Json.Coder Ephemeral -coderEphemeral = - PV.coderEphemeral - - -coderState : Json.Coder State -coderState = - PV.coderState - - -coderClientEventWithoutRoomID : Json.Coder ClientEventWithoutRoomID -coderClientEventWithoutRoomID = - PV.coderClientEventWithoutRoomID - - -coderUnsignedData : Json.Coder UnsignedData -coderUnsignedData = - PV.coderUnsignedData - - -coderRoomSummary : Json.Coder RoomSummary -coderRoomSummary = - PV.coderRoomSummary - - -coderTimeline : Json.Coder Timeline -coderTimeline = - PV.coderTimeline - - -coderUnreadNotificationCounts : Json.Coder UnreadNotificationCounts -coderUnreadNotificationCounts = - PV.coderUnreadNotificationCounts - - -coderKnockedRoom : Json.Coder KnockedRoom -coderKnockedRoom = - PV.coderKnockedRoom - - -coderKnockState : Json.Coder KnockState -coderKnockState = - PV.coderKnockState - - -coderLeftRoom : Json.Coder LeftRoom -coderLeftRoom = - PV.coderLeftRoom - - -coderDeviceLists : Json.Coder DeviceLists -coderDeviceLists = - PV.coderDeviceLists - - -coderToDevice : Json.Coder ToDevice -coderToDevice = - PV.coderToDevice - - -coderToDeviceEvent : Json.Coder ToDeviceEvent -coderToDeviceEvent = - PV.coderToDeviceEvent diff --git a/src/Internal/Api/Sync/V4.elm b/src/Internal/Api/Sync/V4.elm index d65b5a5..83054ed 100644 --- a/src/Internal/Api/Sync/V4.elm +++ b/src/Internal/Api/Sync/V4.elm @@ -8,11 +8,13 @@ module Internal.Api.Sync.V4 exposing (..) This API module represents the /sync endpoint on Matrix spec version v1.1. + + -} import FastDict exposing (Dict) -import Internal.Api.Sync.V3 as PV +import Internal.Api.Sync.V2 as PV import Internal.Tools.Json as Json diff --git a/src/Internal/Api/Sync/V5.elm b/src/Internal/Api/Sync/V5.elm deleted file mode 100644 index 0e85d1c..0000000 --- a/src/Internal/Api/Sync/V5.elm +++ /dev/null @@ -1,249 +0,0 @@ -module Internal.Api.Sync.V5 exposing (..) - -{-| - - -# Sync response - -This API module represents the /sync endpoint on Matrix spec version v1.1. - - - --} - -import FastDict exposing (Dict) -import Internal.Api.Sync.V4 as PV -import Internal.Tools.Json as Json - - -type alias SyncResponse = - { accountData : Maybe AccountData - , deviceLists : Maybe DeviceLists - , deviceOneTimeKeysCount : Maybe (Dict String Int) - , deviceUnusedFallbackKeyTypes : List String - , nextBatch : String - , presence : Maybe Presence - , rooms : Maybe Rooms - , toDevice : Maybe ToDevice - } - - -type alias AccountData = - { events : Maybe (List Event) } - - -type alias Event = - { content : Json.Value - , eventType : String - } - - -type alias Presence = - { events : Maybe (List Event) } - - -type alias Rooms = - { invite : Maybe (Dict String InvitedRoom) - , join : Maybe (Dict String JoinedRoom) - , knock : Maybe (Dict String KnockedRoom) - , leave : Maybe (Dict String LeftRoom) - } - - -type alias InvitedRoom = - { inviteState : Maybe InviteState } - - -type alias InviteState = - { events : Maybe (List StrippedStateEvent) } - - -type alias StrippedStateEvent = - { content : Json.Value - , sender : String - , stateKey : String - , eventType : String - } - - -type alias JoinedRoom = - { accountData : Maybe AccountData - , ephemeral : Maybe Ephemeral - , state : Maybe State - , summary : Maybe RoomSummary - , timeline : Maybe Timeline - , unreadNotifications : Maybe UnreadNotificationCounts - , unreadThreadNotifications : Maybe (Dict String ThreadNotificationCounts) - } - - -type alias Ephemeral = - { events : Maybe (List Event) } - - -type alias State = - { events : Maybe (List ClientEventWithoutRoomID) } - - -type alias ClientEventWithoutRoomID = - { content : Json.Value - , eventId : String - , originServerTs : Int - , sender : String - , stateKey : Maybe String - , eventType : String - , unsigned : Maybe UnsignedData - } - - -type alias UnsignedData = PV.UnsignedData - - -type alias RoomSummary = - { mHeroes : Maybe (List String) - , mInvitedMemberCount : Maybe Int - , mJoinedMemberCount : Maybe Int - } - - -type alias Timeline = - { events : List ClientEventWithoutRoomID - , limited : Maybe Bool - , prevBatch : Maybe String - } - - -type alias UnreadNotificationCounts = - { highlightCount : Maybe Int - , notificationCount : Maybe Int - } - - -type alias ThreadNotificationCounts = - { highlightCount : Maybe Int - , notificationCount : Maybe Int - } - - -type alias KnockedRoom = - { knockState : Maybe KnockState } - - -type alias KnockState = - { events : Maybe (List StrippedStateEvent) } - - -type alias LeftRoom = - { accountData : Maybe AccountData - , state : Maybe State - , timeline : Maybe Timeline - } - - -type alias DeviceLists = - { changed : Maybe (List String) - , left : Maybe (List String) - } - - -type alias ToDevice = - { events : Maybe (List ToDeviceEvent) } - - -type alias ToDeviceEvent = - { content : Maybe Json.Value - , sender : Maybe String - , eventType : Maybe String - } - - -coderSyncResponse : Json.Coder SyncResponse -coderSyncResponse = PV.coderSyncResponse - - -coderAccountData : Json.Coder AccountData -coderAccountData = PV.coderAccountData - - -coderEvent : Json.Coder Event -coderEvent = PV.coderEvent - - -coderPresence : Json.Coder Presence -coderPresence = PV.coderPresence - - -coderRooms : Json.Coder Rooms -coderRooms = PV.coderRooms - - -coderInvitedRoom : Json.Coder InvitedRoom -coderInvitedRoom = PV.coderInvitedRoom - - -coderInviteState : Json.Coder InviteState -coderInviteState = PV.coderInviteState - - -coderStrippedStateEvent : Json.Coder StrippedStateEvent -coderStrippedStateEvent = PV.coderStrippedStateEvent - - -coderJoinedRoom : Json.Coder JoinedRoom -coderJoinedRoom = PV.coderJoinedRoom - - -coderEphemeral : Json.Coder Ephemeral -coderEphemeral = PV.coderEphemeral - - -coderState : Json.Coder State -coderState = PV.coderState - - -coderClientEventWithoutRoomID : Json.Coder ClientEventWithoutRoomID -coderClientEventWithoutRoomID = PV.coderClientEventWithoutRoomID - - -coderUnsignedData : Json.Coder UnsignedData -coderUnsignedData = PV.coderUnsignedData - - -coderRoomSummary : Json.Coder RoomSummary -coderRoomSummary = PV.coderRoomSummary - - -coderTimeline : Json.Coder Timeline -coderTimeline = PV.coderTimeline - - -coderUnreadNotificationCounts : Json.Coder UnreadNotificationCounts -coderUnreadNotificationCounts = PV.coderUnreadNotificationCounts - - -coderThreadNotificationCounts : Json.Coder ThreadNotificationCounts -coderThreadNotificationCounts = PV.coderThreadNotificationCounts - - -coderKnockedRoom : Json.Coder KnockedRoom -coderKnockedRoom = PV.coderKnockedRoom - - -coderKnockState : Json.Coder KnockState -coderKnockState = PV.coderKnockState - - -coderLeftRoom : Json.Coder LeftRoom -coderLeftRoom = PV.coderLeftRoom - - -coderDeviceLists : Json.Coder DeviceLists -coderDeviceLists = PV.coderDeviceLists - - -coderToDevice : Json.Coder ToDevice -coderToDevice = PV.coderToDevice - - -coderToDeviceEvent : Json.Coder ToDeviceEvent -coderToDeviceEvent = PV.coderToDeviceEvent diff --git a/src/Internal/Api/Versions/Api.elm b/src/Internal/Api/Versions/Api.elm index a617ab3..835d0ef 100644 --- a/src/Internal/Api/Versions/Api.elm +++ b/src/Internal/Api/Versions/Api.elm @@ -86,6 +86,5 @@ versionsCoder = Set.empty } , default = ( Set.empty, [] ) - , defaultToString = always "{}" } ) From e122a7b262bedc3b404a75e04f7e81ea6d4a6dc4 Mon Sep 17 00:00:00 2001 From: Bram Date: Mon, 8 Jul 2024 22:11:04 +0200 Subject: [PATCH 13/20] Rename /sync v4 to v3 --- src/Internal/Api/Sync/{V4.elm => V3.elm} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/Internal/Api/Sync/{V4.elm => V3.elm} (99%) diff --git a/src/Internal/Api/Sync/V4.elm b/src/Internal/Api/Sync/V3.elm similarity index 99% rename from src/Internal/Api/Sync/V4.elm rename to src/Internal/Api/Sync/V3.elm index 83054ed..c1c43bf 100644 --- a/src/Internal/Api/Sync/V4.elm +++ b/src/Internal/Api/Sync/V3.elm @@ -1,4 +1,4 @@ -module Internal.Api.Sync.V4 exposing (..) +module Internal.Api.Sync.V3 exposing (..) {-| From e7d3a129b1b2147d53043066a86268d57851d423 Mon Sep 17 00:00:00 2001 From: Bram Date: Mon, 8 Jul 2024 23:07:50 +0200 Subject: [PATCH 14/20] Finish /sync for all spec versions v1.1 to v1.11 --- src/Internal/Api/Sync/V2.elm | 3 +- src/Internal/Api/Sync/V3.elm | 7 +- src/Internal/Api/Sync/V4.elm | 827 +++++++++++++++++++++++++++++++++++ 3 files changed, 835 insertions(+), 2 deletions(-) create mode 100644 src/Internal/Api/Sync/V4.elm diff --git a/src/Internal/Api/Sync/V2.elm b/src/Internal/Api/Sync/V2.elm index d47491d..cfbf29c 100644 --- a/src/Internal/Api/Sync/V2.elm +++ b/src/Internal/Api/Sync/V2.elm @@ -5,7 +5,8 @@ module Internal.Api.Sync.V2 exposing (..) # Sync response -This API module represents the /sync endpoint on Matrix spec version v1.1. +This API module represents the /sync endpoint on Matrix spec version v1.2 and +v1.3. diff --git a/src/Internal/Api/Sync/V3.elm b/src/Internal/Api/Sync/V3.elm index c1c43bf..676a1f1 100644 --- a/src/Internal/Api/Sync/V3.elm +++ b/src/Internal/Api/Sync/V3.elm @@ -5,11 +5,16 @@ module Internal.Api.Sync.V3 exposing (..) # Sync response -This API module represents the /sync endpoint on Matrix spec version v1.1. +This API module represents the /sync endpoint on the following Matrix spec +versions: + + + + -} diff --git a/src/Internal/Api/Sync/V4.elm b/src/Internal/Api/Sync/V4.elm new file mode 100644 index 0000000..7d67b2f --- /dev/null +++ b/src/Internal/Api/Sync/V4.elm @@ -0,0 +1,827 @@ +module Internal.Api.Sync.V4 exposing (..) + +{-| + + +# Sync response + +This API module represents the /sync endpoint on Matrix spec version v1.11. + + + +-} + +import FastDict exposing (Dict) +import Internal.Api.Sync.V3 as PV +import Internal.Tools.Json as Json + + +type alias SyncResponse = + { accountData : Maybe AccountData + , deviceLists : Maybe DeviceLists + , deviceOneTimeKeysCount : Maybe (Dict String Int) + , deviceUnusedFallbackKeyTypes : List String + , nextBatch : String + , presence : Maybe Presence + , rooms : Maybe Rooms + , toDevice : Maybe ToDevice + } + + +type alias AccountData = + { events : Maybe (List Event) } + + +type alias Event = + { content : Json.Value + , eventType : String + } + + +type alias Presence = + { events : Maybe (List Event) } + + +type alias Rooms = + { invite : Maybe (Dict String InvitedRoom) + , join : Maybe (Dict String JoinedRoom) + , knock : Maybe (Dict String KnockedRoom) + , leave : Maybe (Dict String LeftRoom) + } + + +type alias InvitedRoom = + { inviteState : Maybe InviteState } + + +type alias InviteState = + { events : Maybe (List StrippedStateEvent) } + + +type alias StrippedStateEvent = + { content : Json.Value + , sender : String + , stateKey : String + , eventType : String + } + + +type alias JoinedRoom = + { accountData : Maybe AccountData + , ephemeral : Maybe Ephemeral + , state : Maybe State + , summary : Maybe RoomSummary + , timeline : Maybe Timeline + , unreadNotifications : Maybe UnreadNotificationCounts + , unreadThreadNotifications : Maybe (Dict String ThreadNotificationCounts) + } + + +type alias Ephemeral = + { events : Maybe (List Event) } + + +type alias State = + { events : Maybe (List ClientEventWithoutRoomID) } + + +type alias ClientEventWithoutRoomID = + { content : Json.Value + , eventId : String + , originServerTs : Int + , sender : String + , stateKey : Maybe String + , eventType : String + , unsigned : Maybe UnsignedData + } + + +type UnsignedData + = UnsignedData + { age : Maybe Int + , membership : Maybe String + , prevContent : Maybe Json.Value + , redactedBecause : Maybe ClientEventWithoutRoomID + , transactionId : Maybe String + } + + +type alias RoomSummary = + { mHeroes : Maybe (List String) + , mInvitedMemberCount : Maybe Int + , mJoinedMemberCount : Maybe Int + } + + +type alias Timeline = + { events : List ClientEventWithoutRoomID + , limited : Maybe Bool + , prevBatch : Maybe String + } + + +type alias UnreadNotificationCounts = + { highlightCount : Maybe Int + , notificationCount : Maybe Int + } + + +type alias ThreadNotificationCounts = + { highlightCount : Maybe Int + , notificationCount : Maybe Int + } + + +type alias KnockedRoom = + { knockState : Maybe KnockState } + + +type alias KnockState = + { events : Maybe (List StrippedStateEvent) } + + +type alias LeftRoom = + { accountData : Maybe AccountData + , state : Maybe State + , timeline : Maybe Timeline + } + + +type alias DeviceLists = + { changed : Maybe (List String) + , left : Maybe (List String) + } + + +type alias ToDevice = + { events : Maybe (List ToDeviceEvent) } + + +type alias ToDeviceEvent = + { content : Maybe Json.Value + , sender : Maybe String + , eventType : Maybe String + } + + +coderSyncResponse : Json.Coder SyncResponse +coderSyncResponse = + Json.object8 + { name = "SyncResponse" + , description = [ "The response received when the server successfully processes the request." ] + , init = SyncResponse + } + (Json.field.optional.value + { fieldName = "account_data" + , toField = .accountData + , description = [ "The global private data created by this user." ] + , coder = coderAccountData + } + ) + (Json.field.optional.value + { fieldName = "device_lists" + , toField = .deviceLists + , description = [ "Information on end-to-end device updates, as specified in End-to-end encryption." ] + , coder = coderDeviceLists + } + ) + (Json.field.optional.value + { fieldName = "device_one_time_keys_count" + , toField = .deviceOneTimeKeysCount + , description = [ "Information on end-to-end encryption keys, as specified in End-to-end encryption." ] + , coder = Json.fastDict Json.int + } + ) + (Json.field.required + { fieldName = "device_unused_fallback_key_types" + , toField = .deviceUnusedFallbackKeyTypes + , description = [ "The unused fallback key algorithms." ] + , coder = Json.list Json.string + } + ) + (Json.field.required + { fieldName = "next_batch" + , toField = .nextBatch + , description = [ "The batch token to supply in the since param of the next /sync request." ] + , coder = Json.string + } + ) + (Json.field.optional.value + { fieldName = "presence" + , toField = .presence + , description = [ "The updates to the presence status of other users." ] + , coder = coderPresence + } + ) + (Json.field.optional.value + { fieldName = "rooms" + , toField = .rooms + , description = [ "Updates to rooms." ] + , coder = coderRooms + } + ) + (Json.field.optional.value + { fieldName = "to_device" + , toField = .toDevice + , description = [ "Information on the send-to-device messages for the client device, as defined in Send-to-Device messaging." ] + , coder = coderToDevice + } + ) + + +coderAccountData : Json.Coder AccountData +coderAccountData = + Json.object1 + { name = "AccountData" + , description = [ "The global private data created by this user." ] + , init = AccountData + } + (Json.field.optional.value + { fieldName = "events" + , toField = .events + , description = [ "List of events." ] + , coder = Json.list coderEvent + } + ) + + +coderEvent : Json.Coder Event +coderEvent = + Json.object2 + { name = "Event" + , description = [ "Details of an event." ] + , init = Event + } + (Json.field.required + { fieldName = "content" + , toField = .content + , description = [ "The fields in this object will vary depending on the type of event. When interacting with the REST API, this is the HTTP body." ] + , coder = Json.value + } + ) + (Json.field.required + { fieldName = "type" + , toField = .eventType + , description = [ "The type of event. This SHOULD be namespaced similar to Java package naming conventions e.g. ‘com.example.subdomain.event.type’" ] + , coder = Json.string + } + ) + + +coderPresence : Json.Coder Presence +coderPresence = + Json.object1 + { name = "Presence" + , description = [ "The updates to the presence status of other users." ] + , init = Presence + } + (Json.field.optional.value + { fieldName = "events" + , toField = .events + , description = [ "List of events." ] + , coder = Json.list coderEvent + } + ) + + +coderRooms : Json.Coder Rooms +coderRooms = + Json.object4 + { name = "Rooms" + , description = [ "Updates to rooms." ] + , init = Rooms + } + (Json.field.optional.value + { fieldName = "invite" + , toField = .invite + , description = [ "The rooms that the user has been invited to, mapped as room ID to room information." ] + , coder = Json.fastDict coderInvitedRoom + } + ) + (Json.field.optional.value + { fieldName = "join" + , toField = .join + , description = [ "The rooms that the user has joined, mapped as room ID to room information." ] + , coder = Json.fastDict coderJoinedRoom + } + ) + (Json.field.optional.value + { fieldName = "knock" + , toField = .knock + , description = [ "The rooms that the user has knocked upon, mapped as room ID to room information." ] + , coder = Json.fastDict coderKnockedRoom + } + ) + (Json.field.optional.value + { fieldName = "leave" + , toField = .leave + , description = [ "The rooms that the user has left or been banned from, mapped as room ID to room information." ] + , coder = Json.fastDict coderLeftRoom + } + ) + + +coderInvitedRoom : Json.Coder InvitedRoom +coderInvitedRoom = + Json.object1 + { name = "InvitedRoom" + , description = [ "The room that the user has been invited to." ] + , init = InvitedRoom + } + (Json.field.optional.value + { fieldName = "invite_state" + , toField = .inviteState + , description = [ "The stripped state of a room that the user has been invited to." ] + , coder = coderInviteState + } + ) + + +coderInviteState : Json.Coder InviteState +coderInviteState = + Json.object1 + { name = "InviteState" + , description = [ "The stripped state of a room that the user has been invited to." ] + , init = InviteState + } + (Json.field.optional.value + { fieldName = "events" + , toField = .events + , description = [ "The stripped state events that form the invite state." ] + , coder = Json.list coderStrippedStateEvent + } + ) + + +coderStrippedStateEvent : Json.Coder StrippedStateEvent +coderStrippedStateEvent = + Json.object4 + { name = "StrippedStateEvent" + , description = [ "A stripped state event that forms part of the invite state." ] + , init = StrippedStateEvent + } + (Json.field.required + { fieldName = "content" + , toField = .content + , description = [ "The content for the event." ] + , coder = Json.value + } + ) + (Json.field.required + { fieldName = "sender" + , toField = .sender + , description = [ "The sender for the event." ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "state_key" + , toField = .stateKey + , description = [ "The state_key for the event." ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "type" + , toField = .eventType + , description = [ "The type for the event." ] + , coder = Json.string + } + ) + + +coderJoinedRoom : Json.Coder JoinedRoom +coderJoinedRoom = + Json.object7 + { name = "JoinedRoom" + , description = [ "The room that the user has joined." ] + , init = JoinedRoom + } + (Json.field.optional.value + { fieldName = "account_data" + , toField = .accountData + , description = [ "The private data that this user has attached to this room." ] + , coder = coderAccountData + } + ) + (Json.field.optional.value + { fieldName = "ephemeral" + , toField = .ephemeral + , description = [ "The new ephemeral events in the room (events that aren’t recorded in the timeline or state of the room). In this version of the spec, these are typing notification and read receipt events." ] + , coder = coderEphemeral + } + ) + (Json.field.optional.value + { fieldName = "state" + , toField = .state + , description = [ "Updates to the state, between the time indicated by the since parameter, and the start of the timeline (or all state up to the start of the timeline, if since is not given, or full_state is true).", "N.B. state updates for m.room.member events will be incomplete if lazy_load_members is enabled in the /sync filter, and only return the member events required to display the senders of the timeline events in this response." ] + , coder = coderState + } + ) + (Json.field.optional.value + { fieldName = "summary" + , toField = .summary + , description = [ "Information about the room which clients may need to correctly render it to users." ] + , coder = coderRoomSummary + } + ) + (Json.field.optional.value + { fieldName = "timeline" + , toField = .timeline + , description = [ "The timeline of messages and state changes in the room." ] + , coder = coderTimeline + } + ) + (Json.field.optional.value + { fieldName = "unread_notifications" + , toField = .unreadNotifications + , description = [ "Counts of unread notifications for this room. See the Receiving notifications section for more information on how these are calculated.", "If unread_thread_notifications was specified as true on the RoomEventFilter, these counts will only be for the main timeline rather than all events in the room. See the threading module for more information.", "Changed in v1.4: Updated to reflect behaviour of having unread_thread_notifications as true in the RoomEventFilter for /sync." ] + , coder = coderUnreadNotificationCounts + } + ) + (Json.field.optional.value + { fieldName = "unread_thread_notifications" + , toField = .unreadThreadNotifications + , description = [ "If unread_thread_notifications was specified as true on the RoomEventFilter, the notification counts for each thread in this room. The object is keyed by thread root ID, with values matching unread_notifications.", "If a thread does not have any notifications it can be omitted from this object. If no threads have notification counts, this whole object can be omitted.", "Added in v1.4" ] + , coder = Json.fastDict coderThreadNotificationCounts + } + ) + + +coderEphemeral : Json.Coder Ephemeral +coderEphemeral = + Json.object1 + { name = "Ephemeral" + , description = [ "The new ephemeral events in the room." ] + , init = Ephemeral + } + (Json.field.optional.value + { fieldName = "events" + , toField = .events + , description = [ "List of events." ] + , coder = Json.list coderEvent + } + ) + + +coderState : Json.Coder State +coderState = + Json.object1 + { name = "State" + , description = [ "Updates to the state of the room." ] + , init = State + } + (Json.field.optional.value + { fieldName = "events" + , toField = .events + , description = [ "List of events." ] + , coder = Json.list coderClientEventWithoutRoomID + } + ) + + +coderClientEventWithoutRoomID : Json.Coder ClientEventWithoutRoomID +coderClientEventWithoutRoomID = + Json.object7 + { name = "ClientEventWithoutRoomID" + , description = [ "An event without the room ID." ] + , init = ClientEventWithoutRoomID + } + (Json.field.required + { fieldName = "content" + , toField = .content + , description = [ "The body of this event, as created by the client which sent it." ] + , coder = Json.value + } + ) + (Json.field.required + { fieldName = "event_id" + , toField = .eventId + , description = [ "The globally unique identifier for this event." ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "origin_server_ts" + , toField = .originServerTs + , description = [ "Timestamp (in milliseconds since the unix epoch) on originating homeserver when this event was sent." ] + , coder = Json.int + } + ) + (Json.field.required + { fieldName = "sender" + , toField = .sender + , description = [ "Contains the fully-qualified ID of the user who sent this event." ] + , coder = Json.string + } + ) + (Json.field.optional.value + { fieldName = "state_key" + , toField = .stateKey + , description = [ "Present if, and only if, this event is a state event. The key making this piece of state unique in the room. Note that it is often an empty string.", "State keys starting with an @ are reserved for referencing user IDs, such as room members. With the exception of a few events, state events set with a given user’s ID as the state key MUST only be set by that user." ] + , coder = Json.string + } + ) + (Json.field.required + { fieldName = "type" + , toField = .eventType + , description = [ "The type of the event." ] + , coder = Json.string + } + ) + (Json.field.optional.value + { fieldName = "unsigned" + , toField = .unsigned + , description = [ "Contains optional extra information about the event." ] + , coder = coderUnsignedData + } + ) + + +coderUnsignedData : Json.Coder UnsignedData +coderUnsignedData = + Json.object5 + { name = "UnsignedData" + , description = [ "Contains optional extra information about the event." ] + , init = + \a b c d e -> + UnsignedData + { age = a + , membership = b + , prevContent = c + , redactedBecause = d + , transactionId = e + } + } + (Json.field.optional.value + { fieldName = "age" + , toField = \(UnsignedData u) -> u.age + , description = [ "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." ] + , coder = Json.int + } + ) + (Json.field.optional.value + { fieldName = "membership" + , toField = \(UnsignedData u) -> u.membership + , description = [ "The room membership of the user making the request, at the time of the event.", "This property is the value of the membership property of the requesting user’s m.room.member state at the point of the event, including any changes caused by the event. If the user had yet to join the room at the time of the event (i.e, they have no m.room.member state), this property is set to leave.", "Homeservers SHOULD populate this property wherever practical, but they MAY omit it if necessary (for example, if calculating the value is expensive, servers might choose to only implement it in encrypted rooms). The property is not normally populated in events pushed to application services via the application service transaction API (where there is no clear definition of “requesting user”).", "Added in v1.11" ] + , coder = Json.string + } + ) + (Json.field.optional.value + { fieldName = "prev_content" + , toField = \(UnsignedData u) -> u.prevContent + , description = [ "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.", "Changed in v1.2: Previously, this field was specified at the top level of returned events rather than in unsigned (with the exception of the GET .../notifications endpoint), though in practice no known server implementations honoured this." ] + , coder = Json.value + } + ) + (Json.field.optional.value + { fieldName = "redacted_because" + , toField = \(UnsignedData u) -> u.redactedBecause + , description = [ "The event that redacted this event, if any." ] + , coder = Json.lazy (\_ -> coderClientEventWithoutRoomID) + } + ) + (Json.field.optional.value + { fieldName = "transaction_id" + , toField = \(UnsignedData u) -> u.transactionId + , description = [ "The client-supplied transaction ID, for example, provided via PUT /_matrix/client/v3/rooms/{roomId}/send/{eventType}/{txnId}, if the client being given the event is the same one which sent it." ] + , coder = Json.string + } + ) + + +coderRoomSummary : Json.Coder RoomSummary +coderRoomSummary = + Json.object3 + { name = "RoomSummary" + , description = [ "Information about the room which clients may need to correctly render it to users." ] + , init = RoomSummary + } + (Json.field.optional.value + { fieldName = "m.heroes" + , toField = .mHeroes + , description = [ "The users which can be used to generate a room name if the room does not have one. Required if the room’s m.room.name or m.room.canonical_alias state events are unset or empty.", "This should be the first 5 members of the room, ordered by stream ordering, which are joined or invited. The list must never include the client’s own user ID. When no joined or invited members are available, this should consist of the banned and left users. More than 5 members may be provided, however less than 5 should only be provided when there are less than 5 members to represent.", "When lazy-loading room members is enabled, the membership events for the heroes MUST be included in the state, unless they are redundant. When the list of users changes, the server notifies the client by sending a fresh list of heroes. If there are no changes since the last sync, this field may be omitted." ] + , coder = Json.list Json.string + } + ) + (Json.field.optional.value + { fieldName = "m.invited_member_count" + , toField = .mInvitedMemberCount + , description = [ "The number of users with membership of invite. If this field has not changed since the last sync, it may be omitted. Required otherwise." ] + , coder = Json.int + } + ) + (Json.field.optional.value + { fieldName = "m.joined_member_count" + , toField = .mJoinedMemberCount + , description = [ "The number of users with membership of join, including the client’s own user ID. If this field has not changed since the last sync, it may be omitted. Required otherwise." ] + , coder = Json.int + } + ) + + +coderTimeline : Json.Coder Timeline +coderTimeline = + Json.object3 + { name = "Timeline" + , description = [ "The timeline of messages and state changes in the room." ] + , init = Timeline + } + (Json.field.required + { fieldName = "events" + , toField = .events + , description = [ "List of events." ] + , coder = Json.list coderClientEventWithoutRoomID + } + ) + (Json.field.optional.value + { fieldName = "limited" + , toField = .limited + , description = [ "True if the number of events returned was limited by the limit on the filter." ] + , coder = Json.bool + } + ) + (Json.field.optional.value + { fieldName = "prev_batch" + , toField = .prevBatch + , description = [ "A token that can be supplied to the from parameter of the /rooms//messages endpoint in order to retrieve earlier events. If no earlier events are available, this property may be omitted from the response." ] + , coder = Json.string + } + ) + + +coderUnreadNotificationCounts : Json.Coder UnreadNotificationCounts +coderUnreadNotificationCounts = + Json.object2 + { name = "UnreadNotificationCounts" + , description = [ "Counts of unread notifications for this room." ] + , init = UnreadNotificationCounts + } + (Json.field.optional.value + { fieldName = "highlight_count" + , toField = .highlightCount + , description = [ "The number of unread notifications for this room with the highlight flag set." ] + , coder = Json.int + } + ) + (Json.field.optional.value + { fieldName = "notification_count" + , toField = .notificationCount + , description = [ "The total number of unread notifications for this room." ] + , coder = Json.int + } + ) + + +coderThreadNotificationCounts : Json.Coder ThreadNotificationCounts +coderThreadNotificationCounts = + Json.object2 + { name = "ThreadNotificationCounts" + , description = [ "The notification counts for each thread in this room." ] + , init = ThreadNotificationCounts + } + (Json.field.optional.value + { fieldName = "highlight_count" + , toField = .highlightCount + , description = [ "The number of unread notifications for this thread with the highlight flag set." ] + , coder = Json.int + } + ) + (Json.field.optional.value + { fieldName = "notification_count" + , toField = .notificationCount + , description = [ "The total number of unread notifications for this thread." ] + , coder = Json.int + } + ) + + +coderKnockedRoom : Json.Coder KnockedRoom +coderKnockedRoom = + Json.object1 + { name = "KnockedRoom" + , description = [ "The room that the user has knocked upon." ] + , init = KnockedRoom + } + (Json.field.optional.value + { fieldName = "knock_state" + , toField = .knockState + , description = [ "The stripped state of a room that the user has knocked upon." ] + , coder = coderKnockState + } + ) + + +coderKnockState : Json.Coder KnockState +coderKnockState = + Json.object1 + { name = "KnockState" + , description = [ "The stripped state of a room that the user has knocked upon." ] + , init = KnockState + } + (Json.field.optional.value + { fieldName = "events" + , toField = .events + , description = [ "The stripped state events that form the knock state." ] + , coder = Json.list coderStrippedStateEvent + } + ) + + +coderLeftRoom : Json.Coder LeftRoom +coderLeftRoom = + Json.object3 + { name = "LeftRoom" + , description = [ "The room that the user has left or been banned from." ] + , init = LeftRoom + } + (Json.field.optional.value + { fieldName = "account_data" + , toField = .accountData + , description = [ "The private data that this user has attached to this room." ] + , coder = coderAccountData + } + ) + (Json.field.optional.value + { fieldName = "state" + , toField = .state + , description = [ "The state updates for the room up to the start of the timeline." ] + , coder = coderState + } + ) + (Json.field.optional.value + { fieldName = "timeline" + , toField = .timeline + , description = [ "The timeline of messages and state changes in the room up to the point when the user left." ] + , coder = coderTimeline + } + ) + + +coderDeviceLists : Json.Coder DeviceLists +coderDeviceLists = + Json.object2 + { name = "DeviceLists" + , description = [ "Information on end-to-end device updates, as specified in End-to-end encryption." ] + , init = DeviceLists + } + (Json.field.optional.value + { fieldName = "changed" + , toField = .changed + , description = [ "List of users who have updated their device identity or cross-signing keys, or who now share an encrypted room with the client since the previous sync response." ] + , coder = Json.list Json.string + } + ) + (Json.field.optional.value + { fieldName = "left" + , toField = .left + , description = [ "List of users with whom we do not share any encrypted rooms anymore since the previous sync response." ] + , coder = Json.list Json.string + } + ) + + +coderToDevice : Json.Coder ToDevice +coderToDevice = + Json.object1 + { name = "ToDevice" + , description = [ "Information on the send-to-device messages for the client device, as defined in Send-to-Device messaging." ] + , init = ToDevice + } + (Json.field.optional.value + { fieldName = "events" + , toField = .events + , description = [ "List of send-to-device messages." ] + , coder = Json.list coderToDeviceEvent + } + ) + + +coderToDeviceEvent : Json.Coder ToDeviceEvent +coderToDeviceEvent = + Json.object3 + { name = "ToDeviceEvent" + , description = [ "A send-to-device event." ] + , init = ToDeviceEvent + } + (Json.field.optional.value + { fieldName = "content" + , toField = .content + , description = [ "The content of this event. The fields in this object will vary depending on the type of event." ] + , coder = Json.value + } + ) + (Json.field.optional.value + { fieldName = "sender" + , toField = .sender + , description = [ "The Matrix user ID of the user who sent this event." ] + , coder = Json.string + } + ) + (Json.field.optional.value + { fieldName = "type" + , toField = .eventType + , description = [ "The type of event." ] + , coder = Json.string + } + ) From b239eecc6bf3529c12caae70c3dec144882be76c Mon Sep 17 00:00:00 2001 From: Bram Date: Tue, 9 Jul 2024 00:08:46 +0200 Subject: [PATCH 15/20] Add toUpdate function for /sync v1 --- src/Internal/Api/Sync/V1.elm | 88 +++++++++++++++++++++++++++++- src/Internal/Config/Text.elm | 4 ++ src/Internal/Tools/DecodeExtra.elm | 36 +++++++++++- src/Internal/Tools/Json.elm | 79 ++++++++++++++++++++++++++- src/Internal/Values/Context.elm | 11 +++- src/Internal/Values/Envelope.elm | 4 ++ 6 files changed, 216 insertions(+), 6 deletions(-) diff --git a/src/Internal/Api/Sync/V1.elm b/src/Internal/Api/Sync/V1.elm index 8260d66..88f0dd0 100644 --- a/src/Internal/Api/Sync/V1.elm +++ b/src/Internal/Api/Sync/V1.elm @@ -11,10 +11,14 @@ This API module represents the /sync endpoint on Matrix spec version v1.1. -} -import FastDict exposing (Dict) +import FastDict as Dict exposing (Dict) +import Internal.Config.Log exposing (Log) import Internal.Tools.Json as Json import Internal.Tools.StrippedEvent as StrippedEvent exposing (StrippedEvent) import Internal.Tools.Timestamp as Timestamp exposing (Timestamp) +import Internal.Values.Envelope as E +import Internal.Values.Room as R +import Internal.Values.Vault as V type alias SyncResponse = @@ -807,3 +811,85 @@ coderToDeviceEvent = , coder = Json.string } ) + + +updateSyncResponse : SyncResponse -> ( E.EnvelopeUpdate V.VaultUpdate, List Log ) +updateSyncResponse response = + -- TODO: Add account data + -- TODO: Add device lists + -- Next batch + [ Just ( E.SetNextBatch response.nextBatch, [] ) + + -- TODO: Add presence + -- Rooms + , Maybe.map (updateRooms >> Tuple.mapFirst E.ContentUpdate) response.rooms + + -- TODO: Add to_device + ] + |> List.filterMap identity + |> List.unzip + |> Tuple.mapFirst E.More + |> Tuple.mapSecond List.concat + + +updateRooms : Rooms -> ( V.VaultUpdate, List Log ) +updateRooms rooms = + let + ( roomUpdate, roomLogs ) = + rooms.join + |> Maybe.withDefault Dict.empty + |> Dict.toList + |> List.map + (\( roomId, room ) -> + let + ( u, l ) = + updateJoinedRoom 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 : JoinedRoom -> ( R.RoomUpdate, List Log ) +updateJoinedRoom 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 + -- TODO: Add timeline + -- TODO: Add unread notifications + ] + , [] + ) diff --git a/src/Internal/Config/Text.elm b/src/Internal/Config/Text.elm index c40dd9a..3550be2 100644 --- a/src/Internal/Config/Text.elm +++ b/src/Internal/Config/Text.elm @@ -277,6 +277,7 @@ fields : , baseUrl : Desc , deviceId : Desc , experimental : Desc + , nextBatch : Desc , now : Desc , password : Desc , refreshToken : Desc @@ -388,6 +389,9 @@ fields = , experimental = [ "Experimental features supported by the homeserver." ] + , nextBatch = + [ "The batch token to supply in the since param of the next /sync request." + ] , now = [ "The most recently found timestamp." ] diff --git a/src/Internal/Tools/DecodeExtra.elm b/src/Internal/Tools/DecodeExtra.elm index 6460233..b7a0ae8 100644 --- a/src/Internal/Tools/DecodeExtra.elm +++ b/src/Internal/Tools/DecodeExtra.elm @@ -1,6 +1,6 @@ module Internal.Tools.DecodeExtra exposing ( opField, opFieldWithDefault - , map9, map10, map11 + , map9, map10, map11, map12 ) {-| @@ -18,7 +18,7 @@ This module contains helper functions that help decode JSON. ## Extended map functions -@docs map9, map10, map11 +@docs map9, map10, map11, map12 -} @@ -153,3 +153,35 @@ map11 func da db dc dd de df dg dh di dj dk = (D.map2 Tuple.pair df dg) (D.map2 Tuple.pair dh di) (D.map2 Tuple.pair dj dk) + + +{-| Try 12 decoders and combine the result. +-} +map12 : + (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> value) + -> D.Decoder a + -> D.Decoder b + -> D.Decoder c + -> D.Decoder d + -> D.Decoder e + -> D.Decoder f + -> D.Decoder g + -> D.Decoder h + -> D.Decoder i + -> D.Decoder j + -> D.Decoder k + -> D.Decoder l + -> D.Decoder value +map12 func da db dc dd de df dg dh di dj dk dl = + D.map8 + (\a b c d ( e, f ) ( g, h ) ( i, j ) ( k, l ) -> + func a b c d e f g h i j k l + ) + da + db + dc + dd + (D.map2 Tuple.pair de df) + (D.map2 Tuple.pair dg dh) + (D.map2 Tuple.pair di dj) + (D.map2 Tuple.pair dk dl) diff --git a/src/Internal/Tools/Json.elm b/src/Internal/Tools/Json.elm index 3ab93b3..2be4c1d 100644 --- a/src/Internal/Tools/Json.elm +++ b/src/Internal/Tools/Json.elm @@ -5,7 +5,7 @@ module Internal.Tools.Json exposing , Docs(..), RequiredField(..), toDocs , list, listWithOne, slowDict, fastDict, fastIntDict, set, maybe , Field, field, parser - , object1, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11 + , object1, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11, object12 ) {-| @@ -62,7 +62,7 @@ first. Once all fields are constructed, the user can create JSON objects. -@docs object1, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11 +@docs object1, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11, object12 -} @@ -1175,6 +1175,81 @@ object11 { name, description, init } fa fb fc fd fe ff fg fh fi fj fk = } +{-| Define an object with 12 keys +-} +object12 : + Descriptive { init : a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k -> l -> object } + -> Field a object + -> Field b object + -> Field c object + -> Field d object + -> Field e object + -> Field f object + -> Field g object + -> Field h object + -> Field i object + -> Field j object + -> Field k object + -> Field l object + -> Coder object +object12 { name, description, init } fa fb fc fd fe ff fg fh fi fj fk fl = + Coder + { encoder = + objectEncoder + [ toEncodeField fa + , toEncodeField fb + , toEncodeField fc + , toEncodeField fd + , toEncodeField fe + , toEncodeField ff + , toEncodeField fg + , toEncodeField fh + , toEncodeField fi + , toEncodeField fj + , toEncodeField fk + , toEncodeField fl + ] + , decoder = + D.map12 + (\( a, la ) ( b, lb ) ( c, lc ) ( d, ld ) ( e, le ) ( f, lf ) ( g, lg ) ( h, lh ) ( i, li ) ( j, lj ) ( k, lk ) ( l, ll ) -> + ( init a b c d e f g h i j k l + , List.concat [ la, lb, lc, ld, le, lf, lg, lh, li, lj, lk, ll ] + ) + ) + (toDecoderField fa) + (toDecoderField fb) + (toDecoderField fc) + (toDecoderField fd) + (toDecoderField fe) + (toDecoderField ff) + (toDecoderField fg) + (toDecoderField fh) + (toDecoderField fi) + (toDecoderField fj) + (toDecoderField fk) + (toDecoderField fl) + , docs = + DocsObject + { name = name + , description = description + , keys = + [ toDocsField fa + , toDocsField fb + , toDocsField fc + , toDocsField fd + , toDocsField fe + , toDocsField ff + , toDocsField fg + , toDocsField fh + , toDocsField fi + , toDocsField fj + , toDocsField fk + , toDocsField fl + ] + } + } + + {-| Define a parser that converts a string into a custom Elm type. -} parser : { name : String, p : P.Parser ( a, List Log ), toString : a -> String } -> Coder a diff --git a/src/Internal/Values/Context.elm b/src/Internal/Values/Context.elm index ca6a8a1..5fc358d 100644 --- a/src/Internal/Values/Context.elm +++ b/src/Internal/Values/Context.elm @@ -95,6 +95,7 @@ type alias Context = { accessTokens : Hashdict AccessToken , baseUrl : Maybe String , deviceId : Maybe String + , nextBatch : Maybe String , now : Maybe Timestamp , password : Maybe String , refreshToken : Maybe String @@ -152,7 +153,7 @@ fromApiFormat (APIContext c) = -} coder : Json.Coder Context coder = - Json.object11 + Json.object12 { name = Text.docs.context.name , description = Text.docs.context.description , init = Context @@ -178,6 +179,13 @@ coder = , coder = Json.string } ) + (Json.field.optional.value + { fieldName = "nextBatch" + , toField = .nextBatch + , description = Text.fields.context.nextBatch + , coder = Json.string + } + ) (Json.field.optional.value { fieldName = "now" , toField = .now @@ -303,6 +311,7 @@ init sn = { accessTokens = Hashdict.empty .value , baseUrl = Nothing , deviceId = Nothing + , nextBatch = Nothing , now = Nothing , refreshToken = Nothing , password = Nothing diff --git a/src/Internal/Values/Envelope.elm b/src/Internal/Values/Envelope.elm index 7ae8ceb..c9cc126 100644 --- a/src/Internal/Values/Envelope.elm +++ b/src/Internal/Values/Envelope.elm @@ -82,6 +82,7 @@ type EnvelopeUpdate a | SetAccessToken AccessToken | SetBaseUrl String | SetDeviceId String + | SetNextBatch String | SetNow Timestamp | SetRefreshToken String | SetVersions Versions @@ -327,6 +328,9 @@ update updateContent eu ({ context } as data) = SetDeviceId d -> { data | context = { context | deviceId = Just d } } + SetNextBatch nextBatch -> + { data | context = { context | nextBatch = Just nextBatch } } + SetNow n -> { data | context = { context | now = Just n } } From c5d07f0a94c270d185c5241a3a76ed9e6b0b1225 Mon Sep 17 00:00:00 2001 From: Bram Date: Tue, 9 Jul 2024 13:29:45 +0200 Subject: [PATCH 16/20] Add toUpdate function for sync V1 - V3 --- src/Internal/Api/Sync/V1.elm | 120 ++++++++++++++++---- src/Internal/Api/Sync/V2.elm | 212 +++++++++++++++++++++++++++++++++-- src/Internal/Api/Sync/V3.elm | 147 +++++++++++++++++++++++- src/Internal/Config/Text.elm | 3 + 4 files changed, 449 insertions(+), 33 deletions(-) diff --git a/src/Internal/Api/Sync/V1.elm b/src/Internal/Api/Sync/V1.elm index 88f0dd0..cd71914 100644 --- a/src/Internal/Api/Sync/V1.elm +++ b/src/Internal/Api/Sync/V1.elm @@ -12,12 +12,16 @@ This API module represents the /sync endpoint on Matrix spec version v1.1. -} import FastDict as Dict exposing (Dict) -import Internal.Config.Log exposing (Log) +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.StrippedEvent as StrippedEvent exposing (StrippedEvent) +import Internal.Tools.StrippedEvent as StrippedEvent 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 @@ -64,7 +68,7 @@ type alias InviteState = type alias StrippedState = { content : Json.Value - , sender : String + , sender : User , stateKey : String , eventType : String } @@ -93,7 +97,7 @@ type alias SyncStateEvent = , eventId : String , originServerTs : Timestamp , prevContent : Maybe Json.Value - , sender : String + , sender : User , stateKey : String , eventType : String , unsigned : Maybe UnsignedData @@ -125,7 +129,7 @@ type alias SyncRoomEvent = { content : Json.Value , eventId : String , originServerTs : Timestamp - , sender : String + , sender : User , eventType : String , unsigned : Maybe UnsignedData } @@ -164,7 +168,7 @@ type alias ToDevice = type alias ToDeviceEvent = { content : Maybe Json.Value - , sender : Maybe String + , sender : Maybe User , eventType : Maybe String } @@ -351,7 +355,7 @@ coderStrippedState = { fieldName = "sender" , toField = .sender , description = [ "The sender for the event." ] - , coder = Json.string + , coder = User.coder } ) (Json.field.required @@ -492,7 +496,7 @@ coderSyncStateEvent = { fieldName = "sender" , toField = .sender , description = [ "Contains the fully-qualified ID of the user who sent this event." ] - , coder = Json.string + , coder = User.coder } ) (Json.field.required @@ -640,7 +644,7 @@ coderSyncRoomEvent = { fieldName = "sender" , toField = .sender , description = [ "Contains the fully-qualified ID of the user who sent this event." ] - , coder = Json.string + , coder = User.coder } ) (Json.field.required @@ -801,7 +805,7 @@ coderToDeviceEvent = { fieldName = "sender" , toField = .sender , description = [ "The Matrix user ID of the user who sent this event." ] - , coder = Json.string + , coder = User.coder } ) (Json.field.optional.value @@ -813,16 +817,37 @@ coderToDeviceEvent = ) -updateSyncResponse : SyncResponse -> ( E.EnvelopeUpdate V.VaultUpdate, List Log ) -updateSyncResponse response = - -- TODO: Add account data +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, [] ) + , Just ( E.SetNextBatch response.nextBatch, [] ) -- TODO: Add presence -- Rooms - , Maybe.map (updateRooms >> Tuple.mapFirst E.ContentUpdate) response.rooms + , Maybe.map + (updateRooms { filter = filter, nextBatch = response.nextBatch, since = since } + >> Tuple.mapFirst E.ContentUpdate + ) + response.rooms -- TODO: Add to_device ] @@ -832,8 +857,8 @@ updateSyncResponse response = |> Tuple.mapSecond List.concat -updateRooms : Rooms -> ( V.VaultUpdate, List Log ) -updateRooms rooms = +updateRooms : { filter : Filter, nextBatch : String, since : Maybe String } -> Rooms -> ( V.VaultUpdate, List Log ) +updateRooms { filter, nextBatch, since } rooms = let ( roomUpdate, roomLogs ) = rooms.join @@ -843,7 +868,13 @@ updateRooms rooms = (\( roomId, room ) -> let ( u, l ) = - updateJoinedRoom room + updateJoinedRoom + { filter = filter + , nextBatch = nextBatch + , roomId = roomId + , since = since + } + room in ( V.MapRoom roomId u, l ) ) @@ -869,8 +900,8 @@ updateRooms rooms = ) -updateJoinedRoom : JoinedRoom -> ( R.RoomUpdate, List Log ) -updateJoinedRoom room = +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 @@ -888,8 +919,55 @@ updateJoinedRoom room = -- TODO: Add state -- TODO: Add RoomSummary - -- TODO: Add timeline + , room.timeline + |> Maybe.andThen + (updateTimeline data) + |> R.Optional + -- 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 + , prevContent = Nothing + , redactedBecause = Nothing + , transactionId = u.transactionId + } diff --git a/src/Internal/Api/Sync/V2.elm b/src/Internal/Api/Sync/V2.elm index cfbf29c..01b6387 100644 --- a/src/Internal/Api/Sync/V2.elm +++ b/src/Internal/Api/Sync/V2.elm @@ -13,9 +13,19 @@ v1.3. -} -import FastDict exposing (Dict) +import FastDict as Dict exposing (Dict) 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.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 = @@ -62,7 +72,7 @@ type alias InviteState = type alias StrippedStateEvent = { content : Json.Value - , sender : String + , sender : User , stateKey : String , eventType : String } @@ -89,8 +99,8 @@ type alias State = type alias ClientEventWithoutRoomID = { content : Json.Value , eventId : String - , originServerTs : Int - , sender : String + , originServerTs : Timestamp + , sender : User , stateKey : Maybe String , eventType : String , unsigned : Maybe UnsignedData @@ -153,7 +163,7 @@ type alias ToDevice = type alias ToDeviceEvent = { content : Maybe Json.Value - , sender : Maybe String + , sender : Maybe User , eventType : Maybe String } @@ -387,14 +397,14 @@ coderClientEventWithoutRoomID = { fieldName = "origin_server_ts" , toField = .originServerTs , description = [ "Required: Timestamp (in milliseconds since the unix epoch) on originating homeserver when this event was sent." ] - , coder = Json.int + , coder = Timestamp.coder } ) (Json.field.required { fieldName = "sender" , toField = .sender , description = [ "Required: Contains the fully-qualified ID of the user who sent this event." ] - , coder = Json.string + , coder = User.coder } ) (Json.field.optional.value @@ -557,3 +567,191 @@ coderToDevice = coderToDeviceEvent : Json.Coder ToDeviceEvent 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 + , prevContent = Nothing + , redactedBecause = Just e + , transactionId = Nothing + } + |> Event.UnsignedData + |> Just + + ( _, Just (UnsignedData u) ) -> + { age = u.age + , prevContent = u.prevContent + , redactedBecause = ev + , transactionId = u.transactionId + } + |> Event.UnsignedData + |> Just diff --git a/src/Internal/Api/Sync/V3.elm b/src/Internal/Api/Sync/V3.elm index 676a1f1..743a589 100644 --- a/src/Internal/Api/Sync/V3.elm +++ b/src/Internal/Api/Sync/V3.elm @@ -18,9 +18,18 @@ versions: -} -import FastDict exposing (Dict) +import FastDict as Dict exposing (Dict) 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.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 = @@ -67,7 +76,7 @@ type alias InviteState = type alias StrippedStateEvent = { content : Json.Value - , sender : String + , sender : User , stateKey : String , eventType : String } @@ -95,8 +104,8 @@ type alias State = type alias ClientEventWithoutRoomID = { content : Json.Value , eventId : String - , originServerTs : Int - , sender : String + , originServerTs : Timestamp + , sender : User , stateKey : Maybe String , eventType : String , unsigned : Maybe UnsignedData @@ -160,7 +169,7 @@ type alias ToDevice = type alias ToDeviceEvent = { content : Maybe Json.Value - , sender : Maybe String + , sender : Maybe User , eventType : Maybe String } @@ -441,3 +450,131 @@ coderToDevice = coderToDeviceEvent : Json.Coder ToDeviceEvent 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 diff --git a/src/Internal/Config/Text.elm b/src/Internal/Config/Text.elm index 3550be2..122a37a 100644 --- a/src/Internal/Config/Text.elm +++ b/src/Internal/Config/Text.elm @@ -638,6 +638,7 @@ logs : , sendEvent : Maybe String -> String , serverReturnedInvalidJSON : String -> String , serverReturnedUnknownJSON : String -> String + , syncAccountDataFound : Int -> String } logs = { baseUrlFound = @@ -675,6 +676,8 @@ logs = "Sent event, event id not known - make sure to check transaction id" , serverReturnedInvalidJSON = (++) "The server returned invalid JSON: " , 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" ] } From 632158f3099e9b2e3f949140116232dead9c58e1 Mon Sep 17 00:00:00 2001 From: Bram Date: Tue, 9 Jul 2024 16:48:53 +0200 Subject: [PATCH 17/20] Add toUpdate function for sync V4 --- src/Internal/Api/Sync/V1.elm | 1 + src/Internal/Api/Sync/V2.elm | 2 + src/Internal/Api/Sync/V4.elm | 219 ++++++++++++++++++++++++++++++++-- src/Internal/Config/Text.elm | 4 + src/Internal/Values/Event.elm | 12 +- 5 files changed, 227 insertions(+), 11 deletions(-) diff --git a/src/Internal/Api/Sync/V1.elm b/src/Internal/Api/Sync/V1.elm index cd71914..628f727 100644 --- a/src/Internal/Api/Sync/V1.elm +++ b/src/Internal/Api/Sync/V1.elm @@ -967,6 +967,7 @@ toUnsigned : UnsignedData -> Event.UnsignedData toUnsigned u = Event.UnsignedData { age = u.age + , membership = Nothing , prevContent = Nothing , redactedBecause = Nothing , transactionId = u.transactionId diff --git a/src/Internal/Api/Sync/V2.elm b/src/Internal/Api/Sync/V2.elm index 01b6387..304a079 100644 --- a/src/Internal/Api/Sync/V2.elm +++ b/src/Internal/Api/Sync/V2.elm @@ -740,6 +740,7 @@ toUnsigned ev unsigned = ( Just e, Nothing ) -> { age = Nothing + , membership = Nothing , prevContent = Nothing , redactedBecause = Just e , transactionId = Nothing @@ -749,6 +750,7 @@ toUnsigned ev unsigned = ( _, Just (UnsignedData u) ) -> { age = u.age + , membership = Nothing , prevContent = u.prevContent , redactedBecause = ev , transactionId = u.transactionId diff --git a/src/Internal/Api/Sync/V4.elm b/src/Internal/Api/Sync/V4.elm index 7d67b2f..f254401 100644 --- a/src/Internal/Api/Sync/V4.elm +++ b/src/Internal/Api/Sync/V4.elm @@ -11,9 +11,19 @@ This API module represents the /sync endpoint on Matrix spec version v1.11. -} -import FastDict exposing (Dict) +import FastDict as Dict exposing (Dict) 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.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 = @@ -60,7 +70,7 @@ type alias InviteState = type alias StrippedStateEvent = { content : Json.Value - , sender : String + , sender : User , stateKey : String , eventType : String } @@ -88,8 +98,8 @@ type alias State = type alias ClientEventWithoutRoomID = { content : Json.Value , eventId : String - , originServerTs : Int - , sender : String + , originServerTs : Timestamp + , sender : User , stateKey : Maybe String , eventType : String , unsigned : Maybe UnsignedData @@ -159,7 +169,7 @@ type alias ToDevice = type alias ToDeviceEvent = { content : Maybe Json.Value - , sender : Maybe String + , sender : Maybe User , eventType : Maybe String } @@ -371,7 +381,7 @@ coderStrippedStateEvent = { fieldName = "sender" , toField = .sender , description = [ "The sender for the event." ] - , coder = Json.string + , coder = User.coder } ) (Json.field.required @@ -505,14 +515,14 @@ coderClientEventWithoutRoomID = { fieldName = "origin_server_ts" , toField = .originServerTs , description = [ "Timestamp (in milliseconds since the unix epoch) on originating homeserver when this event was sent." ] - , coder = Json.int + , coder = Timestamp.coder } ) (Json.field.required { fieldName = "sender" , toField = .sender , description = [ "Contains the fully-qualified ID of the user who sent this event." ] - , coder = Json.string + , coder = User.coder } ) (Json.field.optional.value @@ -815,7 +825,7 @@ coderToDeviceEvent = { fieldName = "sender" , toField = .sender , description = [ "The Matrix user ID of the user who sent this event." ] - , coder = Json.string + , coder = User.coder } ) (Json.field.optional.value @@ -825,3 +835,194 @@ coderToDeviceEvent = , 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 diff --git a/src/Internal/Config/Text.elm b/src/Internal/Config/Text.elm index 122a37a..7a0326e 100644 --- a/src/Internal/Config/Text.elm +++ b/src/Internal/Config/Text.elm @@ -348,6 +348,7 @@ fields : } , unsigned : { age : Desc + , membership : Desc , prevContent : Desc , redactedBecause : Desc , transactionId : Desc @@ -563,6 +564,9 @@ fields = { 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." ] + , membership = + [ "The room membership of the user making the request, at the time of the event." + ] , 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." ] diff --git a/src/Internal/Values/Event.elm b/src/Internal/Values/Event.elm index 2ee528a..2f54a13 100644 --- a/src/Internal/Values/Event.elm +++ b/src/Internal/Values/Event.elm @@ -59,6 +59,7 @@ helper functions. type UnsignedData = UnsignedData { age : Maybe Int + , membership : Maybe String , prevContent : Maybe Json.Value , redactedBecause : Maybe Event , transactionId : Maybe String @@ -242,10 +243,10 @@ transactionId event = unsignedCoder : Json.Coder UnsignedData unsignedCoder = - Json.object4 + Json.object5 { name = Text.docs.unsigned.name , description = Text.docs.unsigned.description - , init = \a b c d -> UnsignedData { age = a, prevContent = b, redactedBecause = c, transactionId = d } + , init = \a b c d e -> UnsignedData { age = a, membership = b, prevContent = c, redactedBecause = d, transactionId = e } } (Json.field.optional.value { fieldName = "age" @@ -254,6 +255,13 @@ unsignedCoder = , 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 { fieldName = "prevContent" , toField = \(UnsignedData data) -> data.prevContent From 0978e43fc0c253739ff8239e9b90b55a1f439da6 Mon Sep 17 00:00:00 2001 From: Bram Date: Tue, 9 Jul 2024 16:55:19 +0200 Subject: [PATCH 18/20] Finish /sync API definition --- src/Internal/Api/Sync/Api.elm | 119 +++++++++++++++++++++++++++++----- 1 file changed, 104 insertions(+), 15 deletions(-) diff --git a/src/Internal/Api/Sync/Api.elm b/src/Internal/Api/Sync/Api.elm index e51d35f..e18092e 100644 --- a/src/Internal/Api/Sync/Api.elm +++ b/src/Internal/Api/Sync/Api.elm @@ -16,9 +16,30 @@ communicate with the Matrix server about the Vault's needs. import Internal.Api.Api as A import Internal.Api.Request as R 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 +{-| 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 -- and assume that every client always wants to receive all events. @@ -38,6 +59,8 @@ type alias PhantomV1 a = type PresenceV1 = OfflineV1 + | OnlineV1 + | UnavailableV1 type alias SyncInput = @@ -59,10 +82,17 @@ type alias SyncInputV1 a = } -sync : SyncInput -> A.TaskChain (Phantom a) (Phantom a) -sync = - A.startWithVersion "r0.0.0" syncV1 - |> A.versionChain +presenceV1ToString : PresenceV1 -> String +presenceV1ToString p = + case p of + OfflineV1 -> + "offline" + + OnlineV1 -> + "online" + + UnavailableV1 -> + "unavailable" syncV1 : SyncInputV1 i -> A.TaskChain (PhantomV1 a) (PhantomV1 a) @@ -71,24 +101,83 @@ syncV1 data = { attributes = [ R.accessToken , R.queryOpString "filter" Nothing -- FILTER HERE - , R.queryOpString "since" data.since , R.queryOpBool "full_state" data.fullState , data.presenceV1 - |> Maybe.map (always "offline") + |> Maybe.map presenceV1ToString |> R.queryOpString "set_presence" + , R.queryOpString "since" data.since , R.queryOpInt "timeout" data.timeout ] - , coder = V1.syncResponseCoder + , coder = V1.coderSyncResponse , contextChange = always identity , method = "GET" , path = [ "_matrix", "client", "r0", "sync" ] , toUpdate = - \out -> - ( V1.syncResponseToUpdate - { filter = Filter.pass -- FILTER HERE - , since = data.since - } - out - , [] - ) + V1.updateSyncResponse { filter = Filter.pass, since = data.since } + } + + +syncV2 : SyncInputV1 i -> A.TaskChain (PhantomV1 a) (PhantomV1 a) +syncV2 data = + A.request + { attributes = + [ R.accessToken + , R.queryOpString "filter" Nothing + , R.queryOpBool "full_state" data.fullState + , data.presenceV1 + |> Maybe.map presenceV1ToString + |> 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.presenceV1 + |> Maybe.map presenceV1ToString + |> 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.presenceV1 + |> Maybe.map presenceV1ToString + |> 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 } } From 4e378a5f500519c2e0cce2a360a1d2ced68d9b1b Mon Sep 17 00:00:00 2001 From: Bram Date: Tue, 9 Jul 2024 17:33:41 +0200 Subject: [PATCH 19/20] Add /sync as task --- src/Internal/Api/Main.elm | 23 +++++++++++++-- src/Internal/Api/Sync/Api.elm | 49 ++++++++++++++------------------ src/Internal/Api/Task.elm | 14 +++++++-- src/Internal/Config/Text.elm | 4 +++ src/Internal/Values/Settings.elm | 11 ++++++- 5 files changed, 68 insertions(+), 33 deletions(-) diff --git a/src/Internal/Api/Main.elm b/src/Internal/Api/Main.elm index 9807572..04011d0 100644 --- a/src/Internal/Api/Main.elm +++ b/src/Internal/Api/Main.elm @@ -1,6 +1,6 @@ module Internal.Api.Main exposing ( Msg - , sendMessageEvent + , sendMessageEvent, sync ) {-| @@ -18,7 +18,7 @@ This module is used as reference for getting ## Actions -@docs sendMessageEvent +@docs sendMessageEvent, sync -} @@ -57,3 +57,22 @@ sendMessageEvent env data = } ) (Context.apiFormat env.context) + + +{-| Sync with the Matrix API to stay up-to-date. +-} +sync : + E.Envelope a + -> { timeout : Int, 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 data.timeout + } + ) + (Context.apiFormat env.context) diff --git a/src/Internal/Api/Sync/Api.elm b/src/Internal/Api/Sync/Api.elm index e18092e..59a622a 100644 --- a/src/Internal/Api/Sync/Api.elm +++ b/src/Internal/Api/Sync/Api.elm @@ -1,4 +1,4 @@ -module Internal.Api.Sync.Api exposing (..) +module Internal.Api.Sync.Api exposing (sync, Phantom) {-| @@ -9,7 +9,7 @@ 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 communicate with the Matrix server about the Vault's needs. -@docs Phantom +@docs sync, Phantom -} @@ -57,16 +57,10 @@ type alias PhantomV1 a = { a | accessToken : (), baseUrl : () } -type PresenceV1 - = OfflineV1 - | OnlineV1 - | UnavailableV1 - - type alias SyncInput = { -- filter : FilterV1, fullState : Maybe Bool - , presenceV1 : Maybe PresenceV1 + , presence : Maybe String , since : Maybe String , timeout : Maybe Int } @@ -77,22 +71,21 @@ type alias SyncInputV1 a = | -- filter : FilterV1 , since : Maybe String , fullState : Maybe Bool - , presenceV1 : Maybe PresenceV1 + , presence : Maybe String , timeout : Maybe Int } -presenceV1ToString : PresenceV1 -> String -presenceV1ToString p = - case p of - OfflineV1 -> - "offline" +presenceFromOptions : List String -> Maybe String -> Maybe String +presenceFromOptions options = + Maybe.andThen + (\v -> + if List.member v options then + Just v - OnlineV1 -> - "online" - - UnavailableV1 -> - "unavailable" + else + Nothing + ) syncV1 : SyncInputV1 i -> A.TaskChain (PhantomV1 a) (PhantomV1 a) @@ -102,8 +95,8 @@ syncV1 data = [ R.accessToken , R.queryOpString "filter" Nothing -- FILTER HERE , R.queryOpBool "full_state" data.fullState - , data.presenceV1 - |> Maybe.map presenceV1ToString + , data.presence + |> presenceFromOptions [ "offline", "online", "unavailable" ] |> R.queryOpString "set_presence" , R.queryOpString "since" data.since , R.queryOpInt "timeout" data.timeout @@ -124,8 +117,8 @@ syncV2 data = [ R.accessToken , R.queryOpString "filter" Nothing , R.queryOpBool "full_state" data.fullState - , data.presenceV1 - |> Maybe.map presenceV1ToString + , data.presence + |> presenceFromOptions [ "offline", "online", "unavailable" ] |> R.queryOpString "set_presence" , R.queryOpString "since" data.since , R.queryOpInt "timeout" data.timeout @@ -146,8 +139,8 @@ syncV3 data = [ R.accessToken , R.queryOpString "filter" Nothing , R.queryOpBool "full_state" data.fullState - , data.presenceV1 - |> Maybe.map presenceV1ToString + , data.presence + |> presenceFromOptions [ "offline", "online", "unavailable" ] |> R.queryOpString "set_presence" , R.queryOpString "since" data.since , R.queryOpInt "timeout" data.timeout @@ -168,8 +161,8 @@ syncV4 data = [ R.accessToken , R.queryOpString "filter" Nothing , R.queryOpBool "full_state" data.fullState - , data.presenceV1 - |> Maybe.map presenceV1ToString + , data.presence + |> presenceFromOptions [ "offline", "online", "unavailable" ] |> R.queryOpString "set_presence" , R.queryOpString "since" data.since , R.queryOpInt "timeout" data.timeout diff --git a/src/Internal/Api/Task.elm b/src/Internal/Api/Task.elm index 744b1d2..81205da 100644 --- a/src/Internal/Api/Task.elm +++ b/src/Internal/Api/Task.elm @@ -1,6 +1,6 @@ module Internal.Api.Task exposing ( Task, run, Backpack - , sendMessageEvent + , sendMessageEvent, sync ) {-| @@ -23,7 +23,7 @@ up-to-date. ## Tasks -@docs sendMessageEvent +@docs sendMessageEvent, sync -} @@ -33,6 +33,7 @@ import Internal.Api.LoginWithUsernameAndPassword.Api import Internal.Api.Now.Api import Internal.Api.Request as Request import Internal.Api.SendMessageEvent.Api +import Internal.Api.Sync.Api import Internal.Api.Versions.Api import Internal.Config.Log exposing (Log, log) import Internal.Tools.Json as Json @@ -217,6 +218,15 @@ sendMessageEvent input = |> 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. -} run : (Backpack -> msg) -> Task -> APIContext {} -> Cmd msg diff --git a/src/Internal/Config/Text.elm b/src/Internal/Config/Text.elm index 7a0326e..08d5d1b 100644 --- a/src/Internal/Config/Text.elm +++ b/src/Internal/Config/Text.elm @@ -330,6 +330,7 @@ fields : , settings : { currentVersion : Desc , deviceName : Desc + , presence : Desc , removePasswordOnLogin : Desc , syncTime : Desc } @@ -519,6 +520,9 @@ fields = , deviceName = [ "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 = [ "Remove the password as soon as a valid access token has been received." ] diff --git a/src/Internal/Values/Settings.elm b/src/Internal/Values/Settings.elm index d0f40c4..69e1e70 100644 --- a/src/Internal/Values/Settings.elm +++ b/src/Internal/Values/Settings.elm @@ -35,6 +35,7 @@ behave under the user's preferred settings. type alias Settings = { currentVersion : String , deviceName : String + , presence : Maybe String , removePasswordOnLogin : Bool , syncTime : Int } @@ -44,7 +45,7 @@ type alias Settings = -} coder : Json.Coder Settings coder = - Json.object4 + Json.object5 { name = Text.docs.settings.name , description = Text.docs.settings.description , init = Settings @@ -65,6 +66,13 @@ coder = , 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 { fieldName = "removePasswordOnLogin" , toField = .removePasswordOnLogin @@ -103,6 +111,7 @@ init : Settings init = { currentVersion = Default.currentVersion , deviceName = Default.deviceName + , presence = Nothing , removePasswordOnLogin = Default.removePasswordOnLogin , syncTime = Default.syncTime } From 005e103389e9dbf9d7bc5de03e0edbb4007c4515 Mon Sep 17 00:00:00 2001 From: Bram Date: Tue, 9 Jul 2024 17:50:48 +0200 Subject: [PATCH 20/20] Expose sync function --- src/Internal/Api/Main.elm | 4 ++-- src/Matrix.elm | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Internal/Api/Main.elm b/src/Internal/Api/Main.elm index 04011d0..bf9b1ea 100644 --- a/src/Internal/Api/Main.elm +++ b/src/Internal/Api/Main.elm @@ -63,7 +63,7 @@ sendMessageEvent env data = -} sync : E.Envelope a - -> { timeout : Int, toMsg : Msg -> msg } + -> { toMsg : Msg -> msg } -> Cmd msg sync env data = ITask.run @@ -72,7 +72,7 @@ sync env data = { fullState = Nothing , presence = env.settings.presence , since = env.context.nextBatch - , timeout = Just data.timeout + , timeout = Just env.settings.syncTime } ) (Context.apiFormat env.context) diff --git a/src/Matrix.elm b/src/Matrix.elm index f59136a..64f411f 100644 --- a/src/Matrix.elm +++ b/src/Matrix.elm @@ -1,6 +1,6 @@ module Matrix exposing ( Vault, fromUserId, fromUsername - , VaultUpdate, update + , VaultUpdate, update, sync , addAccessToken, sendMessageEvent ) @@ -24,7 +24,7 @@ support a monolithic public registry. (: ## Keeping the Vault up-to-date -@docs VaultUpdate, update +@docs VaultUpdate, update, sync ## Debugging @@ -141,6 +141,17 @@ 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. This allows us to change our perception of the Matrix environment: has anyone