Mercurial > libervia-web
comparison src/pages/chat/page_meta.py @ 1113:cdd389ef97bc
server: code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 29 Jun 2018 17:45:26 +0200 |
parents | 092e910292c9 |
children |
comparison
equal
deleted
inserted
replaced
1112:f287fc8bb31a | 1113:cdd389ef97bc |
---|---|
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 |
7 log = getLogger('pages/chat') | 7 |
8 log = getLogger("pages/chat") | |
8 from sat.tools.common import data_objects | 9 from sat.tools.common import data_objects |
9 from twisted.words.protocols.jabber import jid | 10 from twisted.words.protocols.jabber import jid |
10 from libervia.server.constants import Const as C | 11 from libervia.server.constants import Const as C |
11 from libervia.server import session_iface | 12 from libervia.server import session_iface |
12 | 13 |
13 name = u'chat' | 14 name = u"chat" |
14 access = C.PAGES_ACCESS_PROFILE | 15 access = C.PAGES_ACCESS_PROFILE |
15 template = u"chat/chat.html" | 16 template = u"chat/chat.html" |
16 dynamic = True | 17 dynamic = True |
17 | 18 |
18 | 19 |
28 try: | 29 try: |
29 target_jid = jid.JID(target_jid_s) | 30 target_jid = jid.JID(target_jid_s) |
30 if not target_jid.user: | 31 if not target_jid.user: |
31 raise ValueError(_(u"invalid jid for chat (no local part)")) | 32 raise ValueError(_(u"invalid jid for chat (no local part)")) |
32 except Exception as e: | 33 except Exception as e: |
33 log.warning(_(u"bad chat jid entered: {jid} ({msg})").format( | 34 log.warning( |
34 jid=target_jid, | 35 _(u"bad chat jid entered: {jid} ({msg})").format(jid=target_jid, msg=e) |
35 msg = e)) | 36 ) |
36 self.pageError(request, C.HTTP_BAD_REQUEST) | 37 self.pageError(request, C.HTTP_BAD_REQUEST) |
37 else: | 38 else: |
38 rdata['target'] = target_jid | 39 rdata["target"] = target_jid |
39 | 40 |
40 | 41 |
41 @defer.inlineCallbacks | 42 @defer.inlineCallbacks |
42 def prepare_render(self, request): | 43 def prepare_render(self, request): |
43 # FIXME: bug on room filtering (currently display messages from all rooms) | 44 # FIXME: bug on room filtering (currently display messages from all rooms) |
44 session = self.host.getSessionData(request, session_iface.ISATSession) | 45 session = self.host.getSessionData(request, session_iface.ISATSession) |
45 template_data = request.template_data | 46 template_data = request.template_data |
46 rdata = self.getRData(request) | 47 rdata = self.getRData(request) |
47 target_jid = rdata['target'] | 48 target_jid = rdata["target"] |
48 profile = session.profile | 49 profile = session.profile |
49 profile_jid = session.jid | 50 profile_jid = session.jid |
50 | 51 |
51 disco = yield self.host.bridgeCall(u"discoInfos", target_jid.host, u'', True, profile) | 52 disco = yield self.host.bridgeCall(u"discoInfos", target_jid.host, u"", True, profile) |
52 if "conference" in [i[0] for i in disco[1]]: | 53 if "conference" in [i[0] for i in disco[1]]: |
53 chat_type = C.CHAT_GROUP | 54 chat_type = C.CHAT_GROUP |
54 join_ret = yield self.host.bridgeCall(u"mucJoin", target_jid.userhost(), "", "", profile) | 55 join_ret = yield self.host.bridgeCall( |
56 u"mucJoin", target_jid.userhost(), "", "", profile | |
57 ) | |
55 already_joined, room_jid_s, occupants, user_nick, room_subject, dummy = join_ret | 58 already_joined, room_jid_s, occupants, user_nick, room_subject, dummy = join_ret |
56 template_data[u'subject'] = room_subject | 59 template_data[u"subject"] = room_subject |
57 own_jid = jid.JID(room_jid_s) | 60 own_jid = jid.JID(room_jid_s) |
58 own_jid.resource = user_nick | 61 own_jid.resource = user_nick |
59 else: | 62 else: |
60 chat_type = C.CHAT_ONE2ONE | 63 chat_type = C.CHAT_ONE2ONE |
61 own_jid = profile_jid | 64 own_jid = profile_jid |
62 rdata['chat_type'] = chat_type | 65 rdata["chat_type"] = chat_type |
63 template_data['own_jid'] = own_jid | 66 template_data["own_jid"] = own_jid |
64 | 67 |
65 self.registerSignal(request, u"messageNew") | 68 self.registerSignal(request, u"messageNew") |
66 history = yield self.host.bridgeCall(u'historyGet', profile_jid.userhost(), target_jid.userhost(), 20, True, {}, profile) | 69 history = yield self.host.bridgeCall( |
70 u"historyGet", | |
71 profile_jid.userhost(), | |
72 target_jid.userhost(), | |
73 20, | |
74 True, | |
75 {}, | |
76 profile, | |
77 ) | |
67 authors = {m[2] for m in history} | 78 authors = {m[2] for m in history} |
68 identities = {} | 79 identities = {} |
69 for author in authors: | 80 for author in authors: |
70 identities[author] = yield self.host.bridgeCall(u'identityGet', author, profile) | 81 identities[author] = yield self.host.bridgeCall(u"identityGet", author, profile) |
71 | 82 |
72 template_data[u'messages'] = data_objects.Messages(history) | 83 template_data[u"messages"] = data_objects.Messages(history) |
73 template_data[u'identities'] = identities | 84 template_data[u"identities"] = identities |
74 template_data[u'target_jid'] = target_jid | 85 template_data[u"target_jid"] = target_jid |
75 template_data[u'chat_type'] = chat_type | 86 template_data[u"chat_type"] = chat_type |
76 | 87 |
77 | 88 |
78 def on_data(self, request, data): | 89 def on_data(self, request, data): |
79 session = self.host.getSessionData(request, session_iface.ISATSession) | 90 session = self.host.getSessionData(request, session_iface.ISATSession) |
80 rdata = self.getRData(request) | 91 rdata = self.getRData(request) |
81 target = rdata['target'] | 92 target = rdata["target"] |
82 data_type = data.get(u'type', '') | 93 data_type = data.get(u"type", "") |
83 if data_type == 'msg': | 94 if data_type == "msg": |
84 message = data[u'body'] | 95 message = data[u"body"] |
85 mess_type = C.MESS_TYPE_GROUPCHAT if rdata['chat_type'] == C.CHAT_GROUP else C.MESS_TYPE_CHAT | 96 mess_type = ( |
86 log.debug(u'message received: {}'.format(message)) | 97 C.MESS_TYPE_GROUPCHAT |
87 self.host.bridgeCall(u'messageSend', target.full(), {u'': message}, {}, mess_type, {}, session.profile) | 98 if rdata["chat_type"] == C.CHAT_GROUP |
99 else C.MESS_TYPE_CHAT | |
100 ) | |
101 log.debug(u"message received: {}".format(message)) | |
102 self.host.bridgeCall( | |
103 u"messageSend", | |
104 target.full(), | |
105 {u"": message}, | |
106 {}, | |
107 mess_type, | |
108 {}, | |
109 session.profile, | |
110 ) | |
88 else: | 111 else: |
89 log.warning(u'unknown message type: {type}'.format(type=data_type)) | 112 log.warning(u"unknown message type: {type}".format(type=data_type)) |
90 | 113 |
91 | 114 |
92 @defer.inlineCallbacks | 115 @defer.inlineCallbacks |
93 def on_signal(self, request, signal, *args): | 116 def on_signal(self, request, signal, *args): |
94 if signal == 'messageNew': | 117 if signal == "messageNew": |
95 rdata = self.getRData(request) | 118 rdata = self.getRData(request) |
96 template_data = request.template_data | 119 template_data = request.template_data |
97 template_data_update = {u"msg": data_objects.Message((args))} | 120 template_data_update = {u"msg": data_objects.Message((args))} |
98 target_jid = rdata['target'] | 121 target_jid = rdata["target"] |
99 identities = template_data['identities'] | 122 identities = template_data["identities"] |
100 uid, timestamp, from_jid_s, to_jid_s, message, subject, mess_type, extra, dummy = args | 123 uid, timestamp, from_jid_s, to_jid_s, message, subject, mess_type, extra, dummy = ( |
124 args | |
125 ) | |
101 from_jid = jid.JID(from_jid_s) | 126 from_jid = jid.JID(from_jid_s) |
102 to_jid = jid.JID(to_jid_s) | 127 to_jid = jid.JID(to_jid_s) |
103 if (target_jid.userhostJID() != from_jid.userhostJID() and | 128 if ( |
104 target_jid.userhostJID() != to_jid.userhostJID()): | 129 target_jid.userhostJID() != from_jid.userhostJID() |
130 and target_jid.userhostJID() != to_jid.userhostJID() | |
131 ): | |
105 # the message is not linked with page's room/user | 132 # the message is not linked with page's room/user |
106 return | 133 return |
107 | 134 |
108 | |
109 if from_jid_s not in identities: | 135 if from_jid_s not in identities: |
110 profile = self.getProfile(request) | 136 profile = self.getProfile(request) |
111 identities[from_jid_s] = yield self.host.bridgeCall(u'identityGet', from_jid_s, profile) | 137 identities[from_jid_s] = yield self.host.bridgeCall( |
112 template_data_update['identities'] = identities | 138 u"identityGet", from_jid_s, profile |
113 self.renderAndUpdate(request, u"chat/message.html", "#messages", | 139 ) |
114 template_data_update) | 140 template_data_update["identities"] = identities |
141 self.renderAndUpdate( | |
142 request, u"chat/message.html", "#messages", template_data_update | |
143 ) | |
115 else: | 144 else: |
116 log.error(_(u"Unexpected signal: {signal}").format(signal=signal)) | 145 log.error(_(u"Unexpected signal: {signal}").format(signal=signal)) |