plugin XEP-0277, group blogs: proper asynchronous methods for sending blogs.

This commit is contained in:
Goffi 2013-11-14 17:51:35 +01:00
commit e23ff9f8d1
4 changed files with 19 additions and 19 deletions

View file

@ -85,7 +85,8 @@ class GroupBlog(object):
self.host = host
host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='sassa{ss}s', out_sign='',
method=self.sendGroupBlog)
method=self.sendGroupBlog,
async=True)
host.bridge.addMethod("sendGroupBlogComment", ".plugin", in_sign='sss', out_sign='',
method=self.sendGroupBlogComment,
@ -296,6 +297,7 @@ class GroupBlog(object):
defer_blog = self.host.plugins["XEP-0060"].publish(service, node_name, items=[mblog_item], profile_key=client.profile)
defer_blog.addErrback(self._mblogPublicationFailed)
return defer_blog
def _mblogPublicationFailed(self, failure):
#TODO
@ -317,19 +319,19 @@ class GroupBlog(object):
if access_type == "PUBLIC":
if access_list:
raise Exception("Publishers list must be empty when getting microblogs for all contacts")
self._publishMblog(client.item_access_pubsub, client, "PUBLIC", [], message, extra)
return self._publishMblog(client.item_access_pubsub, client, "PUBLIC", [], message, extra)
elif access_type == "GROUP":
_groups = set(access_list).intersection(client.roster.getGroups()) # We only keep group which actually exist
if not _groups:
raise BadAccessListError("No valid group")
self._publishMblog(client.item_access_pubsub, client, "GROUP", _groups, message, extra)
return self._publishMblog(client.item_access_pubsub, client, "GROUP", _groups, message, extra)
elif access_type == "JID":
raise NotImplementedError
else:
error(_("Unknown access type"))
raise BadAccessTypeError
self.initialise(profile_key).addCallback(initialised)
return self.initialise(profile_key).addCallback(initialised)
def sendGroupBlogComment(self, node_url, message, profile_key='@NONE@'):
"""Publish a comment in the given node

View file

@ -140,7 +140,7 @@ class SatPubSubClient(pubsub.PubSubClient):
return
for node in self.parent_plugin.managedNodes:
if event.nodeIdentifier == node[0]:
return node[1](event, self.parent.profile)
node[1](event, self.parent.profile)
def deleteReceived(self, event):
#TODO: manage delete event

View file

@ -26,6 +26,7 @@ from twisted.words.xish import domish
from wokkel import disco, pubsub
from wokkel.formats import Mood
from sat.core import exceptions
NS_USER_MOOD = 'http://jabber.org/protocol/mood'
@ -50,7 +51,7 @@ class XEP_0163(object):
self.pep_out_cb = {}
host.trigger.add("PubSub Disco Info", self.disoInfoTrigger)
host.bridge.addSignal("personalEvent", ".plugin", signature='ssa{ss}s') # args: from (jid), type(MOOD, TUNE, etc), data, profile
host.bridge.addMethod("sendPersonalEvent", ".plugin", in_sign='sa{ss}s', out_sign='i', method=self.sendPersonalEvent) # args: type(MOOD, TUNE, etc), data, profile_key; return 0 or error_code
host.bridge.addMethod("sendPersonalEvent", ".plugin", in_sign='sa{ss}s', out_sign='', method=self.sendPersonalEvent, async=True) # args: type(MOOD, TUNE, etc), data, profile_key;
self.addPEPEvent("MOOD", NS_USER_MOOD, self.userMoodCB, self.sendMood)
def disoInfoTrigger(self, disco_info, profile):
@ -66,7 +67,7 @@ class XEP_0163(object):
@param event_type: type of the event (always uppercase), can be MOOD, TUNE, etc
@param name: namespace of the node (e.g. http://jabber.org/protocol/mood for User Mood)
@param in_callback: method to call when this event occur
@param out_callback: method to call when we want to publish this event
@param out_callback: method to call when we want to publish this event (must return a deferred)
@param notify: add autosubscribe (+notify) if True"""
if out_callback:
self.pep_out_cb[event_type] = out_callback
@ -82,21 +83,21 @@ class XEP_0163(object):
@param profile: profile which send the data"""
item = pubsub.Item(payload=data)
self.host.plugins["XEP-0060"].publish(None, namespace, [item], profile_key=profile)
return self.host.plugins["XEP-0060"].publish(None, namespace, [item], profile_key=profile)
def sendPersonalEvent(self, event_type, data, profile_key='@DEFAULT@'):
"""Send personal event after checking the data is alright
@param event_type: type of event (eg: MOOD, TUNE), must be in self.pep_out_cb.keys()
@param data: dict of {string:string} of event_type dependant data
@param profile_key: profile who send the event
@return: 0 if success, error code else"""
"""
profile = self.host.memory.getProfileName(profile_key)
if not profile:
error(_('Trying to send personal event with an unknown profile key [%s]') % profile_key)
return 1
raise exceptions.ProfileUnknownError
if not event_type in self.pep_out_cb.keys():
error(_('Trying to send personal event for an unknown type'))
return 2
raise DataError('Type unknown')
return self.pep_out_cb[event_type](data, profile)
def userMoodCB(self, itemsEvent, profile):
@ -122,11 +123,9 @@ class XEP_0163(object):
value = data['mood'].lower()
text = data['text'] if 'text' in data else ''
except KeyError:
error(_("Mood data must contain at least 'mood' key"))
return 3
raise exceptions.DataError("Mood data must contain at least 'mood' key")
mood = UserMood(value, text)
self.sendPEPEvent(NS_USER_MOOD, mood, profile)
return 0
return self.sendPEPEvent(NS_USER_MOOD, mood, profile)
class UserMood(Mood, domish.Element):

View file

@ -160,14 +160,13 @@ class XEP_0277(object):
@param profile: profile which send the mood"""
if 'content' not in data:
error(_("Microblog data must contain at least 'content' key"))
return 3
raise exceptions.DataError('no "content" key found')
content = data['content']
if not content:
error(_("Microblog data's content value must not be empty"))
return 3
raise exceptions.DataError('empty content')
item = self.data2entry(data, profile)
self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key=profile)
return 0
return self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key=profile)
def getLastMicroblogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@'):
"""Get the last published microblogs