Mercurial > libervia-web
annotate libervia/web/pages/chat/_browser/__init__.py @ 1585:9fc4120888be
browser (chat): message retraction implementation
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 30 Nov 2023 13:25:25 +0100 |
parents | eab815e48795 |
children | 7941444c1671 |
rev | line source |
---|---|
1536 | 1 import json |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
2 import re |
1536 | 3 |
4 from bridge import AsyncBridge as Bridge | |
1585
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
5 from browser import DOMNode, aio, console as log, document, window |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
6 from cache import cache, identities |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
7 import dialog |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
8 from file_uploader import FileUploader |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
9 import jid |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
10 from js_modules import emoji_picker_element |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
11 from js_modules.tippy_js import tippy |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
12 import popup |
1536 | 13 from template import Template, safe |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
14 from tools import is_touch_device |
1536 | 15 |
16 log.warning = log.warn | |
17 profile = window.profile or "" | |
18 own_jid = jid.JID(window.own_jid) | |
19 target_jid = jid.JID(window.target_jid) | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
20 chat_type = window.chat_type |
1536 | 21 bridge = Bridge() |
22 | |
23 # Sensible value to consider that user is at the bottom | |
24 SCROLL_SENSITIVITY = 200 | |
25 | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
26 INPUT_MODES = {"normal", "edit", "quote"} |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
27 MODE_CLASS = "mode_{}" |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
28 |
1536 | 29 |
30 class LiberviaWebChat: | |
31 def __init__(self): | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
32 self._input_mode = "normal" |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
33 self.input_data = {} |
1536 | 34 self.message_tpl = Template("chat/message.html") |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
35 self.extra_menu_tpl = Template("chat/extra_menu.html") |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
36 self.reactions_tpl = Template("chat/reactions.html") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
37 self.reactions_details_tpl = Template("chat/reactions_details.html") |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
38 self.url_preview_control_tpl = Template("components/url_preview_control.html") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
39 self.url_preview_tpl = Template("components/url_preview.html") |
1542
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
40 self.new_messages_marker_elt = Template("chat/new_messages_marker.html").get_elt() |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
41 self.editions_tpl = Template("chat/editions.html") |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
42 |
1536 | 43 self.messages_elt = document["messages"] |
44 | |
45 # attachments | |
46 self.file_uploader = FileUploader( | |
47 "", "chat/attachment_preview.html", on_delete_cb=self.on_attachment_delete | |
48 ) | |
49 self.attachments_elt = document["attachments"] | |
50 self.message_input = document["message_input"] | |
51 | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
52 close_button = document.select_one(".modal-close") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
53 close_button.bind("click", self.close_modal) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
54 |
1536 | 55 # hide/show attachments |
56 MutationObserver = window.MutationObserver | |
57 observer = MutationObserver.new(lambda *__: self.update_attachments_visibility()) | |
58 observer.observe(self.attachments_elt, {"childList": True}) | |
59 | |
60 # we want the message scroll to be initially at the bottom | |
61 self.messages_elt.scrollTop = self.messages_elt.scrollHeight | |
62 | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
63 # listeners/dynamic updates |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
64 self.add_message_event_listeners() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
65 self.handle_url_previews() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
66 self.add_reactions_listeners() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
67 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
68 # input |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
69 self.auto_resize_message_input() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
70 self.message_input.focus() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
71 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
72 @property |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
73 def input_mode(self) -> str: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
74 return self._input_mode |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
75 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
76 @input_mode.setter |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
77 def input_mode(self, new_mode: str) -> None: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
78 if new_mode == self.input_mode: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
79 return |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
80 if new_mode not in INPUT_MODES: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
81 raise ValueError(f"Invalid input mode: {new_mode!r}") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
82 target_elt = self.message_input |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
83 target_elt.classList.remove(MODE_CLASS.format(self._input_mode)) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
84 self._input_mode = new_mode |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
85 target_elt.classList.add(MODE_CLASS.format(new_mode)) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
86 self.input_data.clear() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
87 |
1536 | 88 @property |
89 def is_at_bottom(self): | |
90 return ( | |
91 self.messages_elt.scrollHeight | |
92 - self.messages_elt.scrollTop | |
93 - self.messages_elt.clientHeight | |
94 <= SCROLL_SENSITIVITY | |
95 ) | |
96 | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
97 async def send_message(self): |
1536 | 98 """Send message currently in input area |
99 | |
100 The message and corresponding attachment will be sent | |
101 """ | |
102 message = self.message_input.value.rstrip() | |
103 log.info(f"{message=}") | |
104 | |
105 # attachments | |
106 attachments = [] | |
107 for attachment_elt in self.attachments_elt.children: | |
108 file_data = json.loads(attachment_elt.getAttribute("data-file")) | |
109 attachments.append(file_data) | |
110 | |
111 if message or attachments: | |
112 extra = {} | |
113 | |
114 if attachments: | |
115 extra["attachments"] = attachments | |
116 | |
117 # now we send the message | |
118 try: | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
119 if self.input_mode == "edit": |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
120 message_id = self.input_data["id"] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
121 edit_data = { |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
122 "message": {"": message}, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
123 "extra": extra |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
124 } |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
125 await bridge.message_edit( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
126 message_id, json.dumps(edit_data, ensure_ascii=False) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
127 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
128 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
129 await bridge.message_send( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
130 str(target_jid), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
131 {"": message}, {}, "auto", json.dumps(extra, ensure_ascii=False) |
1536 | 132 ) |
133 except Exception as e: | |
134 dialog.notification.show(f"Can't send message: {e}", "error") | |
135 else: | |
136 self.message_input.value = "" | |
137 self.attachments_elt.clear() | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
138 self.input_mode = "normal" |
1536 | 139 |
140 def _on_message_new( | |
141 self, | |
142 uid: str, | |
143 timestamp: float, | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
144 from_jid_s: str, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
145 to_jid_s: str, |
1536 | 146 message: dict, |
147 subject: dict, | |
148 mess_type: str, | |
149 extra_s: str, | |
150 profile: str, | |
151 ) -> None: | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
152 from_jid = jid.JID(from_jid_s) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
153 to_jid = jid.JID(to_jid_s) |
1536 | 154 if ( |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
155 from_jid.bare == window.target_jid |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
156 or to_jid.bare == window.target_jid |
1536 | 157 ): |
158 aio.run( | |
159 self.on_message_new( | |
160 uid, | |
161 timestamp, | |
162 from_jid, | |
163 to_jid, | |
164 message, | |
165 subject, | |
166 mess_type, | |
167 json.loads(extra_s), | |
168 profile, | |
169 ) | |
170 ) | |
171 | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
172 def _on_message_update( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
173 self, uid: str, type_: str, update_data_s: str, profile: str |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
174 ) -> None: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
175 aio.run(self.on_message_update(uid, type_, update_data_s, profile)) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
176 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
177 async def on_message_update( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
178 self, uid: str, type_: str, update_data_s: str, profile: str |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
179 ) -> None: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
180 update_data = json.loads(update_data_s) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
181 is_at_bottom = self.is_at_bottom |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
182 if type_ == "REACTION": |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
183 reactions = update_data["reactions"] |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
184 log.debug(f"new reactions: {reactions}") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
185 try: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
186 reactions_wrapper_elt = document[f"msg_reactions_{uid}"] |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
187 except KeyError: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
188 log.debug(f"Message {uid} not found, no reactions to update") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
189 else: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
190 log.debug(f"Message {uid} found, new reactions: {reactions}") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
191 reactions_elt = self.reactions_tpl.get_elt({"reactions": reactions}) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
192 reactions_wrapper_elt.clear() |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
193 reactions_wrapper_elt <= reactions_elt |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
194 self.add_reactions_listeners(reactions_elt) |
1585
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
195 elif type_ in ("EDIT", "RETRACT"): |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
196 try: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
197 old_message_elt = document[uid] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
198 except KeyError: |
1585
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
199 log.debug(f"Message {uid} not found, no {type_.lower()}ion to apply") |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
200 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
201 template_data = await self.message_to_template_data( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
202 uid, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
203 update_data["timestamp"], |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
204 jid.JID(update_data["from"]), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
205 jid.JID(update_data["to"]), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
206 update_data["message"], |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
207 update_data["subject"], |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
208 update_data["type"], |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
209 update_data["extra"] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
210 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
211 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
212 new_message_elt = self.message_tpl.get_elt( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
213 template_data |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
214 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
215 old_message_elt.replaceWith(new_message_elt) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
216 self.add_message_event_listeners(new_message_elt) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
217 self.handle_url_previews(new_message_elt) |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
218 else: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
219 log.warning(f"Unsupported update type: {type_!r}") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
220 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
221 # If user was at the bottom, keep the scroll at the bottom |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
222 if is_at_bottom: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
223 self.messages_elt.scrollTop = self.messages_elt.scrollHeight |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
224 |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
225 async def message_to_template_data( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
226 self, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
227 uid: str, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
228 timestamp: float, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
229 from_jid: jid.JID, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
230 to_jid: jid.JID, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
231 message_data: dict, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
232 subject_data: dict, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
233 mess_type: str, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
234 extra: dict, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
235 ) -> dict: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
236 """Generate template data to use with [message_tpl] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
237 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
238 @return: template data |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
239 """ |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
240 xhtml_data = extra.get("xhtml") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
241 if not xhtml_data: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
242 xhtml = None |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
243 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
244 try: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
245 xhtml = xhtml_data[""] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
246 except KeyError: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
247 xhtml = next(iter(xhtml_data.values())) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
248 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
249 if chat_type == "group": |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
250 await cache.fill_identities([str(from_jid)]) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
251 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
252 await cache.fill_identities([str(jid.JID(from_jid).bare)]) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
253 from_jid = from_jid.bare |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
254 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
255 return { |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
256 "own_jid": own_jid, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
257 "msg": { |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
258 "id": uid, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
259 "timestamp": extra.get("updated", timestamp), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
260 "type": mess_type, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
261 "from_": str(from_jid), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
262 "text": message_data.get("") or next(iter(message_data.values()), ""), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
263 "subject": subject_data.get("") or next(iter(subject_data.values()), ""), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
264 "type": mess_type, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
265 "thread": extra.get("thread"), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
266 "thread_parent": extra.get("thread_parent"), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
267 "reeceived": extra.get("received_timestamp") or timestamp, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
268 "delay_sender": extra.get("delay_sender"), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
269 "info_type": extra.get("info_type"), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
270 "html": safe(xhtml) if xhtml else None, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
271 "encrypted": extra.get("encrypted", False), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
272 "received": extra.get("received", False), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
273 "attachments": extra.get("attachments", []), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
274 "extra": extra |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
275 }, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
276 "identities": identities, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
277 } |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
278 |
1536 | 279 async def on_message_new( |
280 self, | |
281 uid: str, | |
282 timestamp: float, | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
283 from_jid: jid.JID, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
284 to_jid: jid.JID, |
1536 | 285 message_data: dict, |
286 subject_data: dict, | |
287 mess_type: str, | |
288 extra: dict, | |
289 profile: str, | |
290 ) -> None: | |
1542
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
291 # FIXME: visibilityState doesn't detect OS events such as `Alt + Tab`, using focus |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
292 # event may help to get those use cases, but it gives false positives. |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
293 if ( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
294 document.visibilityState == "hidden" |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
295 and self.new_messages_marker_elt.parent is None |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
296 ): |
1542
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
297 # the page is not visible, and we have no new messages marker yet, so we add |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
298 # it |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
299 self.messages_elt <= self.new_messages_marker_elt |
1536 | 300 |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
301 template_data = await self.message_to_template_data( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
302 uid, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
303 timestamp, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
304 from_jid, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
305 to_jid, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
306 message_data, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
307 subject_data, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
308 mess_type, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
309 extra |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
310 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
311 |
1536 | 312 message_elt = self.message_tpl.get_elt( |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
313 template_data |
1536 | 314 ) |
315 | |
316 # Check if user is viewing older messages or is at the bottom | |
317 is_at_bottom = self.is_at_bottom | |
318 | |
319 self.messages_elt <= message_elt | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
320 self.add_message_event_listeners(message_elt) |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
321 # we add preview in parallel on purpose, as they can be slow to get |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
322 self.handle_url_previews(message_elt) |
1536 | 323 |
324 # If user was at the bottom, keep the scroll at the bottom | |
325 if is_at_bottom: | |
326 self.messages_elt.scrollTop = self.messages_elt.scrollHeight | |
327 | |
328 def auto_resize_message_input(self): | |
329 """Resize the message input field according to content.""" | |
330 | |
331 is_at_bottom = self.is_at_bottom | |
332 | |
333 # The textarea's height is first reset to 'auto' to ensure it's not influenced by | |
334 # the previous content. | |
335 self.message_input.style.height = "auto" | |
336 | |
337 # Then the height is set to the scrollHeight of the textarea (which is the height | |
338 # of the content), plus the vertical border, resulting in a textarea that grows as | |
339 # more lines of text are added. | |
340 self.message_input.style.height = f"{self.message_input.scrollHeight + 2}px" | |
341 | |
342 if is_at_bottom: | |
343 # we want the message are to still display the last message | |
344 self.messages_elt.scrollTop = self.messages_elt.scrollHeight | |
345 | |
346 def on_message_keydown(self, evt): | |
347 """Handle the 'keydown' event of the message input field | |
348 | |
349 @param evt: The event object. 'target' refers to the textarea element. | |
350 """ | |
351 if evt.keyCode == 13: # <Enter> key | |
352 if not window.navigator.maxTouchPoints: | |
353 # we have a non touch device, we send message on <Enter> | |
354 if not evt.shiftKey: | |
355 evt.preventDefault() # Prevents line break | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
356 aio.run(self.send_message()) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
357 elif evt.keyCode == 27: # <ESC> key |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
358 evt.preventDefault() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
359 self.message_input.value = '' |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
360 self.input_mode = 'normal' |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
361 self.auto_resize_message_input() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
362 elif evt.keyCode == 38: # <Up> arrow key |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
363 if self.input_mode == "normal" and self.message_input.value.strip() == "": |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
364 evt.preventDefault() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
365 own_msgs = document.getElementsByClassName('own_msg') |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
366 if own_msgs.length > 0: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
367 last_msg = own_msgs[own_msgs.length - 1] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
368 aio.run(self.on_action_edit(None, last_msg)) |
1536 | 369 |
370 def update_attachments_visibility(self): | |
371 if len(self.attachments_elt.children): | |
372 self.attachments_elt.classList.remove("is-contracted") | |
373 else: | |
374 self.attachments_elt.classList.add("is-contracted") | |
375 | |
376 def on_file_selected(self, evt): | |
377 """Handle file selection""" | |
378 log.info("file selected") | |
379 files = evt.currentTarget.files | |
380 self.file_uploader.upload_files(files, self.attachments_elt) | |
381 self.message_input.focus() | |
382 | |
383 def on_attachment_delete(self, evt): | |
384 evt.stopPropagation() | |
385 target = evt.currentTarget | |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
386 item_elt = DOMNode(target.closest(".attachment-preview")) |
1536 | 387 item_elt.remove() |
388 | |
389 def on_attach_button_click(self, evt): | |
390 document["file_input"].click() | |
391 | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
392 def on_extra_btn_click(self, evt): |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
393 message_elt = evt.target.closest("div.is-chat-message") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
394 is_own = message_elt.classList.contains("own_msg") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
395 if is_own: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
396 own_messages = document.select('.own_msg') |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
397 # with XMPP, we can currently only edit our last message |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
398 can_edit = own_messages and message_elt is own_messages[-1] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
399 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
400 can_edit = False |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
401 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
402 content_elt = self.extra_menu_tpl.get_elt({ |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
403 "edit": can_edit, |
1585
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
404 "retract": is_own, |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
405 }) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
406 extra_popup = popup.create_popup(evt.target, content_elt, focus_elt=message_elt) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
407 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
408 def on_action_click(evt, callback): |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
409 extra_popup.hide() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
410 aio.run( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
411 callback(evt, message_elt) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
412 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
413 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
414 for cls_name, callback in ( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
415 ("action_quote", self.on_action_quote), |
1585
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
416 ("action_edit", self.on_action_edit), |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
417 ("action_retract", self.on_action_retract), |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
418 ): |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
419 for elt in content_elt.select(f".{cls_name}"): |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
420 elt.bind("click", lambda evt, callback=callback: on_action_click( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
421 evt, callback |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
422 )) |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
423 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
424 def on_reaction_click(self, evt, message_elt): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
425 window.evt = evt |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
426 aio.run( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
427 bridge.message_reactions_set( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
428 message_elt["id"], [evt.detail["unicode"]], "toggle" |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
429 ) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
430 ) |
1536 | 431 if evt.deltaY != 0: |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
432 document["attachments"].scrollLeft += evt.deltaY * 0.8 |
1536 | 433 evt.preventDefault() |
434 | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
435 async def get_message_tuple(self, message_elt) -> tuple|None: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
436 """Retrieve message tuple from as sent by [message_new] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
437 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
438 If not corresponding message data is found, an error will shown, and None is |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
439 returned. |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
440 @param message_elt: message element, it's "id" attribute will be use to retrieve |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
441 message data |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
442 @return: message data as a tuple, or None if not message with this ID is found. |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
443 """ |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
444 message_id = message_elt['id'] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
445 history_data = await bridge.history_get( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
446 "", "", -2, True, {"id": message_elt['id']} |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
447 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
448 if not history_data: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
449 dialog.notification.show(f"Can't find message {message_id}", "error") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
450 return None |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
451 return history_data[0] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
452 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
453 async def on_action_quote(self, __, message_elt) -> None: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
454 message_data = await self.get_message_tuple(message_elt) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
455 if message_data is not None: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
456 messages = message_data[4] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
457 body = next(iter(messages.values()), "") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
458 quote = "\n".join(f"> {l}" for l in body.split("\n")) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
459 self.message_input.value = f"{quote}\n{self.message_input.value}" |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
460 self.input_mode = "quote" |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
461 self.input_data["id"] = message_elt["id"] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
462 self.auto_resize_message_input() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
463 self.message_input.focus() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
464 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
465 async def on_action_edit(self, __, message_elt) -> None: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
466 message_data = await self.get_message_tuple(message_elt) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
467 if message_data is not None: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
468 messages = message_data[4] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
469 body = next(iter(messages.values()), "") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
470 if not body: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
471 dialog.notification.show("No content found in message, nothing to edit") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
472 return |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
473 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
474 self.message_input.value = body |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
475 self.input_mode = "edit" |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
476 self.input_data["id"] = message_elt["id"] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
477 self.auto_resize_message_input() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
478 self.message_input.focus() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
479 |
1585
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
480 async def on_action_retract(self, __, message_elt) -> None: |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
481 confirmed = await dialog.Confirm(safe( |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
482 "This message will be permanently removed. Are you sure?<br><br>" |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
483 "WARNING: It is impossible to guarantee that other participants in the " |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
484 "discussion will delete this message as well. You must assume it has been " |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
485 "seen. If a password or other sensitive information has been accidentally " |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
486 "shared, please ensure to take appropriate measures to change it and " |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
487 "mitigate the risks." |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
488 )).ashow() |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
489 if confirmed: |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
490 await bridge.message_retract(message_elt["id"]) |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
491 else: |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
492 log.info(f"Retraction of message {message_elt['id']} cancelled by user.") |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
493 |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
494 def get_reaction_panel(self, source_elt): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
495 emoji_picker_elt = document.createElement("emoji-picker") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
496 message_elt = source_elt.closest("div.is-chat-message") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
497 emoji_picker_elt.bind( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
498 "emoji-click", lambda evt: self.on_reaction_click(evt, message_elt) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
499 ) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
500 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
501 return emoji_picker_elt |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
502 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
503 def add_message_event_listeners(self, parent_elt=None): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
504 """Prepare a message to be dynamic |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
505 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
506 - make attachments dynamically clickable |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
507 - make the extra button clickable |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
508 """ |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
509 ## attachments |
1536 | 510 # FIXME: only handle images for now, and display them in a modal |
511 if parent_elt is None: | |
512 parent_elt = document | |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
513 img_elts = parent_elt.select(".message-attachment img") |
1536 | 514 for img_elt in img_elts: |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
515 img_elt.bind("click", self.open_modal) |
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
516 img_elt.style.cursor = "pointer" |
1536 | 517 |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
518 ## reaction button |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
519 for reaction_btn in parent_elt.select(".reaction-button"): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
520 message_elt = reaction_btn.closest("div.is-chat-message") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
521 tippy( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
522 reaction_btn, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
523 { |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
524 "trigger": "click", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
525 "content": self.get_reaction_panel, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
526 "appendTo": document.body, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
527 "placement": "bottom", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
528 "interactive": True, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
529 "theme": "light", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
530 "onShow": lambda __, message_elt=message_elt: ( |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
531 message_elt.classList.add("has-popup-focus") |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
532 ), |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
533 "onHide": lambda __, message_elt=message_elt: ( |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
534 message_elt.classList.remove("has-popup-focus") |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
535 ), |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
536 }, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
537 ) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
538 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
539 ## extra button |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
540 for extra_btn in parent_elt.select(".extra-button"): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
541 extra_btn.bind("click", self.on_extra_btn_click) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
542 |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
543 ## editions |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
544 for edition_icon_elt in parent_elt.select(".message-editions"): |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
545 message_elt = edition_icon_elt.closest("div.is-chat-message") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
546 dataset = message_elt.dataset.to_dict() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
547 try: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
548 editions = json.loads(dataset["editions"]) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
549 except (ValueError, KeyError): |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
550 log.error( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
551 f"Internal Error: invalid or missing editions data: {message_elt['id']}" |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
552 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
553 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
554 for edition in editions: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
555 edition["text"] = ( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
556 edition["message"].get("") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
557 or next(iter(edition["message"].values()), "") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
558 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
559 editions_elt = self.editions_tpl.get_elt({"editions": editions}) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
560 tippy( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
561 edition_icon_elt, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
562 { |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
563 "content": editions_elt, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
564 "theme": "light", |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
565 "appendTo": document.body |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
566 } |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
567 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
568 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
569 |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
570 def add_reactions_listeners(self, parent_elt=None) -> None: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
571 """Add listener on reactions to handle details and reaction toggle""" |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
572 if parent_elt is None: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
573 parent_elt = document |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
574 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
575 is_touch = is_touch_device() |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
576 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
577 for reaction_elt in parent_elt.select(".reaction"): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
578 # Reaction details |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
579 dataset = reaction_elt.dataset.to_dict() |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
580 reacting_jids = sorted(json.loads(dataset.get("jids", "[]"))) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
581 reaction_details_elt = self.reactions_details_tpl.get_elt( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
582 {"reacting_jids": reacting_jids, "identities": identities} |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
583 ) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
584 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
585 # Configure tippy based on device type |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
586 tippy_config = { |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
587 "content": reaction_details_elt, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
588 "placement": "bottom", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
589 "theme": "light", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
590 "touch": ["hold", 500] if is_touch else True, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
591 "trigger": "click" if is_touch else "mouseenter focus", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
592 "delay": [0, 800] if is_touch else 0, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
593 } |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
594 tippy(reaction_elt, tippy_config) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
595 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
596 # Toggle reaction when clicked/touched |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
597 emoji_elt = reaction_elt.select_one(".emoji") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
598 emoji = emoji_elt.html.strip() |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
599 message_elt = reaction_elt.closest("div.is-chat-message") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
600 msg_id = message_elt["id"] |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
601 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
602 def toggle_reaction(event, msg_id=msg_id, emoji=emoji): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
603 # Prevent default if it's a touch device to distinguish from long press |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
604 if is_touch: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
605 event.preventDefault() |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
606 aio.run(bridge.message_reactions_set(msg_id, [emoji], "toggle")) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
607 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
608 reaction_elt.bind("click", toggle_reaction) |
1536 | 609 |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
610 def find_links(self, message_elt): |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
611 """Find all http and https links within the body of a message.""" |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
612 msg_body_elt = message_elt.select_one(".msg_body") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
613 if not msg_body_elt: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
614 return |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
615 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
616 # Extracting links from text content |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
617 text = msg_body_elt.text |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
618 raw_urls = re.findall( |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
619 r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F])|#)+", |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
620 text, |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
621 ) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
622 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
623 # Extracting links from <a> elements |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
624 a_elements = msg_body_elt.select("a") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
625 for a_elt in a_elements: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
626 href = a_elt.attrs.get("href", "") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
627 if href.startswith("http://") or href.startswith("https://"): |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
628 raw_urls.append(href) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
629 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
630 # we remove duplicates |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
631 urls = list(dict.fromkeys(raw_urls)) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
632 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
633 return urls |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
634 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
635 async def add_url_previews(self, url_previews_elt, urls) -> None: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
636 """Add URL previews to the .url-previews element of a message.""" |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
637 url_previews_elt.clear() |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
638 for url in urls: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
639 try: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
640 url_preview_data_s = await bridge.url_preview_get(url, "") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
641 except Exception as e: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
642 log.warning(f"Couldn't get URL preview for {url}: {e}") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
643 continue |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
644 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
645 if not url_preview_data_s: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
646 log.warning(f"No preview could be found for URL: {url}") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
647 continue |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
648 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
649 url_preview_data = json.loads(url_preview_data_s) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
650 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
651 url_preview_elt = self.url_preview_tpl.get_elt( |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
652 {"url_preview": url_preview_data} |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
653 ) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
654 url_previews_elt <= url_preview_elt |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
655 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
656 def handle_url_previews(self, parent_elt=None): |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
657 """Check if URL are presents in a message and show appropriate element |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
658 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
659 According to settings, either a preview control panel will be shown to wait for |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
660 user click, or directly the previews, or nothing at all. |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
661 """ |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
662 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
663 if parent_elt is None: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
664 parent_elt = document |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
665 chat_message_elts = parent_elt.select(".is-chat-message") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
666 else: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
667 chat_message_elts = [parent_elt] |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
668 for message_elt in chat_message_elts: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
669 urls = self.find_links(message_elt) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
670 if urls: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
671 url_previews_elt = message_elt.select_one(".url-previews") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
672 url_previews_elt.classList.remove("is-hidden") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
673 preview_control_elt = self.url_preview_control_tpl.get_elt() |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
674 fetch_preview_btn = preview_control_elt.select_one( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
675 ".action_fetch_preview" |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
676 ) |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
677 fetch_preview_btn.bind( |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
678 "click", |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
679 lambda __, previews_elt=url_previews_elt, preview_urls=urls: aio.run( |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
680 self.add_url_previews(previews_elt, preview_urls) |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
681 ), |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
682 ) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
683 url_previews_elt <= preview_control_elt |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
684 |
1536 | 685 def open_modal(self, evt): |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
686 modal_image = document.select_one("#modal-image") |
1536 | 687 modal_image.src = evt.target.src |
688 modal_image.alt = evt.target.alt | |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
689 modal = document.select_one("#modal") |
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
690 modal.classList.add("is-active") |
1536 | 691 |
692 def close_modal(self, evt): | |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
693 modal = document.select_one("#modal") |
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
694 modal.classList.remove("is-active") |
1536 | 695 |
1542
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
696 def handle_visibility_change(self, evt): |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
697 if ( |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
698 document.visibilityState == "hidden" |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
699 and self.new_messages_marker_elt.parent is not None |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
700 ): |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
701 # if there is a new messages marker, we remove it |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
702 self.new_messages_marker_elt.remove() |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
703 |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
704 |
1536 | 705 libervia_web_chat = LiberviaWebChat() |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
706 |
1536 | 707 document["message_input"].bind( |
708 "input", lambda __: libervia_web_chat.auto_resize_message_input() | |
709 ) | |
710 document["message_input"].bind("keydown", libervia_web_chat.on_message_keydown) | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
711 document["send_button"].bind( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
712 "click", |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
713 lambda __: aio.run(libervia_web_chat.send_message()) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
714 ) |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
715 document["attach_button"].bind("click", libervia_web_chat.on_attach_button_click) |
1536 | 716 document["file_input"].bind("change", libervia_web_chat.on_file_selected) |
1542
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
717 |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
718 document.bind("visibilitychange", libervia_web_chat.handle_visibility_change) |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
719 |
1536 | 720 bridge.register_signal("message_new", libervia_web_chat._on_message_new) |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
721 bridge.register_signal("message_update", libervia_web_chat._on_message_update) |