annotate src/plugins/plugin_misc_groupblog.py @ 324:b069055320b1

core: fixed bad profile check in connect method
author Goffi <goffi@goffi.org>
date Tue, 10 May 2011 15:46:20 +0200
parents f56108eb2880
children 0f9925193586
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 SAT plugin for microbloging with roster access
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
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
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.protocols.jabber import jid
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import error as jab_error
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import twisted.internet.error
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.words.xish import domish
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat.tools.xml_tools import ElementParser
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from wokkel import disco,pubsub
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from feed.atom import Entry, Author
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 import uuid
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from time import time
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
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
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
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
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 TYPE_COLLECTION = 'collection'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 PLUGIN_INFO = {
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 "name": "Group blogging throught collections",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 "import_name": "groupblog",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 "type": "MISC",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 "protocols": [],
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 "dependencies": ["XEP-0277"],
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 "main": "GroupBlog",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 "handler": "no",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 "description": _("""Implementation of microblogging with roster access""")
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 }
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 class NodeCreationError(Exception):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 pass
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
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
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 class GroupBlog():
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 """This class use a PubSub Collection to manage roster access on microblog"""
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 def __init__(self, host):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 info(_("Group blog plugin initialization"))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.host = host
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
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):
312
f56108eb2880 plugin group blog: a custom cb is now used to specitfy node and groups targetted by the blog
Goffi <goffi@goffi.org>
parents: 311
diff changeset
70 self.host.plugins["XEP-0163"].addPEPEvent("MICROBLOG_%02d" % i, NS_MICROBLOG % i, self.groupblogCB, None)
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
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
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74 doc = {
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 })
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 host.bridge.addMethod("getMblogNodes", ".communication", in_sign='s', out_sign='a{sas}',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 method=self.getMblogNodes,
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 async = True,
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80 doc = { 'summary':"retrieve mblog node, and their association with roster's groups",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81 'param_0':'%(doc_profile)s',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 'return':'list of microblog data (dict)'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 })
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 host.bridge.addMethod("sendGroupBlog", ".communication", in_sign='asss', out_sign='',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 method=self.sendGroupBlog,
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 doc = { 'summary':"Send a microblog to a list of groups",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 'param_0':'list of groups which can read the microblog',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89 'param_1':'text to send',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 'param_2':'%(doc_profile)s'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
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 })
312
f56108eb2880 plugin group blog: a custom cb is now used to specitfy node and groups targetted by the blog
Goffi <goffi@goffi.org>
parents: 311
diff changeset
99
f56108eb2880 plugin group blog: a custom cb is now used to specitfy node and groups targetted by the blog
Goffi <goffi@goffi.org>
parents: 311
diff changeset
100 def groupblogCB(self, itemsEvent, profile):
f56108eb2880 plugin group blog: a custom cb is now used to specitfy node and groups targetted by the blog
Goffi <goffi@goffi.org>
parents: 311
diff changeset
101 for item in itemsEvent.items:
f56108eb2880 plugin group blog: a custom cb is now used to specitfy node and groups targetted by the blog
Goffi <goffi@goffi.org>
parents: 311
diff changeset
102 microblog_data = self.host.plugins["XEP-0277"]._item2mbdata(item)
f56108eb2880 plugin group blog: a custom cb is now used to specitfy node and groups targetted by the blog
Goffi <goffi@goffi.org>
parents: 311
diff changeset
103 microblog_data["node"] = itemsEvent.nodeIdentifier
f56108eb2880 plugin group blog: a custom cb is now used to specitfy node and groups targetted by the blog
Goffi <goffi@goffi.org>
parents: 311
diff changeset
104 microblog_data["groups"] = "\n".join(self._blog_nodes[profile].get(itemsEvent.nodeIdentifier, []))
f56108eb2880 plugin group blog: a custom cb is now used to specitfy node and groups targetted by the blog
Goffi <goffi@goffi.org>
parents: 311
diff changeset
105 self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile)
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
106
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
107 def _getRootNode(self, entity):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
108 return "%(entity)s_%(root_suff)s" % {'entity':entity.userhost(), 'root_suff':MBLOG_COLLECTION}
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109
311
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
110 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
111 """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
112 @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
113 @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
114 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
115
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
116 def _getConfigNode(self, entity):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
117 return "%(entity)s_%(root_suff)s" % {'entity':entity.userhost(), 'root_suff':CONFIG_NODE}
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
118
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
119 def _configNodeCb(self, result, callback, profile):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
120 self._blog_nodes[profile] = {}
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
121 for item in result:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
122 node_ass = item.firstChildElement()
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
123 assert(node_ass.name == "node_association")
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
124 node = node_ass['node']
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
125 groups = [unicode(group) for group in node_ass.children]
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
126 self._blog_nodes[profile][node] = groups
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
127 callback(self._blog_nodes[profile])
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
129 def _configNodeFail(self, failure, errback):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
130 import pdb
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
131 pdb.set_trace()
310
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
132 errback("") #FIXME
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
133
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
134 def _configNodeErr(self, failure, user_jid, pubsub_ent, callback, errback, profile):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
135 if failure.value.condition == 'item-not-found':
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
136 debug(_('Multiblog config node not found, creating it'))
310
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
137 _options = {OPT_ACCESS_MODEL:"whitelist", OPT_PERSIST_ITEMS:1, OPT_MAX_ITEMS:-1}
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
138 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
139 d.addCallback(lambda result: self._configNodeCb([] , callback, profile))
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
140 d.addErrback(self._configNodeFail, errback)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
141 else:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
142 self._configNodeFail(failure, errback)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
143
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
144 def getMblogNodes(self, profile_key='@DEFAULT@', callback=None, errback=None):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
145 debug(_('Getting mblog nodes'))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
146 profile = self.host.memory.getProfileName(profile_key)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
147 if not profile:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
148 error(_("Unknown profile"))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
149 return {}
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
150
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
151 def after_init(ignore):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
152 pubsub_ent = self.host.memory.getServerServiceEntity("pubsub", "service", profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
153 _jid, xmlstream = self.host.getJidNStream(profile_key)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
154 d = self.host.plugins["XEP-0060"].getItems(pubsub_ent, self._getConfigNode(_jid), profile_key=profile_key)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
155 d.addCallbacks(self._configNodeCb, self._configNodeErr, callbackArgs=(callback, profile), errbackArgs=(_jid, pubsub_ent, callback, errback, profile))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
156
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
157 client = self.host.getClient(profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
158 if not client:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
159 error(_('No client for this profile key: %s') % profile_key)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
160 return
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
161 client.client_initialized.addCallback(after_init)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
162
311
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
163 def _publishMblog(self, name, message, client):
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
164 """Actually publish the message on the group blog
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
165 @param name: name of the node where we publish
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
166 @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
167 @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
168 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
169 defer_blog = self.host.plugins["XEP-0060"].publish(client.jid.userhostJID(), name, items=[mblog_item], profile_key=client.profile)
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
170 defer_blog.addErrback(self._mblogPublicationFailed)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
171
311
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
172 def _groupNodeCreated(self, ignore, groups, name, message, client):
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
173 """A group node as been created, we need to add it to the configure node, and send the message to it
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
174 @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
175 @param name: unique name of the node
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
176 @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
177 @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
178 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
179 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
180 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
181
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
182 config_node = self._getConfigNode(client.jid)
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
183 _payload = domish.Element(('','node_association'))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
184 _payload['node'] = name
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
185 for group in groups:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
186 _payload.addElement('group',content=group)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
187 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
188 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
189 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
190 defer_config.addCallback(configNodeUpdated)
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
191 defer_config.addErrback(self._configUpdateFailed)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
192
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
193 #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
194 self._publishMblog(name, message, client)
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
195
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
196
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
197 def _mblogPublicationFailed(self, failure):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
198 #TODO
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
199 import pdb
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
200 pdb.set_trace()
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
201
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
202 def _configUpdateFailed(self, failure):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
203 #TODO
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
204 import pdb
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
205 pdb.set_trace()
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
206
311
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
207 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
208 #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
209 #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
210 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
211 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
212 error(msg)
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
213 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
214
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
215 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
216 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
217 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
218 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
219 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
220 _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
221 _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
222 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
223 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
224 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
225 else:
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
226 unmanagedError(None)
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
227 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
228 error(msg)
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
229 raise NodeCreationError(msg)
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
230
311
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
231 def _createNode(self, ignore, name, groups, message, client):
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
232 """create a group microblog node
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
233 @param ignore: ignored param, necessary to be added as a deferred callback
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
234 @param name: name of the node
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
235 @param groups: list of group than can subscribe to the node
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
236 @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
237 @param client: SatXMPPClient"""
310
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
238 _options = {OPT_ACCESS_MODEL:"roster", OPT_PERSIST_ITEMS:1, OPT_MAX_ITEMS:-1,
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
239 '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
240 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
241 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
242 d.addErrback(self._nodeCreationFailed, name, groups, message, client)
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
243
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
244 def _getNodeForGroups(self, groups, profile):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
245 """Return node associated with the given list of groups
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
246 @param groups: list of groups
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
247 @param profile: profile of publisher"""
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
248 for node in self._blog_nodes[profile]:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
249 node_groups = self._blog_nodes[profile][node]
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
250 if set(node_groups) == set(groups):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
251 return node
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
252 return None
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
253
311
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
254 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
255 """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
256 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
257 _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
258 _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
259 _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
260 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
261 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
262 warning(msg)
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
263 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
264 else:
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
265 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
266
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
267 def sendGroupBlog(self, groups, message, profile_key='@DEFAULT@'):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
268 """Publish a microblog to the node associated to the groups
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
269 If the node doesn't exist, it is created, then the message is posted
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
270 @param groups: list of groups allowed to retrieve the microblog
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
271 @param message: microblog
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
272 @profile_key: %(doc_profile)s
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
273 """
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
274 profile = self.host.memory.getProfileName(profile_key)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
275 if not profile:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
276 error(_("Unknown profile"))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
277 return
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
278
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
279 def after_init(ignore):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
280 _groups = list(set(groups).intersection(client.roster.getGroups())) #We only keep group which actually exist
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
281 #TODO: send an error signal if user want to post to non existant groups
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
282 _groups.sort()
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
283 for group in _groups:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
284 _node = self._getNodeForGroups([group], profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
285 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
286 _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
287 self._createNode(None, _node_name, [group], message, client)
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
288 else:
311
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
289 self._publishMblog(_node, message, client)
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
290
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
291 client = self.host.getClient(profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
292 if not client:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
293 error(_('No client for this profile key: %s') % profile_key)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
294 return
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
295 client.client_initialized.addCallback(after_init)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
296
308
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
297 def _doCleaning(self, result, pubsub_ent, profile):
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
298 """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
299 #TODO: manage groups which don't exist anymore
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
300 assert(len(result)==2)
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
301 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
302 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
303 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
304 to_delete = set(config_nodes).symmetric_difference(existing_nodes)
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
305 def check_deletion(result):
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
306 for (success, value) in result:
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
307 if not success:
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
308 msg = _("Can't delete node")
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
309 error(msg)
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
310 raise NodeDeletionError(msg)
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
311 #TODO: log node which was not deleted
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
312
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
313
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
314 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
315 d.addCallback(check_deletion)
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
316
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
317 def cleanBlogCollection(self, profile_key='@DEFAULT@'):
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
318 """Remove blog nodes not referenced in config node"""
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
319 debug(_('Getting mblog nodes'))
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
320 profile = self.host.memory.getProfileName(profile_key)
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
321 if not profile:
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
322 error(_("Unknown profile"))
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
323 return {}
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 def after_init(ignore):
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
326 pubsub_ent = self.host.memory.getServerServiceEntity("pubsub", "service", profile)
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
327 _jid, xmlstream = self.host.getJidNStream(profile_key)
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
328 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
329 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
330 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
331
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
332 client = self.host.getClient(profile)
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
333 if not client:
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
334 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
335 return
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
336 client.client_initialized.addCallback(after_init)
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
337
310
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
338 def subscribeGroupBlog(self, pub_jid, profile_key='@DEFAULT@'):
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
339 debug(_('subscribing mblog nodes'))
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
340 _pub_jid = jid.JID(pub_jid)
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
341 profile = self.host.memory.getProfileName(profile_key)
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
342 if not profile:
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
343 error(_("Unknown profile"))
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
344 return
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 def after_init(ignore):
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
347 pubsub_ent = self.host.memory.getServerServiceEntity("pubsub", "service", profile)
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
348 _options = {OPT_SUBSCRIPTION_TYPE:'items', OPT_SUBSCRIPTION_DEPTH:'1'}
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
349 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
350 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
351 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
352
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
353 client = self.host.getClient(profile)
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
354 if not client:
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
355 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
356 return
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
357 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
358