comparison src/plugins/plugin_xep_0277.py @ 297:c5554e2939dd

plugin XEP 0277: author for in request + author, updated management for out request - a workaround is now used to parse "nick" tag (Jappix behaviour) - author and updated can now be used in data when sendind microblog. Is no author is given, user jid is used, if no updated is given, current timestamp is used
author Goffi <goffi@goffi.org>
date Fri, 18 Feb 2011 22:32:02 +0100
parents 42438e43104a
children 9f3a6cf91668
comparison
equal deleted inserted replaced
296:d1cc69161f3f 297:c5554e2939dd
26 import twisted.internet.error 26 import twisted.internet.error
27 from twisted.words.xish import domish 27 from twisted.words.xish import domish
28 from sat.tools.xml_tools import ElementParser 28 from sat.tools.xml_tools import ElementParser
29 29
30 from wokkel import disco,pubsub 30 from wokkel import disco,pubsub
31 from feed.atom import Entry 31 from feed.atom import Entry, Author
32 import uuid 32 import uuid
33 from time import time 33 from time import time
34 34
35 NS_MICROBLOG = 'urn:xmpp:microblog:0' 35 NS_MICROBLOG = 'urn:xmpp:microblog:0'
36 36
62 return 62 return
63 _entry = Entry().import_xml(entry_elt.toXml().encode('utf-8')) 63 _entry = Entry().import_xml(entry_elt.toXml().encode('utf-8'))
64 microblog_data={} 64 microblog_data={}
65 try: 65 try:
66 microblog_data['content'] = _entry.title.text 66 microblog_data['content'] = _entry.title.text
67 if len(_entry.authors):
68 microblog_data['author'] = _entry.authors[0].name.text
67 microblog_data['timestamp'] = str(int(_entry.updated.tf)) 69 microblog_data['timestamp'] = str(int(_entry.updated.tf))
68 microblog_data['id'] = item['id'] 70 microblog_data['id'] = item['id']
69 except AttributeError, KeyError: 71 except AttributeError, KeyError:
70 error(_('Error while parsing atom entry for microblogging event')) 72 error(_('Error while parsing atom entry for microblogging event'))
71 return 73 return
74
75 ##XXX: workaround for Jappix behaviour
76 if not 'author' in microblog_data:
77 from xe import NestElement
78 try:
79 author=NestElement('author')
80 author.import_xml(str(_entry))
81 microblog_data['author'] = author.nick.text
82 except:
83 error(_('Cannot find author'))
84 ##end workaround Jappix
85
72 self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile) 86 self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile)
73 87
74 def sendMicroblog(self, data, profile): 88 def sendMicroblog(self, data, profile):
75 """Send XEP-0277's microblog data 89 """Send XEP-0277's microblog data
76 @param data: must include content 90 @param data: must include content
81 content = data['content'] 95 content = data['content']
82 if not content: 96 if not content:
83 error(_("Microblog data's content value must not be empty")) 97 error(_("Microblog data's content value must not be empty"))
84 _uuid = unicode(uuid.uuid1()) 98 _uuid = unicode(uuid.uuid1())
85 _entry = Entry() 99 _entry = Entry()
100 #FIXME: need to escape html
86 _entry.title = content.encode('utf-8') 101 _entry.title = content.encode('utf-8')
87 _entry.updated = time() 102 _entry.author = Author()
103 _entry.author.name = data.get('author',self.host.getJidNStream(profile)[0].userhost()).encode('utf-8')
104 _entry.updated = float(data.get('timestamp',time()))
88 _entry.id = _uuid 105 _entry.id = _uuid
89 _entry_elt = ElementParser()(str(_entry)) 106 _entry_elt = ElementParser()(str(_entry))
90 item = pubsub.Item(payload=_entry_elt) 107 item = pubsub.Item(payload=_entry_elt)
91 item['id'] = _uuid 108 item['id'] = _uuid
92 self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key = profile) 109 self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key = profile)