Add optionals where required.
This commit is contained in:
parent
8bc4ba83bc
commit
c292a4c29b
|
@ -3,6 +3,12 @@ import Dict exposing (Dict)
|
||||||
import Json.Decode as Decode exposing (Decoder, int, string, float, list, value, dict, bool)
|
import Json.Decode as Decode exposing (Decoder, int, string, float, list, value, dict, bool)
|
||||||
import Json.Decode.Pipeline exposing (required, optional)
|
import Json.Decode.Pipeline exposing (required, optional)
|
||||||
|
|
||||||
|
decodeJust : Decoder a -> Decoder (Maybe a)
|
||||||
|
decodeJust = Decode.map Just
|
||||||
|
|
||||||
|
maybeDecode : String -> Decoder a -> Decoder (Maybe a -> b) -> Decoder b
|
||||||
|
maybeDecode s d = optional s (decodeJust d) Nothing
|
||||||
|
|
||||||
-- General Events
|
-- General Events
|
||||||
type alias Event =
|
type alias Event =
|
||||||
{ content : Decode.Value
|
{ content : Decode.Value
|
||||||
|
@ -16,47 +22,47 @@ eventDecoder =
|
||||||
|> required "type" string
|
|> required "type" string
|
||||||
|
|
||||||
type alias EventContent =
|
type alias EventContent =
|
||||||
{ avatarUrl : String
|
{ avatarUrl : Maybe String
|
||||||
, displayname : String
|
, displayname : Maybe String
|
||||||
, membership : String
|
, membership : String
|
||||||
, isDirect : Bool
|
, isDirect : Maybe Bool
|
||||||
-- , thirdPartyInvite : Invite
|
-- , thirdPartyInvite : Invite
|
||||||
, unsigned : UnsignedData
|
, unsigned : Maybe UnsignedData
|
||||||
}
|
}
|
||||||
|
|
||||||
eventContentDecoder : Decoder EventContent
|
eventContentDecoder : Decoder EventContent
|
||||||
eventContentDecoder =
|
eventContentDecoder =
|
||||||
Decode.succeed EventContent
|
Decode.succeed EventContent
|
||||||
|> required "avatar_url" string
|
|> maybeDecode "avatar_url" string
|
||||||
|> required "displayname" string
|
|> maybeDecode "displayname" string
|
||||||
|> required "membership" string
|
|> required "membership" string
|
||||||
|> required "is_direct" bool
|
|> maybeDecode "is_direct" bool
|
||||||
-- |> required "third_party_invite" inviteDecoder
|
-- |> required "third_party_invite" inviteDecoder
|
||||||
|> required "unsigned" unsignedDataDecoder
|
|> maybeDecode "unsigned" unsignedDataDecoder
|
||||||
|
|
||||||
type alias State =
|
|
||||||
{ events : List StateEvent
|
|
||||||
}
|
|
||||||
|
|
||||||
-- Unsigned Data
|
-- Unsigned Data
|
||||||
type alias UnsignedData =
|
type alias UnsignedData =
|
||||||
{ age : Int
|
{ age : Maybe Int
|
||||||
, redactedBecause : Event
|
, redactedBecause : Maybe Event
|
||||||
, transactionId : String
|
, transactionId : Maybe String
|
||||||
}
|
}
|
||||||
|
|
||||||
unsignedDataDecoder : Decoder UnsignedData
|
unsignedDataDecoder : Decoder UnsignedData
|
||||||
unsignedDataDecoder =
|
unsignedDataDecoder =
|
||||||
Decode.succeed UnsignedData
|
Decode.succeed UnsignedData
|
||||||
|> required "age" int
|
|> maybeDecode "age" int
|
||||||
|> required "redacted_because" eventDecoder
|
|> maybeDecode "redacted_because" eventDecoder
|
||||||
|> required "transaction_id" string
|
|> maybeDecode "transaction_id" string
|
||||||
|
|
||||||
-- State
|
-- State
|
||||||
|
type alias State =
|
||||||
|
{ events : Maybe (List StateEvent)
|
||||||
|
}
|
||||||
|
|
||||||
stateDecoder : Decoder State
|
stateDecoder : Decoder State
|
||||||
stateDecoder =
|
stateDecoder =
|
||||||
Decode.succeed State
|
Decode.succeed State
|
||||||
|> required "events" (list stateEventDecoder)
|
|> maybeDecode "events" (list stateEventDecoder)
|
||||||
|
|
||||||
type alias StateEvent =
|
type alias StateEvent =
|
||||||
{ content : Decode.Value
|
{ content : Decode.Value
|
||||||
|
@ -64,8 +70,8 @@ type alias StateEvent =
|
||||||
, eventId : String
|
, eventId : String
|
||||||
, sender : String
|
, sender : String
|
||||||
, originServerTs : Int
|
, originServerTs : Int
|
||||||
, unsigned : UnsignedData
|
, unsigned : Maybe UnsignedData
|
||||||
, prevContent : EventContent
|
, prevContent : Maybe EventContent
|
||||||
, stateKey : String
|
, stateKey : String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,54 +83,54 @@ stateEventDecoder =
|
||||||
|> required "event_id" string
|
|> required "event_id" string
|
||||||
|> required "sender" string
|
|> required "sender" string
|
||||||
|> required "origin_server_ts" int
|
|> required "origin_server_ts" int
|
||||||
|> required "unsigned" unsignedDataDecoder
|
|> maybeDecode "unsigned" unsignedDataDecoder
|
||||||
|> required "prev_content" eventContentDecoder
|
|> maybeDecode "prev_content" eventContentDecoder
|
||||||
|> required "sate_key" string
|
|> required "sate_key" string
|
||||||
|
|
||||||
-- Rooms
|
-- Rooms
|
||||||
type alias Rooms =
|
type alias Rooms =
|
||||||
{ join : Dict String JoinedRoom
|
{ join : Maybe (Dict String JoinedRoom)
|
||||||
, invite : Dict String InvitedRoom
|
, invite : Maybe (Dict String InvitedRoom)
|
||||||
, leave : Dict String LeftRoom
|
, leave : Maybe (Dict String LeftRoom)
|
||||||
}
|
}
|
||||||
|
|
||||||
roomsDecoder : Decoder Rooms
|
roomsDecoder : Decoder Rooms
|
||||||
roomsDecoder =
|
roomsDecoder =
|
||||||
Decode.succeed Rooms
|
Decode.succeed Rooms
|
||||||
|> required "join" (dict joinedRoomDecoder)
|
|> maybeDecode "join" (dict joinedRoomDecoder)
|
||||||
|> required "invite" (dict invitedRoomDecoder)
|
|> maybeDecode "invite" (dict invitedRoomDecoder)
|
||||||
|> required "leave" (dict leftRoomDecoder)
|
|> maybeDecode "leave" (dict leftRoomDecoder)
|
||||||
|
|
||||||
type alias JoinedRoom =
|
type alias JoinedRoom =
|
||||||
{ state : State
|
{ state : Maybe State
|
||||||
, timeline : Timeline
|
, timeline : Maybe Timeline
|
||||||
, ephemeral : Ephemeral
|
, ephemeral : Maybe Ephemeral
|
||||||
, accountData : AccountData
|
, accountData : Maybe AccountData
|
||||||
, unreadNotifications : UnreadNotificationCounts
|
, unreadNotifications : Maybe UnreadNotificationCounts
|
||||||
}
|
}
|
||||||
|
|
||||||
joinedRoomDecoder : Decoder JoinedRoom
|
joinedRoomDecoder : Decoder JoinedRoom
|
||||||
joinedRoomDecoder =
|
joinedRoomDecoder =
|
||||||
Decode.succeed JoinedRoom
|
Decode.succeed JoinedRoom
|
||||||
|> required "state" stateDecoder
|
|> maybeDecode "state" stateDecoder
|
||||||
|> required "timeline" timelineDecoder
|
|> maybeDecode "timeline" timelineDecoder
|
||||||
|> required "ephemeral" ephemeralDecoder
|
|> maybeDecode "ephemeral" ephemeralDecoder
|
||||||
|> required "account_data" accountDataDecoder
|
|> maybeDecode "account_data" accountDataDecoder
|
||||||
|> required "unread_notifications" unreadNotificationCountsDecoder
|
|> maybeDecode "unread_notifications" unreadNotificationCountsDecoder
|
||||||
|
|
||||||
|
|
||||||
-- Joined Room Data
|
-- Joined Room Data
|
||||||
type alias Timeline =
|
type alias Timeline =
|
||||||
{ events : List RoomEvent
|
{ events : Maybe (List RoomEvent)
|
||||||
, limited : Bool
|
, limited : Maybe Bool
|
||||||
, prevBatch : String
|
, prevBatch : Maybe String
|
||||||
}
|
}
|
||||||
|
|
||||||
timelineDecoder =
|
timelineDecoder =
|
||||||
Decode.succeed Timeline
|
Decode.succeed Timeline
|
||||||
|> required "events" (list roomEventDecoder)
|
|> maybeDecode "events" (list roomEventDecoder)
|
||||||
|> required "limited" bool
|
|> maybeDecode "limited" bool
|
||||||
|> required "prev_batch" string
|
|> maybeDecode "prev_batch" string
|
||||||
|
|
||||||
type alias RoomEvent =
|
type alias RoomEvent =
|
||||||
{ content : Decode.Value
|
{ content : Decode.Value
|
||||||
|
@ -132,7 +138,7 @@ type alias RoomEvent =
|
||||||
, eventId : String
|
, eventId : String
|
||||||
, sender : String
|
, sender : String
|
||||||
, originServerTs : Int
|
, originServerTs : Int
|
||||||
, unsigned : UnsignedData
|
, unsigned : Maybe UnsignedData
|
||||||
}
|
}
|
||||||
|
|
||||||
roomEventDecoder : Decoder RoomEvent
|
roomEventDecoder : Decoder RoomEvent
|
||||||
|
@ -143,55 +149,55 @@ roomEventDecoder =
|
||||||
|> required "event_id" string
|
|> required "event_id" string
|
||||||
|> required "sender" string
|
|> required "sender" string
|
||||||
|> required "origin_server_ts" int
|
|> required "origin_server_ts" int
|
||||||
|> required "unsigned" unsignedDataDecoder
|
|> maybeDecode "unsigned" unsignedDataDecoder
|
||||||
|
|
||||||
type alias Ephemeral =
|
type alias Ephemeral =
|
||||||
{ events : List Event
|
{ events : Maybe (List Event)
|
||||||
}
|
}
|
||||||
|
|
||||||
ephemeralDecoder : Decoder Ephemeral
|
ephemeralDecoder : Decoder Ephemeral
|
||||||
ephemeralDecoder =
|
ephemeralDecoder =
|
||||||
Decode.succeed Ephemeral
|
Decode.succeed Ephemeral
|
||||||
|> required "events" (list eventDecoder)
|
|> maybeDecode "events" (list eventDecoder)
|
||||||
|
|
||||||
type alias AccountData =
|
type alias AccountData =
|
||||||
{ events : List Event
|
{ events : Maybe (List Event)
|
||||||
}
|
}
|
||||||
|
|
||||||
accountDataDecoder : Decoder AccountData
|
accountDataDecoder : Decoder AccountData
|
||||||
accountDataDecoder =
|
accountDataDecoder =
|
||||||
Decode.succeed AccountData
|
Decode.succeed AccountData
|
||||||
|> required "events" (list eventDecoder)
|
|> maybeDecode "events" (list eventDecoder)
|
||||||
|
|
||||||
type alias UnreadNotificationCounts =
|
type alias UnreadNotificationCounts =
|
||||||
{ highlightCount : Int
|
{ highlightCount : Maybe Int
|
||||||
, notificationCount : Int
|
, notificationCount : Maybe Int
|
||||||
}
|
}
|
||||||
|
|
||||||
unreadNotificationCountsDecoder : Decoder UnreadNotificationCounts
|
unreadNotificationCountsDecoder : Decoder UnreadNotificationCounts
|
||||||
unreadNotificationCountsDecoder =
|
unreadNotificationCountsDecoder =
|
||||||
Decode.succeed UnreadNotificationCounts
|
Decode.succeed UnreadNotificationCounts
|
||||||
|> required "highlight_count" int
|
|> maybeDecode "highlight_count" int
|
||||||
|> required "notification_count" int
|
|> maybeDecode "notification_count" int
|
||||||
|
|
||||||
-- Invited Room Data
|
-- Invited Room Data
|
||||||
type alias InvitedRoom =
|
type alias InvitedRoom =
|
||||||
{ inviteState : InviteState
|
{ inviteState : Maybe InviteState
|
||||||
}
|
}
|
||||||
|
|
||||||
invitedRoomDecoder : Decoder InvitedRoom
|
invitedRoomDecoder : Decoder InvitedRoom
|
||||||
invitedRoomDecoder =
|
invitedRoomDecoder =
|
||||||
Decode.succeed InvitedRoom
|
Decode.succeed InvitedRoom
|
||||||
|> required "invite_state" inviteStateDecoder
|
|> maybeDecode "invite_state" inviteStateDecoder
|
||||||
|
|
||||||
type alias InviteState =
|
type alias InviteState =
|
||||||
{ events : List StrippedState
|
{ events : Maybe (List StrippedState)
|
||||||
}
|
}
|
||||||
|
|
||||||
inviteStateDecoder : Decoder InviteState
|
inviteStateDecoder : Decoder InviteState
|
||||||
inviteStateDecoder =
|
inviteStateDecoder =
|
||||||
Decode.succeed InviteState
|
Decode.succeed InviteState
|
||||||
|> required "events" (list strippedStateDecoder)
|
|> maybeDecode "events" (list strippedStateDecoder)
|
||||||
|
|
||||||
type alias StrippedState =
|
type alias StrippedState =
|
||||||
{ content : EventContent
|
{ content : EventContent
|
||||||
|
@ -210,14 +216,14 @@ strippedStateDecoder =
|
||||||
|
|
||||||
-- Left Room Data
|
-- Left Room Data
|
||||||
type alias LeftRoom =
|
type alias LeftRoom =
|
||||||
{ state : State
|
{ state : Maybe State
|
||||||
, timeline : Timeline
|
, timeline : Maybe Timeline
|
||||||
, accountData : AccountData
|
, accountData : Maybe AccountData
|
||||||
}
|
}
|
||||||
|
|
||||||
leftRoomDecoder : Decoder LeftRoom
|
leftRoomDecoder : Decoder LeftRoom
|
||||||
leftRoomDecoder =
|
leftRoomDecoder =
|
||||||
Decode.succeed LeftRoom
|
Decode.succeed LeftRoom
|
||||||
|> required "state" stateDecoder
|
|> maybeDecode "state" stateDecoder
|
||||||
|> required "timeline" timelineDecoder
|
|> maybeDecode "timeline" timelineDecoder
|
||||||
|> required "account_data" accountDataDecoder
|
|> maybeDecode "account_data" accountDataDecoder
|
||||||
|
|
Loading…
Reference in New Issue
Block a user