Mercurial > libervia-backend
diff src/plugins/plugin_xep_0277.py @ 615:6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
- PubSub options constants are moved to XEP-0060
- comments url are generated/parsed according to XEP-0277
- microblog data can now have the following keys:
- "comments", with the url as given in the <link> tag
- "comments_service", with the jid of the PubSub service hosting the comments
- "comments_node", with the parsed node
- comments nodes use different access_model according to parent microblog item access
- publisher is not verified yet, see FIXME warning
- so far, comments node are automatically subscribed
- some bug fixes
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 20 May 2013 23:21:29 +0200 |
parents | 84a6e83157c2 |
children | 47d3a22b4629 |
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0277.py Sun Apr 07 23:27:07 2013 +0200 +++ b/src/plugins/plugin_xep_0277.py Mon May 20 23:21:29 2013 +0200 @@ -17,25 +17,18 @@ # You should have received a copy of the GNU Affero 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 client, jid -from twisted.words.protocols.jabber import error as jab_error -import twisted.internet.error -from twisted.words.xish import domish +from logging import debug, info, warning, error +from twisted.words.protocols.jabber import jid +from sat.core import exceptions from sat.tools.xml_tools import ElementParser -from wokkel import disco, pubsub -from feed.atom import Entry, Author +from wokkel import pubsub +from feed import atom import uuid from time import time +import urlparse NS_MICROBLOG = 'urn:xmpp:microblog:0' -OPT_ACCESS_MODEL = 'pubsub#access_model' -OPT_PERSIST_ITEMS = 'pubsub#persist_items' -OPT_MAX_ITEMS = 'pubsub#max_items' -OPT_DELIVER_PAYLOADS = 'pubsub#deliver_payloads' -OPT_SEND_ITEM_SUBSCRIBE = 'pubsub#send_item_subscribe' PLUGIN_INFO = { "name": "Microblogging over XMPP Plugin", @@ -73,6 +66,20 @@ async=True, doc={}) + def parseCommentUrl(self, node_url): + parsed_url = urlparse.urlparse(node_url, 'xmpp') + service = jid.JID(parsed_url.path) + queries = parsed_url.query.split(';') + parsed_queries = dict() + for query in queries: + parsed_queries.update(urlparse.parse_qs(query)) + node = parsed_queries.get('node',[''])[0] + + if not node: + raise exceptions.DataError('Invalid comments link') + + return (service, node) + def item2mbdata(self, item): """Convert an XML Item to microblog data used in bridge API @param item: domish.Element of microblog item @@ -82,7 +89,7 @@ except KeyError: warning(_('No entry element in microblog item')) return - _entry = Entry().import_xml(entry_elt.toXml().encode('utf-8')) + _entry = atom.Entry().import_xml(entry_elt.toXml().encode('utf-8')) microblog_data = {} try: microblog_data['content'] = _entry.title.text @@ -90,6 +97,17 @@ microblog_data['author'] = _entry.authors[0].name.text microblog_data['timestamp'] = str(int(_entry.updated.tf)) microblog_data['id'] = item['id'] + for link in _entry.links: + try: + if link.attrs["title"] == "comments": + microblog_data['comments'] = link.attrs["href"] + service, node = self.parseCommentUrl(microblog_data["comments"]) + microblog_data['comments_service'] = service.full() + microblog_data['comments_node'] = node + break + except (KeyError, exceptions.DataError): + continue + except (AttributeError, KeyError): error(_('Error while parsing atom entry for microblogging event')) return {} @@ -117,13 +135,19 @@ @return: domish.Element""" _uuid = unicode(uuid.uuid1()) content = data['content'] - _entry = Entry() + _entry = atom.Entry() #FIXME: need to escape html _entry.title = unicode(content).encode('utf-8') - _entry.author = Author() + _entry.author = atom.Author() _entry.author.name = data.get('author', self.host.getJidNStream(profile)[0].userhost()).encode('utf-8') _entry.updated = float(data.get('timestamp', time())) _entry.id = str(_uuid) + if 'comments' in data: + link = atom.Link() + link.attrs['href'] = data['comments'] + link.attrs['rel'] = 'replies' + link.attrs['title'] = 'comments' + _entry.links.append(link) _entry_elt = ElementParser()(str(_entry).decode('utf-8')) item = pubsub.Item(payload=_entry_elt) item['id'] = _uuid @@ -149,10 +173,7 @@ @param pub_jid: jid of the publisher @param max_items: how many microblogs we want to get @param profile_key: profile key - @param callback: used for the async answer - @param errback: used for the async answer """ - assert(callback) d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key) d.addCallback(lambda items: map(self.item2mbdata, items)) @@ -166,7 +187,8 @@ if not _jid: error(_("Can't find profile's jid")) return - _options = {OPT_ACCESS_MODEL: access, OPT_PERSIST_ITEMS: 1, OPT_MAX_ITEMS: -1, OPT_DELIVER_PAYLOADS: 1, OPT_SEND_ITEM_SUBSCRIBE: 1} + C = self.host.plugins["XEP-0060"] + _options = {C.OPT_ACCESS_MODEL: access, C.OPT_PERSIST_ITEMS: 1, C.OPT_MAX_ITEMS: -1, C.OPT_DELIVER_PAYLOADS: 1, C.OPT_SEND_ITEM_SUBSCRIBE: 1} def cb(result): #Node is created with right permission