Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0424.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 | 6090141b1b70 |
children | c23cad65ae99 |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
68 class XEP_0424(object): | 68 class XEP_0424(object): |
69 | 69 |
70 def __init__(self, host): | 70 def __init__(self, host): |
71 log.info(_("XEP-0424 (Message Retraction) plugin initialization")) | 71 log.info(_("XEP-0424 (Message Retraction) plugin initialization")) |
72 self.host = host | 72 self.host = host |
73 host.memory.updateParams(PARAMS) | 73 host.memory.update_params(PARAMS) |
74 self._h = host.plugins["XEP-0334"] | 74 self._h = host.plugins["XEP-0334"] |
75 self._f = host.plugins["XEP-0422"] | 75 self._f = host.plugins["XEP-0422"] |
76 host.registerNamespace("message-retract", NS_MESSAGE_RETRACT) | 76 host.register_namespace("message-retract", NS_MESSAGE_RETRACT) |
77 host.trigger.add("messageReceived", self._messageReceivedTrigger, 100) | 77 host.trigger.add("messageReceived", self._message_received_trigger, 100) |
78 host.bridge.addMethod( | 78 host.bridge.add_method( |
79 "messageRetract", | 79 "message_retract", |
80 ".plugin", | 80 ".plugin", |
81 in_sign="ss", | 81 in_sign="ss", |
82 out_sign="", | 82 out_sign="", |
83 method=self._retract, | 83 method=self._retract, |
84 async_=True, | 84 async_=True, |
85 ) | 85 ) |
86 | 86 |
87 def getHandler(self, __): | 87 def get_handler(self, __): |
88 return XEP_0424_handler() | 88 return XEP_0424_handler() |
89 | 89 |
90 def _retract(self, message_id: str, profile: str) -> None: | 90 def _retract(self, message_id: str, profile: str) -> None: |
91 client = self.host.getClient(profile) | 91 client = self.host.get_client(profile) |
92 return defer.ensureDeferred( | 92 return defer.ensureDeferred( |
93 self.retract(client, message_id) | 93 self.retract(client, message_id) |
94 ) | 94 ) |
95 | 95 |
96 def retractByOriginId( | 96 def retract_by_origin_id( |
97 self, | 97 self, |
98 client: SatXMPPEntity, | 98 client: SatXMPPEntity, |
99 dest_jid: jid.JID, | 99 dest_jid: jid.JID, |
100 origin_id: str | 100 origin_id: str |
101 ) -> None: | 101 ) -> None: |
107 @param origin_id: origin-id as specified in XEP-0359 | 107 @param origin_id: origin-id as specified in XEP-0359 |
108 """ | 108 """ |
109 message_elt = domish.Element((None, "message")) | 109 message_elt = domish.Element((None, "message")) |
110 message_elt["from"] = client.jid.full() | 110 message_elt["from"] = client.jid.full() |
111 message_elt["to"] = dest_jid.full() | 111 message_elt["to"] = dest_jid.full() |
112 apply_to_elt = self._f.applyToElt(message_elt, origin_id) | 112 apply_to_elt = self._f.apply_to_elt(message_elt, origin_id) |
113 apply_to_elt.addElement((NS_MESSAGE_RETRACT, "retract")) | 113 apply_to_elt.addElement((NS_MESSAGE_RETRACT, "retract")) |
114 self.host.plugins["XEP-0428"].addFallbackElt( | 114 self.host.plugins["XEP-0428"].add_fallback_elt( |
115 message_elt, | 115 message_elt, |
116 "[A message retraction has been requested, but your client doesn't support " | 116 "[A message retraction has been requested, but your client doesn't support " |
117 "it]" | 117 "it]" |
118 ) | 118 ) |
119 self._h.addHintElements(message_elt, [self._h.HINT_STORE]) | 119 self._h.add_hint_elements(message_elt, [self._h.HINT_STORE]) |
120 client.send(message_elt) | 120 client.send(message_elt) |
121 | 121 |
122 async def retractByHistory( | 122 async def retract_by_history( |
123 self, | 123 self, |
124 client: SatXMPPEntity, | 124 client: SatXMPPEntity, |
125 history: History | 125 history: History |
126 ) -> None: | 126 ) -> None: |
127 """Send a message retraction using History instance | 127 """Send a message retraction using History instance |
136 raise exceptions.FeatureNotFound( | 136 raise exceptions.FeatureNotFound( |
137 f"message to retract doesn't have the necessary origin-id, the sending " | 137 f"message to retract doesn't have the necessary origin-id, the sending " |
138 "client is probably not supporting message retraction." | 138 "client is probably not supporting message retraction." |
139 ) | 139 ) |
140 else: | 140 else: |
141 self.retractByOriginId(client, history.dest_jid, origin_id) | 141 self.retract_by_origin_id(client, history.dest_jid, origin_id) |
142 await self.retractDBHistory(client, history) | 142 await self.retract_db_history(client, history) |
143 | 143 |
144 async def retract( | 144 async def retract( |
145 self, | 145 self, |
146 client: SatXMPPEntity, | 146 client: SatXMPPEntity, |
147 message_id: str, | 147 message_id: str, |
161 ) | 161 ) |
162 if history is None: | 162 if history is None: |
163 raise exceptions.NotFound( | 163 raise exceptions.NotFound( |
164 f"message to retract not found in database ({message_id})" | 164 f"message to retract not found in database ({message_id})" |
165 ) | 165 ) |
166 await self.retractByHistory(client, history) | 166 await self.retract_by_history(client, history) |
167 | 167 |
168 async def retractDBHistory(self, client, history: History) -> None: | 168 async def retract_db_history(self, client, history: History) -> None: |
169 """Mark an history instance in database as retracted | 169 """Mark an history instance in database as retracted |
170 | 170 |
171 @param history: history instance | 171 @param history: history instance |
172 "messages" and "subjects" must be loaded too | 172 "messages" and "subjects" must be loaded too |
173 """ | 173 """ |
174 # FIXME: should be keep history? This is useful to check why a message has been | 174 # FIXME: should be keep history? This is useful to check why a message has been |
175 # retracted, but if may be bad if the user think it's really deleted | 175 # retracted, but if may be bad if the user think it's really deleted |
176 # we assign a new object to be sure to trigger an update | 176 # we assign a new object to be sure to trigger an update |
177 history.extra = deepcopy(history.extra) if history.extra else {} | 177 history.extra = deepcopy(history.extra) if history.extra else {} |
178 history.extra["retracted"] = True | 178 history.extra["retracted"] = True |
179 keep_history = self.host.memory.getParamA( | 179 keep_history = self.host.memory.param_get_a( |
180 NAME, CATEGORY, profile_key=client.profile | 180 NAME, CATEGORY, profile_key=client.profile |
181 ) | 181 ) |
182 old_version: Dict[str, Any] = { | 182 old_version: Dict[str, Any] = { |
183 "timestamp": time.time() | 183 "timestamp": time.time() |
184 } | 184 } |
192 await self.host.memory.storage.delete( | 192 await self.host.memory.storage.delete( |
193 history.messages + history.subjects, | 193 history.messages + history.subjects, |
194 session_add=[history] | 194 session_add=[history] |
195 ) | 195 ) |
196 | 196 |
197 async def _messageReceivedTrigger( | 197 async def _message_received_trigger( |
198 self, | 198 self, |
199 client: SatXMPPEntity, | 199 client: SatXMPPEntity, |
200 message_elt: domish.Element, | 200 message_elt: domish.Element, |
201 post_treat: defer.Deferred | 201 post_treat: defer.Deferred |
202 ) -> bool: | 202 ) -> bool: |
203 fastened_elts = await self._f.getFastenedElts(client, message_elt) | 203 fastened_elts = await self._f.get_fastened_elts(client, message_elt) |
204 if fastened_elts is None: | 204 if fastened_elts is None: |
205 return True | 205 return True |
206 for elt in fastened_elts.elements: | 206 for elt in fastened_elts.elements: |
207 if elt.name == "retract" and elt.uri == NS_MESSAGE_RETRACT: | 207 if elt.name == "retract" and elt.uri == NS_MESSAGE_RETRACT: |
208 if fastened_elts.history is not None: | 208 if fastened_elts.history is not None: |
216 ) | 216 ) |
217 return False | 217 return False |
218 break | 218 break |
219 else: | 219 else: |
220 return True | 220 return True |
221 if not await self.host.trigger.asyncPoint( | 221 if not await self.host.trigger.async_point( |
222 "XEP-0424_retractReceived", client, message_elt, elt, fastened_elts | 222 "XEP-0424_retractReceived", client, message_elt, elt, fastened_elts |
223 ): | 223 ): |
224 return False | 224 return False |
225 if fastened_elts.history is None: | 225 if fastened_elts.history is None: |
226 # we check history after the trigger because we may be in a component which | 226 # we check history after the trigger because we may be in a component which |
228 log.warning( | 228 log.warning( |
229 f"No message found with given origin-id: {message_elt.toXml()}" | 229 f"No message found with given origin-id: {message_elt.toXml()}" |
230 ) | 230 ) |
231 return False | 231 return False |
232 log.info(f"[{client.profile}] retracting message {fastened_elts.id!r}") | 232 log.info(f"[{client.profile}] retracting message {fastened_elts.id!r}") |
233 await self.retractDBHistory(client, fastened_elts.history) | 233 await self.retract_db_history(client, fastened_elts.history) |
234 # TODO: send bridge signal | 234 # TODO: send bridge signal |
235 | 235 |
236 return False | 236 return False |
237 | 237 |
238 | 238 |