annotate src/plugins/plugin_misc_groupblog.py @ 307:1e4575e12581

Group blog first draft - blog can now be sent, node are automatically created
author Goffi <goffi@goffi.org>
date Thu, 07 Apr 2011 22:23:48 +0200
parents
children ce3607b7198d
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
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.internet import protocol
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
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 NS_BLOG_COLLECTION = 'urn:xmpp:blogcollection:0'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 MBLOG_COLLECTION = 'MBLOGCOLLECTION'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 CONFIG_NODE = 'CONFIG'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 NS_ACCESS_MODEL = 'pubsub#access_model'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 NS_PERSIST_ITEMS = 'pubsub#persist_items'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 NS_MAX_ITEMS = 'pubsub#max_items'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 NS_NODE_TYPE = 'pubsub#node_type'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 TYPE_COLLECTION = 'collection'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 PLUGIN_INFO = {
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 "name": "Group blogging throught collections",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 "import_name": "groupblog",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 "type": "MISC",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 "protocols": [],
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 "dependencies": ["XEP-0277"],
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 "main": "GroupBlog",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 "handler": "no",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 "description": _("""Implementation of microblogging with roster access""")
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 }
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 class NodeCreationError(Exception):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 pass
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 class GroupBlog():
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 """This class use a PubSub Collection to manage roster access on microblog"""
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 def __init__(self, host):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 info(_("Group blog plugin initialization"))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 self.host = host
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self._blog_nodes={}
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 """host.bridge.addMethod("getLastMicroblogs", ".communication",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 in_sign='sis', out_sign='aa{ss}',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 method=self.getLastMicroblogs,
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68 async = True,
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 doc = { 'summary':'retrieve items',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 'param_0':'jid: publisher of wanted microblog',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 'param_1':'max_items: see XEP-0060 #6.5.7',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 'param_2':'%(doc_profile)s',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 'return':'list of microblog data (dict)'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74 })
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 host.bridge.addMethod("setMicroblogAccess", ".communication", in_sign='ss', out_sign='',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76 method=self.setMicroblogAccess,
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 doc = {
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 })"""
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80 host.bridge.addMethod("initBlogCollection", ".communication", in_sign='s', out_sign='',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81 method=self.initBlogCollection,
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 doc = {
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("getMblogNodes", ".communication", in_sign='s', out_sign='a{sas}',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 method=self.getMblogNodes,
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 async = True,
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 doc = { 'summary':"retrieve mblog node, and their association with roster's groups",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89 'param_0':'%(doc_profile)s',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 'return':'list of microblog data (dict)'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91 })
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93 host.bridge.addMethod("sendGroupBlog", ".communication", in_sign='asss', out_sign='',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94 method=self.sendGroupBlog,
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95 doc = { 'summary':"Send a microblog to a list of groups",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 'param_0':'list of groups which can read the microblog',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97 'param_1':'text to send',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98 'param_2':'%(doc_profile)s'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
99 })
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101 def _getRootNode(self, entity):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102 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
103
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104 def _getConfigNode(self, entity):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
105 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
106
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
107 def _configNodeCb(self, result, callback, profile):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
108 self._blog_nodes[profile] = {}
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109 for item in result:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110 node_ass = item.firstChildElement()
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
111 assert(node_ass.name == "node_association")
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
112 node = node_ass['node']
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
113 groups = [unicode(group) for group in node_ass.children]
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
114 self._blog_nodes[profile][node] = groups
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
115 callback(self._blog_nodes[profile])
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
116
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
117 def _configNodeFail(self, failure, errback):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
118 import pdb
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
119 pdb.set_trace()
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
120 errback() #FIXME
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
121
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
122 def _configNodeErr(self, failure, user_jid, pubsub_ent, callback, errback, profile):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
123 if failure.value.condition == 'item-not-found':
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
124 debug(_('Multiblog config node not found, creating it'))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
125 _options = {NS_ACCESS_MODEL:"whitelist", NS_PERSIST_ITEMS:1, NS_MAX_ITEMS:-1}
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
126 d = self.host.plugins["XEP-0060"].createNode(pubsub_ent, self._getConfigNode(user_jid), _options, profile_key=profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
127 d.addCallback(self._configNodeCb, callback, profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128 d.addErrback(self._configNodeFail, errback)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
129 else:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
130 self._configNodeFail(failure, errback)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
131
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
132 def getMblogNodes(self, profile_key='@DEFAULT@', callback=None, errback=None):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
133 debug(_('Getting mblog nodes'))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
134 profile = self.host.memory.getProfileName(profile_key)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
135 if not profile:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
136 error(_("Unknown profile"))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
137 return {}
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
138
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
139 def after_init(ignore):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
140 pubsub_ent = self.host.memory.getServerServiceEntity("pubsub", "service", profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
141 _jid, xmlstream = self.host.getJidNStream(profile_key)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
142 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
143 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
144
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
145 client = self.host.getClient(profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
146 if not client:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
147 error(_('No client for this profile key: %s') % profile_key)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
148 return
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
149 client.client_initialized.addCallback(after_init)
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
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
152 def initBlogCollection(self, profile_key="@DEFAULT@"):
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 _options = {NS_NODE_TYPE:TYPE_COLLECTION}
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
155 def cb(result):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
156 #Node is created with right permission
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
157 debug(_("Microblog node collection created"))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
158
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
159 def fatal_err(s_error):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
160 #Something went wrong
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
161 error(_("Can't create node collection"))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
162
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
163 def err_cb(s_error):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
164 #If the node already exists, the condition is "conflict",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
165 #else we have an unmanaged error
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
166 if s_error.value.condition=='conflict':
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
167 fatal_err(s_error)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
168 else:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
169 fatal_err(s_error)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
170
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
171 def create_node():
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
172 #return self.host.plugins["XEP-0060"].createNode(_jid.userhostJID(), NS_BLOG_COLLECTION, _options, profile_key=profile_key)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
173 return self.host.plugins["XEP-0060"].createNode(jid.JID("pubsub.tazar.int"), self._getRootNode(_jid), _options, profile_key=profile_key)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
174
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
175 create_node().addCallback(cb).addErrback(err_cb)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
176
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
177 def _publishMblog(self, name, message, pubsub_ent, profile):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
178 """Actually publish the message on the group blog
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
179 @param name: name of the node where we publish
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
180 @param message: message to publish
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
181 @param pubsub_ent: entity of the publish-subscribe service
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
182 @param profile: profile of the owner of the group"""
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
183 mblog_item = self.host.plugins["XEP-0277"].data2entry({'content':message}, profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
184 defer_blog = self.host.plugins["XEP-0060"].publish(pubsub_ent, name, items=[mblog_item], profile_key=profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
185 defer_blog.addErrback(self._mblogPublicationFailed)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
186
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
187 def _groupNodeCreated(self, ignore, groups, name, message, user_jid, pubsub_ent, profile):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
188 """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
189 @param groups: list of groups authorized to subscribe to the node
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
190 @param name: unique name of the group
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
191 @param message: message to publish to the group
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
192 @param user_jid: jid of the owner of the node
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
193 @param pubsub_ent: entity of the publish-subscribe service
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
194 @param profile: profile of the owner of the group"""
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
195 config_node = self._getConfigNode(user_jid)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
196 _payload = domish.Element(('','node_association'))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
197 _payload['node'] = name
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
198 for group in groups:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
199 _payload.addElement('group',content=group)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
200 config_item = pubsub.Item(payload=_payload)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
201 defer_config = self.host.plugins["XEP-0060"].publish(pubsub_ent, config_node, items=[config_item], profile_key=profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
202 defer_config.addCallback(lambda x: debug(_("Configuration node updated")))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
203 defer_config.addErrback(self._configUpdateFailed)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
204
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
205 #Finally, we publish the message
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
206 self._publishMblog(name, message, pubsub_ent, profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
207
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
208
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
209 def _mblogPublicationFailed(self, failure):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
210 #TODO
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
211 import pdb
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
212 pdb.set_trace()
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
213
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
214 def _configUpdateFailed(self, failure):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
215 #TODO
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
216 import pdb
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
217 pdb.set_trace()
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
218
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
219 def _nodeCreationFailed(self, failure, name, user_jid, groups, pubsub_ent, message, profile):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
220 #TODO
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
221 if failure.value.condition == "item-not-found":
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
222 #The root node doesn't exists
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
223 def err_creating_root_node(failure):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
224 msg = _("Can't create Root node")
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
225 error(msg)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
226 raise NodeCreationError(msg)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
227
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
228 _options = {NS_NODE_TYPE:TYPE_COLLECTION}
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
229 d = self.host.plugins["XEP-0060"].createNode(pubsub_ent, self._getRootNode(user_jid), _options, profile_key=profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
230 d.addCallback(self._createNode, name, user_jid, groups, pubsub_ent, message, profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
231 d.addErrback(err_creating_root_node)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
232 else:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
233 import pdb
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
234 pdb.set_trace()
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
235
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
236 def _createNode(self, ignore, name, user_jid, groups, pubsub_ent, message, profile):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
237 """create a group microblog node
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
238 @param ignore: ignored param, necessary to be added as a deferred callback
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
239 @param name: name of the node
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
240 @param user_jid: jid of the user creating the node
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
241 @param groups: list of group than can subscribe to the node
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
242 @param pubsub_ent: publish/subscribe service's entity
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
243 @param message: message to publish
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
244 @param profile: profile of the user creating the node"""
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
245 _options = {NS_ACCESS_MODEL:"roster", NS_PERSIST_ITEMS:1, NS_MAX_ITEMS:-1,
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
246 'pubsub#node_type':'leaf', 'pubsub#collection':self._getRootNode(user_jid),
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
247 'pubsub#roster_groups_allowed':groups}
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
248 d = self.host.plugins["XEP-0060"].createNode(pubsub_ent, name, _options, profile_key=profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
249 d.addCallback(self._groupNodeCreated, groups, name, message, user_jid, pubsub_ent, profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
250 d.addErrback(self._nodeCreationFailed, name, user_jid, groups, pubsub_ent, message, profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
251
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
252 def _getNodeForGroups(self, groups, profile):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
253 """Return node associated with the given list of groups
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
254 @param groups: list of groups
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
255 @param profile: profile of publisher"""
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
256 for node in self._blog_nodes[profile]:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
257 node_groups = self._blog_nodes[profile][node]
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
258 if set(node_groups) == set(groups):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
259 return node
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
260 return None
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
261
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
262 def sendGroupBlog(self, groups, message, profile_key='@DEFAULT@'):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
263 """Publish a microblog to the node associated to the groups
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
264 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
265 @param groups: list of groups allowed to retrieve the microblog
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
266 @param message: microblog
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
267 @profile_key: %(doc_profile)s
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
268 """
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
269 profile = self.host.memory.getProfileName(profile_key)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
270 if not profile:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
271 error(_("Unknown profile"))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
272 return
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 def after_init(ignore):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
275 _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
276 #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
277 _groups.sort()
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
278 pubsub_ent = self.host.memory.getServerServiceEntity("pubsub", "service", profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
279 for group in _groups:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
280 _node = self._getNodeForGroups([group], profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
281 if not _node:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
282 _node_name = unicode(uuid.uuid4())
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
283 self._createNode(None, _node_name, client.jid, [group], pubsub_ent, message, profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
284 else:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
285 self._publishMblog(_node, message, pubsub_ent, profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
286
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
287 client = self.host.getClient(profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
288 if not client:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
289 error(_('No client for this profile key: %s') % profile_key)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
290 return
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
291 client.client_initialized.addCallback(after_init)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
292