comparison src/plugins/plugin_xep_0277.py @ 706:80e9d3ecb272

plugin XEP-0277, group blog: rich text management for sending microblogs
author Goffi <goffi@goffi.org>
date Thu, 14 Nov 2013 18:35:51 +0100
parents 3c304929af74
children 890fbf2d7fdd
comparison
equal deleted inserted replaced
705:6c8a119dcc94 706:80e9d3ecb272
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from logging import debug, info, warning, error 20 from logging import debug, info, warning, error
21 from twisted.words.protocols.jabber import jid 21 from twisted.words.protocols.jabber import jid
22 from twisted.internet import defer
22 from sat.core import exceptions 23 from sat.core import exceptions
23 from sat.tools.xml_tools import ElementParser 24 from sat.tools.xml_tools import ElementParser
24 25
25 from wokkel import pubsub 26 from wokkel import pubsub
26 from feed import atom 27 from feed import atom
27 import uuid 28 import uuid
28 from time import time 29 from time import time
29 import urlparse 30 import urlparse
31 from cgi import escape
30 32
31 NS_MICROBLOG = 'urn:xmpp:microblog:0' 33 NS_MICROBLOG = 'urn:xmpp:microblog:0'
34 NS_XHTML = 'http://www.w3.org/1999/xhtml'
32 35
33 PLUGIN_INFO = { 36 PLUGIN_INFO = {
34 "name": "Microblogging over XMPP Plugin", 37 "name": "Microblogging over XMPP Plugin",
35 "import_name": "XEP-0277", 38 "import_name": "XEP-0277",
36 "type": "XEP", 39 "type": "XEP",
37 "protocols": [], 40 "protocols": [],
38 "dependencies": ["XEP-0163", "XEP-0060"], 41 "dependencies": ["XEP-0163", "XEP-0060", "TEXT-SYNTAXES"],
39 "main": "XEP_0277", 42 "main": "XEP_0277",
40 "handler": "no", 43 "handler": "no",
41 "description": _("""Implementation of microblogging Protocol""") 44 "description": _("""Implementation of microblogging Protocol""")
42 } 45 }
43 46
128 def microblogCB(self, itemsEvent, profile): 131 def microblogCB(self, itemsEvent, profile):
129 for item in itemsEvent.items: 132 for item in itemsEvent.items:
130 microblog_data = self.item2mbdata(item) 133 microblog_data = self.item2mbdata(item)
131 self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile) 134 self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile)
132 135
136 @defer.inlineCallbacks
133 def data2entry(self, data, profile): 137 def data2entry(self, data, profile):
134 """Convert a data dict to en entry usable to create an item 138 """Convert a data dict to en entry usable to create an item
135 @param data: data dict as given by bridge method 139 @param data: data dict as given by bridge method
136 @return: domish.Element""" 140 @return: deferred which fire domish.Element"""
137 _uuid = unicode(uuid.uuid1()) 141 _uuid = unicode(uuid.uuid1())
138 content = data['content']
139 _entry = atom.Entry() 142 _entry = atom.Entry()
140 #FIXME: need to escape html 143
144 if "rich" in data:
145 synt = self.host.plugins["TEXT-SYNTAXES"]
146 converted = yield synt.convert(data['rich'], synt.getCurrentSyntax(profile), "XHTML")
147 content = u'<div xmlns="%s">%s</div>' % (NS_XHTML, converted)
148 _entry.title.attrs['type'] = 'xhtml'
149 else:
150 content = escape(data['content'])
151 _entry.title.attrs['type'] = 'text'
141 _entry.title = unicode(content).encode('utf-8') 152 _entry.title = unicode(content).encode('utf-8')
153
142 _entry.author = atom.Author() 154 _entry.author = atom.Author()
143 _entry.author.name = data.get('author', self.host.getJidNStream(profile)[0].userhost()).encode('utf-8') 155 _entry.author.name = data.get('author', self.host.getJidNStream(profile)[0].userhost()).encode('utf-8')
144 _entry.updated = float(data.get('timestamp', time())) 156 _entry.updated = float(data.get('timestamp', time()))
145 _entry.id = str(_uuid) 157 _entry.id = str(_uuid)
146 if 'comments' in data: 158 if 'comments' in data:
150 link.attrs['title'] = 'comments' 162 link.attrs['title'] = 'comments'
151 _entry.links.append(link) 163 _entry.links.append(link)
152 _entry_elt = ElementParser()(str(_entry).decode('utf-8')) 164 _entry_elt = ElementParser()(str(_entry).decode('utf-8'))
153 item = pubsub.Item(payload=_entry_elt) 165 item = pubsub.Item(payload=_entry_elt)
154 item['id'] = _uuid 166 item['id'] = _uuid
155 return item 167 defer.returnValue(item)
156 168
169 @defer.inlineCallbacks
157 def sendMicroblog(self, data, profile): 170 def sendMicroblog(self, data, profile):
158 """Send XEP-0277's microblog data 171 """Send XEP-0277's microblog data
159 @param data: must include content 172 @param data: must include content
160 @param profile: profile which send the mood""" 173 @param profile: profile which send the mood"""
161 if 'content' not in data: 174 if 'content' not in data:
163 raise exceptions.DataError('no "content" key found') 176 raise exceptions.DataError('no "content" key found')
164 content = data['content'] 177 content = data['content']
165 if not content: 178 if not content:
166 error(_("Microblog data's content value must not be empty")) 179 error(_("Microblog data's content value must not be empty"))
167 raise exceptions.DataError('empty content') 180 raise exceptions.DataError('empty content')
168 item = self.data2entry(data, profile) 181 item = yield self.data2entry(data, profile)
169 return self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key=profile) 182 ret = yield self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key=profile)
183 defer.returnValue(ret)
170 184
171 def getLastMicroblogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@'): 185 def getLastMicroblogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@'):
172 """Get the last published microblogs 186 """Get the last published microblogs
173 @param pub_jid: jid of the publisher 187 @param pub_jid: jid of the publisher
174 @param max_items: how many microblogs we want to get 188 @param max_items: how many microblogs we want to get