@@ -8,14 +8,15 @@ public protocol MatrixEvent: Codable {
88 static var type : String { get }
99
1010 var eventID : String ? { get }
11- var sender : String ? { get }
11+ var sender : MatrixFullUserIdentifier ? { get }
1212 var date : Date ? { get }
1313 var unsigned : AnyCodable ? { get }
1414}
1515
1616/// The coding keys needed to determine an event's type before decoding.
17- enum MatrixEventTypeKeys : CodingKey {
17+ enum MatrixEventTypeKeys : String , CodingKey {
1818 case type
19+ case stateKey = " state_key "
1920}
2021
2122enum MatrixEventCodableError : Error {
@@ -38,6 +39,29 @@ extension CodingUserInfoKey {
3839 }
3940}
4041
42+ public struct MatrixInvalidEvent : MatrixEvent {
43+ public static let type : String = " "
44+
45+ public var type : String ?
46+
47+ public var eventID : String ?
48+
49+ public var sender : MatrixFullUserIdentifier ?
50+
51+ public var date : Date ? {
52+ nil
53+ }
54+
55+ public var unsigned : AnyCodable ? {
56+ nil
57+ }
58+
59+ enum CodingKeys : String , CodingKey {
60+ case eventID = " event_id "
61+ case sender
62+ }
63+ }
64+
4165// TODO: encodable
4266@propertyWrapper
4367public struct MatrixCodableEvents < Value: Collection > : Codable where Value. Element == MatrixEvent {
@@ -51,28 +75,51 @@ public struct MatrixCodableEvents<Value: Collection>: Codable where Value.Elemen
5175 // these can throw as something has gone seriously wrong if the type key is missing
5276 let container = try decoder. container ( keyedBy: MatrixEventTypeKeys . self)
5377 let typeID = try container. decode ( String . self, forKey: . type)
54-
55- guard let types = decoder. userInfo [ . matrixEventTypes] as? [ MatrixEvent . Type ] else {
56- // the decoder must be supplied with some event types to decode
57- throw MatrixEventCodableError . missingTypes
58- }
59-
60- guard let matchingType = types. first ( where: { $0. type == typeID } ) else {
61- // simply ignore events with no matching type as throwing would prevent access to other events
78+ let stateKey = try container. decodeIfPresent ( String . self, forKey: . stateKey)
79+
80+ do {
81+ if stateKey != nil {
82+ let stateEvent = try MatrixStateEvent ( from: decoder)
83+ guard let decoded = stateEvent as? T
84+ else {
85+ throw MatrixEventCodableError . unableToCast ( decoded: stateEvent, into: " state_event " )
86+ }
87+ wrappedEvent = decoded
88+ return
89+ }
90+
91+ guard let types = decoder. userInfo [ . matrixEventTypes] as? [ MatrixEvent . Type ] else {
92+ // the decoder must be supplied with some event types to decode
93+ throw MatrixEventCodableError . missingTypes
94+ }
95+
96+ guard let matchingType = types. first ( where: { $0. type == typeID } ) else {
97+ // simply ignore events with no matching type as throwing would prevent access to other events
98+ return
99+ }
100+
101+ guard let decoded = try ? matchingType. init ( from: decoder) else {
102+ assertionFailure ( " Failed to decode MatrixEvent as \( String ( describing: T . self) ) " )
103+ return
104+ }
105+
106+ guard let decoded = decoded as? T else {
107+ // something has probably gone very wrong at this stage
108+ throw MatrixEventCodableError . unableToCast ( decoded: decoded, into: String ( describing: T . self) )
109+ }
110+ wrappedEvent = decoded
62111 return
63- }
112+ } catch {
113+ print ( error. localizedDescription)
114+ var event = try ! MatrixInvalidEvent ( from: decoder)
115+ event. type = typeID
64116
65- guard let decoded = try ? matchingType. init ( from: decoder) else {
66- assertionFailure ( " Failed to decode MatrixEvent as \( String ( describing: T . self) ) " )
67- return
68- }
117+ guard let decoded = event as? T else {
118+ throw MatrixEventCodableError . missingTypes
119+ }
69120
70- guard let decoded = decoded as? T else {
71- // something has probably gone very wrong at this stage
72- throw MatrixEventCodableError . unableToCast ( decoded: decoded, into: String ( describing: T . self) )
121+ wrappedEvent = decoded
73122 }
74-
75- wrappedEvent = decoded
76123 }
77124
78125 func encode( to encoder: Encoder ) throws {
@@ -97,11 +144,15 @@ public struct MatrixCodableEvents<Value: Collection>: Codable where Value.Elemen
97144 }
98145
99146 public init ( from decoder: Decoder ) {
100- guard let container = try ? decoder. singleValueContainer ( ) ,
101- let wrappers = try ? container. decode ( [ EventWrapper < Value . Element > ] . self)
102- else { return }
103-
104- wrappedValue = wrappers. compactMap ( \. wrappedEvent) as? Value
147+ do {
148+ let container = try decoder. singleValueContainer ( )
149+ let wrappers = try container. decode ( [ EventWrapper < Value . Element > ] . self)
150+ wrappedValue = wrappers. compactMap ( \. wrappedEvent) as? Value
151+ } catch {
152+ if #available( iOS 14 . 0 , * ) {
153+ MatrixClient . logger. warning ( " Failed to parse sync: \( error. localizedDescription) " )
154+ } else { }
155+ }
105156 }
106157
107158 public func encode( to encoder: Encoder ) throws {
0 commit comments