plugin XEP-0277: blog items data are now entirely serialised before going to bridge:

So far, and for historical reasons, blog items data where serialised using a unicode: unicode dict, which was causing trouble for many types of values (timestamps, booleans, lists).
This patch changes it by serialising the whole items before going to bridge, and deserialising it when going back. This way, complex data can be used easily in items.
This impact psEvent and serItemsData* methods which are renamed transItemsData* because there are not always serialising anymore (a new argument "serialise" allows to specify it).
When editing a blog post in jp, metadata are now more easy to manipulate, specially lists like tags.
This commit is contained in:
Goffi 2019-02-23 18:59:00 +01:00
commit 512ae0b91e
10 changed files with 188 additions and 184 deletions

View file

@ -251,7 +251,7 @@ class PubsubSchema(object):
extra.extra, extra.extra,
form_ns=form_ns or None, form_ns=form_ns or None,
) )
d.addCallback(self._p.serItemsData) d.addCallback(self._p.transItemsData)
return d return d
@defer.inlineCallbacks @defer.inlineCallbacks
@ -494,7 +494,7 @@ class PubsubSchema(object):
form_ns=form_ns, form_ns=form_ns,
filters=filters, filters=filters,
) )
d.addCallback(self._p.serItemsData) d.addCallback(self._p.transItemsData)
return d return d
def prepareBridgeSet(self, service, node, schema, item_id, extra, profile_key): def prepareBridgeSet(self, service, node, schema, item_id, extra, profile_key):

View file

@ -26,7 +26,6 @@ from twisted.internet import defer
from sat.core import exceptions from sat.core import exceptions
from wokkel import disco, data_form, iwokkel from wokkel import disco, data_form, iwokkel
from zope.interface import implements from zope.interface import implements
from sat.tools.common import data_format
try: try:
from twisted.words.protocols.xmlstream import XMPPHandler from twisted.words.protocols.xmlstream import XMPPHandler
@ -107,11 +106,8 @@ class GroupBlog(object):
return return
access_model = config_form.get(self._p.OPT_ACCESS_MODEL, self._p.ACCESS_OPEN) access_model = config_form.get(self._p.OPT_ACCESS_MODEL, self._p.ACCESS_OPEN)
if access_model == self._p.ACCESS_PUBLISHER_ROSTER: if access_model == self._p.ACCESS_PUBLISHER_ROSTER:
data_format.iter2dict( opt = self._p.OPT_ROSTER_GROUPS_ALLOWED
"group", microblog_data['groups'] = config_form.fields[opt].values
config_form.fields[self._p.OPT_ROSTER_GROUPS_ALLOWED].values,
microblog_data,
)
def _data2entryTrigger(self, client, mb_data, entry_elt, item_elt): def _data2entryTrigger(self, client, mb_data, entry_elt, item_elt):
"""Build fine access permission if needed """Build fine access permission if needed
@ -119,7 +115,7 @@ class GroupBlog(object):
This trigger check if "group*" key are present, This trigger check if "group*" key are present,
and create a fine item config to restrict view to these groups and create a fine item config to restrict view to these groups
""" """
groups = list(data_format.dict2iter("group", mb_data)) groups = mb_data.get('groups', [])
if not groups: if not groups:
return return
if not client.server_groupblog_available: if not client.server_groupblog_available:
@ -141,10 +137,7 @@ class GroupBlog(object):
""" """
if "group" in mb_data: if "group" in mb_data:
options[self._p.OPT_ACCESS_MODEL] = self._p.ACCESS_PUBLISHER_ROSTER options[self._p.OPT_ACCESS_MODEL] = self._p.ACCESS_PUBLISHER_ROSTER
options[self._p.OPT_ROSTER_GROUPS_ALLOWED] = list( options[self._p.OPT_ROSTER_GROUPS_ALLOWED] = mb_data['groups']
data_format.dict2iter("group", mb_data)
)
class GroupBlog_handler(XMPPHandler): class GroupBlog_handler(XMPPHandler):
implements(iwokkel.IDisco) implements(iwokkel.IDisco)

View file

@ -142,7 +142,7 @@ class MergeRequests(object):
d = self.get(client, service, node or None, max_items, item_ids, sub_id or None, d = self.get(client, service, node or None, max_items, item_ids, sub_id or None,
extra.rsm_request, extra.extra) extra.rsm_request, extra.extra)
d.addCallback(lambda (tickets, metadata, parsed_patches): ( d.addCallback(lambda (tickets, metadata, parsed_patches): (
self._p.serItemsData((tickets, metadata)) + self._p.transItemsData((tickets, metadata)) +
([[{key: unicode(value) for key, value in p.iteritems()} ([[{key: unicode(value) for key, value in p.iteritems()}
for p in patches] for patches in parsed_patches],))) for p in patches] for patches in parsed_patches],)))
return d return d

View file

@ -24,6 +24,7 @@ from sat.core.log import getLogger
log = getLogger(__name__) log = getLogger(__name__)
from sat.core import exceptions from sat.core import exceptions
from sat.tools import sat_defer from sat.tools import sat_defer
from sat.tools.common import data_format
from twisted.words.protocols.jabber import jid, error from twisted.words.protocols.jabber import jid, error
from twisted.internet import reactor, defer from twisted.internet import reactor, defer
@ -280,7 +281,7 @@ class XEP_0060(object):
#  high level observer method #  high level observer method
host.bridge.addSignal( host.bridge.addSignal(
"psEvent", ".plugin", signature="ssssa{ss}s" "psEvent", ".plugin", signature="ssssss"
) # args: category, service(jid), node, type (C.PS_ITEMS, C.PS_DELETE), data, profile ) # args: category, service(jid), node, type (C.PS_ITEMS, C.PS_DELETE), data, profile
# low level observer method, used if service/node is in watching list (see psNodeWatch* methods) # low level observer method, used if service/node is in watching list (see psNodeWatch* methods)
@ -568,7 +569,7 @@ class XEP_0060(object):
extra.rsm_request, extra.rsm_request,
extra.extra, extra.extra,
) )
d.addCallback(self.serItemsData) d.addCallback(self.transItemsData)
return d return d
def getItems(self, client, service, node, max_items=None, item_ids=None, sub_id=None, def getItems(self, client, service, node, max_items=None, item_ids=None, sub_id=None,
@ -648,7 +649,7 @@ class XEP_0060(object):
) )
def doSubscribe(data): def doSubscribe(data):
self.subscribe(service, node, profile_key=client.profile).addErrback( self.subscribe(client, service, node).addErrback(
subscribeEb, service, node subscribeEb, service, node
) )
return data return data
@ -1075,32 +1076,46 @@ class XEP_0060(object):
): ):
return self.rt_sessions.getResults(session_id, on_success, on_error, profile) return self.rt_sessions.getResults(session_id, on_success, on_error, profile)
def serItemsData(self, items_data, item_cb=lambda item: item.toXml()): def transItemsData(self, items_data, item_cb=lambda item: item.toXml(),
"""Helper method to serialise result from [getItems] serialise=False):
"""Helper method to transform result from [getItems]
the items_data must be a tuple(list[domish.Element], dict[unicode, unicode]) the items_data must be a tuple(list[domish.Element], dict[unicode, unicode])
as returned by [getItems]. metadata values are then casted to unicode and as returned by [getItems]. metadata values are then casted to unicode and
each item is passed to items_cb each item is passed to items_cb then optionally serialised with
data_format.serialise.
@param items_data(tuple): tuple returned by [getItems] @param items_data(tuple): tuple returned by [getItems]
@param item_cb(callable): method to transform each item @param item_cb(callable): method to transform each item
@param serialise(bool): if True do a data_format.serialise
after applying item_cb
@return (tuple): a serialised form ready to go throught bridge @return (tuple): a serialised form ready to go throught bridge
""" """
items, metadata = items_data items, metadata = items_data
if serialise:
items = [data_format.serialise(item_cb(item)) for item in items]
else:
items = [item_cb(item) for item in items]
return ( return (
[item_cb(item) for item in items], items,
{key: unicode(value) for key, value in metadata.iteritems()}, {key: unicode(value) for key, value in metadata.iteritems()},
) )
def serItemsDataD(self, items_data, item_cb): def transItemsDataD(self, items_data, item_cb, serialise=False):
"""Helper method to serialise result from [getItems], deferred version """Helper method to transform result from [getItems], deferred version
the items_data must be a tuple(list[domish.Element], dict[unicode, unicode]) the items_data must be a tuple(list[domish.Element], dict[unicode, unicode])
as returned by [getItems]. metadata values are then casted to unicode and as returned by [getItems]. metadata values are then casted to unicode and
each item is passed to items_cb each item is passed to items_cb then optionally serialised with
An errback is added to item_cb, and when it is fired the value is filtered from final items data_format.serialise.
An errback is added to item_cb, and when it is fired the value is filtered from
final items
@param items_data(tuple): tuple returned by [getItems] @param items_data(tuple): tuple returned by [getItems]
@param item_cb(callable): method to transform each item (must return a deferred) @param item_cb(callable): method to transform each item (must return a deferred)
@return (tuple): a deferred which fire a serialised form ready to go throught bridge @param serialise(bool): if True do a data_format.serialise
after applying item_cb
@return (tuple): a deferred which fire a serialised form ready to go throught
bridge
""" """
items, metadata = items_data items, metadata = items_data
@ -1111,9 +1126,14 @@ class XEP_0060(object):
d = defer.gatherResults([item_cb(item).addErrback(eb) for item in items]) d = defer.gatherResults([item_cb(item).addErrback(eb) for item in items])
def finishSerialisation(serialised_items): def finishSerialisation(parsed_items):
if serialise:
items = [data_format.serialise(i) for i in parsed_items if i is not None]
else:
items = [i for i in parsed_items if i is not None]
return ( return (
[item for item in serialised_items if item is not None], items,
{key: unicode(value) for key, value in metadata.iteritems()}, {key: unicode(value) for key, value in metadata.iteritems()},
) )
@ -1315,7 +1335,7 @@ class XEP_0060(object):
profile = self.host.getClient(profile_key).profile profile = self.host.getClient(profile_key).profile
d = self.rt_sessions.getResults( d = self.rt_sessions.getResults(
session_id, session_id,
on_success=lambda result: ("", self.serItemsData(result)), on_success=lambda result: ("", self.transItemsData(result)),
on_error=lambda failure: (unicode(failure.value) or UNSPECIFIED, ([], {})), on_error=lambda failure: (unicode(failure.value) or UNSPECIFIED, ([], {})),
profile=profile, profile=profile,
) )
@ -1348,14 +1368,8 @@ class XEP_0060(object):
profile_key, profile_key,
) )
def getFromMany( def getFromMany(self, node_data, max_item=None, rsm_request=None, extra=None,
self, profile_key=C.PROF_KEY_NONE):
node_data,
max_item=None,
rsm_request=None,
extra=None,
profile_key=C.PROF_KEY_NONE,
):
"""Get items from many nodes at once """Get items from many nodes at once
@param node_data (iterable[tuple]): iterable of tuple (service, node) where: @param node_data (iterable[tuple]): iterable of tuple (service, node) where:

View file

@ -27,6 +27,7 @@ from twisted.words.xish import domish
from wokkel import disco, pubsub from wokkel import disco, pubsub
from wokkel.formats import Mood from wokkel.formats import Mood
from sat.tools.common import data_format
NS_USER_MOOD = "http://jabber.org/protocol/mood" NS_USER_MOOD = "http://jabber.org/protocol/mood"
@ -156,7 +157,7 @@ class XEP_0163(object):
itemsEvent.sender.full(), itemsEvent.sender.full(),
itemsEvent.nodeIdentifier, itemsEvent.nodeIdentifier,
"MOOD", "MOOD",
{"mood": mood.value or "", "text": mood.text or ""}, data_format.serialise({"mood": mood.value or "", "text": mood.text or ""}),
profile, profile,
) )

View file

@ -85,7 +85,7 @@ class XEP_0277(object):
host.bridge.addMethod( host.bridge.addMethod(
"mbSend", "mbSend",
".plugin", ".plugin",
in_sign="ssa{ss}s", in_sign="ssss",
out_sign="", out_sign="",
method=self._mbSend, method=self._mbSend,
async=True, async=True,
@ -102,7 +102,7 @@ class XEP_0277(object):
"mbGet", "mbGet",
".plugin", ".plugin",
in_sign="ssiasa{ss}s", in_sign="ssiasa{ss}s",
out_sign="(aa{ss}a{ss})", out_sign="(asa{ss})",
method=self._mbGet, method=self._mbGet,
async=True, async=True,
) )
@ -125,7 +125,7 @@ class XEP_0277(object):
"mbGetFromManyRTResult", "mbGetFromManyRTResult",
".plugin", ".plugin",
in_sign="ss", in_sign="ss",
out_sign="(ua(sssaa{ss}a{ss}))", out_sign="(ua(sssasa{ss}))",
method=self._mbGetFromManyRTResult, method=self._mbGetFromManyRTResult,
async=True, async=True,
) )
@ -140,7 +140,7 @@ class XEP_0277(object):
"mbGetFromManyWithCommentsRTResult", "mbGetFromManyWithCommentsRTResult",
".plugin", ".plugin",
in_sign="ss", in_sign="ss",
out_sign="(ua(sssa(a{ss}a(sssaa{ss}a{ss}))a{ss}))", out_sign="(ua(sssa(sa(sssasa{ss}))a{ss}))",
method=self._mbGetFromManyWithCommentsRTResult, method=self._mbGetFromManyWithCommentsRTResult,
async=True, async=True,
) )
@ -178,7 +178,7 @@ class XEP_0277(object):
itemsEvent.sender.full(), itemsEvent.sender.full(),
itemsEvent.nodeIdentifier, itemsEvent.nodeIdentifier,
event, event,
data, data_format.serialise(data),
client.profile, client.profile,
) )
@ -196,7 +196,7 @@ class XEP_0277(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def item2mbdata(self, item_elt): def item2mbdata(self, item_elt):
"""Convert an XML Item to microblog data used in bridge API """Convert an XML Item to microblog data
@param item_elt: domish.Element of microblog item @param item_elt: domish.Element of microblog item
@return: microblog data (dictionary) @return: microblog data (dictionary)
@ -238,7 +238,8 @@ class XEP_0277(object):
if data_elt is None: if data_elt is None:
raise failure.Failure( raise failure.Failure(
exceptions.DataError( exceptions.DataError(
u"XHML content not wrapped in a <div/> element, this is not standard !" u"XHML content not wrapped in a <div/> element, this is not "
u"standard !"
) )
) )
if data_elt.uri != C.NS_XHTML: if data_elt.uri != C.NS_XHTML:
@ -281,9 +282,8 @@ class XEP_0277(object):
try: try:
id_elt = entry_elt.elements(NS_ATOM, "id").next() id_elt = entry_elt.elements(NS_ATOM, "id").next()
except StopIteration: except StopIteration:
msg = u"No atom id found in the pubsub item {}, this is not standard !".format( msg = (u"No atom id found in the pubsub item {}, this is not standard !"
id_ .format(id_))
)
log.warning(msg) log.warning(msg)
microblog_data[u"atom_id"] = "" microblog_data[u"atom_id"] = ""
else: else:
@ -343,18 +343,16 @@ class XEP_0277(object):
except StopIteration: except StopIteration:
msg = u"No atom updated element found in the pubsub item {}".format(id_) msg = u"No atom updated element found in the pubsub item {}".format(id_)
raise failure.Failure(exceptions.DataError(msg)) raise failure.Failure(exceptions.DataError(msg))
microblog_data[u"updated"] = unicode( microblog_data[u"updated"] = calendar.timegm(
calendar.timegm(dateutil.parser.parse(unicode(updated_elt)).utctimetuple()) dateutil.parser.parse(unicode(updated_elt)).utctimetuple()
) )
try: try:
published_elt = entry_elt.elements(NS_ATOM, "published").next() published_elt = entry_elt.elements(NS_ATOM, "published").next()
except StopIteration: except StopIteration:
microblog_data[u"published"] = microblog_data[u"updated"] microblog_data[u"published"] = microblog_data[u"updated"]
else: else:
microblog_data[u"published"] = unicode( microblog_data[u"published"] = calendar.timegm(
calendar.timegm( dateutil.parser.parse(unicode(published_elt)).utctimetuple()
dateutil.parser.parse(unicode(published_elt)).utctimetuple()
)
) )
# links # links
@ -420,16 +418,17 @@ class XEP_0277(object):
if not publisher: if not publisher:
log.debug(u"No publisher attribute, we can't verify author jid") log.debug(u"No publisher attribute, we can't verify author jid")
microblog_data[u"author_jid_verified"] = C.BOOL_FALSE microblog_data[u"author_jid_verified"] = False
elif jid.JID(publisher).userhostJID() == jid.JID(uri).userhostJID(): elif jid.JID(publisher).userhostJID() == jid.JID(uri).userhostJID():
microblog_data[u"author_jid_verified"] = C.BOOL_TRUE microblog_data[u"author_jid_verified"] = True
else: else:
log.warning( log.warning(
u"item atom:uri differ from publisher attribute, spoofing attempt ? atom:uri = {} publisher = {}".format( u"item atom:uri differ from publisher attribute, spoofing "
u"attempt ? atom:uri = {} publisher = {}".format(
uri, item_elt.getAttribute("publisher") uri, item_elt.getAttribute("publisher")
) )
) )
microblog_data[u"author_jid_verified"] = C.BOOL_FALSE microblog_data[u"author_jid_verified"] = False
# email # email
try: try:
email_elt = author_elt.elements(NS_ATOM, "email").next() email_elt = author_elt.elements(NS_ATOM, "email").next()
@ -439,11 +438,11 @@ class XEP_0277(object):
microblog_data[u"author_email"] = unicode(email_elt) microblog_data[u"author_email"] = unicode(email_elt)
# categories # categories
categories = ( categories = [
category_elt.getAttribute("term", "") category_elt.getAttribute("term", "")
for category_elt in entry_elt.elements(NS_ATOM, "category") for category_elt in entry_elt.elements(NS_ATOM, "category")
) ]
data_format.iter2dict("tag", categories, microblog_data) microblog_data[u"tags"] = categories
## the trigger ## ## the trigger ##
# if other plugins have things to add or change # if other plugins have things to add or change
@ -572,7 +571,7 @@ class XEP_0277(object):
) )
## categories ## ## categories ##
for tag in data_format.dict2iter("tag", data): for tag in data.get('tags', []):
category_elt = entry_elt.addElement("category") category_elt = entry_elt.addElement("category")
category_elt["term"] = tag category_elt["term"] = tag
@ -655,12 +654,16 @@ class XEP_0277(object):
@param access(unicode, None): access model @param access(unicode, None): access model
None to use same access model as parent item None to use same access model as parent item
""" """
# FIXME: if 'comments' already exists in mb_data, it is not used to create the Node # FIXME: if 'comments' already exists in mb_data,
allow_comments = C.bool(mb_data.pop("allow_comments", "false")) # it is not used to create the Node
if not allow_comments: allow_comments = mb_data.pop("allow_comments", None)
if allow_comments is None:
return
elif allow_comments == False:
if "comments" in mb_data: if "comments" in mb_data:
log.warning( log.warning(
u"comments are not allowed but there is already a comments node, it may be lost: {uri}".format( u"comments are not allowed but there is already a comments node, "
u"it may be lost: {uri}".format(
uri=mb_data["comments"] uri=mb_data["comments"]
) )
) )
@ -737,7 +740,8 @@ class XEP_0277(object):
) )
if "comments_node" in mb_data or "comments_service" in mb_data: if "comments_node" in mb_data or "comments_service" in mb_data:
raise exceptions.DataError( raise exceptions.DataError(
u"You can't use comments_service/comments_node and comments at the same time" u"You can't use comments_service/comments_node and comments at the "
u"same time"
) )
else: else:
mb_data["comments"] = self._p.getNodeURI(comments_service, comments_node) mb_data["comments"] = self._p.getNodeURI(comments_service, comments_node)
@ -746,6 +750,7 @@ class XEP_0277(object):
service = jid.JID(service) if service else None service = jid.JID(service) if service else None
node = node if node else NS_MICROBLOG node = node if node else NS_MICROBLOG
client = self.host.getClient(profile_key) client = self.host.getClient(profile_key)
data = data_format.deserialise(data)
return self.send(client, data, service, node) return self.send(client, data, service, node)
@defer.inlineCallbacks @defer.inlineCallbacks
@ -760,7 +765,7 @@ class XEP_0277(object):
None is equivalend as using default value None is equivalend as using default value
""" """
# TODO: check that all data keys are used, this would avoid sending publicly a private message # TODO: check that all data keys are used, this would avoid sending publicly a private message
# by accident (e.g. if group pluging is not loaded, and "grou*" key are not used) # by accident (e.g. if group plugin is not loaded, and "group*" key are not used)
if node is None: if node is None:
node = NS_MICROBLOG node = NS_MICROBLOG
@ -788,6 +793,11 @@ class XEP_0277(object):
## get ## ## get ##
def _mbGetSerialise(self, data):
items, metadata = data
items = [data_format.serialise(item) for item in items]
return items, metadata
def _mbGet(self, service="", node="", max_items=10, item_ids=None, extra_dict=None, def _mbGet(self, service="", node="", max_items=10, item_ids=None, extra_dict=None,
profile_key=C.PROF_KEY_NONE): profile_key=C.PROF_KEY_NONE):
""" """
@ -798,15 +808,10 @@ class XEP_0277(object):
service = jid.JID(service) if service else None service = jid.JID(service) if service else None
max_items = None if max_items == C.NO_LIMIT else max_items max_items = None if max_items == C.NO_LIMIT else max_items
extra = self._p.parseExtra(extra_dict) extra = self._p.parseExtra(extra_dict)
return self.mbGet( d = self.mbGet(client, service, node or None, max_items, item_ids,
client, extra.rsm_request, extra.extra)
service, d.addCallback(self._mbGetSerialise)
node or None, return d
max_items,
item_ids,
extra.rsm_request,
extra.extra,
)
@defer.inlineCallbacks @defer.inlineCallbacks
def mbGet(self, client, service=None, node=None, max_items=10, item_ids=None, def mbGet(self, client, service=None, node=None, max_items=10, item_ids=None,
@ -834,8 +839,8 @@ class XEP_0277(object):
rsm_request=rsm_request, rsm_request=rsm_request,
extra=extra, extra=extra,
) )
serialised = yield self._p.serItemsDataD(items_data, self.item2mbdata) mb_data = yield self._p.transItemsDataD(items_data, self.item2mbdata)
defer.returnValue(serialised) defer.returnValue(mb_data)
def parseCommentUrl(self, node_url): def parseCommentUrl(self, node_url):
"""Parse a XMPP URI """Parse a XMPP URI
@ -922,7 +927,8 @@ class XEP_0277(object):
C.ALL: get all jids from roster, publishers is not used C.ALL: get all jids from roster, publishers is not used
C.GROUP: get jids from groups C.GROUP: get jids from groups
C.JID: use publishers directly as list of jids C.JID: use publishers directly as list of jids
@param publishers: list of publishers, according to "publishers_type" (None, list of groups or list of jids) @param publishers: list of publishers, according to "publishers_type" (None,
list of groups or list of jids)
@param profile_key: %(doc_profile_key)s @param profile_key: %(doc_profile_key)s
""" """
client = self.host.getClient(profile_key) client = self.host.getClient(profile_key)
@ -931,7 +937,8 @@ class XEP_0277(object):
else: else:
jids_set = client.roster.getJidsSet(publishers_type, publishers) jids_set = client.roster.getJidsSet(publishers_type, publishers)
if publishers_type == C.ALL: if publishers_type == C.ALL:
try: # display messages from salut-a-toi@libervia.org or other PEP services try:
# display messages from salut-a-toi@libervia.org or other PEP services
services = self.host.plugins["EXTRA-PEP"].getFollowedEntities( services = self.host.plugins["EXTRA-PEP"].getFollowedEntities(
profile_key profile_key
) )
@ -987,7 +994,8 @@ class XEP_0277(object):
C.ALL: get all jids from roster, publishers is not used C.ALL: get all jids from roster, publishers is not used
C.GROUP: get jids from groups C.GROUP: get jids from groups
C.JID: use publishers directly as list of jids C.JID: use publishers directly as list of jids
@param publishers: list of publishers, according to "publishers_type" (None, list of groups or list of jids) @param publishers: list of publishers, according to "publishers_type" (None, list
of groups or list of jids)
@param profile: %(doc_profile)s @param profile: %(doc_profile)s
@return (str): session id @return (str): session id
""" """
@ -1017,7 +1025,7 @@ class XEP_0277(object):
def onSuccess(items_data): def onSuccess(items_data):
"""convert items elements to list of microblog data in items_data""" """convert items elements to list of microblog data in items_data"""
d = self._p.serItemsDataD(items_data, self.item2mbdata) d = self._p.transItemsDataD(items_data, self.item2mbdata, serialise=True)
d.addCallback(lambda serialised: ("", serialised)) d.addCallback(lambda serialised: ("", serialised))
return d return d
@ -1041,14 +1049,8 @@ class XEP_0277(object):
) )
return d return d
def _mbGetFromMany( def _mbGetFromMany(self, publishers_type, publishers, max_items=10, extra_dict=None,
self, profile_key=C.PROF_KEY_NONE):
publishers_type,
publishers,
max_items=10,
extra_dict=None,
profile_key=C.PROF_KEY_NONE,
):
""" """
@param max_items(int): maximum number of item to get, C.NO_LIMIT for no limit @param max_items(int): maximum number of item to get, C.NO_LIMIT for no limit
""" """
@ -1064,19 +1066,14 @@ class XEP_0277(object):
profile_key, profile_key,
) )
def mbGetFromMany( def mbGetFromMany(self, publishers_type, publishers, max_items=None, rsm_request=None,
self, extra=None, profile_key=C.PROF_KEY_NONE):
publishers_type,
publishers,
max_items=None,
rsm_request=None,
extra=None,
profile_key=C.PROF_KEY_NONE,
):
"""Get the published microblogs for a list of groups or jids """Get the published microblogs for a list of groups or jids
@param publishers_type (str): type of the list of publishers (one of "GROUP" or "JID" or "ALL") @param publishers_type (str): type of the list of publishers (one of "GROUP" or
@param publishers (list): list of publishers, according to publishers_type (list of groups or list of jids) "JID" or "ALL")
@param publishers (list): list of publishers, according to publishers_type (list
of groups or list of jids)
@param max_items (int): optional limit on the number of retrieved items. @param max_items (int): optional limit on the number of retrieved items.
@param rsm_request (rsm.RSMRequest): RSM request data, common to all publishers @param rsm_request (rsm.RSMRequest): RSM request data, common to all publishers
@param extra (dict): Extra data @param extra (dict): Extra data
@ -1093,9 +1090,31 @@ class XEP_0277(object):
# comments # # comments #
def _mbGetFromManyWithCommentsRTResult( def _mbGetFromManyWithCommentsRTResultSerialise(self, data):
self, session_id, profile_key=C.PROF_KEY_DEFAULT """Serialisation of result
):
This is probably the longest method name of whole SàT ecosystem ^^
@param data(dict): data as received by rt_sessions
@return (tuple): see [_mbGetFromManyWithCommentsRTResult]
"""
ret = []
data_iter = data[1].iteritems()
for (service, node), (success, (failure_, (items_data, metadata))) in data_iter:
items = []
for item, item_metadata in items_data:
item = data_format.serialise(item)
items.append((item, item_metadata))
ret.append((
service.full(),
node,
failure_,
items,
metadata))
return data[0], ret
def _mbGetFromManyWithCommentsRTResult(self, session_id,
profile_key=C.PROF_KEY_DEFAULT):
"""Get real-time results for [mbGetFromManyWithComments] session """Get real-time results for [mbGetFromManyWithComments] session
@param session_id: id of the real-time deferred session @param session_id: id of the real-time deferred session
@ -1118,32 +1137,16 @@ class XEP_0277(object):
""" """
profile = self.host.getClient(profile_key).profile profile = self.host.getClient(profile_key).profile
d = self.rt_sessions.getResults(session_id, profile=profile) d = self.rt_sessions.getResults(session_id, profile=profile)
d.addCallback( d.addCallback(self._mbGetFromManyWithCommentsRTResultSerialise)
lambda ret: (
ret[0],
[
(service.full(), node, failure, items, metadata)
for (service, node), (success, (failure, (items, metadata))) in ret[
1
].iteritems()
],
)
)
return d return d
def _mbGetFromManyWithComments( def _mbGetFromManyWithComments(self, publishers_type, publishers, max_items=10,
self, max_comments=C.NO_LIMIT, extra_dict=None,
publishers_type, extra_comments_dict=None, profile_key=C.PROF_KEY_NONE):
publishers,
max_items=10,
max_comments=C.NO_LIMIT,
extra_dict=None,
extra_comments_dict=None,
profile_key=C.PROF_KEY_NONE,
):
""" """
@param max_items(int): maximum number of item to get, C.NO_LIMIT for no limit @param max_items(int): maximum number of item to get, C.NO_LIMIT for no limit
@param max_comments(int): maximum number of comments to get, C.NO_LIMIT for no limit @param max_comments(int): maximum number of comments to get, C.NO_LIMIT for no
limit
""" """
max_items = None if max_items == C.NO_LIMIT else max_items max_items = None if max_items == C.NO_LIMIT else max_items
max_comments = None if max_comments == C.NO_LIMIT else max_comments max_comments = None if max_comments == C.NO_LIMIT else max_comments
@ -1162,22 +1165,16 @@ class XEP_0277(object):
profile_key, profile_key,
) )
def mbGetFromManyWithComments( def mbGetFromManyWithComments(self, publishers_type, publishers, max_items=None,
self, max_comments=None, rsm_request=None, extra=None,
publishers_type, rsm_comments=None, extra_comments=None,
publishers, profile_key=C.PROF_KEY_NONE):
max_items=None,
max_comments=None,
rsm_request=None,
extra=None,
rsm_comments=None,
extra_comments=None,
profile_key=C.PROF_KEY_NONE,
):
"""Helper method to get the microblogs and their comments in one shot """Helper method to get the microblogs and their comments in one shot
@param publishers_type (str): type of the list of publishers (one of "GROUP" or "JID" or "ALL") @param publishers_type (str): type of the list of publishers (one of "GROUP" or
@param publishers (list): list of publishers, according to publishers_type (list of groups or list of jids) "JID" or "ALL")
@param publishers (list): list of publishers, according to publishers_type (list
of groups or list of jids)
@param max_items (int): optional limit on the number of retrieved items. @param max_items (int): optional limit on the number of retrieved items.
@param max_comments (int): maximum number of comments to retrieve @param max_comments (int): maximum number of comments to retrieve
@param rsm_request (rsm.RSMRequest): RSM request for initial items only @param rsm_request (rsm.RSMRequest): RSM request for initial items only
@ -1223,8 +1220,8 @@ class XEP_0277(object):
) )
# then serialise # then serialise
d.addCallback( d.addCallback(
lambda items_data: self._p.serItemsDataD( lambda items_data: self._p.transItemsDataD(
items_data, self.item2mbdata items_data, self.item2mbdata, serialise=True
) )
) )
# with failure handling # with failure handling
@ -1232,7 +1229,8 @@ class XEP_0277(object):
lambda serialised_items_data: ("",) + serialised_items_data lambda serialised_items_data: ("",) + serialised_items_data
) )
d.addErrback(lambda failure: (unicode(failure.value), [], {})) d.addErrback(lambda failure: (unicode(failure.value), [], {}))
# and associate with service/node (needed if there are several comments nodes) # and associate with service/node (needed if there are several
# comments nodes)
d.addCallback( d.addCallback(
lambda serialised, service_s=service_s, node=node: ( lambda serialised, service_s=service_s, node=node: (
service_s, service_s,
@ -1260,7 +1258,7 @@ class XEP_0277(object):
client, service, node, max_items, rsm_request=rsm_request, extra=extra client, service, node, max_items, rsm_request=rsm_request, extra=extra
) )
d.addCallback( d.addCallback(
lambda items_data: self._p.serItemsDataD(items_data, self.item2mbdata) lambda items_data: self._p.transItemsDataD(items_data, self.item2mbdata)
) )
d.addCallback(getComments) d.addCallback(getComments)
d.addCallback(lambda items_comments_data: ("", items_comments_data)) d.addCallback(lambda items_comments_data: ("", items_comments_data))

View file

@ -73,10 +73,8 @@ def parsePubSubMetadata(metadata, items):
class BlogItem(object): class BlogItem(object):
def __init__(self, mb_data, parent): def __init__(self, mb_data, parent):
self.mb_data = mb_data self.mb_data = data_format.deserialise(mb_data)
self.parent = parent self.parent = parent
self._tags = None
self._groups = None
self._comments = None self._comments = None
self._comments_items_list = None self._comments_items_list = None
@ -126,15 +124,11 @@ class BlogItem(object):
@property @property
def tags(self): def tags(self):
if self._tags is None: return self.mb_data.get(u'tags', [])
self._tags = list(data_format.dict2iter("tag", self.mb_data))
return self._tags
@property @property
def groups(self): def groups(self):
if self._groups is None: return self.mb_data.get(u'groups', [])
self._groups = list(data_format.dict2iter("group", self.mb_data))
return self._groups
@property @property
def title(self): def title(self):

View file

@ -142,9 +142,10 @@ class BlogPublishCommon(object):
if metadata already exist, it will be overwritten if metadata already exist, it will be overwritten
""" """
mb_data["allow_comments"] = C.boolConst(self.args.comments) if self.args.comments is not None:
mb_data["allow_comments"] = self.args.comments
if self.args.tag: if self.args.tag:
data_format.iter2dict("tag", self.args.tag, mb_data, check_conflict=False) mb_data[u'tags'] = self.args.tag
if self.args.title is not None: if self.args.title is not None:
mb_data["title"] = self.args.title mb_data["title"] = self.args.title
@ -179,7 +180,7 @@ class Set(base.CommandBase, BlogPublishCommon):
self.host.bridge.mbSend( self.host.bridge.mbSend(
self.args.service, self.args.service,
self.args.node, self.args.node,
mb_data, data_format.serialise(mb_data),
self.profile, self.profile,
callback=self.exitCb, callback=self.exitCb,
errback=partial( errback=partial(
@ -239,7 +240,7 @@ class Get(base.CommandBase):
return u"\n".join(lines) return u"\n".join(lines)
def format_tags(self, item, keys): def format_tags(self, item, keys):
tags = data_format.dict2iter("tag", item, pop=True) tags = item.pop(u'tags', [])
return u", ".join(tags) return u", ".join(tags)
def format_updated(self, item, keys): def format_updated(self, item, keys):
@ -318,6 +319,8 @@ class Get(base.CommandBase):
sep=u"\n" if "content" in k else u"", sep=u"\n" if "content" in k else u"",
) )
value = k_cb[k](item, keys) if k in k_cb else item[k] value = k_cb[k](item, keys) if k in k_cb else item[k]
if isinstance(value, bool):
value = unicode(value).lower()
self.disp(header + value) self.disp(header + value)
# we want a separation line after each item but the last one # we want a separation line after each item but the last one
if idx < len(items) - 1: if idx < len(items) - 1:
@ -353,7 +356,7 @@ class Get(base.CommandBase):
else: else:
author = published = updated = None author = published = updated = None
if verbosity > 1: if verbosity > 1:
tags = list(data_format.dict2iter("tag", item, pop=True)) tags = item.pop('tags', [])
else: else:
tags = None tags = None
content = item.get(u"content") content = item.get(u"content")
@ -380,6 +383,9 @@ class Get(base.CommandBase):
print(u"\n" + sep + "\n") print(u"\n" + sep + "\n")
def mbGetCb(self, mb_result): def mbGetCb(self, mb_result):
items, metadata = mb_result
items = [data_format.deserialise(i) for i in items]
mb_result = items, metadata
self.output(mb_result) self.output(mb_result)
self.host.quit(C.EXIT_OK) self.host.quit(C.EXIT_OK)
@ -527,6 +533,8 @@ class Edit(base.CommandBase, BlogPublishCommon, common.BaseEdit):
if self.pubsub_item is not None: if self.pubsub_item is not None:
mb_data["id"] = self.pubsub_item mb_data["id"] = self.pubsub_item
mb_data = data_format.serialise(mb_data)
self.host.bridge.mbSend( self.host.bridge.mbSend(
self.pubsub_service, self.pubsub_node, mb_data, self.profile self.pubsub_service, self.pubsub_node, mb_data, self.profile
) )
@ -539,6 +547,7 @@ class Edit(base.CommandBase, BlogPublishCommon, common.BaseEdit):
def getItemData(self, service, node, item): def getItemData(self, service, node, item):
items = [item] if item is not None else [] items = [item] if item is not None else []
mb_data = self.host.bridge.mbGet(service, node, 1, items, {}, self.profile)[0][0] mb_data = self.host.bridge.mbGet(service, node, 1, items, {}, self.profile)[0][0]
mb_data = data_format.deserialise(mb_data)
try: try:
content = mb_data["content_xhtml"] content = mb_data["content_xhtml"]
except KeyError: except KeyError:

View file

@ -988,8 +988,9 @@ class QuickApp(object):
@param service_s (unicode): pubsub service @param service_s (unicode): pubsub service
@param node (unicode): pubsub node @param node (unicode): pubsub node
@param event_type (unicode): event type (one of C.PUBLISH, C.RETRACT, C.DELETE) @param event_type (unicode): event type (one of C.PUBLISH, C.RETRACT, C.DELETE)
@param data (dict): event data @param data (serialised_dict): event data
""" """
data = data_format.deserialise(data)
service_s = jid.JID(service_s) service_s = jid.JID(service_s)
if category == C.PS_MICROBLOG and self.MB_HANDLER: if category == C.PS_MICROBLOG and self.MB_HANDLER:
@ -997,9 +998,9 @@ class QuickApp(object):
if not "content" in data: if not "content" in data:
log.warning("No content found in microblog data") log.warning("No content found in microblog data")
return return
_groups = (
set(data_format.dict2iter("group", data)) or None # FIXME: check if [] make sense (instead of None)
) # FIXME: check if [] make sense (instead of None) _groups = data.get("group")
for wid in self.widgets.getWidgets(quick_blog.QuickBlog): for wid in self.widgets.getWidgets(quick_blog.QuickBlog):
wid.addEntryIfAccepted(service_s, node, data, _groups, profile) wid.addEntryIfAccepted(service_s, node, data, _groups, profile)

View file

@ -53,7 +53,7 @@ class Item(object):
self.title = data.get("title") self.title = data.get("title")
self.title_rich = None self.title_rich = None
self.title_xhtml = data.get("title_xhtml") self.title_xhtml = data.get("title_xhtml")
self.tags = list(data_format.dict2iter("tag", data)) self.tags = data.get('tags', [])
self.content = data.get("content") self.content = data.get("content")
self.content_rich = None self.content_rich = None
self.content_xhtml = data.get("content_xhtml") self.content_xhtml = data.get("content_xhtml")
@ -64,10 +64,7 @@ class Item(object):
except KeyError: except KeyError:
self.author_jid = None self.author_jid = None
try: self.author_verified = data.get("author_jid_verified", False)
self.author_verified = C.bool(data["author_jid_verified"])
except KeyError:
self.author_verified = False
try: try:
self.updated = float( self.updated = float(
@ -148,16 +145,8 @@ class EntriesManager(object):
for item, comments in items: for item, comments in items:
self.addEntry(item, comments, service=service, node=node, with_update=False) self.addEntry(item, comments, service=service, node=node, with_update=False)
def addEntry( def addEntry(self, item=None, comments=None, service=None, node=None,
self, with_update=True, editable=False, edit_entry=False):
item=None,
comments=None,
service=None,
node=None,
with_update=True,
editable=False,
edit_entry=False,
):
"""Add a microblog entry """Add a microblog entry
@param editable (bool): True if the entry can be modified @param editable (bool): True if the entry can be modified
@ -286,21 +275,21 @@ class Entry(EntriesManager):
if value is not None: if value is not None:
mb_data[name] = value mb_data[name] = value
data_format.iter2dict("tag", self.item.tags, mb_data) mb_data['tags'] = self.item.tags
if self.blog.new_message_target not in (C.PUBLIC, C.GROUP): if self.blog.new_message_target not in (C.PUBLIC, C.GROUP):
raise NotImplementedError raise NotImplementedError
if self.level == 0: if self.level == 0:
mb_data["allow_comments"] = C.BOOL_TRUE mb_data["allow_comments"] = True
if self.blog.new_message_target == C.GROUP: if self.blog.new_message_target == C.GROUP:
data_format.iter2dict("group", self.blog.targets, mb_data) mb_data['groups'] = list(self.blog.targets)
self.blog.host.bridge.mbSend( self.blog.host.bridge.mbSend(
unicode(self.service or ""), unicode(self.service or ""),
self.node or "", self.node or "",
mb_data, data_format.serialise(mb_data),
profile=self.blog.profile, profile=self.blog.profile,
) )
@ -398,9 +387,14 @@ class QuickBlog(EntriesManager, quick_widgets.QuickWidget):
) )
) )
for result in results: for result in results:
service, node, failure, items, metadata = result service, node, failure, items_data, metadata = result
for item_data in items_data:
item_data[0] = data_format.deserialise(item_data[0])
for item_metadata in item_data[1]:
item_metadata[3] = [data_format.deserialise(i) for i in item_metadata[3]]
if not failure: if not failure:
self._addMBItemsWithComments((items, metadata), service=jid.JID(service)) self._addMBItemsWithComments((items_data, metadata),
service=jid.JID(service))
self.update() self.update()
if remaining: if remaining: