changeset 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 f88325b56a6a
children 3bf61858a123
files src/pages/chat/page_meta.py src/pages/common/blog/page_meta.py
diffstat 2 files changed, 105 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pages/chat/page_meta.py	Wed Jan 03 01:11:26 2018 +0100
@@ -0,0 +1,104 @@
+#!/usr/bin/env python2.7
+# -*- coding: utf-8 -*-
+
+from sat.core.i18n import _
+from twisted.internet import defer
+from sat.core.log import getLogger
+log = getLogger('pages/chat')
+from sat.tools.common import data_objects
+from twisted.words.protocols.jabber import jid
+from libervia.server.constants import Const as C
+from libervia.server import session_iface
+
+name = u'chat'
+access = C.PAGES_ACCESS_PROFILE
+template = u"chat/chat.html"
+dynamic = True
+
+
+@defer.inlineCallbacks
+def parse_url(self, request):
+    session = self.host.getSessionData(request, session_iface.ISATSession)
+    rdata = self.getRData(request)
+    template_data = request.template_data
+    profile = session.profile
+
+    try:
+        target_jid_s = self.nextPath(request)
+    except IndexError:
+        log.warning(_(u"missing chat jid"))
+        self.pageError(request, C.HTTP_BAD_REQUEST)
+
+    try:
+        target_jid = jid.JID(target_jid_s)
+        if not target_jid.user:
+            raise ValueError(_(u"invalid jid for chat (no local part)"))
+    except Exception as e:
+        log.warning(_(u"bad chat jid entered: {jid} ({msg})").format(
+            jid=target_jid,
+            msg = e))
+        self.pageError(request, C.HTTP_BAD_REQUEST)
+    else:
+        rdata['target'] = target_jid
+
+    profile_jid = session.jid
+    if profile_jid is None:
+        jid_s = yield self.host.bridgeCall(u'getParamA', "JabberID", "Connection", profile_key=profile)
+        profile_jid = session.jid = jid.JID(jid_s)
+
+    disco = yield self.host.bridgeCall(u"discoInfos", target_jid.host, profile)
+    if "conference" in [i[0] for i in disco[1]]:
+        chat_type = C.CHAT_GROUP
+        join_ret = yield self.host.bridgeCall(u"mucJoin", target_jid.userhost(), "", "", profile)
+        already_joined, room_jid_s, occupants, user_nick, room_subject, dummy = join_ret
+        template_data[u'subject'] = room_subject
+        own_jid = jid.JID(room_jid_s)
+        own_jid.resource = user_nick
+    else:
+        chat_type = C.CHAT_ONE2ONE
+        own_jid = profile_jid
+    rdata['chat_type'] = chat_type
+    template_data['own_jid'] = own_jid
+
+    self.registerSignal(request, u"messageNew")
+    history = yield self.host.bridgeCall(u'historyGet', profile_jid.userhost(), target_jid.userhost(), 20, True, {}, profile)
+    authors = {m[2] for m in history}
+    identities = {}
+    for author in authors:
+        identities[author] = yield self.host.bridgeCall(u'identityGet', author, profile)
+
+    template_data[u'messages'] = data_objects.Messages(history)
+    template_data[u'identities'] = identities
+    template_data[u'target_jid'] = target_jid
+    template_data[u'chat_type'] = chat_type
+
+
+def on_data(self, request, data):
+    session = self.host.getSessionData(request, session_iface.ISATSession)
+    rdata = self.getRData(request)
+    target = rdata['target']
+    data_type = data.get(u'type', '')
+    if data_type == 'msg':
+        message = data[u'body']
+        mess_type = C.MESS_TYPE_GROUPCHAT if rdata['chat_type'] == C.CHAT_GROUP else C.MESS_TYPE_CHAT
+        log.debug(u'message received: {}'.format(message))
+        self.host.bridgeCall(u'messageSend', target.full(), {u'': message}, {}, mess_type, {}, session.profile)
+    else:
+        log.warning(u'unknown message type: {type}'.format(type=data_type))
+
+
+@defer.inlineCallbacks
+def on_signal(self, request, signal, *args):
+    if signal == 'messageNew':
+        profile = self.getProfile(request)
+        template_data = request.template_data
+        template_data_update = {u"msg": data_objects.Message((args))}
+        identities = template_data['identities']
+        uid, timestamp, from_jid, to_jid, message, subject, mess_type, extra, dummy = args
+        if from_jid not in identities:
+            identities[from_jid] = yield self.host.bridgeCall(u'identityGet', from_jid, profile)
+            template_data_update['identities'] = identities
+        self.renderAndUpdate(request, u"chat/message.html", "#messages",
+                             template_data_update)
+    else:
+        log.error(_(u"Unexpected signal: {signal}").format(signal=signal))
--- a/src/pages/common/blog/page_meta.py	Wed Jan 03 01:10:12 2018 +0100
+++ b/src/pages/common/blog/page_meta.py	Wed Jan 03 01:11:26 2018 +0100
@@ -53,7 +53,7 @@
         if identities is not None:
             author = blog_item.author_jid
             if author not in identities:
-                identities[author] = yield self.host.bridge.identityGet(author, profile)
+                identities[author] = yield self.host.bridgeCall(u'identityGet', author, profile)
         for comment_data in blog_item.comments:
             service = comment_data[u'service']
             node = comment_data[u'node']