comparison libervia/pages/chat/page_meta.py @ 1124:28e3eb3bb217

files reorganisation and installation rework: - files have been reorganised to follow other SàT projects and usual Python organisation (no more "/src" directory) - VERSION file is now used, as for other SàT projects - replace the overcomplicated setup.py be a more sane one. Pyjamas part is not compiled anymore by setup.py, it must be done separatly - removed check for data_dir if it's empty - installation tested working in virtual env - libervia launching script is now in bin/libervia
author Goffi <goffi@goffi.org>
date Sat, 25 Aug 2018 17:59:48 +0200
parents src/pages/chat/page_meta.py@cdd389ef97bc
children 29eb15062416
comparison
equal deleted inserted replaced
1123:63a4b8fe9782 1124:28e3eb3bb217
1 #!/usr/bin/env python2.7
2 # -*- coding: utf-8 -*-
3
4 from sat.core.i18n import _
5 from twisted.internet import defer
6 from sat.core.log import getLogger
7
8 log = getLogger("pages/chat")
9 from sat.tools.common import data_objects
10 from twisted.words.protocols.jabber import jid
11 from libervia.server.constants import Const as C
12 from libervia.server import session_iface
13
14 name = u"chat"
15 access = C.PAGES_ACCESS_PROFILE
16 template = u"chat/chat.html"
17 dynamic = True
18
19
20 def parse_url(self, request):
21 rdata = self.getRData(request)
22
23 try:
24 target_jid_s = self.nextPath(request)
25 except IndexError:
26 # not chat jid, we redirect to jid selection page
27 self.pageRedirect(u"chat_select", request)
28
29 try:
30 target_jid = jid.JID(target_jid_s)
31 if not target_jid.user:
32 raise ValueError(_(u"invalid jid for chat (no local part)"))
33 except Exception as e:
34 log.warning(
35 _(u"bad chat jid entered: {jid} ({msg})").format(jid=target_jid, msg=e)
36 )
37 self.pageError(request, C.HTTP_BAD_REQUEST)
38 else:
39 rdata["target"] = target_jid
40
41
42 @defer.inlineCallbacks
43 def prepare_render(self, request):
44 #  FIXME: bug on room filtering (currently display messages from all rooms)
45 session = self.host.getSessionData(request, session_iface.ISATSession)
46 template_data = request.template_data
47 rdata = self.getRData(request)
48 target_jid = rdata["target"]
49 profile = session.profile
50 profile_jid = session.jid
51
52 disco = yield self.host.bridgeCall(u"discoInfos", target_jid.host, u"", True, profile)
53 if "conference" in [i[0] for i in disco[1]]:
54 chat_type = C.CHAT_GROUP
55 join_ret = yield self.host.bridgeCall(
56 u"mucJoin", target_jid.userhost(), "", "", profile
57 )
58 already_joined, room_jid_s, occupants, user_nick, room_subject, dummy = join_ret
59 template_data[u"subject"] = room_subject
60 own_jid = jid.JID(room_jid_s)
61 own_jid.resource = user_nick
62 else:
63 chat_type = C.CHAT_ONE2ONE
64 own_jid = profile_jid
65 rdata["chat_type"] = chat_type
66 template_data["own_jid"] = own_jid
67
68 self.registerSignal(request, u"messageNew")
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 )
78 authors = {m[2] for m in history}
79 identities = {}
80 for author in authors:
81 identities[author] = yield self.host.bridgeCall(u"identityGet", author, profile)
82
83 template_data[u"messages"] = data_objects.Messages(history)
84 template_data[u"identities"] = identities
85 template_data[u"target_jid"] = target_jid
86 template_data[u"chat_type"] = chat_type
87
88
89 def on_data(self, request, data):
90 session = self.host.getSessionData(request, session_iface.ISATSession)
91 rdata = self.getRData(request)
92 target = rdata["target"]
93 data_type = data.get(u"type", "")
94 if data_type == "msg":
95 message = data[u"body"]
96 mess_type = (
97 C.MESS_TYPE_GROUPCHAT
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 )
111 else:
112 log.warning(u"unknown message type: {type}".format(type=data_type))
113
114
115 @defer.inlineCallbacks
116 def on_signal(self, request, signal, *args):
117 if signal == "messageNew":
118 rdata = self.getRData(request)
119 template_data = request.template_data
120 template_data_update = {u"msg": data_objects.Message((args))}
121 target_jid = rdata["target"]
122 identities = template_data["identities"]
123 uid, timestamp, from_jid_s, to_jid_s, message, subject, mess_type, extra, dummy = (
124 args
125 )
126 from_jid = jid.JID(from_jid_s)
127 to_jid = jid.JID(to_jid_s)
128 if (
129 target_jid.userhostJID() != from_jid.userhostJID()
130 and target_jid.userhostJID() != to_jid.userhostJID()
131 ):
132 # the message is not linked with page's room/user
133 return
134
135 if from_jid_s not in identities:
136 profile = self.getProfile(request)
137 identities[from_jid_s] = yield self.host.bridgeCall(
138 u"identityGet", from_jid_s, profile
139 )
140 template_data_update["identities"] = identities
141 self.renderAndUpdate(
142 request, u"chat/message.html", "#messages", template_data_update
143 )
144 else:
145 log.error(_(u"Unexpected signal: {signal}").format(signal=signal))