comparison libervia/web/pages/chat/page_meta.py @ 1518:eb00d593801d

refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 16:49:28 +0200
parents libervia/pages/chat/page_meta.py@106bae41f5c8
children dc81403a5b2f
comparison
equal deleted inserted replaced
1517:b8ed9726525b 1518:eb00d593801d
1 #!/usr/bin/env python3
2
3 from libervia.backend.core.i18n import _
4 from twisted.internet import defer
5 from libervia.backend.core.log import getLogger
6 from libervia.backend.tools.common import data_objects
7 from libervia.backend.tools.common import data_format
8 from twisted.words.protocols.jabber import jid
9 from libervia.web.server.constants import Const as C
10 from libervia.web.server import session_iface
11
12
13 log = getLogger(__name__)
14
15 name = "chat"
16 access = C.PAGES_ACCESS_PROFILE
17 template = "chat/chat.html"
18 dynamic = True
19
20
21 def parse_url(self, request):
22 rdata = self.get_r_data(request)
23
24 try:
25 target_jid_s = self.next_path(request)
26 except IndexError:
27 # not chat jid, we redirect to jid selection page
28 self.page_redirect("chat_select", request)
29
30 try:
31 target_jid = jid.JID(target_jid_s)
32 if not target_jid.user:
33 raise ValueError(_("invalid jid for chat (no local part)"))
34 except Exception as e:
35 log.warning(
36 _("bad chat jid entered: {jid} ({msg})").format(jid=target_jid, msg=e)
37 )
38 self.page_error(request, C.HTTP_BAD_REQUEST)
39 else:
40 rdata["target"] = target_jid
41
42
43 @defer.inlineCallbacks
44 def prepare_render(self, request):
45 #  FIXME: bug on room filtering (currently display messages from all rooms)
46 session = self.host.get_session_data(request, session_iface.IWebSession)
47 template_data = request.template_data
48 rdata = self.get_r_data(request)
49 target_jid = rdata["target"]
50 profile = session.profile
51 profile_jid = session.jid
52
53 disco = yield self.host.bridge_call("disco_infos", target_jid.host, "", True, profile)
54 if "conference" in [i[0] for i in disco[1]]:
55 chat_type = C.CHAT_GROUP
56 join_ret = yield self.host.bridge_call(
57 "muc_join", target_jid.userhost(), "", "", profile
58 )
59 (already_joined,
60 room_jid_s,
61 occupants,
62 user_nick,
63 room_subject,
64 room_statuses,
65 __) = join_ret
66 template_data["subject"] = room_subject
67 template_data["room_statuses"] = room_statuses
68 own_jid = jid.JID(room_jid_s)
69 own_jid.resource = user_nick
70 else:
71 chat_type = C.CHAT_ONE2ONE
72 own_jid = profile_jid
73 rdata["chat_type"] = chat_type
74 template_data["own_jid"] = own_jid
75
76 self.register_signal(request, "message_new")
77 history = yield self.host.bridge_call(
78 "history_get",
79 profile_jid.userhost(),
80 target_jid.userhost(),
81 20,
82 True,
83 {},
84 profile,
85 )
86 authors = {m[2] for m in history}
87 identities = session.identities
88 for author in authors:
89 id_raw = yield self.host.bridge_call(
90 "identity_get", author, [], True, profile)
91 identities[author] = data_format.deserialise(id_raw)
92
93 template_data["messages"] = data_objects.Messages(history)
94 rdata['identities'] = identities
95 template_data["target_jid"] = target_jid
96 template_data["chat_type"] = chat_type
97
98
99 def on_data(self, request, data):
100 session = self.host.get_session_data(request, session_iface.IWebSession)
101 rdata = self.get_r_data(request)
102 target = rdata["target"]
103 data_type = data.get("type", "")
104 if data_type == "msg":
105 message = data["body"]
106 mess_type = (
107 C.MESS_TYPE_GROUPCHAT
108 if rdata["chat_type"] == C.CHAT_GROUP
109 else C.MESS_TYPE_CHAT
110 )
111 log.debug("message received: {}".format(message))
112 self.host.bridge_call(
113 "message_send",
114 target.full(),
115 {"": message},
116 {},
117 mess_type,
118 "",
119 session.profile,
120 )
121 else:
122 log.warning("unknown message type: {type}".format(type=data_type))