comparison src/plugins/plugin_xep_0277.py @ 306:169e7386650a

plugin xep-0277: bridge data is now converted in pubsub item in a separate function
author Goffi <goffi@goffi.org>
date Thu, 07 Apr 2011 22:22:41 +0200
parents e04ccf122bb6
children f1a3db8ee04a
comparison
equal deleted inserted replaced
305:15a12bf2bb62 306:169e7386650a
105 def microblogCB(self, itemsEvent, profile): 105 def microblogCB(self, itemsEvent, profile):
106 for item in itemsEvent.items: 106 for item in itemsEvent.items:
107 microblog_data = self._item2mbdata(item) 107 microblog_data = self._item2mbdata(item)
108 self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile) 108 self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile)
109 109
110 def sendMicroblog(self, data, profile): 110 def data2entry(self, data, profile):
111 """Send XEP-0277's microblog data 111 """Convert a data dict to en entry usable to create an item
112 @param data: must include content 112 @param data: data dict as given by bridge method
113 @param profile: profile which send the mood""" 113 @return: domish.Element"""
114 if not data.has_key('content'): 114 _uuid = unicode(uuid.uuid1())
115 error(_("Microblog data must contain at least 'content' key"))
116 return 3
117 content = data['content'] 115 content = data['content']
118 if not content:
119 error(_("Microblog data's content value must not be empty"))
120 return 3
121 _uuid = unicode(uuid.uuid1())
122 _entry = Entry() 116 _entry = Entry()
123 #FIXME: need to escape html 117 #FIXME: need to escape html
124 _entry.title = unicode(content).encode('utf-8') 118 _entry.title = unicode(content).encode('utf-8')
125 _entry.author = Author() 119 _entry.author = Author()
126 _entry.author.name = data.get('author',self.host.getJidNStream(profile)[0].userhost()).encode('utf-8') 120 _entry.author.name = data.get('author',self.host.getJidNStream(profile)[0].userhost()).encode('utf-8')
127 _entry.updated = float(data.get('timestamp',time())) 121 _entry.updated = float(data.get('timestamp',time()))
128 _entry.id = str(_uuid) 122 _entry.id = str(_uuid)
129 _entry_elt = ElementParser()(str(_entry).decode('utf-8')) 123 _entry_elt = ElementParser()(str(_entry).decode('utf-8'))
130 item = pubsub.Item(payload=_entry_elt) 124 item = pubsub.Item(payload=_entry_elt)
131 item['id'] = _uuid 125 item['id'] = _uuid
126 return item
127
128 def sendMicroblog(self, data, profile):
129 """Send XEP-0277's microblog data
130 @param data: must include content
131 @param profile: profile which send the mood"""
132 if not data.has_key('content'):
133 error(_("Microblog data must contain at least 'content' key"))
134 return 3
135 content = data['content']
136 if not content.has_key("content"):
137 error(_("Microblog data's content value must not be empty"))
138 return 3
139 item = self.data2entry(data, profile)
132 self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key = profile) 140 self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key = profile)
133 return 0 141 return 0
134 142
135 def getLastMicroblogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@', callback=None, errback=None): 143 def getLastMicroblogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@', callback=None, errback=None):
136 """Get the last published microblogs 144 """Get the last published microblogs
148 """Create a microblog node on PEP with given access 156 """Create a microblog node on PEP with given access
149 If the node already exists, it change options 157 If the node already exists, it change options
150 @param access: Node access model, according to xep-0060 #4.5 158 @param access: Node access model, according to xep-0060 #4.5
151 @param profile_key: profile key""" 159 @param profile_key: profile key"""
152 160
153 jid, xmlstream = self.host.getJidNStream(profile_key) 161 _jid, xmlstream = self.host.getJidNStream(profile_key)
154 _options = {NS_ACCESS_MODEL:access, NS_PERSIST_ITEMS:1, NS_MAX_ITEMS:-1} 162 _options = {NS_ACCESS_MODEL:access, NS_PERSIST_ITEMS:1, NS_MAX_ITEMS:-1}
155 def cb(result): 163 def cb(result):
156 #Node is created with right permission 164 #Node is created with right permission
157 debug(_("Microblog node has now access %s") % access) 165 debug(_("Microblog node has now access %s") % access)
158 166
162 170
163 def err_cb(s_error): 171 def err_cb(s_error):
164 #If the node already exists, the condition is "conflict", 172 #If the node already exists, the condition is "conflict",
165 #else we have an unmanaged error 173 #else we have an unmanaged error
166 if s_error.value.condition=='conflict': 174 if s_error.value.condition=='conflict':
167 #d = self.host.plugins["XEP-0060"].deleteNode(jid.userhostJID(), NS_MICROBLOG, profile_key=profile_key) 175 #d = self.host.plugins["XEP-0060"].deleteNode(_jid.userhostJID(), NS_MICROBLOG, profile_key=profile_key)
168 #d.addCallback(lambda x: create_node().addCallback(cb).addErrback(fatal_err)) 176 #d.addCallback(lambda x: create_node().addCallback(cb).addErrback(fatal_err))
169 change_node_options().addCallback(cb).addErrback(fatal_err) 177 change_node_options().addCallback(cb).addErrback(fatal_err)
170 else: 178 else:
171 fatal_err(s_error) 179 fatal_err(s_error)
172 180
173 def create_node(): 181 def create_node():
174 return self.host.plugins["XEP-0060"].createNode(jid.userhostJID(), NS_MICROBLOG, _options, profile_key=profile_key) 182 return self.host.plugins["XEP-0060"].createNode(_jid.userhostJID(), NS_MICROBLOG, _options, profile_key=profile_key)
175 183
176 def change_node_options(): 184 def change_node_options():
177 return self.host.plugins["XEP-0060"].setOptions(jid.userhostJID(), NS_MICROBLOG, jid.userhostJID(), _options, profile_key=profile_key) 185 return self.host.plugins["XEP-0060"].setOptions(_jid.userhostJID(), NS_MICROBLOG, _jid.userhostJID(), _options, profile_key=profile_key)
178 186
179 create_node().addCallback(cb).addErrback(err_cb) 187 create_node().addCallback(cb).addErrback(err_cb)
180 188
181 189
182 190