Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0444.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 | 1f88ca90c3de |
children | c23cad65ae99 |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
51 | 51 |
52 class XEP_0444: | 52 class XEP_0444: |
53 | 53 |
54 def __init__(self, host): | 54 def __init__(self, host): |
55 log.info(_("Message Reactions initialization")) | 55 log.info(_("Message Reactions initialization")) |
56 host.registerNamespace("reactions", NS_REACTIONS) | 56 host.register_namespace("reactions", NS_REACTIONS) |
57 self.host = host | 57 self.host = host |
58 self._h = host.plugins["XEP-0334"] | 58 self._h = host.plugins["XEP-0334"] |
59 host.bridge.addMethod( | 59 host.bridge.add_method( |
60 "messageReactionsSet", | 60 "message_reactions_set", |
61 ".plugin", | 61 ".plugin", |
62 in_sign="ssas", | 62 in_sign="ssas", |
63 out_sign="", | 63 out_sign="", |
64 method=self._reactionsSet, | 64 method=self._reactions_set, |
65 async_=True, | 65 async_=True, |
66 ) | 66 ) |
67 host.trigger.add("messageReceived", self._messageReceivedTrigger) | 67 host.trigger.add("messageReceived", self._message_received_trigger) |
68 | 68 |
69 def getHandler(self, client): | 69 def get_handler(self, client): |
70 return XEP_0444_Handler() | 70 return XEP_0444_Handler() |
71 | 71 |
72 async def _messageReceivedTrigger( | 72 async def _message_received_trigger( |
73 self, | 73 self, |
74 client: SatXMPPEntity, | 74 client: SatXMPPEntity, |
75 message_elt: domish.Element, | 75 message_elt: domish.Element, |
76 post_treat: defer.Deferred | 76 post_treat: defer.Deferred |
77 ) -> bool: | 77 ) -> bool: |
78 return True | 78 return True |
79 | 79 |
80 def _reactionsSet(self, message_id: str, profile: str, reactions: List[str]) -> None: | 80 def _reactions_set(self, message_id: str, profile: str, reactions: List[str]) -> None: |
81 client = self.host.getClient(profile) | 81 client = self.host.get_client(profile) |
82 return defer.ensureDeferred( | 82 return defer.ensureDeferred( |
83 self.setReactions(client, message_id) | 83 self.set_reactions(client, message_id) |
84 ) | 84 ) |
85 | 85 |
86 def sendReactions( | 86 def send_reactions( |
87 self, | 87 self, |
88 client: SatXMPPEntity, | 88 client: SatXMPPEntity, |
89 dest_jid: jid.JID, | 89 dest_jid: jid.JID, |
90 message_id: str, | 90 message_id: str, |
91 reactions: Iterable[str] | 91 reactions: Iterable[str] |
101 message_elt["to"] = dest_jid.full() | 101 message_elt["to"] = dest_jid.full() |
102 reactions_elt = message_elt.addElement((NS_REACTIONS, "reactions")) | 102 reactions_elt = message_elt.addElement((NS_REACTIONS, "reactions")) |
103 reactions_elt["id"] = message_id | 103 reactions_elt["id"] = message_id |
104 for r in set(reactions): | 104 for r in set(reactions): |
105 reactions_elt.addElement("reaction", content=r) | 105 reactions_elt.addElement("reaction", content=r) |
106 self._h.addHintElements(message_elt, [self._h.HINT_STORE]) | 106 self._h.add_hint_elements(message_elt, [self._h.HINT_STORE]) |
107 client.send(message_elt) | 107 client.send(message_elt) |
108 | 108 |
109 async def addReactionsToHistory( | 109 async def add_reactions_to_history( |
110 self, | 110 self, |
111 history: History, | 111 history: History, |
112 from_jid: jid.JID, | 112 from_jid: jid.JID, |
113 reactions: Iterable[str] | 113 reactions: Iterable[str] |
114 ) -> None: | 114 ) -> None: |
127 # reactions are sorted to in summary to keep a consistent order | 127 # reactions are sorted to in summary to keep a consistent order |
128 h_reactions["by_jid"][from_jid.userhost()] = sorted(list(set(reactions))) | 128 h_reactions["by_jid"][from_jid.userhost()] = sorted(list(set(reactions))) |
129 h_reactions["summary"] = sorted(list(set().union(*by_jid.values()))) | 129 h_reactions["summary"] = sorted(list(set().union(*by_jid.values()))) |
130 await self.host.memory.storage.session_add(history) | 130 await self.host.memory.storage.session_add(history) |
131 | 131 |
132 async def setReactions( | 132 async def set_reactions( |
133 self, | 133 self, |
134 client: SatXMPPEntity, | 134 client: SatXMPPEntity, |
135 message_id: str, | 135 message_id: str, |
136 reactions: Iterable[str] | 136 reactions: Iterable[str] |
137 ) -> None: | 137 ) -> None: |
155 if not mess_id: | 155 if not mess_id: |
156 raise exceptions.DataError( | 156 raise exceptions.DataError( |
157 "target message has neither origin-id nor message-id, we can't send a " | 157 "target message has neither origin-id nor message-id, we can't send a " |
158 "reaction" | 158 "reaction" |
159 ) | 159 ) |
160 await self.addReactionsToHistory(history, client.jid, reactions) | 160 await self.add_reactions_to_history(history, client.jid, reactions) |
161 self.sendReactions(client, history.dest_jid, mess_id, reactions) | 161 self.send_reactions(client, history.dest_jid, mess_id, reactions) |
162 | 162 |
163 | 163 |
164 @implementer(iwokkel.IDisco) | 164 @implementer(iwokkel.IDisco) |
165 class XEP_0444_Handler(xmlstream.XMPPHandler): | 165 class XEP_0444_Handler(xmlstream.XMPPHandler): |
166 | 166 |