comparison libervia/web/pages/chat/_browser/__init__.py @ 1585:9fc4120888be

browser (chat): message retraction implementation
author Goffi <goffi@goffi.org>
date Thu, 30 Nov 2023 13:25:25 +0100
parents eab815e48795
children 7941444c1671
comparison
equal deleted inserted replaced
1584:eab815e48795 1585:9fc4120888be
1 import json 1 import json
2 import re 2 import re
3 3
4 from bridge import AsyncBridge as Bridge 4 from bridge import AsyncBridge as Bridge
5 from browser import DOMNode, aio, bind, console as log, document, timer, window 5 from browser import DOMNode, aio, console as log, document, window
6 from cache import cache, identities 6 from cache import cache, identities
7 import dialog 7 import dialog
8 from file_uploader import FileUploader 8 from file_uploader import FileUploader
9 import jid 9 import jid
10 from js_modules import emoji_picker_element 10 from js_modules import emoji_picker_element
190 log.debug(f"Message {uid} found, new reactions: {reactions}") 190 log.debug(f"Message {uid} found, new reactions: {reactions}")
191 reactions_elt = self.reactions_tpl.get_elt({"reactions": reactions}) 191 reactions_elt = self.reactions_tpl.get_elt({"reactions": reactions})
192 reactions_wrapper_elt.clear() 192 reactions_wrapper_elt.clear()
193 reactions_wrapper_elt <= reactions_elt 193 reactions_wrapper_elt <= reactions_elt
194 self.add_reactions_listeners(reactions_elt) 194 self.add_reactions_listeners(reactions_elt)
195 elif type_ == "EDIT": 195 elif type_ in ("EDIT", "RETRACT"):
196 try: 196 try:
197 old_message_elt = document[uid] 197 old_message_elt = document[uid]
198 except KeyError: 198 except KeyError:
199 log.debug(f"Message {uid} not found, no edition to apply") 199 log.debug(f"Message {uid} not found, no {type_.lower()}ion to apply")
200 else: 200 else:
201 template_data = await self.message_to_template_data( 201 template_data = await self.message_to_template_data(
202 uid, 202 uid,
203 update_data["timestamp"], 203 update_data["timestamp"],
204 jid.JID(update_data["from"]), 204 jid.JID(update_data["from"]),
399 else: 399 else:
400 can_edit = False 400 can_edit = False
401 401
402 content_elt = self.extra_menu_tpl.get_elt({ 402 content_elt = self.extra_menu_tpl.get_elt({
403 "edit": can_edit, 403 "edit": can_edit,
404 "retract": is_own,
404 }) 405 })
405 extra_popup = popup.create_popup(evt.target, content_elt, focus_elt=message_elt) 406 extra_popup = popup.create_popup(evt.target, content_elt, focus_elt=message_elt)
406 407
407 def on_action_click(evt, callback): 408 def on_action_click(evt, callback):
408 extra_popup.hide() 409 extra_popup.hide()
410 callback(evt, message_elt) 411 callback(evt, message_elt)
411 ) 412 )
412 413
413 for cls_name, callback in ( 414 for cls_name, callback in (
414 ("action_quote", self.on_action_quote), 415 ("action_quote", self.on_action_quote),
415 ("action_edit", self.on_action_edit) 416 ("action_edit", self.on_action_edit),
417 ("action_retract", self.on_action_retract),
416 ): 418 ):
417 for elt in content_elt.select(f".{cls_name}"): 419 for elt in content_elt.select(f".{cls_name}"):
418 elt.bind("click", lambda evt, callback=callback: on_action_click( 420 elt.bind("click", lambda evt, callback=callback: on_action_click(
419 evt, callback 421 evt, callback
420 )) 422 ))
472 self.message_input.value = body 474 self.message_input.value = body
473 self.input_mode = "edit" 475 self.input_mode = "edit"
474 self.input_data["id"] = message_elt["id"] 476 self.input_data["id"] = message_elt["id"]
475 self.auto_resize_message_input() 477 self.auto_resize_message_input()
476 self.message_input.focus() 478 self.message_input.focus()
479
480 async def on_action_retract(self, __, message_elt) -> None:
481 confirmed = await dialog.Confirm(safe(
482 "This message will be permanently removed. Are you sure?<br><br>"
483 "WARNING: It is impossible to guarantee that other participants in the "
484 "discussion will delete this message as well. You must assume it has been "
485 "seen. If a password or other sensitive information has been accidentally "
486 "shared, please ensure to take appropriate measures to change it and "
487 "mitigate the risks."
488 )).ashow()
489 if confirmed:
490 await bridge.message_retract(message_elt["id"])
491 else:
492 log.info(f"Retraction of message {message_elt['id']} cancelled by user.")
477 493
478 def get_reaction_panel(self, source_elt): 494 def get_reaction_panel(self, source_elt):
479 emoji_picker_elt = document.createElement("emoji-picker") 495 emoji_picker_elt = document.createElement("emoji-picker")
480 message_elt = source_elt.closest("div.is-chat-message") 496 message_elt = source_elt.closest("div.is-chat-message")
481 emoji_picker_elt.bind( 497 emoji_picker_elt.bind(