Mercurial > libervia-web
annotate libervia/web/pages/chat/_browser/__init__.py @ 1635:332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
A new "extra" menu is now available next to input field, allowing to toggle to rich
editor. Rich editors allows message styling using bold, italic, underline, (un)numbered
list and link. Other features will probably follow with time.
An extra menu item allows to add recipients, with `to`, `cc` or `bcc` flag like for
emails.
Messages can now be forwarded to any entity with a new item in the 3 dots menu.
rel 461
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 04 Jul 2025 17:47:37 +0200 |
parents | 8400d3b58515 |
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( | |
290 "", "chat/attachment_preview.html", on_delete_cb=self.on_attachment_delete | |
291 ) | |
292 self.attachments_elt = document["attachments"] | |
1619 | 293 self.message_input = document["message_input_area"] |
1536 | 294 |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
295 # reply to/thread |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
296 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
|
297 # current thread panel, if any. |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
298 self.sub_messages_panel = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
299 # 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
|
300 # panel. |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
301 self.sub_messages_check_cb: Callable|None = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
302 self._reply_to: str|None = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
303 self.thread_id: str|None = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
304 self.keyword: str|None = None |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
305 document["cancel_reply_btn"].bind( |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
306 "click", |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
307 lambda __: setattr(self, "reply_to", None) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
308 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
309 # use `thead` property to modify. |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
310 self._show_thread = None |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
311 |
1619 | 312 # close_button = document.select_one(".modal-close") |
313 # close_button.bind("click", self.close_modal) | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
314 |
1536 | 315 # hide/show attachments |
316 MutationObserver = window.MutationObserver | |
317 observer = MutationObserver.new(lambda *__: self.update_attachments_visibility()) | |
318 observer.observe(self.attachments_elt, {"childList": True}) | |
319 | |
320 # we want the message scroll to be initially at the bottom | |
321 self.messages_elt.scrollTop = self.messages_elt.scrollHeight | |
322 | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
323 # listeners/dynamic updates |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
324 self.add_message_event_listeners() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
325 self.handle_url_previews() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
326 self.add_reactions_listeners() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
327 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
328 # input |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
329 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
|
330 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
|
331 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
|
332 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
|
333 self.auto_resize_message_input() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
334 self.message_input.focus() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
335 |
1619 | 336 # direct messages |
337 direct_messages_elt = self.direct_messages_tpl.get_elt( | |
338 { | |
339 "roster": roster, | |
340 "identities": identities, | |
341 "chat_url": chat_url | |
342 } | |
343 ) | |
344 document["direct-messages"] <= direct_messages_elt | |
345 | |
1632
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
346 # data policy |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
347 try: |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
348 data_policy = window.data_policy |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
349 except AttributeError: |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
350 pass |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
351 else: |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
352 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
|
353 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
|
354 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
|
355 "detail": data_policy["score"]["detail"], |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
356 "score": data_policy_score, |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
357 }) |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
358 tippy( |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
359 ".data-policy-badge", |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
360 { |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
361 "content": data_policy_panel_elt, |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
362 "appendTo": document.body, |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
363 "placement": "bottom", |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
364 "theme": "light", |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
365 } |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
366 ) |
8400d3b58515
browser (chat): Show Data Policy Panel on badge hover/touch.
Goffi <goffi@goffi.org>
parents:
1628
diff
changeset
|
367 |
1619 | 368 async def post_init(self) -> None: |
369 if chat_type == "group": | |
370 occupants = await bridge.muc_occupants_get( | |
371 str(target_jid) | |
372 ) | |
373 document["occupants-count"].text = str(len(occupants)) | |
374 for occupant, occupant_data in occupants.items(): | |
375 occupant_elt = self.occupant_item_tpl.get_elt({ | |
376 "nick": occupant, | |
377 "item": occupant_data, | |
378 "identities": identities, | |
379 }) | |
380 document["group-occupants"] <= occupant_elt | |
381 | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
382 @property |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
383 def input_mode(self) -> str: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
384 return self._input_mode |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
385 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
386 @input_mode.setter |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
387 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
|
388 if new_mode == self._input_mode: |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
389 return |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
390 if new_mode not in INPUT_MODES: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
391 raise ValueError(f"Invalid input mode: {new_mode!r}") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
392 target_elt = self.message_input |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
393 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
|
394 self._input_mode = new_mode |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
395 target_elt.classList.add(MODE_CLASS.format(new_mode)) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
396 self.input_data.clear() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
397 |
1536 | 398 @property |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
399 def rich_edit(self) -> bool: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
400 return self._rich_edit |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
401 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
402 @rich_edit.setter |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
403 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
|
404 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
|
405 return |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
406 self._rich_edit = rich_edit_activated |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
407 if rich_edit_activated: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
408 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
|
409 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
|
410 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
|
411 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
|
412 else: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
413 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
|
414 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
|
415 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
|
416 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
|
417 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
418 @property |
1536 | 419 def is_at_bottom(self): |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
420 return self.is_elt_at_bottom(self.messages_elt) |
1536 | 421 |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
422 @property |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
423 def reply_to(self) -> dict|None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
424 return self._reply_to |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
425 |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
426 @reply_to.setter |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
427 def reply_to(self, reply_to: dict|None) -> None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
428 if reply_to == self._reply_to: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
429 return |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
430 self._reply_to = reply_to |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
431 document["reply-to_message"].clear() |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
432 if reply_to is None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
433 document["reply-to"].classList.add("is-hidden") |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
434 else: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
435 document["reply-to"].classList.remove("is-hidden") |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
436 parent_message_elt = document[reply_to["id"]] |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
437 cloned_parent_elt = parent_message_elt.cloneNode(True) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
438 remove_ids(cloned_parent_elt) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
439 message_actions_elt = cloned_parent_elt.select_one(".message-actions") |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
440 if message_actions_elt is not None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
441 message_actions_elt.remove() |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
442 document["reply-to_message"] <= cloned_parent_elt |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
443 |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
444 @property |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
445 def show_thread(self) -> str|None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
446 """Indicate the thread to highlight""" |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
447 return self._show_thread |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
448 |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
449 @show_thread.setter |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
450 def show_thread(self, thread_id: str|None) -> None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
451 """Set the thread to highlight, or None to clear view.""" |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
452 if self._show_thread == thread_id: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
453 return |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
454 if self._show_thread is not None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
455 # 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
|
456 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
|
457 message_core_elt.classList.remove(SELECTED_THREAD_CLS) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
458 self._show_thread = thread_id |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
459 if thread_id is not None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
460 for message_elt in self.messages_elt.select(".chat-message"): |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
461 try: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
462 message_thread_id = message_elt.dataset["thread"] |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
463 except KeyError: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
464 continue |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
465 if message_thread_id == thread_id: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
466 message_core_elt = message_elt.select_one(".message-core") |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
467 if not message_core_elt: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
468 log.debug( |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
469 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
|
470 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
471 else: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
472 message_core_elt.classList.add(SELECTED_THREAD_CLS) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
473 |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
474 def is_elt_at_bottom(self, elt) -> bool: |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
475 """Tell is a scrollable elemement is scrolled down. |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
476 |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
477 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
|
478 @param element: Scrollable element to check. |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
479 @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
|
480 """ |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
481 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
|
482 |
1619 | 483 def open_chat(self, entity_jid: str) -> None: |
484 """Change the current chat for the given one.""" | |
485 # For now we keep it simple and just load the new location. | |
486 window.location = f"{chat_url}/{entity_jid}" | |
487 | |
488 async def on_new_chat(self) -> None: | |
489 new_chat_dialog = NewChatDialog(on_select = self.open_chat) | |
490 new_chat_dialog.show() | |
491 | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
492 async def send_message(self): |
1536 | 493 """Send message currently in input area |
494 | |
495 The message and corresponding attachment will be sent | |
496 """ | |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
497 extra = {} |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
498 if not self.rich_edit: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
499 message = self.message_input.value.rstrip() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
500 else: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
501 message = self.rich_editor.getText().strip() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
502 if self.has_rich_formatting(): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
503 extra["xhtml"] = self.rich_editor.getSemanticHTML() |
1536 | 504 |
505 # attachments | |
506 attachments = [] | |
507 for attachment_elt in self.attachments_elt.children: | |
508 file_data = json.loads(attachment_elt.getAttribute("data-file")) | |
509 attachments.append(file_data) | |
510 | |
511 if message or attachments: | |
512 | |
513 if attachments: | |
514 extra["attachments"] = attachments | |
515 | |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
516 if self.reply_to: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
517 extra["reply"] = self.reply_to |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
518 |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
519 if self.thread_id: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
520 extra["thread"] = self.thread_id |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
521 |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
522 if self.keyword: |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
523 extra.setdefault("keywords", []).append(self.keyword) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
524 |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
525 input_panel = document["input-panel"] |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
526 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
|
527 if recipient_field_elts: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
528 addresses = {} |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
529 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
|
530 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
|
531 assert input_elt is not None |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
532 recipient_jid = input_elt.value.strip() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
533 if not recipient_jid: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
534 continue |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
535 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
|
536 assert select_elt is not None |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
537 recipient_type = select_elt.value |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
538 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
|
539 dialog.notification.show( |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
540 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
|
541 "error" |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
542 ) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
543 recipient_type = "to" |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
544 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
|
545 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
546 if addresses: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
547 extra["addresses"] = addresses |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
548 |
1536 | 549 # now we send the message |
550 try: | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
551 if self.input_mode == "edit": |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
552 message_id = self.input_data["id"] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
553 edit_data = { |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
554 "message": {"": message}, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
555 "extra": extra |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
556 } |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
557 await bridge.message_edit( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
558 message_id, json.dumps(edit_data, ensure_ascii=False) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
559 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
560 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
561 await bridge.message_send( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
562 str(target_jid), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
563 {"": message}, {}, "auto", json.dumps(extra, ensure_ascii=False) |
1536 | 564 ) |
565 except Exception as e: | |
566 dialog.notification.show(f"Can't send message: {e}", "error") | |
567 else: | |
568 self.message_input.value = "" | |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
569 self.rich_editor.setText("") |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
570 # 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
|
571 # closed. |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
572 # 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
|
573 # 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
|
574 # 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
|
575 # message input is used. |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
576 self.reply_to = None |
1536 | 577 self.attachments_elt.clear() |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
578 if recipient_field_elts: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
579 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
|
580 recipient_field_elt.remove() |
1619 | 581 self.auto_resize_message_input() |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
582 self.input_mode = "normal" |
1536 | 583 |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
584 def has_rich_formatting(self): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
585 """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
|
586 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
587 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
|
588 """ |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
589 delta = self.rich_editor.getContents() |
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 ops = getattr(delta, 'ops', []) |
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 for op in ops: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
594 op = dict(op) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
595 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
|
596 return True |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
597 return False |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
598 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
599 def on_rich_action(self, evt): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
600 btn_elt = evt.currentTarget |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
601 action = btn_elt.dataset["action"] |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
602 text_range = self.rich_editor.getSelection() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
603 if not text_range: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
604 return |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
605 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
|
606 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
607 try: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
608 match action: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
609 case "bold": |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
610 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
|
611 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
|
612 case "italic": |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
613 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
|
614 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
|
615 case "underline": |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
616 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
|
617 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
|
618 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
|
619 list_type = list_type[5:] |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
620 current_list = format_data.get("list") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
621 if current_list == list_type: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
622 self.rich_editor.format("list", False) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
623 else: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
624 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
|
625 case "link": |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
626 current_url = format_data.get("link") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
627 if current_url: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
628 self.rich_editor.format("link", False) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
629 else: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
630 url = window.prompt("Enter URL:", "https://") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
631 if url: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
632 self.rich_editor.format("link", url) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
633 case _: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
634 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
|
635 except Exception as e: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
636 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
|
637 raise e |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
638 |
1536 | 639 def _on_message_new( |
640 self, | |
641 uid: str, | |
642 timestamp: float, | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
643 from_jid_s: str, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
644 to_jid_s: str, |
1536 | 645 message: dict, |
646 subject: dict, | |
647 mess_type: str, | |
648 extra_s: str, | |
649 profile: str, | |
650 ) -> None: | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
651 from_jid = jid.JID(from_jid_s) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
652 to_jid = jid.JID(to_jid_s) |
1536 | 653 if ( |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
654 from_jid.bare == window.target_jid |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
655 or to_jid.bare == window.target_jid |
1536 | 656 ): |
657 aio.run( | |
658 self.on_message_new( | |
659 uid, | |
660 timestamp, | |
661 from_jid, | |
662 to_jid, | |
663 message, | |
664 subject, | |
665 mess_type, | |
666 json.loads(extra_s), | |
667 profile, | |
668 ) | |
669 ) | |
670 | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
671 def _on_message_update( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
672 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
|
673 ) -> None: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
674 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
|
675 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
676 async def on_message_update( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
677 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
|
678 ) -> None: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
679 update_data = json.loads(update_data_s) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
680 is_at_bottom = self.is_at_bottom |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
681 if type_ == "REACTION": |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
682 reactions = update_data["reactions"] |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
683 log.debug(f"new reactions: {reactions}") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
684 try: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
685 reactions_wrapper_elt = document[f"msg_reactions_{uid}"] |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
686 except KeyError: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
687 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
|
688 else: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
689 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
|
690 reactions_elt = self.reactions_tpl.get_elt( |
52098b5bab8d
browser (chat): add data for reactions
Goffi <goffi@goffi.org>
parents:
1595
diff
changeset
|
691 { |
1623
fdb5689fb826
browser (chat): Update following template change + some cleaning:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
692 "own_jid": str( |
fdb5689fb826
browser (chat): Update following template change + some cleaning:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
693 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
|
694 ), |
1596
52098b5bab8d
browser (chat): add data for reactions
Goffi <goffi@goffi.org>
parents:
1595
diff
changeset
|
695 "reactions": reactions |
52098b5bab8d
browser (chat): add data for reactions
Goffi <goffi@goffi.org>
parents:
1595
diff
changeset
|
696 }) |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
697 reactions_wrapper_elt.clear() |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
698 reactions_wrapper_elt <= reactions_elt |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
699 self.add_reactions_listeners(reactions_elt) |
1585
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
700 elif type_ in ("EDIT", "RETRACT"): |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
701 try: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
702 old_message_elt = document[uid] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
703 except KeyError: |
1585
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
704 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
|
705 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
706 template_data = await self.message_to_template_data( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
707 uid, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
708 update_data["timestamp"], |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
709 jid.JID(update_data["from"]), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
710 jid.JID(update_data["to"]), |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
711 update_data["message"], |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
712 update_data["subject"], |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
713 update_data["type"], |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
714 update_data["extra"] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
715 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
716 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
717 new_message_elt = self.message_tpl.get_elt( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
718 template_data |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
719 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
720 old_message_elt.replaceWith(new_message_elt) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
721 self.add_message_event_listeners(new_message_elt) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
722 self.handle_url_previews(new_message_elt) |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
723 else: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
724 log.warning(f"Unsupported update type: {type_!r}") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
725 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
726 # 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
|
727 if is_at_bottom: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
728 self.messages_elt.scrollTop = self.messages_elt.scrollHeight |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
729 |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
730 async def message_to_template_data( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
731 self, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
732 uid: str, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
733 timestamp: float, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
734 from_jid: jid.JID, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
735 to_jid: jid.JID, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
736 message_data: dict, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
737 subject_data: dict, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
738 mess_type: str, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
739 extra: dict, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
740 ) -> dict: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
741 """Generate template data to use with [message_tpl] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
742 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
743 @return: template data |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
744 """ |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
745 # 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
|
746 # 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
|
747 # 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
|
748 # 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
|
749 xhtml_data = { |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
750 key.partition("_")[2]: xhtml |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
751 for key, xhtml in extra.items() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
752 if key .startswith("xhtml") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
753 } |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
754 if not xhtml_data: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
755 xhtml = None |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
756 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
757 try: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
758 xhtml = xhtml_data[""] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
759 except KeyError: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
760 xhtml = next(iter(xhtml_data.values())) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
761 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
762 if chat_type == "group": |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
763 await cache.fill_identities([str(from_jid)]) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
764 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
765 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
|
766 from_jid = from_jid.bare |
1613
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
767 attachments = extra.get("attachments", []) |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
768 for attachment in attachments: |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
769 if "url" not in attachment: |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
770 try: |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
771 attachment["url"] = next( |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
772 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
|
773 ) |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
774 except (StopIteration, KeyError): |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
775 log.warning( |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
776 f"An attachment has no URL: {attachment}" |
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
777 ) |
1610
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
778 msg_data = { |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
779 "id": uid, |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
780 "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
|
781 "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
|
782 "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
|
783 "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
|
784 "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
|
785 "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
|
786 "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
|
787 "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
|
788 "received": extra.get("received", False), |
1613
c4407befc52a
browser (chat): fix attachments URL for messages.
Goffi <goffi@goffi.org>
parents:
1610
diff
changeset
|
789 "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
|
790 "extra": extra |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
791 } |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
792 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
|
793 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
|
794 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
|
795 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
|
796 |
19c83dd943df
browser (chat): don't set `None` value in message data used in template:
Goffi <goffi@goffi.org>
parents:
1596
diff
changeset
|
797 if xhtml: |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
798 # 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
|
799 msg_data["html"] = safe(xhtml) |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
800 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
801 return { |
1595
7941444c1671
pages: set `own_local_jid` to avoid confusion with `own_jid`:
Goffi <goffi@goffi.org>
parents:
1585
diff
changeset
|
802 "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
|
803 "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
|
804 "msg": msg_data, |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
805 "identities": identities, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
806 } |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
807 |
1536 | 808 async def on_message_new( |
809 self, | |
810 uid: str, | |
811 timestamp: float, | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
812 from_jid: jid.JID, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
813 to_jid: jid.JID, |
1536 | 814 message_data: dict, |
815 subject_data: dict, | |
816 mess_type: str, | |
817 extra: dict, | |
818 profile: str, | |
819 ) -> None: | |
1542
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
820 # 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
|
821 # 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
|
822 if ( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
823 document.visibilityState == "hidden" |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
824 and self.new_messages_marker_elt.parent is None |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
825 ): |
1542
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
826 # 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
|
827 # it |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
828 self.messages_elt <= self.new_messages_marker_elt |
1536 | 829 |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
830 template_data = await self.message_to_template_data( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
831 uid, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
832 timestamp, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
833 from_jid, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
834 to_jid, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
835 message_data, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
836 subject_data, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
837 mess_type, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
838 extra |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
839 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
840 |
1536 | 841 message_elt = self.message_tpl.get_elt( |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
842 template_data |
1536 | 843 ) |
844 | |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
845 |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
846 if "reply" in extra: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
847 parent_id = extra["reply"]["id"] |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
848 try: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
849 parent_message_elt = document[parent_id] |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
850 except KeyError: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
851 log.info( |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
852 "Parent message of reply not found in current history: " |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
853 f"{parent_id!r}" |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
854 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
855 else: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
856 thread_id = extra.get("thread", parent_id) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
857 if "thread" not in parent_message_elt.dataset: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
858 parent_message_elt.dataset["thread"] = thread_id |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
859 # TODO: Regenerate parent message, so thread icon will appear. |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
860 |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
861 if ( |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
862 self.sub_messages_check_cb is not None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
863 and self.sub_messages_panel is not None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
864 and self.sub_messages_check_cb(extra) |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
865 ): |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
866 # 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
|
867 # may not be working correctly, |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
868 cloned_message_elt = message_elt.cloneNode(True) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
869 remove_ids(cloned_message_elt) |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
870 sub_messages_elt = document["sub-messages"] |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
871 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
|
872 sub_messages_elt.appendChild(cloned_message_elt) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
873 if is_at_bottom: |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
874 sub_messages_elt.scrollTop = sub_messages_elt.scrollHeight |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
875 |
1536 | 876 # Check if user is viewing older messages or is at the bottom |
877 is_at_bottom = self.is_at_bottom | |
878 | |
879 self.messages_elt <= message_elt | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
880 self.add_message_event_listeners(message_elt) |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
881 # 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
|
882 self.handle_url_previews(message_elt) |
1536 | 883 |
884 # If user was at the bottom, keep the scroll at the bottom | |
885 if is_at_bottom: | |
886 self.messages_elt.scrollTop = self.messages_elt.scrollHeight | |
887 | |
888 def auto_resize_message_input(self): | |
889 """Resize the message input field according to content.""" | |
890 is_at_bottom = self.is_at_bottom | |
891 | |
892 # The textarea's height is first reset to 'auto' to ensure it's not influenced by | |
893 # the previous content. | |
894 self.message_input.style.height = "auto" | |
895 | |
896 # Then the height is set to the scrollHeight of the textarea (which is the height | |
897 # of the content), plus the vertical border, resulting in a textarea that grows as | |
898 # more lines of text are added. | |
899 self.message_input.style.height = f"{self.message_input.scrollHeight + 2}px" | |
900 | |
901 if is_at_bottom: | |
902 # we want the message are to still display the last message | |
903 self.messages_elt.scrollTop = self.messages_elt.scrollHeight | |
904 | |
1619 | 905 def on_left_panel_toggle_click(self, evt) -> None: |
906 """Show/Hide side bar.""" | |
907 self.left_panel.classList.toggle("is-collapsed") | |
908 self.main_panel.classList.toggle("is-expanded-left") | |
909 | |
910 def on_right_panel_toggle_click(self, evt) -> None: | |
911 """Show/Hide side bar.""" | |
912 self.right_panel.classList.toggle("is-collapsed") | |
913 self.main_panel.classList.toggle("is-expanded-right") | |
914 | |
1536 | 915 def on_message_keydown(self, evt): |
916 """Handle the 'keydown' event of the message input field | |
917 | |
918 @param evt: The event object. 'target' refers to the textarea element. | |
919 """ | |
920 if evt.keyCode == 13: # <Enter> key | |
921 if not window.navigator.maxTouchPoints: | |
922 # we have a non touch device, we send message on <Enter> | |
923 if not evt.shiftKey: | |
924 evt.preventDefault() # Prevents line break | |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
925 aio.run(self.send_message()) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
926 elif evt.keyCode == 27: # <ESC> key |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
927 evt.preventDefault() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
928 self.message_input.value = '' |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
929 self.input_mode = 'normal' |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
930 self.auto_resize_message_input() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
931 elif evt.keyCode == 38: # <Up> arrow key |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
932 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
|
933 evt.preventDefault() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
934 own_msgs = document.getElementsByClassName('own_msg') |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
935 if own_msgs.length > 0: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
936 last_msg = own_msgs[own_msgs.length - 1] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
937 aio.run(self.on_action_edit(None, last_msg)) |
1536 | 938 |
939 def update_attachments_visibility(self): | |
940 if len(self.attachments_elt.children): | |
941 self.attachments_elt.classList.remove("is-contracted") | |
942 else: | |
943 self.attachments_elt.classList.add("is-contracted") | |
944 | |
945 def on_file_selected(self, evt): | |
946 """Handle file selection""" | |
947 log.info("file selected") | |
948 files = evt.currentTarget.files | |
949 self.file_uploader.upload_files(files, self.attachments_elt) | |
950 self.message_input.focus() | |
951 | |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
952 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
|
953 evt.stopPropagation() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
954 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
|
955 "rich_edit": self.rich_edit, |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
956 }) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
957 input_extra_popup = popup.create_popup( |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
958 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
|
959 ) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
960 def on_action_click(evt, callback): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
961 input_extra_popup.hide() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
962 aio.run( |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
963 callback(evt) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
964 ) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
965 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
966 for cls_name, callback in ( |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
967 ("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
|
968 ("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
|
969 ): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
970 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
|
971 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
|
972 evt, callback |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
973 )) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
974 |
1536 | 975 def on_attachment_delete(self, evt): |
976 evt.stopPropagation() | |
977 target = evt.currentTarget | |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
978 item_elt = DOMNode(target.closest(".attachment-preview")) |
1536 | 979 item_elt.remove() |
980 | |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
981 def on_attach_btn_click(self, evt): |
1619 | 982 document["file-input"].click() |
1536 | 983 |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
984 def on_reply_btn_click(self, evt) -> None: |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
985 chat_message_elt = evt.currentTarget.closest("div.chat-message") |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
986 self.reply_to = { |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
987 "id": chat_message_elt["id"], |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
988 "to": chat_message_elt.dataset["from"] |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
989 } |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
990 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
|
991 |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
992 |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
993 def on_extra_btn_click(self, evt): |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
994 message_elt = evt.currentTarget.closest("div.chat-message") |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
995 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
|
996 is_own = message_elt.classList.contains("own_msg") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
997 if is_own: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
998 own_messages = document.select('.own_msg') |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
999 # 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
|
1000 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
|
1001 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1002 can_edit = False |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1003 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1004 content_elt = self.extra_menu_tpl.get_elt({ |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1005 "edit": can_edit, |
1585
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1006 "retract": is_own, |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1007 }) |
1619 | 1008 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
|
1009 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1010 def on_action_click(evt, callback): |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1011 extra_popup.hide() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1012 aio.run( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1013 callback(evt, message_elt) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1014 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1015 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1016 for cls_name, callback in ( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1017 ("action_quote", self.on_action_quote), |
1585
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1018 ("action_edit", self.on_action_edit), |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1019 ("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
|
1020 ("action_forward", self.on_action_forward), |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1021 ): |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1022 for elt in content_elt.select(f".{cls_name}"): |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1023 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
|
1024 evt, callback |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1025 )) |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1026 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1027 def on_reaction_click(self, evt, message_elt): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1028 window.evt = evt |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1029 aio.run( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1030 bridge.message_reactions_set( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1031 message_elt["id"], [evt.detail["unicode"]], "toggle" |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1032 ) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1033 ) |
1619 | 1034 # if evt.deltaY != 0: |
1035 # document["attachments"].scrollLeft += evt.deltaY * 0.8 | |
1036 # evt.preventDefault() | |
1536 | 1037 |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1038 async def show_filtered_messages( |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1039 self, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1040 filters: dict, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1041 sub_messages_panel_args: dict|None = None, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1042 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
|
1043 ) -> None: |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1044 """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
|
1045 |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1046 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
|
1047 @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
|
1048 @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
|
1049 @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
|
1050 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
|
1051 displayed in the panel. |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1052 """ |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1053 if sub_messages_panel_args is None: |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1054 sub_messages_panel_args = {} |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1055 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
|
1056 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
|
1057 sub_messages_panel_args |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1058 ) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1059 self.sub_messages_panel = sub_messages_panel_elt |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1060 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
|
1061 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
|
1062 assert sub_messages_elt is not None |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1063 history_data = await bridge.history_get( |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1064 "", "", -2, True, filters |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1065 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1066 for message_data in history_data: |
1623
fdb5689fb826
browser (chat): Update following template change + some cleaning:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
1067 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
|
1068 template_data = await self.message_to_template_data( |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1069 uid, |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1070 timestamp=timestamp, |
1623
fdb5689fb826
browser (chat): Update following template change + some cleaning:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
1071 from_jid=jid.JID(from_jid_s), |
fdb5689fb826
browser (chat): Update following template change + some cleaning:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
1072 to_jid=jid.JID(to_jid_s), |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1073 message_data=message_data, |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1074 subject_data=subject_data, |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1075 mess_type=mess_type, |
1623
fdb5689fb826
browser (chat): Update following template change + some cleaning:
Goffi <goffi@goffi.org>
parents:
1620
diff
changeset
|
1076 extra=json.loads(extra_s) |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1077 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1078 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
|
1079 sub_messages_elt <= message_elt |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1080 # 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
|
1081 # 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
|
1082 # 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
|
1083 # could be used. |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1084 input_panel_elt = document["input-panel"] |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1085 placeholder = make_placeholder(input_panel_elt) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1086 sub_messages_input_elt <= input_panel_elt |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1087 document["input-panel-area"].appendChild(placeholder) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1088 |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1089 def on_sub_messages_panel_close(): |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1090 placeholder.replaceWith(input_panel_elt) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1091 self.thread_id = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1092 self.keyword = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1093 self.sub_messages_panel = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1094 self.sub_messages_check_cb = None |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1095 |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1096 sub_messages_modal = dialog.Modal( |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1097 sub_messages_panel_elt, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1098 closable=True, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1099 close_cb=on_sub_messages_panel_close, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1100 position="right" |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1101 ) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1102 sub_messages_modal.show() |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1103 |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1104 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
|
1105 assert thread_id |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1106 self.thread_id = thread_id |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1107 def sub_messages_check_cb(extra: dict) -> bool: |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1108 return bool( |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1109 self.thread_id |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1110 and "thread" in extra |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1111 and extra["thread"] == self.thread_id |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1112 ) |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1113 |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1114 await self.show_filtered_messages( |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1115 {"thread_id": thread_id}, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1116 {"title": "Thread view"}, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1117 sub_messages_check_cb |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1118 ) |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1119 |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1120 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
|
1121 self.keyword = keyword_elt.text |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1122 def sub_messages_check_cb(extra: dict) -> bool: |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1123 return self.keyword in extra.get("keywords", []) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1124 await self.show_filtered_messages( |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1125 {"keyword": self.keyword}, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1126 {"title": f"🏷 Label view: {self.keyword}."}, |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1127 sub_messages_check_cb |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1128 ) |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1129 |
1628
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1130 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
|
1131 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
|
1132 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
|
1133 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
|
1134 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
|
1135 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
|
1136 {"origin": origin}, |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1137 {"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
|
1138 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
|
1139 ) |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1140 |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1141 |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1142 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
|
1143 """Retrieve message tuple from as sent by [message_new] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1144 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1145 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
|
1146 returned. |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1147 @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
|
1148 message data |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1149 @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
|
1150 """ |
1619 | 1151 print(f"{message_elt=}") |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1152 message_id = message_elt['id'] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1153 history_data = await bridge.history_get( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1154 "", "", -2, True, {"id": message_elt['id']} |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1155 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1156 if not history_data: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1157 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
|
1158 return None |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1159 return history_data[0] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1160 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1161 async def on_action_quote(self, __, message_elt) -> None: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1162 message_data = await self.get_message_tuple(message_elt) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1163 if message_data is not None: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1164 messages = message_data[4] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1165 body = next(iter(messages.values()), "") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1166 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
|
1167 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
|
1168 self.input_mode = "quote" |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1169 self.input_data["id"] = message_elt["id"] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1170 self.auto_resize_message_input() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1171 self.message_input.focus() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1172 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1173 async def on_action_edit(self, __, message_elt) -> None: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1174 message_data = await self.get_message_tuple(message_elt) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1175 if message_data is not None: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1176 messages = message_data[4] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1177 body = next(iter(messages.values()), "") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1178 if not body: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1179 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
|
1180 return |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1181 |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1182 self.message_input.value = body |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1183 self.input_mode = "edit" |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1184 self.input_data["id"] = message_elt["id"] |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1185 self.auto_resize_message_input() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1186 self.message_input.focus() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1187 |
1585
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1188 async def on_action_retract(self, __, message_elt) -> None: |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1189 confirmed = await dialog.Confirm(safe( |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1190 "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
|
1191 "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
|
1192 "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
|
1193 "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
|
1194 "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
|
1195 "mitigate the risks." |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1196 )).ashow() |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1197 if confirmed: |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1198 await bridge.message_retract(message_elt["id"]) |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1199 else: |
9fc4120888be
browser (chat): message retraction implementation
Goffi <goffi@goffi.org>
parents:
1584
diff
changeset
|
1200 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
|
1201 |
1635
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1202 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
|
1203 # TODO: Use a real dialog here. |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1204 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
|
1205 if recipient_jid is NULL: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1206 return |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1207 recipient_jid = recipient_jid.strip() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1208 if recipient_jid: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1209 message_id = message_elt["id"] |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1210 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
|
1211 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
|
1212 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1213 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1214 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
|
1215 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
|
1216 confirmed = await dialog.Confirm( |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1217 "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
|
1218 ).ashow() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1219 if not confirmed: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1220 return |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1221 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1222 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
|
1223 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1224 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
|
1225 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
|
1226 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
|
1227 "click", |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1228 lambda __: elt.remove() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1229 ) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1230 # 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
|
1231 def on_keypress(evt): |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1232 if evt.key == "Enter": |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1233 evt.preventDefault() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1234 evt.stopPropagation() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1235 selected = elt.select_one("select").value |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1236 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
|
1237 input_elt = elt.select_one("input") |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1238 if input_elt is None: |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1239 dialog.notification.show( |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1240 "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
|
1241 "error" |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1242 ) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1243 return |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1244 input_elt.bind("keydown", on_keypress) |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1245 |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1246 toolbar_elt = document["rich-edit-toolbar"] |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1247 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
|
1248 input_elt.focus() |
332822ceae85
browser (chat): Add rich editor, forward and extra recipients:
Goffi <goffi@goffi.org>
parents:
1632
diff
changeset
|
1249 |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1250 def get_reaction_panel(self, source_elt): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1251 emoji_picker_elt = document.createElement("emoji-picker") |
1619 | 1252 message_elt = source_elt.closest("div.chat-message") |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1253 emoji_picker_elt.bind( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1254 "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
|
1255 ) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1256 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1257 return emoji_picker_elt |
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 def add_message_event_listeners(self, parent_elt=None): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1260 """Prepare a message to be dynamic |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1261 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1262 - make attachments dynamically clickable |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1263 - make the extra button clickable |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1264 """ |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1265 ## attachments |
1536 | 1266 # FIXME: only handle images for now, and display them in a modal |
1267 if parent_elt is None: | |
1268 parent_elt = document | |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1269 img_elts = parent_elt.select(".message-attachment img") |
1536 | 1270 for img_elt in img_elts: |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1271 img_elt.bind("click", self.open_modal) |
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1272 img_elt.style.cursor = "pointer" |
1536 | 1273 |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1274 ## reply button |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1275 for reply_btn in parent_elt.select(".reply-button"): |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1276 reply_btn.bind("click", self.on_reply_btn_click) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1277 |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1278 ## reaction button |
1619 | 1279 i = 0 |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1280 for reaction_btn in parent_elt.select(".reaction-button"): |
1619 | 1281 i+=1 |
1282 message_elt = reaction_btn.closest("div.message-core") | |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1283 tippy( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1284 reaction_btn, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1285 { |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1286 "trigger": "click", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1287 "content": self.get_reaction_panel, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1288 "appendTo": document.body, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1289 "placement": "bottom", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1290 "interactive": True, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1291 "theme": "light", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1292 "onShow": lambda __, message_elt=message_elt: ( |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1293 message_elt.classList.add("has-popup-focus") |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1294 ), |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1295 "onHide": lambda __, message_elt=message_elt: ( |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1296 message_elt.classList.remove("has-popup-focus") |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1297 ), |
1619 | 1298 } |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1299 ) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1300 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1301 ## extra button |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1302 for extra_btn in parent_elt.select(".extra-button"): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1303 extra_btn.bind("click", self.on_extra_btn_click) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1304 |
1584
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1305 ## editions |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1306 for edition_icon_elt in parent_elt.select(".message-editions"): |
1619 | 1307 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
|
1308 dataset = message_elt.dataset.to_dict() |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1309 try: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1310 editions = json.loads(dataset["editions"]) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1311 except (ValueError, KeyError): |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1312 log.error( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1313 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
|
1314 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1315 else: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1316 for edition in editions: |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1317 edition["text"] = ( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1318 edition["message"].get("") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1319 or next(iter(edition["message"].values()), "") |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1320 ) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1321 editions_elt = self.editions_tpl.get_elt({"editions": editions}) |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1322 tippy( |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1323 edition_icon_elt, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1324 { |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1325 "content": editions_elt, |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1326 "theme": "light", |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1327 "appendTo": document.body |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1328 } |
eab815e48795
browser (chat): message edition + extra menu:
Goffi <goffi@goffi.org>
parents:
1577
diff
changeset
|
1329 |
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 |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1332 ## thread |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1333 for thread_icon_elt in parent_elt.select(".message-thread"): |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1334 message_elt = thread_icon_elt.closest("div.chat-message") |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1335 thread_id = message_elt.dataset["thread"] |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1336 thread_icon_elt.bind( |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1337 "mouseenter", |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1338 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
|
1339 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1340 thread_icon_elt.bind( |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1341 "mouseleave", |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1342 lambda __, thread_id=thread_id: setattr(self, "show_thread", None) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1343 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1344 thread_icon_elt.bind( |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1345 "click", |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1346 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
|
1347 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1348 |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1349 ## keywords |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1350 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
|
1351 keyword_elt.bind( |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1352 "click", |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1353 lambda __, keyword_elt=keyword_elt: aio.run( |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1354 self._on_keyword_click(keyword_elt) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1355 ) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1356 ) |
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1357 |
1628
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1358 ## origin |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1359 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
|
1360 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
|
1361 "click", |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1362 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
|
1363 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
|
1364 ) |
84e287565fab
browser (chat): show origin in message, and activate suitable side panel on click:
Goffi <goffi@goffi.org>
parents:
1625
diff
changeset
|
1365 ) |
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 |
1625
698eaabfca0e
browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1368 |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1369 def add_reactions_listeners(self, parent_elt=None) -> None: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1370 """Add listener on reactions to handle details and reaction toggle""" |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1371 if parent_elt is None: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1372 parent_elt = document |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1373 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1374 is_touch = is_touch_device() |
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 for reaction_elt in parent_elt.select(".reaction"): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1377 # Reaction details |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1378 dataset = reaction_elt.dataset.to_dict() |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1379 reacting_jids = sorted(json.loads(dataset.get("jids", "[]"))) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1380 reaction_details_elt = self.reactions_details_tpl.get_elt( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1381 {"reacting_jids": reacting_jids, "identities": identities} |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1382 ) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1383 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1384 # Configure tippy based on device type |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1385 tippy_config = { |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1386 "content": reaction_details_elt, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1387 "placement": "bottom", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1388 "theme": "light", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1389 "touch": ["hold", 500] if is_touch else True, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1390 "trigger": "click" if is_touch else "mouseenter focus", |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1391 "delay": [0, 800] if is_touch else 0, |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1392 } |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1393 tippy(reaction_elt, tippy_config) |
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 # Toggle reaction when clicked/touched |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1396 emoji_elt = reaction_elt.select_one(".emoji") |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1397 emoji = emoji_elt.html.strip() |
1619 | 1398 message_elt = reaction_elt.closest("div.chat-message") |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1399 msg_id = message_elt["id"] |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1400 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1401 def toggle_reaction(event, msg_id=msg_id, emoji=emoji): |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1402 # 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
|
1403 if is_touch: |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1404 event.preventDefault() |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1405 aio.run(bridge.message_reactions_set(msg_id, [emoji], "toggle")) |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1406 |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1407 reaction_elt.bind("click", toggle_reaction) |
1536 | 1408 |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1409 def find_links(self, message_elt): |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1410 """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
|
1411 msg_body_elt = message_elt.select_one(".msg_body") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1412 if not msg_body_elt: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1413 return |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1414 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1415 # Extracting links from text content |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1416 text = msg_body_elt.text |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1417 raw_urls = re.findall( |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1418 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
|
1419 text, |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1420 ) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1421 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1422 # Extracting links from <a> elements |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1423 a_elements = msg_body_elt.select("a") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1424 for a_elt in a_elements: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1425 href = a_elt.attrs.get("href", "") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1426 if href.startswith("http://") or href.startswith("https://"): |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1427 raw_urls.append(href) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1428 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1429 # we remove duplicates |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1430 urls = list(dict.fromkeys(raw_urls)) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1431 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1432 return 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 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
|
1435 """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
|
1436 url_previews_elt.clear() |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1437 for url in urls: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1438 try: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1439 url_preview_data_s = await bridge.url_preview_get(url, "") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1440 except Exception as e: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1441 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
|
1442 continue |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1443 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1444 if not url_preview_data_s: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1445 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
|
1446 continue |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1447 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1448 url_preview_data = json.loads(url_preview_data_s) |
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_elt = self.url_preview_tpl.get_elt( |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1451 {"url_preview": url_preview_data} |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1452 ) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1453 url_previews_elt <= url_preview_elt |
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 def handle_url_previews(self, parent_elt=None): |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1456 """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
|
1457 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1458 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
|
1459 user click, or directly the previews, or nothing at all. |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1460 """ |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1461 |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1462 if parent_elt is None: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1463 parent_elt = document |
1619 | 1464 chat_message_elts = parent_elt.select(".chat-message") |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1465 else: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1466 chat_message_elts = [parent_elt] |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1467 for message_elt in chat_message_elts: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1468 urls = self.find_links(message_elt) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1469 if urls: |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1470 url_previews_elt = message_elt.select_one(".url-previews") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1471 url_previews_elt.classList.remove("is-hidden") |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1472 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
|
1473 fetch_preview_btn = preview_control_elt.select_one( |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1474 ".action_fetch_preview" |
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1475 ) |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1476 fetch_preview_btn.bind( |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1477 "click", |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1478 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
|
1479 self.add_url_previews(previews_elt, preview_urls) |
1577
9ba532041a8e
browser (chat): implement message reactions.
Goffi <goffi@goffi.org>
parents:
1542
diff
changeset
|
1480 ), |
1541
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1481 ) |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1482 url_previews_elt <= preview_control_elt |
3c384b51b2a1
browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
1483 |
1536 | 1484 def open_modal(self, evt): |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1485 modal_image = document.select_one("#modal-image") |
1620
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1486 modal_image.src = evt.currentTarget.src |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1487 modal_image.alt = evt.currentTarget.alt |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1488 modal = document.select_one("#modal") |
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1489 modal.classList.add("is-active") |
1536 | 1490 |
1491 def close_modal(self, evt): | |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1492 modal = document.select_one("#modal") |
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1493 modal.classList.remove("is-active") |
1536 | 1494 |
1542
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
1495 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
|
1496 if ( |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
1497 document.visibilityState == "hidden" |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
1498 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
|
1499 ): |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
1500 # 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
|
1501 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
|
1502 |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
1503 |
1536 | 1504 libervia_web_chat = LiberviaWebChat() |
1540
b4342176fa0a
browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents:
1536
diff
changeset
|
1505 |
1619 | 1506 document["new_chat_btn"].bind( |
1507 "click", lambda __: aio.run(libervia_web_chat.on_new_chat()) | |
1508 ) | |
1509 | |
1536 | 1510 document["message_input"].bind( |
1511 "input", lambda __: libervia_web_chat.auto_resize_message_input() | |
1512 ) | |
1513 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
|
1514 document["send_button"].bind( |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1515 "click", |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1516 lambda __: aio.run(libervia_web_chat.send_message()) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1517 ) |
3a60bf3762ef
browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents:
1619
diff
changeset
|
1518 document["attach-button"].bind("click", libervia_web_chat.on_attach_btn_click) |
1619 | 1519 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
|
1520 |
fb31d3dba0c3
browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents:
1541
diff
changeset
|
1521 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
|
1522 |
1536 | 1523 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
|
1524 bridge.register_signal("message_update", libervia_web_chat._on_message_update) |
1619 | 1525 aio.run(libervia_web_chat.post_init()) |
1526 remove_loading_screen() |