annotate src/pages/chat/page_meta.py @ 1000:4cc4d49e1d0f

pages (chat): moved rendering preparation in prepare_render, and redirect to page_select if no jid is specified.
author Goffi <goffi@goffi.org>
date Fri, 05 Jan 2018 16:30:05 +0100
parents d821c112e656
children ae7e9ce4c64c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2.7
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 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
5 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
6 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
7 log = getLogger('pages/chat')
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 from sat.tools.common import data_objects
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 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
10 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
11 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
12
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 name = u'chat'
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 access = C.PAGES_ACCESS_PROFILE
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 template = u"chat/chat.html"
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 dynamic = True
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 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
20 rdata = self.getRData(request)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 try:
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 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
24 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
25 # not chat jid, we redirect to jid selection page
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
26 self.pageRedirect(u"chat_select", request)
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 try:
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 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
30 if not target_jid.user:
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 raise ValueError(_(u"invalid jid for chat (no local part)"))
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 except Exception as e:
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 log.warning(_(u"bad chat jid entered: {jid} ({msg})").format(
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 jid=target_jid,
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 msg = e))
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 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
37 else:
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 rdata['target'] = target_jid
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39
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
40
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
41 @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
42 def prepare_render(self, request):
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 # FIXME: bug on room filtering (currently display messages from all rooms)
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 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
45 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
46 rdata = self.getRData(request)
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 target_jid = rdata['target']
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 profile = session.profile
996
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 profile_jid = session.jid
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 if profile_jid is None:
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 jid_s = yield self.host.bridgeCall(u'getParamA', "JabberID", "Connection", profile_key=profile)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 profile_jid = session.jid = jid.JID(jid_s)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 disco = yield self.host.bridgeCall(u"discoInfos", target_jid.host, profile)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 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
56 chat_type = C.CHAT_GROUP
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 join_ret = yield self.host.bridgeCall(u"mucJoin", target_jid.userhost(), "", "", profile)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 already_joined, room_jid_s, occupants, user_nick, room_subject, dummy = join_ret
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 template_data[u'subject'] = room_subject
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 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
61 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
62 else:
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 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
64 own_jid = profile_jid
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 rdata['chat_type'] = chat_type
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 template_data['own_jid'] = own_jid
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self.registerSignal(request, u"messageNew")
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 history = yield self.host.bridgeCall(u'historyGet', profile_jid.userhost(), target_jid.userhost(), 20, True, {}, profile)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 authors = {m[2] for m in history}
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 identities = {}
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 for author in authors:
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 identities[author] = yield self.host.bridgeCall(u'identityGet', author, profile)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 template_data[u'messages'] = data_objects.Messages(history)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76 template_data[u'identities'] = identities
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 template_data[u'target_jid'] = target_jid
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 template_data[u'chat_type'] = chat_type
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81 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
82 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
83 rdata = self.getRData(request)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84 target = rdata['target']
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 data_type = data.get(u'type', '')
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 if data_type == 'msg':
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 message = data[u'body']
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 mess_type = C.MESS_TYPE_GROUPCHAT if rdata['chat_type'] == C.CHAT_GROUP else C.MESS_TYPE_CHAT
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89 log.debug(u'message received: {}'.format(message))
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 self.host.bridgeCall(u'messageSend', target.full(), {u'': message}, {}, mess_type, {}, session.profile)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91 else:
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92 log.warning(u'unknown message type: {type}'.format(type=data_type))
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95 @defer.inlineCallbacks
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 def on_signal(self, request, signal, *args):
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97 if signal == 'messageNew':
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98 profile = self.getProfile(request)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
99 template_data = request.template_data
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100 template_data_update = {u"msg": data_objects.Message((args))}
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101 identities = template_data['identities']
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102 uid, timestamp, from_jid, to_jid, message, subject, mess_type, extra, dummy = args
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
103 if from_jid not in identities:
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104 identities[from_jid] = yield self.host.bridgeCall(u'identityGet', from_jid, profile)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
105 template_data_update['identities'] = identities
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
106 self.renderAndUpdate(request, u"chat/message.html", "#messages",
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
107 template_data_update)
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
108 else:
d821c112e656 pages (chat): implementation of chat page using new dynamic pages, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109 log.error(_(u"Unexpected signal: {signal}").format(signal=signal))