@@ -16,59 +16,83 @@ def __init__(self, config, opsdroid=None):
16
16
super ().__init__ (config , opsdroid = opsdroid )
17
17
self .name = "matrix"
18
18
self .room = config .get ("default_room" , "main" )
19
- self ._state_key = config .get ("state_key" ,"opsdroid.database" )
20
- _LOGGER .debug ('Loaded matrix database connector' )
19
+ self ._event_type = "opsdroid.database"
20
+ self ._single_state_key = config .get ("single_state_key" , True )
21
+ _LOGGER .debug ("Loaded matrix database connector" )
21
22
22
23
async def connect (self ):
23
24
"""Connect to the database."""
24
- # Currently can't actually get connectors when this runs, so just store opsdroid instead
25
- _LOGGER .info (self .opsdroid )
26
- _LOGGER .info ("Plugged into the matrix" )
25
+ _LOGGER .info ("Matrix Database connector initialised." )
27
26
28
27
async def put (self , key , value ):
29
28
"""Insert or replace an object into the database for a given key."""
30
- room = self .room or 'main'
31
- room_id = room if room [0 ] == '!' else self .connector .room_ids [room ]
32
29
33
- _LOGGER .debug (f"===== Putting { key } into matrix room { room_id } " )
30
+ # If the single state key flag is set then use that else use state key.
31
+ state_key = "" if self ._single_state_key is True else key
32
+
33
+ room = self .room or "main"
34
+ room_id = room if room [0 ] == "!" else self .connector .room_ids [room ]
35
+
36
+ _LOGGER .debug (f"Putting { key } into matrix room { room_id } ." )
37
+
38
+ ori_data = await self .get_state_event (room_id , state_key )
39
+
40
+ if self ._single_state_key :
41
+ value = {key : value }
42
+
43
+ elif not isinstance (value , dict ):
44
+ raise ValueError ("When single_state_key is False value must be a dict." )
45
+
46
+ data = {** ori_data , ** value }
34
47
35
- ori_data = await self .get_state_event (room_id , key )
36
- data = {key : value }
37
- data = {** ori_data , ** data }
38
48
if data == ori_data :
39
49
_LOGGER .debug ("Not updating matrix state, as content hasn't changed." )
40
50
return
51
+
41
52
_LOGGER .debug (f"===== Putting { key } into matrix room { room_id } with { data } " )
42
53
43
- await self .opsdroid .send (MatrixStateEvent (key = self ._state_key ,
44
- content = data ,
45
- target = room_id ,
46
- connector = self .connector ,
47
- state_key = key ))
54
+ await self .opsdroid .send (
55
+ MatrixStateEvent (
56
+ key = self ._event_type ,
57
+ content = data ,
58
+ target = room_id ,
59
+ connector = self .connector ,
60
+ state_key = state_key ,
61
+ )
62
+ )
48
63
49
64
async def get (self , key ):
50
65
"""Get a document from the database for a given key."""
51
- room = self .room or ' main'
52
- room_id = room if room .startswith ('!' ) else self .connector .room_ids [room ]
66
+ room = self .room or " main"
67
+ room_id = room if room .startswith ("!" ) else self .connector .room_ids [room ]
53
68
54
69
_LOGGER .debug (f"Getting { key } from matrix room { room_id } " )
55
70
56
71
try :
57
72
data = await self .get_state_event (room_id , key )
58
- data = data . get ( key )
59
- except MatrixRequestError :
73
+ except MatrixRequestError as e :
74
+ _LOGGER . info ( f"Failed to get state event with state_key= { key } : { e } " )
60
75
data = None
61
76
77
+ if not data :
78
+ return
79
+
80
+ if self ._single_state_key :
81
+ data = data .get (key )
82
+
62
83
return data
63
84
64
85
@property
65
86
def connector (self ):
66
- return self .opsdroid ._connector_names [' matrix' ]
87
+ return self .opsdroid ._connector_names [" matrix" ]
67
88
68
89
async def get_state_event (self , room_id , key ):
90
+
91
+ url = f"/rooms/{ room_id } /state/{ self ._event_type } "
92
+ if not self ._single_state_key :
93
+ url += f"/{ key } "
69
94
try :
70
- return await self .connector .connection ._send ("GET" ,
71
- quote (f"/rooms/{ room_id } /state/{ self ._state_key } /{ key } " ))
95
+ return await self .connector .connection ._send ("GET" , quote (url ))
72
96
except MatrixRequestError as e :
73
97
if e .code != 404 :
74
98
raise e
0 commit comments