Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0184.py @ 4037:524856bd7b19
massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.
However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.
To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.
This is a massive change, and may result in some bugs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:54:42 +0200 |
parents | 31628770a15a |
children |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
100 log.info(_("Plugin XEP_0184 (message delivery receipts) initialization")) | 100 log.info(_("Plugin XEP_0184 (message delivery receipts) initialization")) |
101 self.host = host | 101 self.host = host |
102 self._dictRequest = dict() | 102 self._dictRequest = dict() |
103 | 103 |
104 # parameter value is retrieved before each use | 104 # parameter value is retrieved before each use |
105 host.memory.updateParams(self.params) | 105 host.memory.update_params(self.params) |
106 | 106 |
107 host.trigger.add("sendMessage", self.sendMessageTrigger) | 107 host.trigger.add("sendMessage", self.send_message_trigger) |
108 host.bridge.addSignal( | 108 host.bridge.add_signal( |
109 "messageState", ".plugin", signature="sss" | 109 "message_state", ".plugin", signature="sss" |
110 ) # message_uid, status, profile | 110 ) # message_uid, status, profile |
111 | 111 |
112 def getHandler(self, client): | 112 def get_handler(self, client): |
113 return XEP_0184_handler(self, client.profile) | 113 return XEP_0184_handler(self, client.profile) |
114 | 114 |
115 def sendMessageTrigger( | 115 def send_message_trigger( |
116 self, client, mess_data, pre_xml_treatments, post_xml_treatments | 116 self, client, mess_data, pre_xml_treatments, post_xml_treatments |
117 ): | 117 ): |
118 """Install SendMessage command hook """ | 118 """Install SendMessage command hook """ |
119 | 119 |
120 def treatment(mess_data): | 120 def treatment(mess_data): |
121 message = mess_data["xml"] | 121 message = mess_data["xml"] |
122 message_type = message.getAttribute("type") | 122 message_type = message.getAttribute("type") |
123 | 123 |
124 if self._isActif(client.profile) and ( | 124 if self._is_actif(client.profile) and ( |
125 message_type == "chat" or message_type == "normal" | 125 message_type == "chat" or message_type == "normal" |
126 ): | 126 ): |
127 message.addElement("request", NS_MESSAGE_DELIVERY_RECEIPTS) | 127 message.addElement("request", NS_MESSAGE_DELIVERY_RECEIPTS) |
128 uid = mess_data["uid"] | 128 uid = mess_data["uid"] |
129 msg_id = message.getAttribute("id") | 129 msg_id = message.getAttribute("id") |
130 self._dictRequest[msg_id] = uid | 130 self._dictRequest[msg_id] = uid |
131 reactor.callLater( | 131 reactor.callLater( |
132 TEMPO_DELETE_WAITING_ACK_S, self._clearDictRequest, msg_id | 132 TEMPO_DELETE_WAITING_ACK_S, self._clear_dict_request, msg_id |
133 ) | 133 ) |
134 log.debug( | 134 log.debug( |
135 _( | 135 _( |
136 "[XEP-0184] Request acknowledgment for message id {}".format( | 136 "[XEP-0184] Request acknowledgment for message id {}".format( |
137 msg_id | 137 msg_id |
142 return mess_data | 142 return mess_data |
143 | 143 |
144 post_xml_treatments.addCallback(treatment) | 144 post_xml_treatments.addCallback(treatment) |
145 return True | 145 return True |
146 | 146 |
147 def onMessageDeliveryReceiptsRequest(self, msg_elt, client): | 147 def on_message_delivery_receipts_request(self, msg_elt, client): |
148 """This method is called on message delivery receipts **request** (XEP-0184 #7) | 148 """This method is called on message delivery receipts **request** (XEP-0184 #7) |
149 @param msg_elt: message element | 149 @param msg_elt: message element |
150 @param client: %(doc_client)s""" | 150 @param client: %(doc_client)s""" |
151 from_jid = jid.JID(msg_elt["from"]) | 151 from_jid = jid.JID(msg_elt["from"]) |
152 | 152 |
153 if self._isActif(client.profile) and client.roster.isSubscribedFrom(from_jid): | 153 if self._is_actif(client.profile) and client.roster.is_subscribed_from(from_jid): |
154 received_elt_ret = domish.Element((NS_MESSAGE_DELIVERY_RECEIPTS, "received")) | 154 received_elt_ret = domish.Element((NS_MESSAGE_DELIVERY_RECEIPTS, "received")) |
155 try: | 155 try: |
156 received_elt_ret["id"] = msg_elt["id"] | 156 received_elt_ret["id"] = msg_elt["id"] |
157 except KeyError: | 157 except KeyError: |
158 log.warning(f"missing id for message element: {msg_elt.toXml}") | 158 log.warning(f"missing id for message element: {msg_elt.toXml}") |
160 | 160 |
161 msg_result_elt = xmlstream.toResponse(msg_elt, "result") | 161 msg_result_elt = xmlstream.toResponse(msg_elt, "result") |
162 msg_result_elt.addChild(received_elt_ret) | 162 msg_result_elt.addChild(received_elt_ret) |
163 client.send(msg_result_elt) | 163 client.send(msg_result_elt) |
164 | 164 |
165 def onMessageDeliveryReceiptsReceived(self, msg_elt, client): | 165 def on_message_delivery_receipts_received(self, msg_elt, client): |
166 """This method is called on message delivery receipts **received** (XEP-0184 #7) | 166 """This method is called on message delivery receipts **received** (XEP-0184 #7) |
167 @param msg_elt: message element | 167 @param msg_elt: message element |
168 @param client: %(doc_client)s""" | 168 @param client: %(doc_client)s""" |
169 msg_elt.handled = True | 169 msg_elt.handled = True |
170 rcv_elt = next(msg_elt.elements(NS_MESSAGE_DELIVERY_RECEIPTS, "received")) | 170 rcv_elt = next(msg_elt.elements(NS_MESSAGE_DELIVERY_RECEIPTS, "received")) |
171 msg_id = rcv_elt["id"] | 171 msg_id = rcv_elt["id"] |
172 | 172 |
173 try: | 173 try: |
174 uid = self._dictRequest[msg_id] | 174 uid = self._dictRequest[msg_id] |
175 del self._dictRequest[msg_id] | 175 del self._dictRequest[msg_id] |
176 self.host.bridge.messageState( | 176 self.host.bridge.message_state( |
177 uid, STATUS_MESSAGE_DELIVERY_RECEIVED, client.profile | 177 uid, STATUS_MESSAGE_DELIVERY_RECEIVED, client.profile |
178 ) | 178 ) |
179 log.debug( | 179 log.debug( |
180 _("[XEP-0184] Receive acknowledgment for message id {}".format(msg_id)) | 180 _("[XEP-0184] Receive acknowledgment for message id {}".format(msg_id)) |
181 ) | 181 ) |
182 except KeyError: | 182 except KeyError: |
183 pass | 183 pass |
184 | 184 |
185 def _clearDictRequest(self, msg_id): | 185 def _clear_dict_request(self, msg_id): |
186 try: | 186 try: |
187 del self._dictRequest[msg_id] | 187 del self._dictRequest[msg_id] |
188 log.debug( | 188 log.debug( |
189 _( | 189 _( |
190 "[XEP-0184] Delete waiting acknowledgment for message id {}".format( | 190 "[XEP-0184] Delete waiting acknowledgment for message id {}".format( |
193 ) | 193 ) |
194 ) | 194 ) |
195 except KeyError: | 195 except KeyError: |
196 pass | 196 pass |
197 | 197 |
198 def _isActif(self, profile): | 198 def _is_actif(self, profile): |
199 return self.host.memory.getParamA(PARAM_NAME, PARAM_KEY, profile_key=profile) | 199 return self.host.memory.param_get_a(PARAM_NAME, PARAM_KEY, profile_key=profile) |
200 | 200 |
201 | 201 |
202 @implementer(iwokkel.IDisco) | 202 @implementer(iwokkel.IDisco) |
203 class XEP_0184_handler(XMPPHandler): | 203 class XEP_0184_handler(XMPPHandler): |
204 | 204 |
208 self.profile = profile | 208 self.profile = profile |
209 | 209 |
210 def connectionInitialized(self): | 210 def connectionInitialized(self): |
211 self.xmlstream.addObserver( | 211 self.xmlstream.addObserver( |
212 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_REQUEST, | 212 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_REQUEST, |
213 self.plugin_parent.onMessageDeliveryReceiptsRequest, | 213 self.plugin_parent.on_message_delivery_receipts_request, |
214 client=self.parent, | 214 client=self.parent, |
215 ) | 215 ) |
216 self.xmlstream.addObserver( | 216 self.xmlstream.addObserver( |
217 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_RECEIVED, | 217 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_RECEIVED, |
218 self.plugin_parent.onMessageDeliveryReceiptsReceived, | 218 self.plugin_parent.on_message_delivery_receipts_received, |
219 client=self.parent, | 219 client=self.parent, |
220 ) | 220 ) |
221 | 221 |
222 self.xmlstream.addObserver( | 222 self.xmlstream.addObserver( |
223 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_REQUEST, | 223 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_REQUEST, |
224 self.plugin_parent.onMessageDeliveryReceiptsRequest, | 224 self.plugin_parent.on_message_delivery_receipts_request, |
225 client=self.parent, | 225 client=self.parent, |
226 ) | 226 ) |
227 self.xmlstream.addObserver( | 227 self.xmlstream.addObserver( |
228 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_RECEIVED, | 228 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_RECEIVED, |
229 self.plugin_parent.onMessageDeliveryReceiptsReceived, | 229 self.plugin_parent.on_message_delivery_receipts_received, |
230 client=self.parent, | 230 client=self.parent, |
231 ) | 231 ) |
232 | 232 |
233 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): | 233 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
234 return [disco.DiscoFeature(NS_MESSAGE_DELIVERY_RECEIPTS)] | 234 return [disco.DiscoFeature(NS_MESSAGE_DELIVERY_RECEIPTS)] |