Mercurial > libervia-backend
annotate src/plugins/plugin_misc_groupblog.py @ 311:0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 12 Apr 2011 20:40:47 +0200 |
parents | 53adec87d1d7 |
children | f56108eb2880 |
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 | |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
35 NS_MICROBLOG = 'urn:xmpp:microblog:%02d' |
307 | 36 CONFIG_NODE = 'CONFIG' |
310
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
37 OPT_ACCESS_MODEL = 'pubsub#access_model' |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
38 OPT_PERSIST_ITEMS = 'pubsub#persist_items' |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
39 OPT_MAX_ITEMS = 'pubsub#max_items' |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
40 OPT_NODE_TYPE = 'pubsub#node_type' |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
41 OPT_SUBSCRIPTION_TYPE = 'pubsub#subscription_type' |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
42 OPT_SUBSCRIPTION_DEPTH = 'pubsub#subscription_depth' |
307 | 43 TYPE_COLLECTION = 'collection' |
44 | |
45 PLUGIN_INFO = { | |
46 "name": "Group blogging throught collections", | |
47 "import_name": "groupblog", | |
48 "type": "MISC", | |
49 "protocols": [], | |
50 "dependencies": ["XEP-0277"], | |
51 "main": "GroupBlog", | |
52 "handler": "no", | |
53 "description": _("""Implementation of microblogging with roster access""") | |
54 } | |
55 | |
56 class NodeCreationError(Exception): | |
57 pass | |
58 | |
308
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
59 class NodeDeletionError(Exception): |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
60 pass |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
61 |
307 | 62 class GroupBlog(): |
63 """This class use a PubSub Collection to manage roster access on microblog""" | |
64 | |
65 def __init__(self, host): | |
66 info(_("Group blog plugin initialization")) | |
67 self.host = host | |
68 self._blog_nodes={} | |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
69 for i in range(1,21): |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
70 self.host.plugins["XEP-0163"].addPEPEvent("MICROBLOG_%02d" % i, NS_MICROBLOG % i, self.host.plugins["XEP-0277"].microblogCB, None) |
307 | 71 |
308
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
72 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
|
73 method=self.cleanBlogCollection, |
307 | 74 doc = { |
75 }) | |
76 | |
77 host.bridge.addMethod("getMblogNodes", ".communication", in_sign='s', out_sign='a{sas}', | |
78 method=self.getMblogNodes, | |
79 async = True, | |
80 doc = { 'summary':"retrieve mblog node, and their association with roster's groups", | |
81 'param_0':'%(doc_profile)s', | |
82 'return':'list of microblog data (dict)' | |
83 }) | |
84 | |
85 host.bridge.addMethod("sendGroupBlog", ".communication", in_sign='asss', out_sign='', | |
86 method=self.sendGroupBlog, | |
87 doc = { 'summary':"Send a microblog to a list of groups", | |
88 'param_0':'list of groups which can read the microblog', | |
89 'param_1':'text to send', | |
90 'param_2':'%(doc_profile)s' | |
91 }) | |
310
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
92 |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
93 host.bridge.addMethod("subscribeGroupBlog", ".communication", in_sign='ss', out_sign='', |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
94 method=self.subscribeGroupBlog, |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
95 doc = { 'summary':"Subscribe to the group blog of somebody", |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
96 'param_0':'jid of the group node published', |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
97 'param_1':'%(doc_profile)s' |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
98 }) |
307 | 99 |
100 def _getRootNode(self, entity): | |
101 return "%(entity)s_%(root_suff)s" % {'entity':entity.userhost(), 'root_suff':MBLOG_COLLECTION} | |
102 | |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
103 def _getNodeName(self, number): |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
104 """Return the node name |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
105 @param number: int number of the node |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
106 @param entity: jid of the owner""" |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
107 return NS_MICROBLOG % number |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
108 |
307 | 109 def _getConfigNode(self, entity): |
110 return "%(entity)s_%(root_suff)s" % {'entity':entity.userhost(), 'root_suff':CONFIG_NODE} | |
111 | |
112 def _configNodeCb(self, result, callback, profile): | |
113 self._blog_nodes[profile] = {} | |
114 for item in result: | |
115 node_ass = item.firstChildElement() | |
116 assert(node_ass.name == "node_association") | |
117 node = node_ass['node'] | |
118 groups = [unicode(group) for group in node_ass.children] | |
119 self._blog_nodes[profile][node] = groups | |
120 callback(self._blog_nodes[profile]) | |
121 | |
122 def _configNodeFail(self, failure, errback): | |
123 import pdb | |
124 pdb.set_trace() | |
310
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
125 errback("") #FIXME |
307 | 126 |
127 def _configNodeErr(self, failure, user_jid, pubsub_ent, callback, errback, profile): | |
128 if failure.value.condition == 'item-not-found': | |
129 debug(_('Multiblog config node not found, creating it')) | |
310
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
130 _options = {OPT_ACCESS_MODEL:"whitelist", OPT_PERSIST_ITEMS:1, OPT_MAX_ITEMS:-1} |
307 | 131 d = self.host.plugins["XEP-0060"].createNode(pubsub_ent, self._getConfigNode(user_jid), _options, profile_key=profile) |
310
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
132 d.addCallback(lambda result: self._configNodeCb([] , callback, profile)) |
307 | 133 d.addErrback(self._configNodeFail, errback) |
134 else: | |
135 self._configNodeFail(failure, errback) | |
136 | |
137 def getMblogNodes(self, profile_key='@DEFAULT@', callback=None, errback=None): | |
138 debug(_('Getting mblog nodes')) | |
139 profile = self.host.memory.getProfileName(profile_key) | |
140 if not profile: | |
141 error(_("Unknown profile")) | |
142 return {} | |
143 | |
144 def after_init(ignore): | |
145 pubsub_ent = self.host.memory.getServerServiceEntity("pubsub", "service", profile) | |
146 _jid, xmlstream = self.host.getJidNStream(profile_key) | |
147 d = self.host.plugins["XEP-0060"].getItems(pubsub_ent, self._getConfigNode(_jid), profile_key=profile_key) | |
148 d.addCallbacks(self._configNodeCb, self._configNodeErr, callbackArgs=(callback, profile), errbackArgs=(_jid, pubsub_ent, callback, errback, profile)) | |
149 | |
150 client = self.host.getClient(profile) | |
151 if not client: | |
152 error(_('No client for this profile key: %s') % profile_key) | |
153 return | |
154 client.client_initialized.addCallback(after_init) | |
155 | |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
156 def _publishMblog(self, name, message, client): |
307 | 157 """Actually publish the message on the group blog |
158 @param name: name of the node where we publish | |
159 @param message: message to publish | |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
160 @param client: SatXMPPClient of the published""" |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
161 mblog_item = self.host.plugins["XEP-0277"].data2entry({'content':message}, client.profile) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
162 defer_blog = self.host.plugins["XEP-0060"].publish(client.jid.userhostJID(), name, items=[mblog_item], profile_key=client.profile) |
307 | 163 defer_blog.addErrback(self._mblogPublicationFailed) |
164 | |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
165 def _groupNodeCreated(self, ignore, groups, name, message, client): |
307 | 166 """A group node as been created, we need to add it to the configure node, and send the message to it |
167 @param groups: list of groups authorized to subscribe to the node | |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
168 @param name: unique name of the node |
307 | 169 @param message: message to publish to the group |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
170 @param client: SatXMPPClient""" |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
171 def configNodeUpdated(result): |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
172 self._blog_nodes[client.profile][name] = groups |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
173 debug(_("Configuration node updated")) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
174 |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
175 config_node = self._getConfigNode(client.jid) |
307 | 176 _payload = domish.Element(('','node_association')) |
177 _payload['node'] = name | |
178 for group in groups: | |
179 _payload.addElement('group',content=group) | |
180 config_item = pubsub.Item(payload=_payload) | |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
181 pubsub_ent = self.host.memory.getServerServiceEntity("pubsub", "service", client.profile) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
182 defer_config = self.host.plugins["XEP-0060"].publish(pubsub_ent, config_node, items=[config_item], profile_key=client.profile) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
183 defer_config.addCallback(configNodeUpdated) |
307 | 184 defer_config.addErrback(self._configUpdateFailed) |
185 | |
186 #Finally, we publish the message | |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
187 self._publishMblog(name, message, client) |
307 | 188 |
189 | |
190 def _mblogPublicationFailed(self, failure): | |
191 #TODO | |
192 import pdb | |
193 pdb.set_trace() | |
194 | |
195 def _configUpdateFailed(self, failure): | |
196 #TODO | |
197 import pdb | |
198 pdb.set_trace() | |
199 | |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
200 def _nodeCreationFailed(self, failure, name, groups, message, client): |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
201 #FIXME: temporary behaviour is to delete the node, |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
202 #user input should be required in the future |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
203 def unmanagedError(failure): |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
204 msg = _("Can't create node") |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
205 error(msg) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
206 raise NodeCreationError(msg) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
207 |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
208 if failure.value.condition == 'conflict': |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
209 pubsub_elts = filter(lambda elt: elt.name == 'pubsub', failure.value.children) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
210 if pubsub_elts: |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
211 create_elts = filter(lambda elt: elt.name == 'create', pubsub_elts[0].children) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
212 if create_elts: |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
213 _from = jid.JID(failure.value.stanza['from']) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
214 _node = create_elts[0]['node'] |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
215 d = self.host.plugins["XEP-0060"].deleteNode(_from, _node, client.profile) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
216 d.addCallback(self._createNode, name, groups, message, client) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
217 d.addErrback(unmanagedError) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
218 else: |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
219 unmanagedError(None) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
220 msg = _("Can't create node") |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
221 error(msg) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
222 raise NodeCreationError(msg) |
307 | 223 |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
224 def _createNode(self, ignore, name, groups, message, client): |
307 | 225 """create a group microblog node |
226 @param ignore: ignored param, necessary to be added as a deferred callback | |
227 @param name: name of the node | |
228 @param groups: list of group than can subscribe to the node | |
229 @param message: message to publish | |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
230 @param client: SatXMPPClient""" |
310
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
231 _options = {OPT_ACCESS_MODEL:"roster", OPT_PERSIST_ITEMS:1, OPT_MAX_ITEMS:-1, |
307 | 232 'pubsub#roster_groups_allowed':groups} |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
233 d = self.host.plugins["XEP-0060"].createNode(client.jid.userhostJID(), name, _options, client.profile) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
234 d.addCallback(self._groupNodeCreated, groups, name, message, client) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
235 d.addErrback(self._nodeCreationFailed, name, groups, message, client) |
307 | 236 |
237 def _getNodeForGroups(self, groups, profile): | |
238 """Return node associated with the given list of groups | |
239 @param groups: list of groups | |
240 @param profile: profile of publisher""" | |
241 for node in self._blog_nodes[profile]: | |
242 node_groups = self._blog_nodes[profile][node] | |
243 if set(node_groups) == set(groups): | |
244 return node | |
245 return None | |
246 | |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
247 def _getFreeNode(self, entity, profile): |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
248 """Return a free group number, |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
249 raise an exception if we have reach limit""" |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
250 _all = set([self._getNodeName(idx) for idx in range(1,21)]) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
251 _used = set(self._blog_nodes[profile].keys()) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
252 _free = _all.difference(_used) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
253 if not _free: |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
254 msg = _("Can't create group node: no more free node available") |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
255 warning(msg) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
256 raise NodeCreationError(msg) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
257 else: |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
258 return _free.pop() |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
259 |
307 | 260 def sendGroupBlog(self, groups, message, profile_key='@DEFAULT@'): |
261 """Publish a microblog to the node associated to the groups | |
262 If the node doesn't exist, it is created, then the message is posted | |
263 @param groups: list of groups allowed to retrieve the microblog | |
264 @param message: microblog | |
265 @profile_key: %(doc_profile)s | |
266 """ | |
267 profile = self.host.memory.getProfileName(profile_key) | |
268 if not profile: | |
269 error(_("Unknown profile")) | |
270 return | |
271 | |
272 def after_init(ignore): | |
273 _groups = list(set(groups).intersection(client.roster.getGroups())) #We only keep group which actually exist | |
274 #TODO: send an error signal if user want to post to non existant groups | |
275 _groups.sort() | |
276 for group in _groups: | |
277 _node = self._getNodeForGroups([group], profile) | |
278 if not _node: | |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
279 _node_name = self._getFreeNode(client.jid, profile) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
280 self._createNode(None, _node_name, [group], message, client) |
307 | 281 else: |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
282 self._publishMblog(_node, message, client) |
307 | 283 |
284 client = self.host.getClient(profile) | |
285 if not client: | |
286 error(_('No client for this profile key: %s') % profile_key) | |
287 return | |
288 client.client_initialized.addCallback(after_init) | |
289 | |
308
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
290 def _doCleaning(self, result, pubsub_ent, profile): |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
291 """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
|
292 #TODO: manage groups which don't exist anymore |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
293 assert(len(result)==2) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
294 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
|
295 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
|
296 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
|
297 to_delete = set(config_nodes).symmetric_difference(existing_nodes) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
298 def check_deletion(result): |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
299 for (success, value) in result: |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
300 if not success: |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
301 msg = _("Can't delete node") |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
302 error(msg) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
303 raise NodeDeletionError(msg) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
304 #TODO: log node which was not deleted |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
305 |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
306 |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
307 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
|
308 d.addCallback(check_deletion) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
309 |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
310 def cleanBlogCollection(self, profile_key='@DEFAULT@'): |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
311 """Remove blog nodes not referenced in config node""" |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
312 debug(_('Getting mblog nodes')) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
313 profile = self.host.memory.getProfileName(profile_key) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
314 if not profile: |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
315 error(_("Unknown profile")) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
316 return {} |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
317 |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
318 def after_init(ignore): |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
319 pubsub_ent = self.host.memory.getServerServiceEntity("pubsub", "service", profile) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
320 _jid, xmlstream = self.host.getJidNStream(profile_key) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
321 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
|
322 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
|
323 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
|
324 |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
325 client = self.host.getClient(profile) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
326 if not client: |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
327 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
|
328 return |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
329 client.client_initialized.addCallback(after_init) |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
330 |
310
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
331 def subscribeGroupBlog(self, pub_jid, profile_key='@DEFAULT@'): |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
332 debug(_('subscribing mblog nodes')) |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
333 _pub_jid = jid.JID(pub_jid) |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
334 profile = self.host.memory.getProfileName(profile_key) |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
335 if not profile: |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
336 error(_("Unknown profile")) |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
337 return |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
338 |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
339 def after_init(ignore): |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
340 pubsub_ent = self.host.memory.getServerServiceEntity("pubsub", "service", profile) |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
341 _options = {OPT_SUBSCRIPTION_TYPE:'items', OPT_SUBSCRIPTION_DEPTH:'1'} |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
342 d = self.host.plugins["XEP-0060"].subscribe(pubsub_ent, self._getRootNode(_pub_jid), options = _options, profile_key=profile) |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
343 d.addCallback(lambda x: debug(_("%(publisher)s's group node subscribed [%(profile)s]") % {'publisher':_pub_jid.userhost(), 'profile': profile})) |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
344 d.addErrback(lambda x: error(_("Can't subscribe group node [%(profile)s]") % {'profile': profile})) |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
345 |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
346 client = self.host.getClient(profile) |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
347 if not client: |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
348 error(_('No client for this profile key: %s') % profile_key) |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
349 return |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
350 client.client_initialized.addCallback(after_init) |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
351 |