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

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):