comparison src/plugins/plugin_misc_groupblog.py @ 831:d7f9cd8a08cd

plugin groupblog: added method getLastGroupBlogsAtom returns the Atom feed of blog posts
author souliane <souliane@mailoo.org>
date Wed, 22 Jan 2014 17:10:28 +0100
parents 1fe00f0c9a91
children c4b22aedb7d7
comparison
equal deleted inserted replaced
830:d6bdf6022180 831:d7f9cd8a08cd
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 from logging import debug, info, warning, error 21 from logging import debug, info, warning, error
22 from twisted.internet import defer 22 from twisted.internet import defer
23 from twisted.words.protocols.jabber import jid 23 from twisted.words.protocols.jabber import jid
24 from twisted.words.xish.domish import Element
24 from sat.core import exceptions 25 from sat.core import exceptions
25 from wokkel import disco, data_form, iwokkel 26 from wokkel import disco, data_form, iwokkel
26 from zope.interface import implements 27 from zope.interface import implements
27 28 from feed import date
28 import uuid 29 import uuid
29 import urllib 30 import urllib
30 31
31 try: 32 try:
32 from twisted.words.protocols.xmlstream import XMPPHandler 33 from twisted.words.protocols.xmlstream import XMPPHandler
101 async=True) 102 async=True)
102 103
103 host.bridge.addMethod("getLastGroupBlogs", ".plugin", 104 host.bridge.addMethod("getLastGroupBlogs", ".plugin",
104 in_sign='sis', out_sign='aa{ss}', 105 in_sign='sis', out_sign='aa{ss}',
105 method=self.getLastGroupBlogs, 106 method=self.getLastGroupBlogs,
107 async=True)
108
109 host.bridge.addMethod("getLastGroupBlogsAtom", ".plugin",
110 in_sign='sis', out_sign='s',
111 method=self.getLastGroupBlogsAtom,
106 async=True) 112 async=True)
107 113
108 host.bridge.addMethod("getMassiveLastGroupBlogs", ".plugin", 114 host.bridge.addMethod("getMassiveLastGroupBlogs", ".plugin",
109 in_sign='sasis', out_sign='a{saa{ss}}', 115 in_sign='sasis', out_sign='a{saa{ss}}',
110 method=self._getMassiveLastGroupBlogs, 116 method=self._getMassiveLastGroupBlogs,
494 return d 500 return d
495 501
496 #TODO: we need to use the server corresponding the the host of the jid 502 #TODO: we need to use the server corresponding the the host of the jid
497 return self._initialise(profile_key).addCallback(initialised) 503 return self._initialise(profile_key).addCallback(initialised)
498 504
505 def getLastGroupBlogsAtom(self, pub_jid_s, max_items=10, profile_key='@NONE@'):
506 """Get the atom feed of the last published microblogs
507 @param pub_jid: jid of the publisher
508 @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7)
509 @param profile_key: profile key
510 @return: atom XML feed (unicode)
511 """
512 pub_jid = jid.JID(pub_jid_s)
513
514 def removeAllURIs(element):
515 """Recursively remove the URIs of the element and its children.
516 Without that, the entry would still be valid but not displayed
517 by Firefox nor Thunderbird (and probably more readers)"""
518 element.uri = element.defaultUri = None
519 for child in element.children:
520 if isinstance(child, Element):
521 removeAllURIs(child)
522
523 def items2feed(items, pub_jid, client):
524 feed = """<?xml version="1.0" encoding="utf-8"?>
525 <feed xmlns="http://www.w3.org/2005/Atom">
526 <title>%(user)s's blogposts</title>
527 <link href="%(feed)s" rel="self" />
528 <link href="%(blog)s" />
529 <id>%(id)s</id>
530 <updated>%(date)s</updated>\n""" % {'user': pub_jid.user,
531 'feed': 'http://%s/blog/%s/atom.xml' % (client.jid.host, pub_jid.user),
532 'blog': 'http://%s/blog/%s' % (client.jid.host, pub_jid.user),
533 'id': self.getNodeName(pub_jid),
534 'date': date.rfc3339.timestamp_from_tf(date.rfc3339.tf_utc())}
535 for item in items:
536 entry = item.firstChildElement()
537 removeAllURIs(entry)
538 feed += " " + entry.toXml().encode('utf-8') + "\n"
539 return feed + "</feed>"
540
541 def initialised(result):
542 profile, client = result
543 d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(pub_jid),
544 max_items=max_items, profile_key=profile_key)
545 d.addCallback(items2feed, pub_jid, client)
546 d.addErrback(lambda ignore: {}) # TODO: more complete error management (log !)
547 return d
548
549 #TODO: we need to use the server corresponding the the host of the jid
550 return self._initialise(profile_key).addCallback(initialised)
551
499 def getGroupBlogComments(self, service_s, node, profile_key='@NONE@'): 552 def getGroupBlogComments(self, service_s, node, profile_key='@NONE@'):
500 """Get all comments of given node 553 """Get all comments of given node
501 @param service_s: service hosting the node 554 @param service_s: service hosting the node
502 @param node: comments node 555 @param node: comments node
503 @param profile_key: profile key 556 @param profile_key: profile key