@@ -12,17 +12,45 @@ import MatrixClient
1212import MatrixCore
1313
1414public struct MatrixRoomState : MatrixStoreRoomState {
15+ public init ( roomId: String , event: MatrixStateEvent ) throws {
16+ guard let eventId = event. eventID,
17+ let sender = event. sender
18+ else {
19+ throw MatrixCodableStateEventType . StateTypeError. missingTypes
20+ }
21+ self . eventId = eventId
22+ self . roomId = roomId
23+ stateKey = event. stateKey
24+ // self.sender = sender
25+ _content = event
26+ }
27+
1528 public var eventId : String
1629 public var roomId : String
1730
1831 public var stateKey : String
1932
33+ public var sender : MatrixFullUserIdentifier ? {
34+ _content. sender
35+ }
36+
2037 // TODO: custom type in MatrixClient?
21- public var contentType : String
38+ public var contentType : String {
39+ type ( of: content) . type
40+ }
2241
2342 // TODO: some event type?
24- @MatrixCodableStateEventType
25- public var content : MatrixStateEventType
43+ // @MatrixCodableStateEventType
44+ private var _content : MatrixStateEvent
45+
46+ public var content : MatrixStateEventType {
47+ get {
48+ _content. content
49+ }
50+ set {
51+ _content. content = newValue
52+ }
53+ }
2654}
2755
2856extension MatrixRoomState : Codable , FetchableRecord , PersistableRecord , Equatable , Hashable {
@@ -50,14 +78,21 @@ extension MatrixRoomState: Codable, FetchableRecord, PersistableRecord, Equatabl
5078 case content
5179 }
5280
81+ enum Columns {
82+ static let eventId = Column ( CodingKeys . eventId)
83+ static let roomId = Column ( CodingKeys . roomId)
84+ static let contentType = Column ( CodingKeys . contentType)
85+ static let stateKey = Column ( CodingKeys . stateKey)
86+ static let content = Column ( CodingKeys . content)
87+ }
88+
5389 public init ( from decoder: Decoder ) throws {
5490 let container = try decoder. container ( keyedBy: CodingKeys . self)
5591 eventId = try container. decode ( String . self, forKey: . eventId)
5692 roomId = try container. decode ( String . self, forKey: . roomId)
5793 stateKey = try container. decode ( String . self, forKey: . stateKey)
58- contentType = try container. decode ( String . self, forKey: . contentType)
5994
60- _content = try MatrixCodableStateEventType ( from : container. superDecoder ( forKey : . content ) , typeID : contentType )
95+ _content = try container. decode ( MatrixStateEvent . self , forKey : . content )
6196 }
6297
6398 public func encode( to encoder: Encoder ) throws {
@@ -67,7 +102,7 @@ extension MatrixRoomState: Codable, FetchableRecord, PersistableRecord, Equatabl
67102 try container. encode ( stateKey, forKey: . stateKey)
68103 try container. encode ( contentType, forKey: . contentType)
69104
70- try _content . encode ( to : encoder )
105+ try container . encode ( _content , forKey : . content )
71106 }
72107
73108 /* public static func databaseJSONEncoder(for column: String) -> JSONEncoder {
@@ -98,13 +133,21 @@ public extension MatrixSQLiteStore {
98133
99134 func addRoomState( state: MatrixRoomState ) async throws {
100135 try await dbWriter. write { db in
136+ try ? db. execute (
137+ sql: " DELETE FROM \" \( MatrixRoomState . databaseTableName) \" INDEXED BY \" room_state_type_key_index \" WHERE room_id = ? AND type = ? AND state_key = ? " ,
138+ arguments: [ state. roomId, state. contentType, state. stateKey]
139+ )
101140 try state. insert ( db)
102141 }
103142 }
104143
105144 func getRoomState( roomId: String ) async throws -> [ RoomState ] {
106145 try await dbWriter. read { db in
107- try RoomState . fetchAll ( db, sql: " SELECT * FROM room_state WHERE room_id = ? " , arguments: [ roomId] )
146+ try RoomState . fetchAll (
147+ db,
148+ sql: " SELECT * FROM room_state INDEXED BY room_state_on_room_id WHERE room_id = ? " ,
149+ arguments: [ roomId]
150+ )
108151 }
109152 }
110153
@@ -134,6 +177,7 @@ public extension MatrixSQLiteStore {
134177 }
135178 }
136179
180+ /// - Throws: An error of type ``DatabaseError``
137181 func getRoomState( roomId: String , stateType: String , stateKey: String ) async throws -> [ RoomState ] {
138182 try await dbWriter. read { db in
139183 try RoomState . fetchAll (
0 commit comments