Mercurial > libervia-web
annotate libervia/web/pages/chat/_browser/__init__.py @ 1644:8a09eea9003f default tip
doc (user): Add doc for forum feature:
fix 463
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 06 Sep 2025 17:28:07 +0200 |
parents | c03297bb8d19 |
children |
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 |
1619 | 3 from typing import Callable |
4 import errors | |
1536 | 5 |
6 from bridge import AsyncBridge as Bridge | |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
7 from browser import DOMNode, aio, console as log, document, html, window |
1619 | 8 from cache import cache, identities, roster |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
9 import dialog |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
10 from file_uploader import FileUploader |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
11 import jid |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
12 from javascript import pyobj2jsobj, NULL |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
13 from js_modules import emoji_picker_element |
1619 | 14 from js_modules.tippy_js import tippy as tippy_ori |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
15 from js_modules.quill import Quill |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
16 import popup |
1536 | 17 from template import Template, safe |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
18 from tools import is_touch_device, remove_ids, make_placeholder |
1619 | 19 from loading import remove_loading_screen |
20 from jid_search import JidSearch | |
21 from interpreter import Inspector | |
22 from components import init_collapsible_cards | |
1536 | 23 |
24 log.warning = log.warn | |
25 profile = window.profile or "" | |
1595
7941444c1671
pages: set `own_local_jid` to avoid confusion with `own_jid`:
Goffi <goffi@goffi.org>
parents:
1585
diff
changeset
|
26 # JID used in the local chat (real JID for one2one, room JID otherwise) |
7941444c1671
pages: set `own_local_jid` to avoid confusion with `own_jid`:
Goffi <goffi@goffi.org>
parents:
1585
diff
changeset
|
27 own_local_jid = jid.JID(window.own_local_jid) |
1536 | 28 target_jid = jid.JID(window.target_jid) |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
29 chat_type = window.chat_type |
1619 | 30 chat_url = window.chat_url |
1536 | 31 bridge = Bridge() |
32 | |
33 # Sensible value to consider that user is at the bottom | |
34 SCROLL_SENSITIVITY = 200 | |
35 | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
36 INPUT_MODES = {"normal", "edit", "quote"} |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
37 MODE_CLASS = "mode_{}" |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
38 SELECTED_THREAD_CLS = "has-background-info-light" |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
39 |
1536 | 40 |
1619 | 41 # FIXME: workaround for https://github.com/brython-dev/brython/issues/2542 |
42 def tippy(target, data): | |
43 return tippy_ori(target, pyobj2jsobj(data)) | |
44 | |
45 | |
46 class NewChatDialog: | |
47 | |
48 def __init__(self, on_select: Callable[[str], None]|None = None) -> None: | |
49 self.on_select = on_select | |
50 self.new_chat_dialog_tpl = Template("chat/new_chat_dialog.html") | |
51 self.dialog_elt = self.new_chat_dialog_tpl.get_elt() | |
52 self.modal = dialog.Modal(self.dialog_elt, is_card=True) | |
53 | |
54 # direct chat | |
55 self.direct_search_input_elt = self.dialog_elt.select_one( | |
56 "div.direct-content input.search-input" | |
57 ) | |
58 self.direct_items_container = self.dialog_elt.select_one(".direct-items") | |
59 self.direct_count_elt = self.dialog_elt.select_one(".direct-count") | |
60 self.start_chat_btn = self.dialog_elt.select_one(".action_ok") | |
61 assert self.start_chat_btn is not None | |
62 self.start_chat_btn.bind("click", self.on_start_chat_btn) | |
63 if not self.direct_count_elt or not self.start_chat_btn: | |
64 log.error('"direct-count" or "action_ok" element is missing.') | |
65 self.selected_entities = set() | |
66 self.jid_search = JidSearch( | |
67 self.direct_search_input_elt, | |
68 self.direct_items_container, | |
69 click_cb = self.on_search_item_click, | |
70 template = "chat/search_item.html", | |
71 ) | |
72 for elt in self.dialog_elt.select(".action_close"): | |
73 elt.bind("click", lambda __: self.close()) | |
74 for elt in self.dialog_elt.select("div.direct-content .action_clear_search"): | |
75 elt.bind("click", lambda __: self.clear_search_input()) | |
76 | |
77 # groups | |
78 self.groups_search_input_elt = self.dialog_elt.select_one( | |
79 "div.groups-content input.search-input" | |
80 ) | |
81 self.groups_items_container = self.dialog_elt.select_one(".groups-items") | |
82 | |
83 self.selected_entities = set() | |
84 | |
85 self.groups_jid_search = JidSearch( | |
86 self.groups_search_input_elt, | |
87 self.groups_items_container, | |
88 click_cb = self.on_group_search_item_click, | |
89 options={"type": "group"}, | |
90 template = "chat/groups_search_item.html", | |
91 ) | |
92 for elt in self.dialog_elt.select(".action_close"): | |
93 elt.bind("click", lambda __: self.close()) | |
94 for elt in self.dialog_elt.select("div.groups-content .action_clear_search"): | |
95 elt.bind("click", lambda __: self.clear_groups_search_input()) | |
96 | |
97 self.new_room_btn = self.dialog_elt.select_one(".action_new_room") | |
98 assert self.new_room_btn is not None | |
99 self.new_room_btn.bind("click", self.on_new_room_btn) | |
100 self.panel_new_room = self.dialog_elt.select_one(".panel_new_room") | |
101 assert self.panel_new_room is not None | |
102 self.create_room_btn = self.dialog_elt.select_one(".action_create_room") | |
103 assert self.create_room_btn is not None | |
104 self.create_room_btn.bind("click", self._on_create_room_btn) | |
105 | |
106 self.error_message_elt = self.dialog_elt.select_one("div.error-message") | |
107 assert self.error_message_elt is not None | |
108 self.error_message_elt.select_one("button.delete").bind( | |
109 "click", | |
110 lambda __: self.error_message_elt.classList.add("is-hidden") | |
111 ) | |
112 | |
113 # tabs | |
114 self.tabs = {} | |
115 self.selected_tab_elt = None | |
116 for tab_elt in self.dialog_elt.select('div.tabs>ul>li'): | |
117 if tab_elt.classList.contains("is-active"): | |
118 self.selected_tab_elt = tab_elt | |
119 tab_name = tab_elt.dataset.tab | |
120 tab_content_elt = self.dialog_elt.select_one(f".{tab_name}-content") | |
121 assert tab_content_elt is not None | |
122 self.tabs[tab_elt] = tab_content_elt | |
123 tab_elt.bind( | |
124 'click', | |
125 lambda __, tab_elt=tab_elt: self.set_active_tab(tab_elt) | |
126 ) | |
127 | |
128 def set_active_tab(self, selected_tab_elt) -> None: | |
129 """Display a tab.""" | |
130 self.selected_tab_elt = selected_tab_elt | |
131 for tab_elt, tab_content_elt in self.tabs.items(): | |
132 if tab_elt == selected_tab_elt: | |
133 tab_elt.classList.add("is-active") | |
134 tab_content_elt.classList.remove("is-hidden") | |
135 else: | |
136 tab_elt.classList.remove("is-active") | |
137 tab_content_elt.classList.add("is-hidden") | |
138 self.update() | |
139 | |
140 def clear_search_input(self) -> None: | |
141 """Clear search input, and update dialog.""" | |
142 self.direct_search_input_elt.value = "" | |
143 self.direct_search_input_elt.dispatchEvent(window.Event.new("input")) | |
144 self.update() | |
145 | |
146 def clear_groups_search_input(self) -> None: | |
147 """Clear search input, and update dialog.""" | |
148 self.groups_search_input_elt.value = "" | |
149 self.groups_search_input_elt.dispatchEvent(window.Event.new("input")) | |
150 self.groups_update() | |
151 | |
152 def on_search_item_click(self, event, item) -> None: | |
153 """A search item has been clicked""" | |
154 search_item_elt = event.currentTarget | |
155 search_item_elt.classList.toggle("is-selected") | |
156 self.update() | |
157 | |
158 def on_group_search_item_click(self, event, item) -> None: | |
159 """A search item has been clicked""" | |
160 for item_elt in self.groups_items_container.select(".search-item"): | |
161 if item_elt == event.currentTarget: | |
162 item_elt.classList.add("is-selected") | |
163 else: | |
164 item_elt.classList.remove("is-selected") | |
165 self.update() | |
166 | |
167 def update(self) -> None: | |
168 """Update dialog elements (counter, button) when search items change.""" | |
169 assert self.selected_tab_elt is not None | |
170 current_tab = self.selected_tab_elt.dataset.tab | |
171 match current_tab: | |
172 case "direct": | |
173 self.selected_entities = { | |
174 item_elt.dataset.entity for item_elt in | |
175 self.direct_items_container.select(".search-item.is-selected") | |
176 } | |
177 self.direct_count_elt.text = str(len(self.selected_entities)) | |
178 case "groups": | |
179 self.selected_entities = { | |
180 item_elt.dataset.entity for item_elt in | |
181 self.groups_items_container.select(".search-item.is-selected") | |
182 } | |
183 case _: | |
184 raise ValueError(f"Unknown tab: {current_tab!r}.") | |
185 | |
186 self.start_chat_btn.disabled = not bool(self.selected_entities) | |
187 | |
188 def groups_update(self) -> None: | |
189 """Update dialog elements when groups search items change.""" | |
190 self.selected_entities = { | |
191 item_elt.dataset.entity for item_elt in | |
192 self.direct_items_container.select(".search-item.is-selected") | |
193 } | |
194 self.start_chat_btn.disabled = not bool(self.selected_entities) | |
195 | |
196 def on_new_room_btn(self, evt) -> None: | |
197 self.panel_new_room.classList.toggle("is-hidden") | |
198 | |
199 def _on_create_room_btn(self, evt) -> None: | |
200 aio.run(self.on_create_room_btn()) | |
201 | |
202 async def on_create_room_btn(self) -> None: | |
203 assert self.on_select is not None | |
204 input_elt = self.dialog_elt.select_one(".input-room-name") | |
205 assert input_elt is not None | |
206 try: | |
207 joined_data = await bridge.muc_join(input_elt.value.strip(), "", {}) | |
208 except Exception as e: | |
209 msg = f"Can't create room: {e}" | |
210 log.error(msg) | |
211 self.error_message_elt.select_one("p").text = msg | |
212 self.error_message_elt.classList.remove("is-hidden") | |
213 return | |
214 | |
215 joined, room_jid_s, occupants, user_nick, subject, statuses, profile = joined_data | |
216 self.on_select(room_jid_s) | |
217 | |
218 def on_start_chat_btn(self, evt) -> None: | |
219 evt.stopPropagation() | |
220 if self.on_select is None: | |
221 return | |
222 if not self.selected_entities: | |
223 raise errors.InternalError( | |
224 "Start button should never be called when no entity is selected." | |
225 ) | |
226 if len(self.selected_entities) == 1: | |
227 selected_entity = next(iter(self.selected_entities)) | |
228 self.on_select(selected_entity) | |
229 else: | |
230 aio.run(self.create_room_selected_jids()) | |
231 | |
232 async def create_room_selected_jids(self) -> None: | |
233 assert self.on_select is not None | |
234 joined_data = await bridge.muc_join("", "", {}) | |
235 joined, room_jid_s, occupants, user_nick, subject, statuses, profile = joined_data | |
236 if not self.selected_entities: | |
237 Inspector() | |
238 for entity_jid in self.selected_entities: | |
239 print(f"inviting {entity_jid=}") | |
240 await bridge.muc_invite(entity_jid, room_jid_s, {}) | |
241 self.on_select(room_jid_s) | |
242 | |
243 | |
244 def show(self) -> None: | |
245 """Show the dialog.""" | |
246 # We want ot be sure to have the elements correctly set when dialog is shown | |
247 self.update() | |
248 self.modal.show() | |
249 | |
250 def close(self) -> None: | |
251 """Close the dialog.""" | |
252 self.modal.close() | |
253 | |
254 | |
1536 | 255 class LiberviaWebChat: |
256 def __init__(self): | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
257 self._input_mode = "normal" |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
258 self._rich_edit = False |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
259 self.input_data = {} |
1619 | 260 self.direct_messages_tpl = Template("chat/direct_messages.html") |
1536 | 261 self.message_tpl = Template("chat/message.html") |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
262 self.extra_menu_tpl = Template("chat/extra_menu.html") |
1623
fdb5689fb826
browser (chat): Update following template change + some cleaning:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
263 self.reactions_tpl = Template("components/reactions.html") |
fdb5689fb826
browser (chat): Update following template change + some cleaning:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
264 self.reactions_details_tpl = Template("components/reactions_details.html") |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
265 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
|
266 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
|
267 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
|
268 self.editions_tpl = Template("chat/editions.html") |
1619 | 269 self.occupant_item_tpl = Template("chat/occupant_item.html") |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
270 self.input_extra_menu_tpl = Template("chat/input_extra_menu.html") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
271 self.extra_recipient_field_tpl = Template("chat/extra_recipient_field.html") |
1619 | 272 |
273 # panels and their toggle buttons | |
274 | |
275 self.left_panel = document["left_panel"] | |
276 self.left_toggle = document["left_panel-toggle"] | |
277 self.left_toggle.bind("click", self.on_left_panel_toggle_click) | |
278 self.main_panel = document["main_panel"] | |
279 self.right_panel = document["right_panel"] | |
280 self.right_toggle = document["right_panel-toggle"] | |
281 self.right_toggle.bind("click", self.on_right_panel_toggle_click) | |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
282 |
1536 | 283 self.messages_elt = document["messages"] |
284 | |
1619 | 285 # right-panel internal buttons |
286 init_collapsible_cards(self.right_panel) | |
287 | |
1536 | 288 # attachments |
289 self.file_uploader = FileUploader( | |
1642
c03297bb8d19
server, browser (forums): Redesign of the forum feature:
Goffi <goffi@goffi.org>
parents:
1635
diff
changeset
|
290 "", |
c03297bb8d19
server, browser (forums): Redesign of the forum feature:
Goffi <goffi@goffi.org>
parents:
1635
diff
changeset
|
291 "components/attachment_preview.html", |
c03297bb8d19
server, browser (forums): Redesign of the forum feature:
Goffi <goffi@goffi.org>
parents:
1635
diff
changeset
|
292 on_delete_cb=self.on_attachment_delete |
1536 | 293 ) |
294 self.attachments_elt = document["attachments"] | |
1619 | 295 self.message_input = document["message_input_area"] |
1536 | 296 |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
297 # reply to/thread |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
298 self.sub_messages_panel_tpl = Template("chat/sub_messages_panel.html") |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
299 # current thread panel, if any. |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
300 self.sub_messages_panel = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
301 # Method to call to check if a new message must be displayer in sub-messages |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
302 # panel. |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
303 self.sub_messages_check_cb: Callable|None = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
304 self._reply_to: str|None = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
305 self.thread_id: str|None = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
306 self.keyword: str|None = None |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
307 document["cancel_reply_btn"].bind( |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
308 "click", |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
309 lambda __: setattr(self, "reply_to", None) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
310 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
311 # use `thead` property to modify. |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
312 self._show_thread = None |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
313 |
1619 | 314 # close_button = document.select_one(".modal-close") |
315 # close_button.bind("click", self.close_modal) | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
316 |
1536 | 317 # hide/show attachments |
318 MutationObserver = window.MutationObserver | |
319 observer = MutationObserver.new(lambda *__: self.update_attachments_visibility()) | |
320 observer.observe(self.attachments_elt, {"childList": True}) | |
321 | |
322 # we want the message scroll to be initially at the bottom | |
323 self.messages_elt.scrollTop = self.messages_elt.scrollHeight | |
324 | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
325 # listeners/dynamic updates |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
326 self.add_message_event_listeners() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
327 self.handle_url_previews() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
328 self.add_reactions_listeners() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
329 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
330 # input |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
331 document["input-extra-button"].bind("click", self.on_input_extra_btn) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
332 self.rich_editor = Quill.new("#message_input_area_rich") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
333 for rich_btn_elt in document.select(".rich-editor-btn"): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
334 rich_btn_elt.bind("click", self.on_rich_action) |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
335 self.auto_resize_message_input() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
336 self.message_input.focus() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
337 |
1619 | 338 # direct messages |
339 direct_messages_elt = self.direct_messages_tpl.get_elt( | |
340 { | |
341 "roster": roster, | |
342 "identities": identities, | |
343 "chat_url": chat_url | |
344 } | |
345 ) | |
346 document["direct-messages"] <= direct_messages_elt | |
347 | |
1632
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
348 # data policy |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
349 try: |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
350 data_policy = window.data_policy |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
351 except AttributeError: |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
352 pass |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
353 else: |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
354 data_policy_score = window.data_policy_score |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
355 data_policy_panel_tpl = Template("components/data_policy_panel.html") |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
356 data_policy_panel_elt = data_policy_panel_tpl.get_elt({ |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
357 "detail": data_policy["score"]["detail"], |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
358 "score": data_policy_score, |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
359 }) |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
360 tippy( |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
361 ".data-policy-badge", |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
362 { |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
363 "content": data_policy_panel_elt, |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
364 "appendTo": document.body, |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
365 "placement": "bottom", |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
366 "theme": "light", |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
367 } |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
368 ) |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
369 |
1619 | 370 async def post_init(self) -> None: |
371 if chat_type == "group": | |
372 occupants = await bridge.muc_occupants_get( | |
373 str(target_jid) | |
374 ) | |
375 document["occupants-count"].text = str(len(occupants)) | |
376 for occupant, occupant_data in occupants.items(): | |
377 occupant_elt = self.occupant_item_tpl.get_elt({ | |
378 "nick": occupant, | |
379 "item": occupant_data, | |
380 "identities": identities, | |
381 }) | |
382 document["group-occupants"] <= occupant_elt | |
383 | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
384 @property |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
385 def input_mode(self) -> str: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
386 return self._input_mode |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
387 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
388 @input_mode.setter |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
389 def input_mode(self, new_mode: str) -> None: |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
390 if new_mode == self._input_mode: |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
391 return |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
392 if new_mode not in INPUT_MODES: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
393 raise ValueError(f"Invalid input mode: {new_mode!r}") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
394 target_elt = self.message_input |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
395 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
|
396 self._input_mode = new_mode |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
397 target_elt.classList.add(MODE_CLASS.format(new_mode)) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
398 self.input_data.clear() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
399 |
1536 | 400 @property |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
401 def rich_edit(self) -> bool: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
402 return self._rich_edit |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
403 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
404 @rich_edit.setter |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
405 def rich_edit(self, rich_edit_activated: bool) -> None: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
406 if rich_edit_activated == self._rich_edit: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
407 return |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
408 self._rich_edit = rich_edit_activated |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
409 if rich_edit_activated: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
410 document["rich-edit-toolbar"].classList.remove("is-hidden") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
411 document["message_input_area"].classList.add("is-hidden") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
412 document["message_input_area_rich"].classList.remove("is-hidden") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
413 self.rich_editor.setText(document["message_input_area"].value) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
414 else: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
415 document["rich-edit-toolbar"].classList.add("is-hidden") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
416 document["message_input_area_rich"].classList.add("is-hidden") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
417 document["message_input_area"].classList.remove("is-hidden") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
418 document["message_input_area"].value = self.rich_editor.getText().strip() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
419 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
420 @property |
1536 | 421 def is_at_bottom(self): |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
422 return self.is_elt_at_bottom(self.messages_elt) |
1536 | 423 |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
424 @property |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
425 def reply_to(self) -> dict|None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
426 return self._reply_to |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
427 |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
428 @reply_to.setter |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
429 def reply_to(self, reply_to: dict|None) -> None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
430 if reply_to == self._reply_to: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
431 return |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
432 self._reply_to = reply_to |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
433 document["reply-to_message"].clear() |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
434 if reply_to is None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
435 document["reply-to"].classList.add("is-hidden") |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
436 else: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
437 document["reply-to"].classList.remove("is-hidden") |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
438 parent_message_elt = document[reply_to["id"]] |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
439 cloned_parent_elt = parent_message_elt.cloneNode(True) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
440 remove_ids(cloned_parent_elt) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
441 message_actions_elt = cloned_parent_elt.select_one(".message-actions") |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
442 if message_actions_elt is not None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
443 message_actions_elt.remove() |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
444 document["reply-to_message"] <= cloned_parent_elt |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
445 |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
446 @property |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
447 def show_thread(self) -> str|None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
448 """Indicate the thread to highlight""" |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
449 return self._show_thread |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
450 |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
451 @show_thread.setter |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
452 def show_thread(self, thread_id: str|None) -> None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
453 """Set the thread to highlight, or None to clear view.""" |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
454 if self._show_thread == thread_id: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
455 return |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
456 if self._show_thread is not None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
457 # If we have a previously selected thread, we clean the view. |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
458 for message_core_elt in self.messages_elt.select(".message-core"): |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
459 message_core_elt.classList.remove(SELECTED_THREAD_CLS) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
460 self._show_thread = thread_id |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
461 if thread_id is not None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
462 for message_elt in self.messages_elt.select(".chat-message"): |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
463 try: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
464 message_thread_id = message_elt.dataset["thread"] |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
465 except KeyError: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
466 continue |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
467 if message_thread_id == thread_id: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
468 message_core_elt = message_elt.select_one(".message-core") |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
469 if not message_core_elt: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
470 log.debug( |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
471 f"Not message core found for message {message_elt['id']!r}." |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
472 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
473 else: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
474 message_core_elt.classList.add(SELECTED_THREAD_CLS) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
475 |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
476 def is_elt_at_bottom(self, elt) -> bool: |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
477 """Tell is a scrollable elemement is scrolled down. |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
478 |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
479 There is a margin to check it set with SCROLL_SENSITIVITY. |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
480 @param element: Scrollable element to check. |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
481 @return: True if the element is as its bottom. |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
482 """ |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
483 return elt.scrollHeight - elt.scrollTop - elt.clientHeight <= SCROLL_SENSITIVITY |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
484 |
1619 | 485 def open_chat(self, entity_jid: str) -> None: |
486 """Change the current chat for the given one.""" | |
487 # For now we keep it simple and just load the new location. | |
488 window.location = f"{chat_url}/{entity_jid}" | |
489 | |
490 async def on_new_chat(self) -> None: | |
491 new_chat_dialog = NewChatDialog(on_select = self.open_chat) | |
492 new_chat_dialog.show() | |
493 | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
494 async def send_message(self): |
1536 | 495 """Send message currently in input area |
496 | |
497 The message and corresponding attachment will be sent | |
498 """ | |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
499 extra = {} |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
500 if not self.rich_edit: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
501 message = self.message_input.value.rstrip() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
502 else: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
503 message = self.rich_editor.getText().strip() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
504 if self.has_rich_formatting(): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
505 extra["xhtml"] = self.rich_editor.getSemanticHTML() |
1536 | 506 |
507 # attachments | |
508 attachments = [] | |
509 for attachment_elt in self.attachments_elt.children: | |
510 file_data = json.loads(attachment_elt.getAttribute("data-file")) | |
511 attachments.append(file_data) | |
512 | |
513 if message or attachments: | |
514 | |
515 if attachments: | |
516 extra["attachments"] = attachments | |
517 | |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
518 if self.reply_to: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
519 extra["reply"] = self.reply_to |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
520 |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
521 if self.thread_id: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
522 extra["thread"] = self.thread_id |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
523 |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
524 if self.keyword: |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
525 extra.setdefault("keywords", []).append(self.keyword) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
526 |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
527 input_panel = document["input-panel"] |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
528 recipient_field_elts = list(input_panel.select("div.recipient-field")) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
529 if recipient_field_elts: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
530 addresses = {} |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
531 for recipient_field_elt in recipient_field_elts: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
532 input_elt = recipient_field_elt.select_one("input") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
533 assert input_elt is not None |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
534 recipient_jid = input_elt.value.strip() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
535 if not recipient_jid: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
536 continue |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
537 select_elt = recipient_field_elt.select_one("select") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
538 assert select_elt is not None |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
539 recipient_type = select_elt.value |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
540 if recipient_type not in ("to", "cc", "bcc"): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
541 dialog.notification.show( |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
542 f"Unexpected recipient type: {recipient_type:r}.", |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
543 "error" |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
544 ) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
545 recipient_type = "to" |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
546 addresses.setdefault(recipient_type, []).append({"jid": recipient_jid}) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
547 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
548 if addresses: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
549 extra["addresses"] = addresses |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
550 |
1536 | 551 # now we send the message |
552 try: | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
553 if self.input_mode == "edit": |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
554 message_id = self.input_data["id"] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
555 edit_data = { |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
556 "message": {"": message}, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
557 "extra": extra |
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 await bridge.message_edit( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
560 message_id, json.dumps(edit_data, ensure_ascii=False) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
561 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
562 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
563 await bridge.message_send( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
564 str(target_jid), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
565 {"": message}, {}, "auto", json.dumps(extra, ensure_ascii=False) |
1536 | 566 ) |
567 except Exception as e: | |
568 dialog.notification.show(f"Can't send message: {e}", "error") | |
569 else: | |
570 self.message_input.value = "" | |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
571 self.rich_editor.setText("") |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
572 # We must not reset self.thread_id here, it is when the thread panel is |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
573 # closed. |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
574 # FIXME: Another mechanism must be used if the input panel is cloned at |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
575 # some point, with a slide-in panel for thread, as thread_id would then |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
576 # be used for the thread panel's message input, but not if the main area |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
577 # message input is used. |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
578 self.reply_to = None |
1536 | 579 self.attachments_elt.clear() |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
580 if recipient_field_elts: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
581 for recipient_field_elt in recipient_field_elts: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
582 recipient_field_elt.remove() |
1619 | 583 self.auto_resize_message_input() |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
584 self.input_mode = "normal" |
1536 | 585 |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
586 def has_rich_formatting(self): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
587 """Indicates if there is formatting in the Rich Editor. |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
588 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
589 It works by looking for attributes inserted in Quill contents. |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
590 """ |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
591 delta = self.rich_editor.getContents() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
592 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
593 ops = getattr(delta, 'ops', []) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
594 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
595 for op in ops: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
596 op = dict(op) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
597 if "insert" in op and op.get("attributes"): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
598 return True |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
599 return False |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
600 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
601 def on_rich_action(self, evt): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
602 btn_elt = evt.currentTarget |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
603 action = btn_elt.dataset["action"] |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
604 text_range = self.rich_editor.getSelection() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
605 if not text_range: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
606 return |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
607 format_data = dict(self.rich_editor.getFormat(text_range.index, 1)) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
608 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
609 try: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
610 match action: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
611 case "bold": |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
612 is_bold = format_data.get("bold", False) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
613 self.rich_editor.format("bold", not is_bold) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
614 case "italic": |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
615 is_italic = format_data.get("italic", False) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
616 self.rich_editor.format("italic", not is_italic) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
617 case "underline": |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
618 is_underline = format_data.get("underline", False) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
619 self.rich_editor.format("underline", not is_underline) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
620 case list_type if list_type.startswith("list-"): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
621 list_type = list_type[5:] |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
622 current_list = format_data.get("list") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
623 if current_list == list_type: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
624 self.rich_editor.format("list", False) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
625 else: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
626 self.rich_editor.format("list", list_type) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
627 case "link": |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
628 current_url = format_data.get("link") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
629 if current_url: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
630 self.rich_editor.format("link", False) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
631 else: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
632 url = window.prompt("Enter URL:", "https://") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
633 if url: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
634 self.rich_editor.format("link", url) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
635 case _: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
636 dialog.notification.show(f"Unknown action {action!r}", "error") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
637 except Exception as e: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
638 dialog.notification.show(f"Can't apply action: {e}", "error") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
639 raise e |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
640 |
1536 | 641 def _on_message_new( |
642 self, | |
643 uid: str, | |
644 timestamp: float, | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
645 from_jid_s: str, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
646 to_jid_s: str, |
1536 | 647 message: dict, |
648 subject: dict, | |
649 mess_type: str, | |
650 extra_s: str, | |
651 profile: str, | |
652 ) -> None: | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
653 from_jid = jid.JID(from_jid_s) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
654 to_jid = jid.JID(to_jid_s) |
1536 | 655 if ( |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
656 from_jid.bare == window.target_jid |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
657 or to_jid.bare == window.target_jid |
1536 | 658 ): |
659 aio.run( | |
660 self.on_message_new( | |
661 uid, | |
662 timestamp, | |
663 from_jid, | |
664 to_jid, | |
665 message, | |
666 subject, | |
667 mess_type, | |
668 json.loads(extra_s), | |
669 profile, | |
670 ) | |
671 ) | |
672 | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
673 def _on_message_update( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
674 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
|
675 ) -> None: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
676 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
|
677 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
678 async def on_message_update( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
679 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
|
680 ) -> None: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
681 update_data = json.loads(update_data_s) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
682 is_at_bottom = self.is_at_bottom |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
683 if type_ == "REACTION": |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
684 reactions = update_data["reactions"] |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
685 log.debug(f"new reactions: {reactions}") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
686 try: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
687 reactions_wrapper_elt = document[f"msg_reactions_{uid}"] |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
688 except KeyError: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
689 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
|
690 else: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
691 log.debug(f"Message {uid} found, new reactions: {reactions}") |
1596
52098b5bab8d
browser (chat): add data for reactions
Goffi <goffi@goffi.org>
parents:
1595
diff
changeset
|
692 reactions_elt = self.reactions_tpl.get_elt( |
52098b5bab8d
browser (chat): add data for reactions
Goffi <goffi@goffi.org>
parents:
1595
diff
changeset
|
693 { |
1623
fdb5689fb826
browser (chat): Update following template change + some cleaning:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
694 "own_jid": str( |
fdb5689fb826
browser (chat): Update following template change + some cleaning:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
695 own_local_jid if chat_type == "group" else own_local_jid.bare |
fdb5689fb826
browser (chat): Update following template change + some cleaning:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
696 ), |
1596
52098b5bab8d
browser (chat): add data for reactions
Goffi <goffi@goffi.org>
parents:
1595
diff
changeset
|
697 "reactions": reactions |
52098b5bab8d
browser (chat): add data for reactions
Goffi <goffi@goffi.org>
parents:
1595
diff
changeset
|
698 }) |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
699 reactions_wrapper_elt.clear() |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
700 reactions_wrapper_elt <= reactions_elt |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
701 self.add_reactions_listeners(reactions_elt) |
1585
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
702 elif type_ in ("EDIT", "RETRACT"): |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
703 try: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
704 old_message_elt = document[uid] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
705 except KeyError: |
1585
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
706 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
|
707 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
708 template_data = await self.message_to_template_data( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
709 uid, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
710 update_data["timestamp"], |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
711 jid.JID(update_data["from"]), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
712 jid.JID(update_data["to"]), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
713 update_data["message"], |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
714 update_data["subject"], |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
715 update_data["type"], |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
716 update_data["extra"] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
717 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
718 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
719 new_message_elt = self.message_tpl.get_elt( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
720 template_data |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
721 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
722 old_message_elt.replaceWith(new_message_elt) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
723 self.add_message_event_listeners(new_message_elt) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
724 self.handle_url_previews(new_message_elt) |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
725 else: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
726 log.warning(f"Unsupported update type: {type_!r}") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
727 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
728 # 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
|
729 if is_at_bottom: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
730 self.messages_elt.scrollTop = self.messages_elt.scrollHeight |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
731 |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
732 async def message_to_template_data( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
733 self, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
734 uid: str, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
735 timestamp: float, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
736 from_jid: jid.JID, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
737 to_jid: jid.JID, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
738 message_data: dict, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
739 subject_data: dict, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
740 mess_type: str, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
741 extra: dict, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
742 ) -> dict: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
743 """Generate template data to use with [message_tpl] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
744 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
745 @return: template data |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
746 """ |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
747 # FIXME: We do this deconstruction because of the historical way XEP-0071 build |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
748 # the `xhtml` keys (i.e., `xhtml_<lang>`, because `extra` was a string to string |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
749 # mapping). This will be refactored when we'll fully move messages to Pydantic |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
750 # models, then a proper mapping from language to data will be used. |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
751 xhtml_data = { |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
752 key.partition("_")[2]: xhtml |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
753 for key, xhtml in extra.items() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
754 if key .startswith("xhtml") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
755 } |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
756 if not xhtml_data: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
757 xhtml = None |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
758 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
759 try: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
760 xhtml = xhtml_data[""] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
761 except KeyError: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
762 xhtml = next(iter(xhtml_data.values())) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
763 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
764 if chat_type == "group": |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
765 await cache.fill_identities([str(from_jid)]) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
766 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
767 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
|
768 from_jid = from_jid.bare |
1613
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
769 attachments = extra.get("attachments", []) |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
770 for attachment in attachments: |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
771 if "url" not in attachment: |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
772 try: |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
773 attachment["url"] = next( |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
774 s['url'] for s in attachment["sources"] if 'url' in s |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
775 ) |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
776 except (StopIteration, KeyError): |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
777 log.warning( |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
778 f"An attachment has no URL: {attachment}" |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
779 ) |
1610
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
780 msg_data = { |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
781 "id": uid, |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
782 "timestamp": extra.get("updated", timestamp), |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
783 "type": mess_type, |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
784 "from_": str(from_jid), |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
785 "text": message_data.get("") or next(iter(message_data.values()), ""), |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
786 "subject": subject_data.get("") or next(iter(subject_data.values()), ""), |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
787 "type": mess_type, |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
788 "reeceived": extra.get("received_timestamp") or timestamp, |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
789 "encrypted": extra.get("encrypted", False), |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
790 "received": extra.get("received", False), |
1613
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
791 "attachments": attachments, |
1610
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
792 "extra": extra |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
793 } |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
794 for key in ("thread", "thread_parent", "delay_sender", "info_type"): |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
795 value = extra.get(key) |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
796 if value is not None: |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
797 msg_data[key] = value |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
798 |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
799 if xhtml: |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
800 # XHTML is guaranteed to be safe by the backend. |
1610
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
801 msg_data["html"] = safe(xhtml) |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
802 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
803 return { |
1595
7941444c1671
pages: set `own_local_jid` to avoid confusion with `own_jid`:
Goffi <goffi@goffi.org>
parents:
1585
diff
changeset
|
804 "own_local_jid": str(own_local_jid), |
7941444c1671
pages: set `own_local_jid` to avoid confusion with `own_jid`:
Goffi <goffi@goffi.org>
parents:
1585
diff
changeset
|
805 "chat_type": chat_type, |
1610
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
806 "msg": msg_data, |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
807 "identities": identities, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
808 } |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
809 |
1536 | 810 async def on_message_new( |
811 self, | |
812 uid: str, | |
813 timestamp: float, | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
814 from_jid: jid.JID, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
815 to_jid: jid.JID, |
1536 | 816 message_data: dict, |
817 subject_data: dict, | |
818 mess_type: str, | |
819 extra: dict, | |
820 profile: str, | |
821 ) -> None: | |
1542
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
822 # 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
|
823 # 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
|
824 if ( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
825 document.visibilityState == "hidden" |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
826 and self.new_messages_marker_elt.parent is None |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
827 ): |
1542
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
828 # 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
|
829 # it |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
830 self.messages_elt <= self.new_messages_marker_elt |
1536 | 831 |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
832 template_data = await self.message_to_template_data( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
833 uid, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
834 timestamp, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
835 from_jid, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
836 to_jid, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
837 message_data, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
838 subject_data, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
839 mess_type, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
840 extra |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
841 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
842 |
1536 | 843 message_elt = self.message_tpl.get_elt( |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
844 template_data |
1536 | 845 ) |
846 | |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
847 |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
848 if "reply" in extra: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
849 parent_id = extra["reply"]["id"] |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
850 try: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
851 parent_message_elt = document[parent_id] |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
852 except KeyError: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
853 log.info( |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
854 "Parent message of reply not found in current history: " |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
855 f"{parent_id!r}" |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
856 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
857 else: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
858 thread_id = extra.get("thread", parent_id) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
859 if "thread" not in parent_message_elt.dataset: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
860 parent_message_elt.dataset["thread"] = thread_id |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
861 # TODO: Regenerate parent message, so thread icon will appear. |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
862 |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
863 if ( |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
864 self.sub_messages_check_cb is not None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
865 and self.sub_messages_panel is not None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
866 and self.sub_messages_check_cb(extra) |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
867 ): |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
868 # FIXME: This is quite fragile, IDs are removed, meaning that some listener |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
869 # may not be working correctly, |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
870 cloned_message_elt = message_elt.cloneNode(True) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
871 remove_ids(cloned_message_elt) |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
872 sub_messages_elt = document["sub-messages"] |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
873 is_at_bottom = self.is_elt_at_bottom(sub_messages_elt) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
874 sub_messages_elt.appendChild(cloned_message_elt) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
875 if is_at_bottom: |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
876 sub_messages_elt.scrollTop = sub_messages_elt.scrollHeight |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
877 |
1536 | 878 # Check if user is viewing older messages or is at the bottom |
879 is_at_bottom = self.is_at_bottom | |
880 | |
881 self.messages_elt <= message_elt | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
882 self.add_message_event_listeners(message_elt) |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
883 # 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
|
884 self.handle_url_previews(message_elt) |
1536 | 885 |
886 # If user was at the bottom, keep the scroll at the bottom | |
887 if is_at_bottom: | |
888 self.messages_elt.scrollTop = self.messages_elt.scrollHeight | |
889 | |
890 def auto_resize_message_input(self): | |
891 """Resize the message input field according to content.""" | |
892 is_at_bottom = self.is_at_bottom | |
893 | |
894 # The textarea's height is first reset to 'auto' to ensure it's not influenced by | |
895 # the previous content. | |
896 self.message_input.style.height = "auto" | |
897 | |
898 # Then the height is set to the scrollHeight of the textarea (which is the height | |
899 # of the content), plus the vertical border, resulting in a textarea that grows as | |
900 # more lines of text are added. | |
901 self.message_input.style.height = f"{self.message_input.scrollHeight + 2}px" | |
902 | |
903 if is_at_bottom: | |
904 # we want the message are to still display the last message | |
905 self.messages_elt.scrollTop = self.messages_elt.scrollHeight | |
906 | |
1619 | 907 def on_left_panel_toggle_click(self, evt) -> None: |
908 """Show/Hide side bar.""" | |
909 self.left_panel.classList.toggle("is-collapsed") | |
910 self.main_panel.classList.toggle("is-expanded-left") | |
911 | |
912 def on_right_panel_toggle_click(self, evt) -> None: | |
913 """Show/Hide side bar.""" | |
914 self.right_panel.classList.toggle("is-collapsed") | |
915 self.main_panel.classList.toggle("is-expanded-right") | |
916 | |
1536 | 917 def on_message_keydown(self, evt): |
918 """Handle the 'keydown' event of the message input field | |
919 | |
920 @param evt: The event object. 'target' refers to the textarea element. | |
921 """ | |
922 if evt.keyCode == 13: # <Enter> key | |
923 if not window.navigator.maxTouchPoints: | |
924 # we have a non touch device, we send message on <Enter> | |
925 if not evt.shiftKey: | |
926 evt.preventDefault() # Prevents line break | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
927 aio.run(self.send_message()) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
928 elif evt.keyCode == 27: # <ESC> key |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
929 evt.preventDefault() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
930 self.message_input.value = '' |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
931 self.input_mode = 'normal' |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
932 self.auto_resize_message_input() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
933 elif evt.keyCode == 38: # <Up> arrow key |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
934 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
|
935 evt.preventDefault() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
936 own_msgs = document.getElementsByClassName('own_msg') |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
937 if own_msgs.length > 0: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
938 last_msg = own_msgs[own_msgs.length - 1] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
939 aio.run(self.on_action_edit(None, last_msg)) |
1536 | 940 |
941 def update_attachments_visibility(self): | |
942 if len(self.attachments_elt.children): | |
943 self.attachments_elt.classList.remove("is-contracted") | |
944 else: | |
945 self.attachments_elt.classList.add("is-contracted") | |
946 | |
947 def on_file_selected(self, evt): | |
948 """Handle file selection""" | |
949 log.info("file selected") | |
950 files = evt.currentTarget.files | |
951 self.file_uploader.upload_files(files, self.attachments_elt) | |
952 self.message_input.focus() | |
953 | |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
954 def on_input_extra_btn(self, evt) -> None: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
955 evt.stopPropagation() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
956 input_extra_menu_elt = self.input_extra_menu_tpl.get_elt({ |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
957 "rich_edit": self.rich_edit, |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
958 }) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
959 input_extra_popup = popup.create_popup( |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
960 evt.currentTarget, input_extra_menu_elt, placement="top" |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
961 ) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
962 def on_action_click(evt, callback): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
963 input_extra_popup.hide() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
964 aio.run( |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
965 callback(evt) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
966 ) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
967 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
968 for cls_name, callback in ( |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
969 ("action_toggle_rich_editor", self.on_action_rich_editor), |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
970 ("action_toggle_extra_recipients", self.on_action_extra_recipients), |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
971 ): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
972 for elt in input_extra_menu_elt.select(f".{cls_name}"): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
973 elt.bind("click", lambda evt, callback=callback: on_action_click( |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
974 evt, callback |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
975 )) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
976 |
1536 | 977 def on_attachment_delete(self, evt): |
978 evt.stopPropagation() | |
979 target = evt.currentTarget | |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
980 item_elt = DOMNode(target.closest(".attachment-preview")) |
1536 | 981 item_elt.remove() |
982 | |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
983 def on_attach_btn_click(self, evt): |
1619 | 984 document["file-input"].click() |
1536 | 985 |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
986 def on_reply_btn_click(self, evt) -> None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
987 chat_message_elt = evt.currentTarget.closest("div.chat-message") |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
988 self.reply_to = { |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
989 "id": chat_message_elt["id"], |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
990 "to": chat_message_elt.dataset["from"] |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
991 } |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
992 log.debug(f'"reply_to" set to {self.reply_to!r}') |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
993 |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
994 |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
995 def on_extra_btn_click(self, evt): |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
996 message_elt = evt.currentTarget.closest("div.chat-message") |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
997 message_core_elt = evt.currentTarget.closest("div.message-core") |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
998 is_own = message_elt.classList.contains("own_msg") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
999 if is_own: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1000 own_messages = document.select('.own_msg') |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1001 # 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
|
1002 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
|
1003 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1004 can_edit = False |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1005 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1006 content_elt = self.extra_menu_tpl.get_elt({ |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1007 "edit": can_edit, |
1585
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1008 "retract": is_own, |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1009 }) |
1619 | 1010 extra_popup = popup.create_popup(evt.target, content_elt, focus_elt=message_core_elt) |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1011 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1012 def on_action_click(evt, callback): |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1013 extra_popup.hide() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1014 aio.run( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1015 callback(evt, message_elt) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1016 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1017 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1018 for cls_name, callback in ( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1019 ("action_quote", self.on_action_quote), |
1585
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1020 ("action_edit", self.on_action_edit), |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1021 ("action_retract", self.on_action_retract), |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1022 ("action_forward", self.on_action_forward), |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1023 ): |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1024 for elt in content_elt.select(f".{cls_name}"): |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1025 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
|
1026 evt, callback |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1027 )) |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1028 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1029 def on_reaction_click(self, evt, message_elt): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1030 window.evt = evt |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1031 aio.run( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1032 bridge.message_reactions_set( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1033 message_elt["id"], [evt.detail["unicode"]], "toggle" |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1034 ) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1035 ) |
1619 | 1036 # if evt.deltaY != 0: |
1037 # document["attachments"].scrollLeft += evt.deltaY * 0.8 | |
1038 # evt.preventDefault() | |
1536 | 1039 |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1040 async def show_filtered_messages( |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1041 self, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1042 filters: dict, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1043 sub_messages_panel_args: dict|None = None, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1044 sub_messages_check_cb: Callable[[dict], bool]|None = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1045 ) -> None: |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1046 """Show message matching a filter in a sub-messages panel. |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1047 |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1048 A panel will slide-in from the right to show filtered messages. |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1049 @param filters: Filters to use in ``bridge.history_get``. |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1050 @param sub_messages_panel_args: Args to use with sub_messages_panel template. |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1051 @param sub_messages_check_cb: Method to call to check if a new message must go to |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1052 the sub-messages panel. The method must return True if the message is to be |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1053 displayed in the panel. |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1054 """ |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1055 if sub_messages_panel_args is None: |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1056 sub_messages_panel_args = {} |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1057 self.sub_messages_check_cb = sub_messages_check_cb |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1058 sub_messages_panel_elt = self.sub_messages_panel_tpl.get_elt( |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1059 sub_messages_panel_args |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1060 ) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1061 self.sub_messages_panel = sub_messages_panel_elt |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1062 sub_messages_elt = sub_messages_panel_elt.select_one("#sub-messages") |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1063 sub_messages_input_elt = sub_messages_panel_elt.select_one("#sub-messages-input") |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1064 assert sub_messages_elt is not None |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1065 history_data = await bridge.history_get( |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1066 "", "", -2, True, filters |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1067 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1068 for message_data in history_data: |
1623
fdb5689fb826
browser (chat): Update following template change + some cleaning:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
1069 uid, timestamp, from_jid_s, to_jid_s, message_data, subject_data, mess_type, extra_s = message_data |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1070 template_data = await self.message_to_template_data( |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1071 uid, |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1072 timestamp=timestamp, |
1623
fdb5689fb826
browser (chat): Update following template change + some cleaning:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
1073 from_jid=jid.JID(from_jid_s), |
fdb5689fb826
browser (chat): Update following template change + some cleaning:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
1074 to_jid=jid.JID(to_jid_s), |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1075 message_data=message_data, |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1076 subject_data=subject_data, |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1077 mess_type=mess_type, |
1623
fdb5689fb826
browser (chat): Update following template change + some cleaning:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
1078 extra=json.loads(extra_s) |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1079 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1080 message_elt = self.message_tpl.get_elt(template_data) |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1081 sub_messages_elt <= message_elt |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1082 # FIXME: The whole input-panel is currently moved to the filtered messages panel |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1083 # so listeners don't have to be moved. At some point, it may be better to make a |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1084 # clone (without the IDs) and to clone listeners too, this way a non-modal panel |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1085 # could be used. |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1086 input_panel_elt = document["input-panel"] |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1087 placeholder = make_placeholder(input_panel_elt) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1088 sub_messages_input_elt <= input_panel_elt |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1089 document["input-panel-area"].appendChild(placeholder) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1090 |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1091 def on_sub_messages_panel_close(): |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1092 placeholder.replaceWith(input_panel_elt) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1093 self.thread_id = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1094 self.keyword = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1095 self.sub_messages_panel = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1096 self.sub_messages_check_cb = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1097 |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1098 sub_messages_modal = dialog.Modal( |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1099 sub_messages_panel_elt, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1100 closable=True, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1101 close_cb=on_sub_messages_panel_close, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1102 position="right" |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1103 ) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1104 sub_messages_modal.show() |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1105 |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1106 async def on_show_thread(self, thread_id: str) -> None: |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1107 assert thread_id |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1108 self.thread_id = thread_id |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1109 def sub_messages_check_cb(extra: dict) -> bool: |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1110 return bool( |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1111 self.thread_id |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1112 and "thread" in extra |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1113 and extra["thread"] == self.thread_id |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1114 ) |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1115 |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1116 await self.show_filtered_messages( |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1117 {"thread_id": thread_id}, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1118 {"title": "Thread view"}, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1119 sub_messages_check_cb |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1120 ) |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1121 |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1122 async def _on_keyword_click(self, keyword_elt) -> None: |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1123 self.keyword = keyword_elt.text |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1124 def sub_messages_check_cb(extra: dict) -> bool: |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1125 return self.keyword in extra.get("keywords", []) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1126 await self.show_filtered_messages( |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1127 {"keyword": self.keyword}, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1128 {"title": f"🏷 Label view: {self.keyword}."}, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1129 sub_messages_check_cb |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1130 ) |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1131 |
1628
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1132 async def _on_origin_click(self, origin_elt) -> None: |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1133 origin = origin_elt.dataset["type"] |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1134 label = origin_elt.text |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1135 def sub_messages_check_cb(extra: dict) -> bool: |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1136 return origin in extra.get("origins", []) |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1137 await self.show_filtered_messages( |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1138 {"origin": origin}, |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1139 {"title": f"{label} messages."}, |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1140 sub_messages_check_cb |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1141 ) |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1142 |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1143 |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1144 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
|
1145 """Retrieve message tuple from as sent by [message_new] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1146 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1147 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
|
1148 returned. |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1149 @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
|
1150 message data |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1151 @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
|
1152 """ |
1619 | 1153 print(f"{message_elt=}") |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1154 message_id = message_elt['id'] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1155 history_data = await bridge.history_get( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1156 "", "", -2, True, {"id": message_elt['id']} |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1157 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1158 if not history_data: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1159 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
|
1160 return None |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1161 return history_data[0] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1162 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1163 async def on_action_quote(self, __, message_elt) -> None: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1164 message_data = await self.get_message_tuple(message_elt) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1165 if message_data is not None: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1166 messages = message_data[4] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1167 body = next(iter(messages.values()), "") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1168 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
|
1169 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
|
1170 self.input_mode = "quote" |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1171 self.input_data["id"] = message_elt["id"] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1172 self.auto_resize_message_input() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1173 self.message_input.focus() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1174 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1175 async def on_action_edit(self, __, message_elt) -> None: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1176 message_data = await self.get_message_tuple(message_elt) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1177 if message_data is not None: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1178 messages = message_data[4] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1179 body = next(iter(messages.values()), "") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1180 if not body: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1181 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
|
1182 return |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1183 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1184 self.message_input.value = body |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1185 self.input_mode = "edit" |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1186 self.input_data["id"] = message_elt["id"] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1187 self.auto_resize_message_input() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1188 self.message_input.focus() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1189 |
1585
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1190 async def on_action_retract(self, __, message_elt) -> None: |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1191 confirmed = await dialog.Confirm(safe( |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1192 "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
|
1193 "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
|
1194 "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
|
1195 "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
|
1196 "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
|
1197 "mitigate the risks." |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1198 )).ashow() |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1199 if confirmed: |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1200 await bridge.message_retract(message_elt["id"]) |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1201 else: |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1202 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
|
1203 |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1204 async def on_action_forward(self, __, message_elt) -> None: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1205 # TODO: Use a real dialog here. |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1206 recipient_jid = window.prompt("Enter JID to forward this message to:") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1207 if recipient_jid is NULL: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1208 return |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1209 recipient_jid = recipient_jid.strip() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1210 if recipient_jid: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1211 message_id = message_elt["id"] |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1212 await bridge.message_forward(message_id, recipient_jid) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1213 dialog.notification.show(f"Message forwarded to {recipient_jid}.") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1214 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1215 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1216 async def on_action_rich_editor(self, evt) -> None: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1217 if self.rich_edit and self.has_rich_formatting(): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1218 confirmed = await dialog.Confirm( |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1219 "Switching to simple edit will lose all formatting, are you sure?" |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1220 ).ashow() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1221 if not confirmed: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1222 return |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1223 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1224 self.rich_edit = not self.rich_edit |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1225 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1226 async def on_action_extra_recipients(self, __=None, selected: str = "to") -> None: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1227 elt = self.extra_recipient_field_tpl.get_elt({"selected": selected}) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1228 elt.select_one("button.delete-action").bind( |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1229 "click", |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1230 lambda __: elt.remove() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1231 ) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1232 # We add a new recipient field on [enter]. |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1233 def on_keypress(evt): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1234 if evt.key == "Enter": |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1235 evt.preventDefault() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1236 evt.stopPropagation() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1237 selected = elt.select_one("select").value |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1238 aio.run(self.on_action_extra_recipients(selected=selected)) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1239 input_elt = elt.select_one("input") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1240 if input_elt is None: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1241 dialog.notification.show( |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1242 "Internal error: can't find recipient field's input", |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1243 "error" |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1244 ) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1245 return |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1246 input_elt.bind("keydown", on_keypress) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1247 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1248 toolbar_elt = document["rich-edit-toolbar"] |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1249 toolbar_elt.parent.insertBefore(elt, toolbar_elt) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1250 input_elt.focus() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1251 |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1252 def get_reaction_panel(self, source_elt): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1253 emoji_picker_elt = document.createElement("emoji-picker") |
1619 | 1254 message_elt = source_elt.closest("div.chat-message") |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1255 emoji_picker_elt.bind( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1256 "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
|
1257 ) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1258 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1259 return emoji_picker_elt |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1260 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1261 def add_message_event_listeners(self, parent_elt=None): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1262 """Prepare a message to be dynamic |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1263 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1264 - make attachments dynamically clickable |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1265 - make the extra button clickable |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1266 """ |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1267 ## attachments |
1536 | 1268 # FIXME: only handle images for now, and display them in a modal |
1269 if parent_elt is None: | |
1270 parent_elt = document | |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1271 img_elts = parent_elt.select(".message-attachment img") |
1536 | 1272 for img_elt in img_elts: |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1273 img_elt.bind("click", self.open_modal) |
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1274 img_elt.style.cursor = "pointer" |
1536 | 1275 |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1276 ## reply button |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1277 for reply_btn in parent_elt.select(".reply-button"): |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1278 reply_btn.bind("click", self.on_reply_btn_click) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1279 |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1280 ## reaction button |
1619 | 1281 i = 0 |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1282 for reaction_btn in parent_elt.select(".reaction-button"): |
1619 | 1283 i+=1 |
1284 message_elt = reaction_btn.closest("div.message-core") | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1285 tippy( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1286 reaction_btn, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1287 { |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1288 "trigger": "click", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1289 "content": self.get_reaction_panel, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1290 "appendTo": document.body, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1291 "placement": "bottom", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1292 "interactive": True, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1293 "theme": "light", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1294 "onShow": lambda __, message_elt=message_elt: ( |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1295 message_elt.classList.add("has-popup-focus") |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1296 ), |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1297 "onHide": lambda __, message_elt=message_elt: ( |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1298 message_elt.classList.remove("has-popup-focus") |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1299 ), |
1619 | 1300 } |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1301 ) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1302 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1303 ## extra button |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1304 for extra_btn in parent_elt.select(".extra-button"): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1305 extra_btn.bind("click", self.on_extra_btn_click) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1306 |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1307 ## editions |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1308 for edition_icon_elt in parent_elt.select(".message-editions"): |
1619 | 1309 message_elt = edition_icon_elt.closest("div.chat-message") |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1310 dataset = message_elt.dataset.to_dict() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1311 try: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1312 editions = json.loads(dataset["editions"]) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1313 except (ValueError, KeyError): |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1314 log.error( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1315 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
|
1316 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1317 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1318 for edition in editions: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1319 edition["text"] = ( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1320 edition["message"].get("") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1321 or next(iter(edition["message"].values()), "") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1322 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1323 editions_elt = self.editions_tpl.get_elt({"editions": editions}) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1324 tippy( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1325 edition_icon_elt, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1326 { |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1327 "content": editions_elt, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1328 "theme": "light", |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1329 "appendTo": document.body |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1330 } |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1331 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1332 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1333 |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1334 ## thread |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1335 for thread_icon_elt in parent_elt.select(".message-thread"): |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1336 message_elt = thread_icon_elt.closest("div.chat-message") |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1337 thread_id = message_elt.dataset["thread"] |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1338 thread_icon_elt.bind( |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1339 "mouseenter", |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1340 lambda __, thread_id=thread_id: setattr(self, "show_thread", thread_id) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1341 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1342 thread_icon_elt.bind( |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1343 "mouseleave", |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1344 lambda __, thread_id=thread_id: setattr(self, "show_thread", None) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1345 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1346 thread_icon_elt.bind( |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1347 "click", |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1348 lambda __, thread_id=thread_id: aio.run(self.on_show_thread(thread_id)) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1349 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1350 |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1351 ## keywords |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1352 for keyword_elt in parent_elt.select(".message-keyword"): |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1353 keyword_elt.bind( |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1354 "click", |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1355 lambda __, keyword_elt=keyword_elt: aio.run( |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1356 self._on_keyword_click(keyword_elt) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1357 ) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1358 ) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1359 |
1628
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1360 ## origin |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1361 for origin_elt in parent_elt.select(".message-origin"): |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1362 origin_elt.bind( |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1363 "click", |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1364 lambda __, origin_elt=origin_elt: aio.run( |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1365 self._on_origin_click(origin_elt) |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1366 ) |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1367 ) |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1368 |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1369 |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1370 |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1371 def add_reactions_listeners(self, parent_elt=None) -> None: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1372 """Add listener on reactions to handle details and reaction toggle""" |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1373 if parent_elt is None: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1374 parent_elt = document |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1375 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1376 is_touch = is_touch_device() |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1377 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1378 for reaction_elt in parent_elt.select(".reaction"): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1379 # Reaction details |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1380 dataset = reaction_elt.dataset.to_dict() |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1381 reacting_jids = sorted(json.loads(dataset.get("jids", "[]"))) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1382 reaction_details_elt = self.reactions_details_tpl.get_elt( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1383 {"reacting_jids": reacting_jids, "identities": identities} |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1384 ) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1385 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1386 # Configure tippy based on device type |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1387 tippy_config = { |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1388 "content": reaction_details_elt, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1389 "placement": "bottom", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1390 "theme": "light", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1391 "touch": ["hold", 500] if is_touch else True, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1392 "trigger": "click" if is_touch else "mouseenter focus", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1393 "delay": [0, 800] if is_touch else 0, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1394 } |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1395 tippy(reaction_elt, tippy_config) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1396 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1397 # Toggle reaction when clicked/touched |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1398 emoji_elt = reaction_elt.select_one(".emoji") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1399 emoji = emoji_elt.html.strip() |
1619 | 1400 message_elt = reaction_elt.closest("div.chat-message") |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1401 msg_id = message_elt["id"] |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1402 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1403 def toggle_reaction(event, msg_id=msg_id, emoji=emoji): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1404 # 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
|
1405 if is_touch: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1406 event.preventDefault() |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1407 aio.run(bridge.message_reactions_set(msg_id, [emoji], "toggle")) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1408 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1409 reaction_elt.bind("click", toggle_reaction) |
1536 | 1410 |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1411 def find_links(self, message_elt): |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1412 """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
|
1413 msg_body_elt = message_elt.select_one(".msg_body") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1414 if not msg_body_elt: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1415 return |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1416 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1417 # Extracting links from text content |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1418 text = msg_body_elt.text |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1419 raw_urls = re.findall( |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1420 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
|
1421 text, |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1422 ) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1423 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1424 # Extracting links from <a> elements |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1425 a_elements = msg_body_elt.select("a") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1426 for a_elt in a_elements: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1427 href = a_elt.attrs.get("href", "") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1428 if href.startswith("http://") or href.startswith("https://"): |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1429 raw_urls.append(href) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1430 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1431 # we remove duplicates |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1432 urls = list(dict.fromkeys(raw_urls)) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1433 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1434 return urls |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1435 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1436 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
|
1437 """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
|
1438 url_previews_elt.clear() |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1439 for url in urls: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1440 try: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1441 url_preview_data_s = await bridge.url_preview_get(url, "") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1442 except Exception as e: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1443 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
|
1444 continue |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1445 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1446 if not url_preview_data_s: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1447 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
|
1448 continue |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1449 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1450 url_preview_data = json.loads(url_preview_data_s) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1451 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1452 url_preview_elt = self.url_preview_tpl.get_elt( |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1453 {"url_preview": url_preview_data} |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1454 ) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1455 url_previews_elt <= url_preview_elt |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1456 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1457 def handle_url_previews(self, parent_elt=None): |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1458 """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
|
1459 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1460 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
|
1461 user click, or directly the previews, or nothing at all. |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1462 """ |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1463 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1464 if parent_elt is None: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1465 parent_elt = document |
1619 | 1466 chat_message_elts = parent_elt.select(".chat-message") |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1467 else: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1468 chat_message_elts = [parent_elt] |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1469 for message_elt in chat_message_elts: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1470 urls = self.find_links(message_elt) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1471 if urls: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1472 url_previews_elt = message_elt.select_one(".url-previews") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1473 url_previews_elt.classList.remove("is-hidden") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1474 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
|
1475 fetch_preview_btn = preview_control_elt.select_one( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1476 ".action_fetch_preview" |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1477 ) |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1478 fetch_preview_btn.bind( |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1479 "click", |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1480 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
|
1481 self.add_url_previews(previews_elt, preview_urls) |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1482 ), |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1483 ) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1484 url_previews_elt <= preview_control_elt |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1485 |
1536 | 1486 def open_modal(self, evt): |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1487 modal_image = document.select_one("#modal-image") |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1488 modal_image.src = evt.currentTarget.src |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1489 modal_image.alt = evt.currentTarget.alt |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1490 modal = document.select_one("#modal") |
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1491 modal.classList.add("is-active") |
1536 | 1492 |
1493 def close_modal(self, evt): | |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1494 modal = document.select_one("#modal") |
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1495 modal.classList.remove("is-active") |
1536 | 1496 |
1542
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
1497 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
|
1498 if ( |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
1499 document.visibilityState == "hidden" |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
1500 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
|
1501 ): |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
1502 # 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
|
1503 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
|
1504 |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
1505 |
1536 | 1506 libervia_web_chat = LiberviaWebChat() |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1507 |
1619 | 1508 document["new_chat_btn"].bind( |
1509 "click", lambda __: aio.run(libervia_web_chat.on_new_chat()) | |
1510 ) | |
1511 | |
1536 | 1512 document["message_input"].bind( |
1513 "input", lambda __: libervia_web_chat.auto_resize_message_input() | |
1514 ) | |
1515 document["message_input"].bind("keydown", libervia_web_chat.on_message_keydown) | |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1516 document["send_button"].bind( |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1517 "click", |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1518 lambda __: aio.run(libervia_web_chat.send_message()) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1519 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1520 document["attach-button"].bind("click", libervia_web_chat.on_attach_btn_click) |
1619 | 1521 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
|
1522 |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
1523 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
|
1524 |
1536 | 1525 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
|
1526 bridge.register_signal("message_update", libervia_web_chat._on_message_update) |
1619 | 1527 aio.run(libervia_web_chat.post_init()) |
1528 remove_loading_screen() |