view src/plugins/plugin_misc_groupblog.py @ 308:ce3607b7198d

plugin group blog: blog collection cleaning
author Goffi <goffi@goffi.org>
date Thu, 07 Apr 2011 23:40:33 +0200
parents 1e4575e12581
children 53adec87d1d7
line wrap: on
line source

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

"""
SAT plugin for microbloging with roster access
Copyright (C) 2009, 2010, 2011  Jérôme Poisson (goffi@goffi.org)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from logging import debug, info, error
from twisted.internet import protocol, defer
from twisted.words.protocols.jabber import jid
from twisted.words.protocols.jabber import error as jab_error
import twisted.internet.error
from twisted.words.xish import domish
from sat.tools.xml_tools import ElementParser

from wokkel import disco,pubsub
from feed.atom import Entry, Author
import uuid
from time import time

MBLOG_COLLECTION = 'MBLOGCOLLECTION'
CONFIG_NODE = 'CONFIG'
NS_ACCESS_MODEL = 'pubsub#access_model'
NS_PERSIST_ITEMS = 'pubsub#persist_items'
NS_MAX_ITEMS = 'pubsub#max_items'
NS_NODE_TYPE = 'pubsub#node_type'
TYPE_COLLECTION = 'collection'

PLUGIN_INFO = {
"name": "Group blogging throught collections",
"import_name": "groupblog",
"type": "MISC",
"protocols": [],
"dependencies": ["XEP-0277"],
"main": "GroupBlog",
"handler": "no",
"description": _("""Implementation of microblogging with roster access""")
}

class NodeCreationError(Exception):
    pass

class NodeDeletionError(Exception):
    pass

class GroupBlog():
    """This class use a PubSub Collection to manage roster access on microblog"""

    def __init__(self, host):
        info(_("Group blog plugin initialization"))
        self.host = host
        self._blog_nodes={}

        host.bridge.addMethod("cleanBlogCollection", ".communication", in_sign='s', out_sign='',
                               method=self.cleanBlogCollection,
                               doc = {
                                     })
        
        host.bridge.addMethod("getMblogNodes", ".communication", in_sign='s', out_sign='a{sas}',
                               method=self.getMblogNodes,
                               async = True,
                               doc = { 'summary':"retrieve mblog node, and their association with roster's groups",
                                       'param_0':'%(doc_profile)s',
                                       'return':'list of microblog data (dict)'
                                     })

        host.bridge.addMethod("sendGroupBlog", ".communication", in_sign='asss', out_sign='',
                               method=self.sendGroupBlog,
                               doc = { 'summary':"Send a microblog to a list of groups",
                                       'param_0':'list of groups which can read the microblog',
                                       'param_1':'text to send',
                                       'param_2':'%(doc_profile)s'
                                     })

    def _getRootNode(self, entity):
        return "%(entity)s_%(root_suff)s" % {'entity':entity.userhost(), 'root_suff':MBLOG_COLLECTION}

    def _getConfigNode(self, entity):
        return "%(entity)s_%(root_suff)s" % {'entity':entity.userhost(), 'root_suff':CONFIG_NODE}

    def _configNodeCb(self, result, callback, profile):
        self._blog_nodes[profile] = {}
        for item in result:
            node_ass = item.firstChildElement()
            assert(node_ass.name == "node_association")
            node = node_ass['node']
            groups = [unicode(group) for group in node_ass.children]
            self._blog_nodes[profile][node] = groups
        callback(self._blog_nodes[profile])
    
    def _configNodeFail(self, failure, errback):
        import pdb
        pdb.set_trace()
        errback() #FIXME

    def _configNodeErr(self, failure, user_jid, pubsub_ent, callback, errback, profile):
        if failure.value.condition == 'item-not-found':
            debug(_('Multiblog config node not found, creating it'))
            _options = {NS_ACCESS_MODEL:"whitelist", NS_PERSIST_ITEMS:1, NS_MAX_ITEMS:-1}
            d = self.host.plugins["XEP-0060"].createNode(pubsub_ent, self._getConfigNode(user_jid), _options, profile_key=profile)
            d.addCallback(self._configNodeCb, callback, profile)
            d.addErrback(self._configNodeFail, errback)
        else:
            self._configNodeFail(failure, errback)
    
    def getMblogNodes(self, profile_key='@DEFAULT@', callback=None, errback=None):
        debug(_('Getting mblog nodes'))
        profile = self.host.memory.getProfileName(profile_key)
        if not profile:
            error(_("Unknown profile"))
            return {}
        
        def after_init(ignore):
            pubsub_ent = self.host.memory.getServerServiceEntity("pubsub", "service", profile)
            _jid, xmlstream = self.host.getJidNStream(profile_key)
            d = self.host.plugins["XEP-0060"].getItems(pubsub_ent, self._getConfigNode(_jid), profile_key=profile_key)
            d.addCallbacks(self._configNodeCb, self._configNodeErr, callbackArgs=(callback, profile), errbackArgs=(_jid, pubsub_ent, callback, errback, profile))

        client = self.host.getClient(profile)
        if not client:
            error(_('No client for this profile key: %s') % profile_key)
            return
        client.client_initialized.addCallback(after_init)

    def _publishMblog(self, name, message, pubsub_ent, profile):
        """Actually publish the message on the group blog
        @param name: name of the node where we publish
        @param message: message to publish
        @param pubsub_ent: entity of the publish-subscribe service
        @param profile: profile of the owner of the group"""
        mblog_item = self.host.plugins["XEP-0277"].data2entry({'content':message}, profile)
        defer_blog = self.host.plugins["XEP-0060"].publish(pubsub_ent, name, items=[mblog_item], profile_key=profile)
        defer_blog.addErrback(self._mblogPublicationFailed)

    def _groupNodeCreated(self, ignore, groups, name, message, user_jid, pubsub_ent, profile):
        """A group node as been created, we need to add it to the configure node, and send the message to it
        @param groups: list of groups authorized to subscribe to the node
        @param name: unique name of the group
        @param message: message to publish to the group
        @param user_jid: jid of the owner of the node 
        @param pubsub_ent: entity of the publish-subscribe service
        @param profile: profile of the owner of the group"""
        config_node = self._getConfigNode(user_jid)
        _payload = domish.Element(('','node_association'))
        _payload['node'] = name
        for group in groups:
            _payload.addElement('group',content=group)
        config_item = pubsub.Item(payload=_payload)
        defer_config = self.host.plugins["XEP-0060"].publish(pubsub_ent, config_node, items=[config_item], profile_key=profile)
        defer_config.addCallback(lambda x: debug(_("Configuration node updated")))
        defer_config.addErrback(self._configUpdateFailed)

        #Finally, we publish the message
        self._publishMblog(name, message, pubsub_ent, profile)


    def _mblogPublicationFailed(self, failure):
        #TODO
        import pdb
        pdb.set_trace()

    def _configUpdateFailed(self, failure):
        #TODO
        import pdb
        pdb.set_trace()

    def _nodeCreationFailed(self, failure, name, user_jid, groups, pubsub_ent, message, profile):
        #TODO
        if failure.value.condition == "item-not-found":
            #The root node doesn't exists
            def err_creating_root_node(failure):
                msg = _("Can't create Root node")
                error(msg)
                raise NodeCreationError(msg)

            _options = {NS_NODE_TYPE:TYPE_COLLECTION}
            d = self.host.plugins["XEP-0060"].createNode(pubsub_ent, self._getRootNode(user_jid), _options, profile_key=profile)
            d.addCallback(self._createNode, name, user_jid, groups, pubsub_ent, message, profile)
            d.addErrback(err_creating_root_node) 
        else:
            import pdb
            pdb.set_trace()

    def _createNode(self, ignore, name, user_jid, groups, pubsub_ent, message, profile):
        """create a group microblog node
        @param ignore: ignored param, necessary to be added as a deferred callback
        @param name: name of the node
        @param user_jid: jid of the user creating the node
        @param groups: list of group than can subscribe to the node
        @param pubsub_ent: publish/subscribe service's entity
        @param message: message to publish
        @param profile: profile of the user creating the node"""
        _options = {NS_ACCESS_MODEL:"roster", NS_PERSIST_ITEMS:1, NS_MAX_ITEMS:-1,
                    'pubsub#node_type':'leaf', 'pubsub#collection':self._getRootNode(user_jid),
                    'pubsub#roster_groups_allowed':groups}
        d = self.host.plugins["XEP-0060"].createNode(pubsub_ent, name, _options, profile_key=profile)
        d.addCallback(self._groupNodeCreated, groups, name, message, user_jid, pubsub_ent, profile)
        d.addErrback(self._nodeCreationFailed, name, user_jid, groups, pubsub_ent, message, profile)
    
    def _getNodeForGroups(self, groups, profile):
        """Return node associated with the given list of groups
        @param groups: list of groups
        @param profile: profile of publisher"""
        for node in self._blog_nodes[profile]:
            node_groups = self._blog_nodes[profile][node]
            if set(node_groups) == set(groups):
                return node
        return None

    def sendGroupBlog(self, groups, message, profile_key='@DEFAULT@'):
        """Publish a microblog to the node associated to the groups
        If the node doesn't exist, it is created, then the message is posted
        @param groups: list of groups allowed to retrieve the microblog
        @param message: microblog
        @profile_key: %(doc_profile)s
        """
        profile = self.host.memory.getProfileName(profile_key)
        if not profile:
            error(_("Unknown profile"))
            return
        
        def after_init(ignore):
            _groups = list(set(groups).intersection(client.roster.getGroups())) #We only keep group which actually exist
            #TODO: send an error signal if user want to post to non existant groups
            _groups.sort()
            pubsub_ent = self.host.memory.getServerServiceEntity("pubsub", "service", profile)
            for group in _groups:
                _node = self._getNodeForGroups([group], profile)
                if not _node:
                    _node_name = unicode(uuid.uuid4())
                    self._createNode(None, _node_name, client.jid, [group], pubsub_ent, message, profile)
                else:
                    self._publishMblog(_node, message, pubsub_ent, profile)
        
        client = self.host.getClient(profile)
        if not client:
            error(_('No client for this profile key: %s') % profile_key)
            return
        client.client_initialized.addCallback(after_init)

    def _doCleaning(self, result, pubsub_ent, profile):
        """Compare the node in config node, and the existing nodes, and delete unknown ones"""
        #TODO: manage groups which don't exist anymore
        assert(len(result)==2)
        assert(result[0][0]==True and result[1][0]==True)
        config_nodes = [item.firstChildElement()["node"] for item in result[0][1]]
        existing_nodes = [item.nodeIdentifier for item in result[1][1]._items]
        to_delete = set(config_nodes).symmetric_difference(existing_nodes)
        def check_deletion(result):
            for (success, value) in result:
                if not success:
                    msg = _("Can't delete node")
                    error(msg)
                    raise NodeDeletionError(msg)
                    #TODO: log node which was not deleted

        
        d = defer.DeferredList([self.host.plugins["XEP-0060"].deleteNode(pubsub_ent, node, profile) for node in to_delete])
        d.addCallback(check_deletion)

    def cleanBlogCollection(self, profile_key='@DEFAULT@'):
        """Remove blog nodes not referenced in config node"""
        debug(_('Getting mblog nodes'))
        profile = self.host.memory.getProfileName(profile_key)
        if not profile:
            error(_("Unknown profile"))
            return {}
        
        def after_init(ignore):
            pubsub_ent = self.host.memory.getServerServiceEntity("pubsub", "service", profile)
            _jid, xmlstream = self.host.getJidNStream(profile_key)
            d_config = self.host.plugins["XEP-0060"].getItems(pubsub_ent, self._getConfigNode(_jid), profile_key=profile_key)
            d_root = client.disco.requestItems(pubsub_ent, self._getRootNode(client.jid))
            defer.DeferredList([d_config, d_root]).addCallback(self._doCleaning, pubsub_ent, profile)

        client = self.host.getClient(profile)
        if not client:
            error(_('No client for this profile key: %s') % profile_key)
            return
        client.client_initialized.addCallback(after_init)