Mercurial > libervia-backend
annotate src/plugins/plugin_misc_groupblog.py @ 308:ce3607b7198d
plugin group blog: blog collection cleaning
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 07 Apr 2011 23:40:33 +0200 |
parents | 1e4575e12581 |
children | 53adec87d1d7 |
rev | line source |
---|---|
307 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT plugin for microbloging with roster access | |
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 from logging import debug, info, error | |
308
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
23 from twisted.internet import protocol, defer |
307 | 24 from twisted.words.protocols.jabber import jid |
25 from twisted.words.protocols.jabber import error as jab_error | |
26 import twisted.internet.error | |
27 from twisted.words.xish import domish | |
28 from sat.tools.xml_tools import ElementParser | |
29 | |
30 from wokkel import disco,pubsub | |
31 from feed.atom import Entry, Author | |
32 import uuid | |
33 from time import time | |
34 | |
35 MBLOG_COLLECTION = 'MBLOGCOLLECTION' | |
36 CONFIG_NODE = 'CONFIG' | |
37 NS_ACCESS_MODEL = 'pubsub#access_model' | |
38 NS_PERSIST_ITEMS = 'pubsub#persist_items' | |
39 NS_MAX_ITEMS = 'pubsub#max_items' | |
40 NS_NODE_TYPE = 'pubsub#node_type' | |
41 TYPE_COLLECTION = 'collection' | |
42 | |
43 PLUGIN_INFO = { | |
44 "name": "Group blogging throught collections", | |
45 "import_name": "groupblog", | |
46 "type": "MISC", | |
47 "protocols": [], | |
48 "dependencies": ["XEP-0277"], | |
49 "main": "GroupBlog", | |
50 "handler": "no", | |
51 "description": _("""Implementation of microblogging with roster access""") | |
52 } | |
53 | |
54 class NodeCreationError(Exception): | |
55 pass | |
56 | |
308
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
57 class NodeDeletionError(Exception): |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
58 pass |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
59 |
307 | 60 class GroupBlog(): |
61 """This class use a PubSub Collection to manage roster access on microblog""" | |
62 | |
63 def __init__(self, host): | |
64 info(_("Group blog plugin initialization")) | |
65 self.host = host | |
66 self._blog_nodes={} | |
67 | |
308
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
68 host.bridge.addMethod("cleanBlogCollection", ".communication", in_sign='s', out_sign='', |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
69 method=self.cleanBlogCollection, |
307 | 70 doc = { |
71 }) | |
72 | |
73 host.bridge.addMethod("getMblogNodes", ".communication", in_sign='s', out_sign='a{sas}', | |
74 method=self.getMblogNodes, | |
75 async = True, | |
76 doc = { 'summary':"retrieve mblog node, and their association with roster's groups", | |
77 'param_0':'%(doc_profile)s', | |
78 'return':'list of microblog data (dict)' | |
79 }) | |
80 | |
81 host.bridge.addMethod("sendGroupBlog", ".communication", in_sign='asss', out_sign='', | |
82 method=self.sendGroupBlog, | |
83 doc = { 'summary':"Send a microblog to a list of groups", | |
84 'param_0':'list of groups which can read the microblog', | |
85 'param_1':'text to send', | |
86 'param_2':'%(doc_profile)s' | |
87 }) | |
88 | |
89 def _getRootNode(self, entity): | |
90 return "%(entity)s_%(root_suff)s" % {'entity':entity.userhost(), 'root_suff':MBLOG_COLLECTION} | |
91 | |
92 def _getConfigNode(self, entity): | |
93 return "%(entity)s_%(root_suff)s" % {'entity':entity.userhost(), 'root_suff':CONFIG_NODE} | |
94 | |
95 def _configNodeCb(self, result, callback, profile): | |
96 self._blog_nodes[profile] = {} | |
97 for item in result: | |
98 node_ass = item.firstChildElement() | |
99 assert(node_ass.name == "node_association") | |
100 node = node_ass['node'] | |
101 groups = [unicode(group) for group in node_ass.children] | |
102 self._blog_nodes[profile][node] = groups | |
103 callback(self._blog_nodes[profile]) | |
104 | |
105 def _configNodeFail(self, failure, errback): | |
106 import pdb | |
107 pdb.set_trace() | |
108 errback() #FIXME | |
109 | |
110 def _configNodeErr(self, failure, user_jid, pubsub_ent, callback, errback, profile): | |
111 if failure.value.condition == 'item-not-found': | |
112 debug(_('Multiblog config node not found, creating it')) | |
113 _options = {NS_ACCESS_MODEL:"whitelist", NS_PERSIST_ITEMS:1, NS_MAX_ITEMS:-1} | |
114 d = self.host.plugins["XEP-0060"].createNode(pubsub_ent, self._getConfigNode(user_jid), _options, profile_key=profile) | |
115 d.addCallback(self._configNodeCb, callback, profile) | |
116 d.addErrback(self._configNodeFail, errback) | |
117 else: | |
118 self._configNodeFail(failure, errback) | |
119 | |
120 def getMblogNodes(self, profile_key='@DEFAULT@', callback=None, errback=None): | |
121 debug(_('Getting mblog nodes')) | |
122 profile = self.host.memory.getProfileName(profile_key) | |
123 if not profile: | |
124 error(_("Unknown profile")) | |
125 return {} | |
126 | |
127 def after_init(ignore): | |
128 pubsub_ent = self.host.memory.getServerServiceEntity("pubsub", "service", profile) | |
129 _jid, xmlstream = self.host.getJidNStream(profile_key) | |
130 d = self.host.plugins["XEP-0060"].getItems(pubsub_ent, self._getConfigNode(_jid), profile_key=profile_key) | |
131 d.addCallbacks(self._configNodeCb, self._configNodeErr, callbackArgs=(callback, profile), errbackArgs=(_jid, pubsub_ent, callback, errback, profile)) | |
132 | |
133 client = self.host.getClient(profile) | |
134 if not client: | |
135 error(_('No client for this profile key: %s') % profile_key) | |
136 return | |
137 client.client_initialized.addCallback(after_init) | |
138 | |
139 def _publishMblog(self, name, message, pubsub_ent, profile): | |
140 """Actually publish the message on the group blog | |
141 @param name: name of the node where we publish | |
142 @param message: message to publish | |
143 @param pubsub_ent: entity of the publish-subscribe service | |
144 @param profile: profile of the owner of the group""" | |
145 mblog_item = self.host.plugins["XEP-0277"].data2entry({'content':message}, profile) | |
146 defer_blog = self.host.plugins["XEP-0060"].publish(pubsub_ent, name, items=[mblog_item], profile_key=profile) | |
147 defer_blog.addErrback(self._mblogPublicationFailed) | |
148 | |
149 def _groupNodeCreated(self, ignore, groups, name, message, user_jid, pubsub_ent, profile): | |
150 """A group node as been created, we need to add it to the configure node, and send the message to it | |
151 @param groups: list of groups authorized to subscribe to the node | |
152 @param name: unique name of the group | |
153 @param message: message to publish to the group | |
154 @param user_jid: jid of the owner of the node | |
155 @param pubsub_ent: entity of the publish-subscribe service | |
156 @param profile: profile of the owner of the group""" | |
157 config_node = self._getConfigNode(user_jid) | |
158 _payload = domish.Element(('','node_association')) | |
159 _payload['node'] = name | |
160 for group in groups: | |
161 _payload.addElement('group',content=group) | |
162 config_item = pubsub.Item(payload=_payload) | |
163 defer_config = self.host.plugins["XEP-0060"].publish(pubsub_ent, config_node, items=[config_item], profile_key=profile) | |
164 defer_config.addCallback(lambda x: debug(_("Configuration node updated"))) | |
165 defer_config.addErrback(self._configUpdateFailed) | |
166 | |
167 #Finally, we publish the message | |
168 self._publishMblog(name, message, pubsub_ent, profile) | |
169 | |
170 | |
171 def _mblogPublicationFailed(self, failure): | |
172 #TODO | |
173 import pdb | |
174 pdb.set_trace() | |
175 | |
176 def _configUpdateFailed(self, failure): | |
177 #TODO | |
178 import pdb | |
179 pdb.set_trace() | |
180 | |
181 def _nodeCreationFailed(self, failure, name, user_jid, groups, pubsub_ent, message, profile): | |
182 #TODO | |
183 if failure.value.condition == "item-not-found": | |
184 #The root node doesn't exists | |
185 def err_creating_root_node(failure): | |
186 msg = _("Can't create Root node") | |
187 error(msg) | |
188 raise NodeCreationError(msg) | |
189 | |
190 _options = {NS_NODE_TYPE:TYPE_COLLECTION} | |
191 d = self.host.plugins["XEP-0060"].createNode(pubsub_ent, self._getRootNode(user_jid), _options, profile_key=profile) | |
192 d.addCallback(self._createNode, name, user_jid, groups, pubsub_ent, message, profile) | |
193 d.addErrback(err_creating_root_node) | |
194 else: | |
195 import pdb | |
196 pdb.set_trace() | |
197 | |
198 def _createNode(self, ignore, name, user_jid, groups, pubsub_ent, message, profile): | |
199 """create a group microblog node | |
200 @param ignore: ignored param, necessary to be added as a deferred callback | |
201 @param name: name of the node | |
202 @param user_jid: jid of the user creating the node | |
203 @param groups: list of group than can subscribe to the node | |
204 @param pubsub_ent: publish/subscribe service's entity | |
205 @param message: message to publish | |
206 @param profile: profile of the user creating the node""" | |
207 _options = {NS_ACCESS_MODEL:"roster", NS_PERSIST_ITEMS:1, NS_MAX_ITEMS:-1, | |
208 'pubsub#node_type':'leaf', 'pubsub#collection':self._getRootNode(user_jid), | |
209 'pubsub#roster_groups_allowed':groups} | |
210 d = self.host.plugins["XEP-0060"].createNode(pubsub_ent, name, _options, profile_key=profile) | |
211 d.addCallback(self._groupNodeCreated, groups, name, message, user_jid, pubsub_ent, profile) | |
212 d.addErrback(self._nodeCreationFailed, name, user_jid, groups, pubsub_ent, message, profile) | |
213 | |
214 def _getNodeForGroups(self, groups, profile): | |
215 """Return node associated with the given list of groups | |
216 @param groups: list of groups | |
217 @param profile: profile of publisher""" | |
218 for node in self._blog_nodes[profile]: | |
219 node_groups = self._blog_nodes[profile][node] | |
220 if set(node_groups) == set(groups): | |
221 return node | |
222 return None | |
223 | |
224 def sendGroupBlog(self, groups, message, profile_key='@DEFAULT@'): | |
225 """Publish a microblog to the node associated to the groups | |
226 If the node doesn't exist, it is created, then the message is posted | |
227 @param groups: list of groups allowed to retrieve the microblog | |
228 @param message: microblog | |
229 @profile_key: %(doc_profile)s | |
230 """ | |
231 profile = self.host.memory.getProfileName(profile_key) | |
232 if not profile: | |
233 error(_("Unknown profile")) | |
234 return | |
235 | |
236 def after_init(ignore): | |
237 _groups = list(set(groups).intersection(client.roster.getGroups())) #We only keep group which actually exist | |
238 #TODO: send an error signal if user want to post to non existant groups | |
239 _groups.sort() | |
240 pubsub_ent = self.host.memory.getServerServiceEntity("pubsub", "service", profile) | |
241 for group in _groups: | |
242 _node = self._getNodeForGroups([group], profile) | |
243 if not _node: | |
244 _node_name = unicode(uuid.uuid4()) | |
245 self._createNode(None, _node_name, client.jid, [group], pubsub_ent, message, profile) | |
246 else: | |
247 self._publishMblog(_node, message, pubsub_ent, profile) | |
248 | |
249 client = self.host.getClient(profile) | |
250 if not client: | |
251 error(_('No client for this profile key: %s') % profile_key) | |
252 return | |
253 client.client_initialized.addCallback(after_init) | |
254 | |
308
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
255 def _doCleaning(self, result, pubsub_ent, profile): |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
256 """Compare the node in config node, and the existing nodes, and delete unknown ones""" |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
257 #TODO: manage groups which don't exist anymore |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
258 assert(len(result)==2) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
259 assert(result[0][0]==True and result[1][0]==True) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
260 config_nodes = [item.firstChildElement()["node"] for item in result[0][1]] |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
261 existing_nodes = [item.nodeIdentifier for item in result[1][1]._items] |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
262 to_delete = set(config_nodes).symmetric_difference(existing_nodes) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
263 def check_deletion(result): |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
264 for (success, value) in result: |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
265 if not success: |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
266 msg = _("Can't delete node") |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
267 error(msg) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
268 raise NodeDeletionError(msg) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
269 #TODO: log node which was not deleted |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
270 |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
271 |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
272 d = defer.DeferredList([self.host.plugins["XEP-0060"].deleteNode(pubsub_ent, node, profile) for node in to_delete]) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
273 d.addCallback(check_deletion) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
274 |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
275 def cleanBlogCollection(self, profile_key='@DEFAULT@'): |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
276 """Remove blog nodes not referenced in config node""" |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
277 debug(_('Getting mblog nodes')) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
278 profile = self.host.memory.getProfileName(profile_key) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
279 if not profile: |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
280 error(_("Unknown profile")) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
281 return {} |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
282 |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
283 def after_init(ignore): |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
284 pubsub_ent = self.host.memory.getServerServiceEntity("pubsub", "service", profile) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
285 _jid, xmlstream = self.host.getJidNStream(profile_key) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
286 d_config = self.host.plugins["XEP-0060"].getItems(pubsub_ent, self._getConfigNode(_jid), profile_key=profile_key) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
287 d_root = client.disco.requestItems(pubsub_ent, self._getRootNode(client.jid)) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
288 defer.DeferredList([d_config, d_root]).addCallback(self._doCleaning, pubsub_ent, profile) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
289 |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
290 client = self.host.getClient(profile) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
291 if not client: |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
292 error(_('No client for this profile key: %s') % profile_key) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
293 return |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
294 client.client_initialized.addCallback(after_init) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
295 |