comparison src/pages/chat/page_meta.py @ 996:d821c112e656

pages (chat): implementation of chat page using new dynamic pages, first draft
author Goffi <goffi@goffi.org>
date Wed, 03 Jan 2018 01:11:26 +0100
parents
children 4cc4d49e1d0f
comparison
equal deleted inserted replaced
995:f88325b56a6a 996:d821c112e656
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 log = getLogger('pages/chat')
8 from sat.tools.common import data_objects
9 from twisted.words.protocols.jabber import jid
10 from libervia.server.constants import Const as C
11 from libervia.server import session_iface
12
13 name = u'chat'
14 access = C.PAGES_ACCESS_PROFILE
15 template = u"chat/chat.html"
16 dynamic = True
17
18
19 @defer.inlineCallbacks
20 def parse_url(self, request):
21 session = self.host.getSessionData(request, session_iface.ISATSession)
22 rdata = self.getRData(request)
23 template_data = request.template_data
24 profile = session.profile
25
26 try:
27 target_jid_s = self.nextPath(request)
28 except IndexError:
29 log.warning(_(u"missing chat jid"))
30 self.pageError(request, C.HTTP_BAD_REQUEST)
31
32 try:
33 target_jid = jid.JID(target_jid_s)
34 if not target_jid.user:
35 raise ValueError(_(u"invalid jid for chat (no local part)"))
36 except Exception as e:
37 log.warning(_(u"bad chat jid entered: {jid} ({msg})").format(
38 jid=target_jid,
39 msg = e))
40 self.pageError(request, C.HTTP_BAD_REQUEST)
41 else:
42 rdata['target'] = target_jid
43
44 profile_jid = session.jid
45 if profile_jid is None:
46 jid_s = yield self.host.bridgeCall(u'getParamA', "JabberID", "Connection", profile_key=profile)
47 profile_jid = session.jid = jid.JID(jid_s)
48
49 disco = yield self.host.bridgeCall(u"discoInfos", target_jid.host, profile)
50 if "conference" in [i[0] for i in disco[1]]:
51 chat_type = C.CHAT_GROUP
52 join_ret = yield self.host.bridgeCall(u"mucJoin", target_jid.userhost(), "", "", profile)
53 already_joined, room_jid_s, occupants, user_nick, room_subject, dummy = join_ret
54 template_data[u'subject'] = room_subject
55 own_jid = jid.JID(room_jid_s)
56 own_jid.resource = user_nick
57 else:
58 chat_type = C.CHAT_ONE2ONE
59 own_jid = profile_jid
60 rdata['chat_type'] = chat_type
61 template_data['own_jid'] = own_jid
62
63 self.registerSignal(request, u"messageNew")
64 history = yield self.host.bridgeCall(u'historyGet', profile_jid.userhost(), target_jid.userhost(), 20, True, {}, profile)
65 authors = {m[2] for m in history}
66 identities = {}
67 for author in authors:
68 identities[author] = yield self.host.bridgeCall(u'identityGet', author, profile)
69
70 template_data[u'messages'] = data_objects.Messages(history)
71 template_data[u'identities'] = identities
72 template_data[u'target_jid'] = target_jid
73 template_data[u'chat_type'] = chat_type
74
75
76 def on_data(self, request, data):
77 session = self.host.getSessionData(request, session_iface.ISATSession)
78 rdata = self.getRData(request)
79 target = rdata['target']
80 data_type = data.get(u'type', '')
81 if data_type == 'msg':
82 message = data[u'body']
83 mess_type = C.MESS_TYPE_GROUPCHAT if rdata['chat_type'] == C.CHAT_GROUP else C.MESS_TYPE_CHAT
84 log.debug(u'message received: {}'.format(message))
85 self.host.bridgeCall(u'messageSend', target.full(), {u'': message}, {}, mess_type, {}, session.profile)
86 else:
87 log.warning(u'unknown message type: {type}'.format(type=data_type))
88
89
90 @defer.inlineCallbacks
91 def on_signal(self, request, signal, *args):
92 if signal == 'messageNew':
93 profile = self.getProfile(request)
94 template_data = request.template_data
95 template_data_update = {u"msg": data_objects.Message((args))}
96 identities = template_data['identities']
97 uid, timestamp, from_jid, to_jid, message, subject, mess_type, extra, dummy = args
98 if from_jid not in identities:
99 identities[from_jid] = yield self.host.bridgeCall(u'identityGet', from_jid, profile)
100 template_data_update['identities'] = identities
101 self.renderAndUpdate(request, u"chat/message.html", "#messages",
102 template_data_update)
103 else:
104 log.error(_(u"Unexpected signal: {signal}").format(signal=signal))