Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0277.py @ 303:2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
plugin XEP_0060: added some method to manage pubsub nodes
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 24 Mar 2011 21:15:26 +0100 |
parents | 9f3a6cf91668 |
children | e04ccf122bb6 |
comparison
equal
deleted
inserted
replaced
302:9f3a6cf91668 | 303:2b52a5da0978 |
---|---|
31 from feed.atom import Entry, Author | 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 NS_ACCESS_MODEL = 'pubsub#access_model' | |
37 NS_PERSIST_ITEMS = 'pubsub#persist_items' | |
38 NS_MAX_ITEMS = 'pubsub#max_items' | |
36 | 39 |
37 PLUGIN_INFO = { | 40 PLUGIN_INFO = { |
38 "name": "Microblogging over XMPP Plugin", | 41 "name": "Microblogging over XMPP Plugin", |
39 "import_name": "XEP-0277", | 42 "import_name": "XEP-0277", |
40 "type": "XEP", | 43 "type": "XEP", |
59 'param_0':'jid: publisher of wanted microblog', | 62 'param_0':'jid: publisher of wanted microblog', |
60 'param_1':'max_items: see XEP-0060 #6.5.7', | 63 'param_1':'max_items: see XEP-0060 #6.5.7', |
61 'param_2':'%(doc_profile)s', | 64 'param_2':'%(doc_profile)s', |
62 'return':'list of microblog data (dict)' | 65 'return':'list of microblog data (dict)' |
63 }) | 66 }) |
67 host.bridge.addMethod("setMicroblogAccess", ".communication", in_sign='ss', out_sign='', | |
68 method=self.setMicroblogAccess, | |
69 doc = { | |
70 }) | |
64 | 71 |
65 def _item2mbdata(self, item): | 72 def _item2mbdata(self, item): |
66 """Convert an XML Item to microblog data used in bridge API | 73 """Convert an XML Item to microblog data used in bridge API |
67 @param item: domish.Element of microblog item | 74 @param item: domish.Element of microblog item |
68 @return: microblog data (dictionary)""" | 75 @return: microblog data (dictionary)""" |
122 item = pubsub.Item(payload=_entry_elt) | 129 item = pubsub.Item(payload=_entry_elt) |
123 item['id'] = _uuid | 130 item['id'] = _uuid |
124 self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key = profile) | 131 self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key = profile) |
125 return 0 | 132 return 0 |
126 | 133 |
127 def getLastMicroblogs(self, pub_jid, max_items=1, profile_key='@DEFAULT@', callback=None, errback=None): | 134 def getLastMicroblogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@', callback=None, errback=None): |
135 """Get the last published microblogs | |
136 @param pub_jid: jid of the publisher | |
137 @param max_items: how many microblogs we wants to get | |
138 @param profile_key: profile key | |
139 @param callback: used for the async answer | |
140 @param errback: used for the async answer | |
141 """ | |
128 assert(callback) | 142 assert(callback) |
129 d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key) | 143 d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key) |
130 d.addCallbacks(lambda items: callback(map(self._item2mbdata, items)), errback) | 144 d.addCallbacks(lambda items: callback(map(self._item2mbdata, items)), errback) |
131 | 145 |
146 def setMicroblogAccess(self, access="presence", profile_key='@DEFAULT@'): | |
147 """Create a microblog node on PEP with given access | |
148 If the node already exists, it is deleted and recreated | |
149 @param access: Node access model, according to xep-0060 #4.5 | |
150 @param profile_key: profile key""" | |
151 | |
152 jid, xmlstream = self.host.getJidNStream(profile_key) | |
153 def cb(result): | |
154 #Node is created with right permission | |
155 debug(_("Microblog node created")) | |
156 | |
157 def fatal_err(s_error): | |
158 #Something went wrong | |
159 error(_("Can't set microblog access")) | |
160 | |
161 def err_cb(s_error): | |
162 #If the node already exists, the condition is "conflict", | |
163 #else we have an unmanaged error | |
164 if s_error.value.condition=='conflict': | |
165 d = self.host.plugins["XEP-0060"].deleteNode(jid.userhostJID(), NS_MICROBLOG, profile_key=profile_key) | |
166 d.addCallback(lambda x: create_node().addCallback(cb).addErrback(fatal_err)) | |
167 d.addErrback(fatal_err) | |
168 else: | |
169 fatal_err(s_error) | |
170 | |
171 def create_node(): | |
172 return self.host.plugins["XEP-0060"].createNode(jid.userhostJID(), NS_MICROBLOG, {NS_ACCESS_MODEL:access, NS_PERSIST_ITEMS:1, NS_MAX_ITEMS:-1}, profile_key=profile_key) | |
173 | |
174 create_node().addCallback(cb).addErrback(err_cb) | |
132 | 175 |
133 | 176 |
177 |