Mercurial > libervia-web
comparison libervia/pages/chat/page_meta.py @ 1216:b2d067339de3
python 3 port:
/!\ Python 3.6+ is now needed to use libervia
/!\ instability may occur and features may not be working anymore, this will improve with time
/!\ TxJSONRPC dependency has been removed
The same procedure as in backend has been applied (check backend commit ab2696e34d29 logs
for details). Removed now deprecated code (Pyjamas compiled browser part, legacy blog,
JSON RPC related code).
Adapted code to work without `html` and `themes` dirs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 13 Aug 2019 19:12:31 +0200 |
parents | 251eba911d4d |
children | 4ccc5bb65be2 |
comparison
equal
deleted
inserted
replaced
1215:f14ab8a25e8b | 1216:b2d067339de3 |
---|---|
1 #!/usr/bin/env python2.7 | 1 #!/usr/bin/env python3 |
2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
3 | 3 |
4 from sat.core.i18n import _ | 4 from sat.core.i18n import _ |
5 from twisted.internet import defer | 5 from twisted.internet import defer |
6 from sat.core.log import getLogger | 6 from sat.core.log import getLogger |
9 from sat.tools.common import data_objects | 9 from sat.tools.common import data_objects |
10 from twisted.words.protocols.jabber import jid | 10 from twisted.words.protocols.jabber import jid |
11 from libervia.server.constants import Const as C | 11 from libervia.server.constants import Const as C |
12 from libervia.server import session_iface | 12 from libervia.server import session_iface |
13 | 13 |
14 name = u"chat" | 14 name = "chat" |
15 access = C.PAGES_ACCESS_PROFILE | 15 access = C.PAGES_ACCESS_PROFILE |
16 template = u"chat/chat.html" | 16 template = "chat/chat.html" |
17 dynamic = True | 17 dynamic = True |
18 | 18 |
19 | 19 |
20 def parse_url(self, request): | 20 def parse_url(self, request): |
21 rdata = self.getRData(request) | 21 rdata = self.getRData(request) |
22 | 22 |
23 try: | 23 try: |
24 target_jid_s = self.nextPath(request) | 24 target_jid_s = self.nextPath(request) |
25 except IndexError: | 25 except IndexError: |
26 # not chat jid, we redirect to jid selection page | 26 # not chat jid, we redirect to jid selection page |
27 self.pageRedirect(u"chat_select", request) | 27 self.pageRedirect("chat_select", request) |
28 | 28 |
29 try: | 29 try: |
30 target_jid = jid.JID(target_jid_s) | 30 target_jid = jid.JID(target_jid_s) |
31 if not target_jid.user: | 31 if not target_jid.user: |
32 raise ValueError(_(u"invalid jid for chat (no local part)")) | 32 raise ValueError(_("invalid jid for chat (no local part)")) |
33 except Exception as e: | 33 except Exception as e: |
34 log.warning( | 34 log.warning( |
35 _(u"bad chat jid entered: {jid} ({msg})").format(jid=target_jid, msg=e) | 35 _("bad chat jid entered: {jid} ({msg})").format(jid=target_jid, msg=e) |
36 ) | 36 ) |
37 self.pageError(request, C.HTTP_BAD_REQUEST) | 37 self.pageError(request, C.HTTP_BAD_REQUEST) |
38 else: | 38 else: |
39 rdata["target"] = target_jid | 39 rdata["target"] = target_jid |
40 | 40 |
47 rdata = self.getRData(request) | 47 rdata = self.getRData(request) |
48 target_jid = rdata["target"] | 48 target_jid = rdata["target"] |
49 profile = session.profile | 49 profile = session.profile |
50 profile_jid = session.jid | 50 profile_jid = session.jid |
51 | 51 |
52 disco = yield self.host.bridgeCall(u"discoInfos", target_jid.host, u"", True, profile) | 52 disco = yield self.host.bridgeCall("discoInfos", target_jid.host, "", True, profile) |
53 if "conference" in [i[0] for i in disco[1]]: | 53 if "conference" in [i[0] for i in disco[1]]: |
54 chat_type = C.CHAT_GROUP | 54 chat_type = C.CHAT_GROUP |
55 join_ret = yield self.host.bridgeCall( | 55 join_ret = yield self.host.bridgeCall( |
56 u"mucJoin", target_jid.userhost(), "", "", profile | 56 "mucJoin", target_jid.userhost(), "", "", profile |
57 ) | 57 ) |
58 already_joined, room_jid_s, occupants, user_nick, room_subject, __ = join_ret | 58 already_joined, room_jid_s, occupants, user_nick, room_subject, __ = join_ret |
59 template_data[u"subject"] = room_subject | 59 template_data["subject"] = room_subject |
60 own_jid = jid.JID(room_jid_s) | 60 own_jid = jid.JID(room_jid_s) |
61 own_jid.resource = user_nick | 61 own_jid.resource = user_nick |
62 else: | 62 else: |
63 chat_type = C.CHAT_ONE2ONE | 63 chat_type = C.CHAT_ONE2ONE |
64 own_jid = profile_jid | 64 own_jid = profile_jid |
65 rdata["chat_type"] = chat_type | 65 rdata["chat_type"] = chat_type |
66 template_data["own_jid"] = own_jid | 66 template_data["own_jid"] = own_jid |
67 | 67 |
68 self.registerSignal(request, u"messageNew") | 68 self.registerSignal(request, "messageNew") |
69 history = yield self.host.bridgeCall( | 69 history = yield self.host.bridgeCall( |
70 u"historyGet", | 70 "historyGet", |
71 profile_jid.userhost(), | 71 profile_jid.userhost(), |
72 target_jid.userhost(), | 72 target_jid.userhost(), |
73 20, | 73 20, |
74 True, | 74 True, |
75 {}, | 75 {}, |
76 profile, | 76 profile, |
77 ) | 77 ) |
78 authors = {m[2] for m in history} | 78 authors = {m[2] for m in history} |
79 identities = {} | 79 identities = {} |
80 for author in authors: | 80 for author in authors: |
81 identities[author] = yield self.host.bridgeCall(u"identityGet", author, profile) | 81 identities[author] = yield self.host.bridgeCall("identityGet", author, profile) |
82 | 82 |
83 template_data[u"messages"] = data_objects.Messages(history) | 83 template_data["messages"] = data_objects.Messages(history) |
84 rdata[u'identities'] = template_data[u"identities"] = identities | 84 rdata['identities'] = template_data["identities"] = identities |
85 template_data[u"target_jid"] = target_jid | 85 template_data["target_jid"] = target_jid |
86 template_data[u"chat_type"] = chat_type | 86 template_data["chat_type"] = chat_type |
87 | 87 |
88 | 88 |
89 def on_data(self, request, data): | 89 def on_data(self, request, data): |
90 session = self.host.getSessionData(request, session_iface.ISATSession) | 90 session = self.host.getSessionData(request, session_iface.ISATSession) |
91 rdata = self.getRData(request) | 91 rdata = self.getRData(request) |
92 target = rdata["target"] | 92 target = rdata["target"] |
93 data_type = data.get(u"type", "") | 93 data_type = data.get("type", "") |
94 if data_type == "msg": | 94 if data_type == "msg": |
95 message = data[u"body"] | 95 message = data["body"] |
96 mess_type = ( | 96 mess_type = ( |
97 C.MESS_TYPE_GROUPCHAT | 97 C.MESS_TYPE_GROUPCHAT |
98 if rdata["chat_type"] == C.CHAT_GROUP | 98 if rdata["chat_type"] == C.CHAT_GROUP |
99 else C.MESS_TYPE_CHAT | 99 else C.MESS_TYPE_CHAT |
100 ) | 100 ) |
101 log.debug(u"message received: {}".format(message)) | 101 log.debug("message received: {}".format(message)) |
102 self.host.bridgeCall( | 102 self.host.bridgeCall( |
103 u"messageSend", | 103 "messageSend", |
104 target.full(), | 104 target.full(), |
105 {u"": message}, | 105 {"": message}, |
106 {}, | 106 {}, |
107 mess_type, | 107 mess_type, |
108 {}, | 108 {}, |
109 session.profile, | 109 session.profile, |
110 ) | 110 ) |
111 else: | 111 else: |
112 log.warning(u"unknown message type: {type}".format(type=data_type)) | 112 log.warning("unknown message type: {type}".format(type=data_type)) |
113 | 113 |
114 | 114 |
115 @defer.inlineCallbacks | 115 @defer.inlineCallbacks |
116 def on_signal(self, request, signal, *args): | 116 def on_signal(self, request, signal, *args): |
117 if signal == "messageNew": | 117 if signal == "messageNew": |
118 rdata = self.getRData(request) | 118 rdata = self.getRData(request) |
119 template_data_update = {u"msg": data_objects.Message((args))} | 119 template_data_update = {"msg": data_objects.Message((args))} |
120 target_jid = rdata["target"] | 120 target_jid = rdata["target"] |
121 identities = rdata["identities"] | 121 identities = rdata["identities"] |
122 uid, timestamp, from_jid_s, to_jid_s, message, subject, mess_type, extra, __ = ( | 122 uid, timestamp, from_jid_s, to_jid_s, message, subject, mess_type, extra, __ = ( |
123 args | 123 args |
124 ) | 124 ) |
132 return | 132 return |
133 | 133 |
134 if from_jid_s not in identities: | 134 if from_jid_s not in identities: |
135 profile = self.getProfile(request) | 135 profile = self.getProfile(request) |
136 identities[from_jid_s] = yield self.host.bridgeCall( | 136 identities[from_jid_s] = yield self.host.bridgeCall( |
137 u"identityGet", from_jid_s, profile | 137 "identityGet", from_jid_s, profile |
138 ) | 138 ) |
139 template_data_update["identities"] = identities | 139 template_data_update["identities"] = identities |
140 self.renderAndUpdate( | 140 self.renderAndUpdate( |
141 request, u"chat/message.html", "#messages", template_data_update | 141 request, "chat/message.html", "#messages", template_data_update |
142 ) | 142 ) |
143 else: | 143 else: |
144 log.error(_(u"Unexpected signal: {signal}").format(signal=signal)) | 144 log.error(_("Unexpected signal: {signal}").format(signal=signal)) |