Group blog first draft
- blog can now be sent, node are automatically created
This commit is contained in:
parent
1a579d9087
commit
0450772d2e
1 changed files with 304 additions and 0 deletions
292
src/plugins/plugin_misc_groupblog.py
Executable file
292
src/plugins/plugin_misc_groupblog.py
Executable file
|
|
@ -0,0 +1,292 @@
|
|||
#!/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
|
||||
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
|
||||
|
||||
NS_BLOG_COLLECTION = 'urn:xmpp:blogcollection:0'
|
||||
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 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("getLastMicroblogs", ".communication",
|
||||
in_sign='sis', out_sign='aa{ss}',
|
||||
method=self.getLastMicroblogs,
|
||||
async = True,
|
||||
doc = { 'summary':'retrieve items',
|
||||
'param_0':'jid: publisher of wanted microblog',
|
||||
'param_1':'max_items: see XEP-0060 #6.5.7',
|
||||
'param_2':'%(doc_profile)s',
|
||||
'return':'list of microblog data (dict)'
|
||||
})
|
||||
host.bridge.addMethod("setMicroblogAccess", ".communication", in_sign='ss', out_sign='',
|
||||
method=self.setMicroblogAccess,
|
||||
doc = {
|
||||
})"""
|
||||
|
||||
host.bridge.addMethod("initBlogCollection", ".communication", in_sign='s', out_sign='',
|
||||
method=self.initBlogCollection,
|
||||
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 initBlogCollection(self, profile_key="@DEFAULT@"):
|
||||
_jid, xmlstream = self.host.getJidNStream(profile_key)
|
||||
_options = {NS_NODE_TYPE:TYPE_COLLECTION}
|
||||
def cb(result):
|
||||
#Node is created with right permission
|
||||
debug(_("Microblog node collection created"))
|
||||
|
||||
def fatal_err(s_error):
|
||||
#Something went wrong
|
||||
error(_("Can't create node collection"))
|
||||
|
||||
def err_cb(s_error):
|
||||
#If the node already exists, the condition is "conflict",
|
||||
#else we have an unmanaged error
|
||||
if s_error.value.condition=='conflict':
|
||||
fatal_err(s_error)
|
||||
else:
|
||||
fatal_err(s_error)
|
||||
|
||||
def create_node():
|
||||
#return self.host.plugins["XEP-0060"].createNode(_jid.userhostJID(), NS_BLOG_COLLECTION, _options, profile_key=profile_key)
|
||||
return self.host.plugins["XEP-0060"].createNode(jid.JID("pubsub.tazar.int"), self._getRootNode(_jid), _options, profile_key=profile_key)
|
||||
|
||||
create_node().addCallback(cb).addErrback(err_cb)
|
||||
|
||||
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)
|
||||
|
||||
Loading…
Reference in a new issue