view sat_pubsub/mam.py @ 313:e8e1ca94fd8c

removed reference to sat.tmp.delay as it is not used anymore
author Goffi <goffi@goffi.org>
date Sun, 03 Jan 2016 18:33:20 +0100
parents a776544d84e5
children cca47e9977a5
line wrap: on
line source

#!/usr/bin/python
#-*- coding: utf-8 -*-

# Copyright (c) 2015-2016 Adrien Cossa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
XMPP Message Archive Management protocol.

This protocol is specified in
U{XEP-0313<http://xmpp.org/extensions/xep-0313.html>}.
"""


from zope.interface import implements

from twisted.words.xish import domish

from sat_pubsub import const
from sat_pubsub.backend import PubSubResourceFromBackend
from wokkel.pubsub import NS_PUBSUB_EVENT

from dateutil import parser

# TODO: change this when RSM and MAM are in wokkel
from sat.tmp.wokkel.rsm import RSMRequest
from sat.tmp.wokkel.mam import MAMPrefs, IMAMResource

NS_CLIENT = 'jabber:client'


class MAMResource(object):

    implements(IMAMResource)

    def __init__(self, backend):
        self.backend = backend

    def onArchiveRequest(self, mam, requestor):
        """

        @param mam: The MAM <query/> request.
        @type mam: L{MAMQueryReques<wokkel.mam.MAMQueryRequest>}

        @param requestor: JID of the requestor.
        @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>}

        @return: The RSM answer.
        @rtype: L{RSMResponse<wokkel.rsm.RSMResponse>}
        """
        ext_data = {}
        if mam.form:
            ext_data['filters'] = mam.form.fields.values()
        if mam.rsm is None:
            mam.rsm = RSMRequest(const.VAL_RSM_MAX_DEFAULT)
        ext_data['rsm'] = mam.rsm

        d = self.backend.getItems(mam.node, requestor, mam.rsm.max, None, ext_data)

        def make_message(elt):
            # XXX: http://xmpp.org/extensions/xep-0297.html#sect-idp629952 (rule 3)
            message = domish.Element((NS_CLIENT, "message"))
            event = message.addElement((NS_PUBSUB_EVENT, "event"))
            items = event.addElement('items')
            items["node"] = mam.node
            items.addChild(elt)
            return message

        def cb(elts):
            msg_data = []
            rsm_elt = None
            for elt in elts:
                if elt.name == 'set':
                    rsm_elt = elt
                elif elt.name == 'item':
                    date = parser.parse(''.join(elt.entry.published.children))
                    msg_data.append([elt['id'], make_message(elt), date])
            return (msg_data, rsm_elt)

        d.addErrback(PubSubResourceFromBackend._mapErrors)
        return d.addCallback(cb)

    def onPrefsGetRequest(self, requestor):
        """

        @param requestor: JID of the requestor.
        @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>}

        @return: The current settings.
        @rtype: L{wokkel.mam.MAMPrefs}
        """
        # TODO: return the actual current settings
        return MAMPrefs()

    def onPrefsSetRequest(self, prefs, requestor):
        """

        @param prefs: The new settings to set.
        @type prefs: L{wokkel.mam.MAMPrefs}

        @param requestor: JID of the requestor.
        @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>}

        @return: The settings that have actually been set.
        @rtype: L{wokkel.mam.MAMPrefs}
        """
        # TODO: set the new settings and return them
        return MAMPrefs()