comparison libervia/web/pages/chat/page_meta.py @ 1536:dc81403a5b2f

browser: chat page: since the move to Brython, the chat was really basic and not really usable. Now that dynamism has been re-implemented correctly in the new frontend, a real advanced chat page can be done. This is the first draft in this direction.
author Goffi <goffi@goffi.org>
date Wed, 28 Jun 2023 10:05:13 +0200
parents eb00d593801d
children f00497c00e38
comparison
equal deleted inserted replaced
1535:de09d4d25194 1536:dc81403a5b2f
3 from libervia.backend.core.i18n import _ 3 from libervia.backend.core.i18n import _
4 from twisted.internet import defer 4 from twisted.internet import defer
5 from libervia.backend.core.log import getLogger 5 from libervia.backend.core.log import getLogger
6 from libervia.backend.tools.common import data_objects 6 from libervia.backend.tools.common import data_objects
7 from libervia.backend.tools.common import data_format 7 from libervia.backend.tools.common import data_format
8 from twisted.words.protocols.jabber import jid 8 from libervia.frontends.tools import jid
9 from libervia.web.server.constants import Const as C 9 from libervia.web.server.constants import Const as C
10 from libervia.web.server import session_iface 10 from libervia.web.server import session_iface
11 11
12 12
13 log = getLogger(__name__) 13 log = getLogger(__name__)
24 try: 24 try:
25 target_jid_s = self.next_path(request) 25 target_jid_s = self.next_path(request)
26 except IndexError: 26 except IndexError:
27 # not chat jid, we redirect to jid selection page 27 # not chat jid, we redirect to jid selection page
28 self.page_redirect("chat_select", request) 28 self.page_redirect("chat_select", request)
29 return
29 30
30 try: 31 try:
31 target_jid = jid.JID(target_jid_s) 32 target_jid = jid.JID(target_jid_s)
32 if not target_jid.user: 33 if not target_jid.local:
33 raise ValueError(_("invalid jid for chat (no local part)")) 34 raise ValueError(_("invalid jid for chat (no local part)"))
34 except Exception as e: 35 except Exception as e:
35 log.warning( 36 log.warning(
36 _("bad chat jid entered: {jid} ({msg})").format(jid=target_jid, msg=e) 37 _("bad chat jid entered: {jid} ({msg})").format(jid=target_jid_s, msg=e)
37 ) 38 )
38 self.page_error(request, C.HTTP_BAD_REQUEST) 39 self.page_error(request, C.HTTP_BAD_REQUEST)
39 else: 40 else:
40 rdata["target"] = target_jid 41 rdata["target"] = target_jid
41 42
42 43
43 @defer.inlineCallbacks 44 async def prepare_render(self, request):
44 def prepare_render(self, request):
45 #  FIXME: bug on room filtering (currently display messages from all rooms) 45 #  FIXME: bug on room filtering (currently display messages from all rooms)
46 session = self.host.get_session_data(request, session_iface.IWebSession) 46 session = self.host.get_session_data(request, session_iface.IWebSession)
47 template_data = request.template_data 47 template_data = request.template_data
48 rdata = self.get_r_data(request) 48 rdata = self.get_r_data(request)
49 target_jid = rdata["target"] 49 target_jid = rdata["target"]
50 profile = session.profile 50 profile = session.profile
51 profile_jid = session.jid 51 profile_jid = session.jid
52 52
53 disco = yield self.host.bridge_call("disco_infos", target_jid.host, "", True, profile) 53 disco = await self.host.bridge_call(
54 "disco_infos", target_jid.domain, "", True, profile
55 )
54 if "conference" in [i[0] for i in disco[1]]: 56 if "conference" in [i[0] for i in disco[1]]:
55 chat_type = C.CHAT_GROUP 57 chat_type = C.CHAT_GROUP
56 join_ret = yield self.host.bridge_call( 58 join_ret = await self.host.bridge_call(
57 "muc_join", target_jid.userhost(), "", "", profile 59 "muc_join", target_jid.bare, "", "", profile
58 ) 60 )
59 (already_joined, 61 (already_joined,
60 room_jid_s, 62 room_jid_s,
61 occupants, 63 occupants,
62 user_nick, 64 user_nick,
64 room_statuses, 66 room_statuses,
65 __) = join_ret 67 __) = join_ret
66 template_data["subject"] = room_subject 68 template_data["subject"] = room_subject
67 template_data["room_statuses"] = room_statuses 69 template_data["room_statuses"] = room_statuses
68 own_jid = jid.JID(room_jid_s) 70 own_jid = jid.JID(room_jid_s)
69 own_jid.resource = user_nick 71 own_jid = own_jid.change_resource(user_nick)
70 else: 72 else:
73 room_subject = None
71 chat_type = C.CHAT_ONE2ONE 74 chat_type = C.CHAT_ONE2ONE
72 own_jid = profile_jid 75 own_jid = profile_jid
73 rdata["chat_type"] = chat_type 76 rdata["chat_type"] = chat_type
74 template_data["own_jid"] = own_jid 77 template_data["own_jid"] = own_jid
75 78
76 self.register_signal(request, "message_new") 79 history = await self.host.bridge_call(
77 history = yield self.host.bridge_call(
78 "history_get", 80 "history_get",
79 profile_jid.userhost(), 81 profile_jid.userhost(),
80 target_jid.userhost(), 82 target_jid.bare,
81 20, 83 20,
82 True, 84 True,
83 {}, 85 {},
84 profile, 86 profile,
85 ) 87 )
88
86 authors = {m[2] for m in history} 89 authors = {m[2] for m in history}
87 identities = session.identities 90 identities = session.identities
88 for author in authors: 91 for author in authors:
89 id_raw = yield self.host.bridge_call( 92 id_raw = await self.host.bridge_call(
90 "identity_get", author, [], True, profile) 93 "identity_get", author, [], True, profile)
91 identities[author] = data_format.deserialise(id_raw) 94 identities[author] = data_format.deserialise(id_raw)
92 95
93 template_data["messages"] = data_objects.Messages(history) 96 template_data["messages"] = data_objects.Messages(history)
94 rdata['identities'] = identities 97 rdata['identities'] = identities
95 template_data["target_jid"] = target_jid 98 template_data["target_jid"] = target_jid
96 template_data["chat_type"] = chat_type 99 template_data["chat_type"] = chat_type
100 self.expose_to_scripts(
101 request,
102 room_subject=room_subject,
103 own_jid=own_jid,
104 target_jid=target_jid,
105 chat_type=chat_type,
106 )
97 107
98 108
99 def on_data(self, request, data): 109 def on_data(self, request, data):
100 session = self.host.get_session_data(request, session_iface.IWebSession) 110 session = self.host.get_session_data(request, session_iface.IWebSession)
101 rdata = self.get_r_data(request) 111 rdata = self.get_r_data(request)