annotate libervia/pages/chat/page_meta.py @ 1504:409d10211b20

server, browser: dynamic pages refactoring: dynamic pages has been reworked, to change the initial basic implementation. Pages are now dynamic by default, and a websocket is established by the first connected page of a session. The socket is used to transmit bridge signals, and then the signal is broadcasted to other tabs using broadcast channel. If the connecting tab is closed, an other one is chosen. Some tests are made to retry connecting in case of problem, and sometimes reload the pages (e.g. if profile is connected). Signals (or other data) are cached during reconnection phase, to avoid lost of data. All previous partial rendering mechanism have been removed, chat page is temporarily not working anymore, but will be eventually redone (one of the goal of this work is to have proper chat).
author Goffi <goffi@goffi.org>
date Wed, 01 Mar 2023 18:02:44 +0100
parents 01936fc55cd9
children ce879da7fcf7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
1 #!/usr/bin/env python3
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3 from sat.core.i18n import _
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from twisted.internet import defer
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 from sat.core.log import getLogger
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 from sat.tools.common import data_objects
1243
8aff742d0dd0 pages: updated `identityGet` call, following backend changes
Goffi <goffi@goffi.org>
parents: 1241
diff changeset
7 from sat.tools.common import data_format
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 from twisted.words.protocols.jabber import jid
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 from libervia.server.constants import Const as C
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 from libervia.server import session_iface
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11
1241
921e9f2a97af pages (chat): renamed `extra` argument to `extra_s` as it is now serialised
Goffi <goffi@goffi.org>
parents: 1232
diff changeset
12
921e9f2a97af pages (chat): renamed `extra` argument to `extra_s` as it is now serialised
Goffi <goffi@goffi.org>
parents: 1232
diff changeset
13 log = getLogger(__name__)
921e9f2a97af pages (chat): renamed `extra` argument to `extra_s` as it is now serialised
Goffi <goffi@goffi.org>
parents: 1232
diff changeset
14
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
15 name = "chat"
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 access = C.PAGES_ACCESS_PROFILE
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
17 template = "chat/chat.html"
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 dynamic = True
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21 def parse_url(self, request):
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 rdata = self.getRData(request)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 try:
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 target_jid_s = self.nextPath(request)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 except IndexError:
1000
4cc4d49e1d0f pages (chat): moved rendering preparation in prepare_render, and redirect to page_select if no jid is specified.
Goffi <goffi@goffi.org>
parents: 996
diff changeset
27 # not chat jid, we redirect to jid selection page
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
28 self.pageRedirect("chat_select", request)
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 try:
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 target_jid = jid.JID(target_jid_s)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 if not target_jid.user:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
33 raise ValueError(_("invalid jid for chat (no local part)"))
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 except Exception as e:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
35 log.warning(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
36 _("bad chat jid entered: {jid} ({msg})").format(jid=target_jid, msg=e)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
37 )
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 self.pageError(request, C.HTTP_BAD_REQUEST)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 else:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
40 rdata["target"] = target_jid
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41
1000
4cc4d49e1d0f pages (chat): moved rendering preparation in prepare_render, and redirect to page_select if no jid is specified.
Goffi <goffi@goffi.org>
parents: 996
diff changeset
42
4cc4d49e1d0f pages (chat): moved rendering preparation in prepare_render, and redirect to page_select if no jid is specified.
Goffi <goffi@goffi.org>
parents: 996
diff changeset
43 @defer.inlineCallbacks
4cc4d49e1d0f pages (chat): moved rendering preparation in prepare_render, and redirect to page_select if no jid is specified.
Goffi <goffi@goffi.org>
parents: 996
diff changeset
44 def prepare_render(self, request):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
45 #  FIXME: bug on room filtering (currently display messages from all rooms)
1000
4cc4d49e1d0f pages (chat): moved rendering preparation in prepare_render, and redirect to page_select if no jid is specified.
Goffi <goffi@goffi.org>
parents: 996
diff changeset
46 session = self.host.getSessionData(request, session_iface.ISATSession)
4cc4d49e1d0f pages (chat): moved rendering preparation in prepare_render, and redirect to page_select if no jid is specified.
Goffi <goffi@goffi.org>
parents: 996
diff changeset
47 template_data = request.template_data
4cc4d49e1d0f pages (chat): moved rendering preparation in prepare_render, and redirect to page_select if no jid is specified.
Goffi <goffi@goffi.org>
parents: 996
diff changeset
48 rdata = self.getRData(request)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
49 target_jid = rdata["target"]
1000
4cc4d49e1d0f pages (chat): moved rendering preparation in prepare_render, and redirect to page_select if no jid is specified.
Goffi <goffi@goffi.org>
parents: 996
diff changeset
50 profile = session.profile
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 profile_jid = session.jid
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
53 disco = yield self.host.bridgeCall("discoInfos", target_jid.host, "", True, profile)
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 if "conference" in [i[0] for i in disco[1]]:
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 chat_type = C.CHAT_GROUP
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
56 join_ret = yield self.host.bridgeCall(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
57 "mucJoin", target_jid.userhost(), "", "", profile
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
58 )
1232
4ccc5bb65be2 pages (chat): handle room statuses following backend change
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
59 (already_joined,
4ccc5bb65be2 pages (chat): handle room statuses following backend change
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
60 room_jid_s,
4ccc5bb65be2 pages (chat): handle room statuses following backend change
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
61 occupants,
4ccc5bb65be2 pages (chat): handle room statuses following backend change
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
62 user_nick,
4ccc5bb65be2 pages (chat): handle room statuses following backend change
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
63 room_subject,
4ccc5bb65be2 pages (chat): handle room statuses following backend change
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
64 room_statuses,
4ccc5bb65be2 pages (chat): handle room statuses following backend change
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
65 __) = join_ret
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
66 template_data["subject"] = room_subject
1232
4ccc5bb65be2 pages (chat): handle room statuses following backend change
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
67 template_data["room_statuses"] = room_statuses
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68 own_jid = jid.JID(room_jid_s)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 own_jid.resource = user_nick
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 else:
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 chat_type = C.CHAT_ONE2ONE
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 own_jid = profile_jid
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
73 rdata["chat_type"] = chat_type
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
74 template_data["own_jid"] = own_jid
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
76 self.registerSignal(request, "messageNew")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
77 history = yield self.host.bridgeCall(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
78 "historyGet",
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
79 profile_jid.userhost(),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
80 target_jid.userhost(),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
81 20,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
82 True,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
83 {},
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
84 profile,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
85 )
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 authors = {m[2] for m in history}
1266
6b7f9c3558cc server, pages: better identities handling:
Goffi <goffi@goffi.org>
parents: 1243
diff changeset
87 identities = session.identities
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 for author in authors:
1243
8aff742d0dd0 pages: updated `identityGet` call, following backend changes
Goffi <goffi@goffi.org>
parents: 1241
diff changeset
89 id_raw = yield self.host.bridgeCall(
8aff742d0dd0 pages: updated `identityGet` call, following backend changes
Goffi <goffi@goffi.org>
parents: 1241
diff changeset
90 "identityGet", author, [], True, profile)
8aff742d0dd0 pages: updated `identityGet` call, following backend changes
Goffi <goffi@goffi.org>
parents: 1241
diff changeset
91 identities[author] = data_format.deserialise(id_raw)
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
93 template_data["messages"] = data_objects.Messages(history)
1266
6b7f9c3558cc server, pages: better identities handling:
Goffi <goffi@goffi.org>
parents: 1243
diff changeset
94 rdata['identities'] = identities
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
95 template_data["target_jid"] = target_jid
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
96 template_data["chat_type"] = chat_type
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
99 def on_data(self, request, data):
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100 session = self.host.getSessionData(request, session_iface.ISATSession)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101 rdata = self.getRData(request)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
102 target = rdata["target"]
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
103 data_type = data.get("type", "")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
104 if data_type == "msg":
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
105 message = data["body"]
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
106 mess_type = (
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
107 C.MESS_TYPE_GROUPCHAT
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
108 if rdata["chat_type"] == C.CHAT_GROUP
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
109 else C.MESS_TYPE_CHAT
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
110 )
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
111 log.debug("message received: {}".format(message))
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
112 self.host.bridgeCall(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
113 "messageSend",
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
114 target.full(),
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
115 {"": message},
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
116 {},
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
117 mess_type,
1429
01936fc55cd9 pages (chat): fix message sending
Goffi <goffi@goffi.org>
parents: 1266
diff changeset
118 "",
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
119 session.profile,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1091
diff changeset
120 )
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
121 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
122 log.warning("unknown message type: {type}".format(type=data_type))