annotate libervia/web/pages/chat/_browser/__init__.py @ 1610:19c83dd943df

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