annotate src/plugins/plugin_misc_groupblog.py @ 474:e519f8f33cba

misc: Add the build directory to the ignore file.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 14 Apr 2012 13:55:11 +0200
parents 6cd04adddaea
children b9fd32b46306
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
459
cf005701624b copyleft date update
Goffi <goffi@goffi.org>
parents: 372
diff changeset
6 Copyright (C) 2009, 2010, 2011, 2012 Jérôme Poisson (goffi@goffi.org)
307
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
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
30 from wokkel import disco, pubsub, data_form
307
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
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
35 NS_PUBSUB = 'http://jabber.org/protocol/pubsub'
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
36 #NS_PUBSUB_EXP = 'http://goffi.org/protocol/pubsub' #for non official features
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
37 NS_PUBSUB_EXP = NS_PUBSUB #XXX: we can't use custom namespace as Wokkel's PubSubService use official NS
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
38 NS_PUBSUB_ITEM_ACCESS = NS_PUBSUB_EXP + "#item-access"
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
39 NS_PUBSUB_CREATOR_JID_CHECK = NS_PUBSUB_EXP + "#creator-jid-check"
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
40 NS_PUBSUB_ITEM_CONFIG = NS_PUBSUB_EXP + "#item-config"
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
41 NS_PUBSUB_AUTO_CREATE = NS_PUBSUB + "#auto-create"
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
42 OPT_ROSTER_GROUPS_ALLOWED = 'pubsub#roster_groups_allowed'
310
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
43 OPT_ACCESS_MODEL = 'pubsub#access_model'
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
44 OPT_PERSIST_ITEMS = 'pubsub#persist_items'
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
45 OPT_MAX_ITEMS = 'pubsub#max_items'
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
46 OPT_NODE_TYPE = 'pubsub#node_type'
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
47 OPT_SUBSCRIPTION_TYPE = 'pubsub#subscription_type'
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
48 OPT_SUBSCRIPTION_DEPTH = 'pubsub#subscription_depth'
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 TYPE_COLLECTION = 'collection'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 PLUGIN_INFO = {
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 "name": "Group blogging throught collections",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 "import_name": "groupblog",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 "type": "MISC",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 "protocols": [],
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 "dependencies": ["XEP-0277"],
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 "main": "GroupBlog",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 "handler": "no",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 "description": _("""Implementation of microblogging with roster access""")
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
465
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
62 class NoCompatiblePubSubServerFound(Exception):
308
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
63 pass
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
64
471
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
65 class BadAccessTypeError(Exception):
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
66 pass
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
67
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
68 class BadAccessListError(Exception):
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
69 pass
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
70
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 class GroupBlog():
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
72 """This class use a SàT PubSub Service to manage access on microblog"""
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74 def __init__(self, host):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 info(_("Group blog plugin initialization"))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76 self.host = host
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77
471
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
78 host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='sasss', out_sign='',
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
79 method=self.sendGroupBlog)
465
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
80
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
81 host.bridge.addMethod("getLastGroupBlogs", ".plugin",
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
82 in_sign='sis', out_sign='aa{ss}',
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
83 method=self.getLastGroupBlogs,
471
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
84 async = True)
467
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
85
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
86 host.bridge.addMethod("getMassiveLastGroupBlogs", ".plugin",
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
87 in_sign='sasis', out_sign='a{saa{ss}}',
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
88 method=self.getMassiveLastGroupBlogs,
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
89 async = True)
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
90
465
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
91
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
92 @defer.inlineCallbacks
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
93 def initialise(self, profile_key):
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
94 """Check that this data for this profile are initialised, and do it else
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
95 @param client: client of the profile
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
96 @profile_key: %(doc_profile)s"""
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
97 profile = self.host.memory.getProfileName(profile_key)
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
98 if not profile:
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
99 error(_("Unknown profile"))
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
100 raise Exception("Unknown profile")
310
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
101
465
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
102 client = self.host.getClient(profile)
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
103 if not client:
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
104 error(_('No client for this profile key: %s') % profile_key)
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
105 raise Exception("Unknown profile")
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
106 yield client.client_initialized #we want to be sure that the client is initialized
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
107
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
108 #we first check that we have a item-access pubsub server
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
109 if not hasattr(client,"item_access_pubsub"):
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
110 debug(_('Looking for item-access power pubsub server'))
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
111 #we don't have any pubsub server featuring item access yet
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
112 test = self.host.memory.getServerServiceEntities("pubsub", "service", profile)
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
113 client.item_access_pubsub = None
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
114 for entity in self.host.memory.getServerServiceEntities("pubsub", "service", profile):
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
115 disco = yield client.disco.requestInfo(entity)
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
116 if set([NS_PUBSUB_ITEM_ACCESS, NS_PUBSUB_AUTO_CREATE, NS_PUBSUB_CREATOR_JID_CHECK]).issubset(disco.features):
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
117 info(_("item-access powered pubsub service found: [%s]") % entity.full())
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
118 client.item_access_pubsub = entity
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
119
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
120 if not client.item_access_pubsub:
467
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
121 error(_("No item-access powered pubsub server found, can't use group blog"))
465
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
122 raise NoCompatiblePubSubServerFound
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
123
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
124 defer.returnValue((profile, client))
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
125
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
126
471
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
127 def _publishMblog(self, service, client, access_type, access_list, message):
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128 """Actually publish the message on the group blog
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
129 @param service: jid of the item-access pubsub service
471
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
130 @param client: SatXMPPClient of the published
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
131 @param access_type: one of "PUBLIC", "GROUP", "JID"
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
132 @param access_list: set of entities (empty list for all, groups or jids) allowed to see the item
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
133 @param message: message to publish
471
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
134 """
311
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
135 mblog_item = self.host.plugins["XEP-0277"].data2entry({'content':message}, client.profile)
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
136 form = data_form.Form('submit', formNamespace=NS_PUBSUB_ITEM_CONFIG)
471
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
137 if access_type == "PUBLIC":
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
138 if access_list:
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
139 raise BadAccessListError("access_list must be empty for PUBLIC access")
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
140 elif access_type == "GROUP":
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
141 field = data_form.Field('list-multi', OPT_ROSTER_GROUPS_ALLOWED, values=access_list)
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
142 form.addField(field)
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
143 mblog_item.addChild(form.toElement())
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
144 elif access_type == "JID":
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
145 raise NotImplementedError
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
146 else:
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
147 error(_("Unknown access_type"))
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
148 raise BadAccessTypeError
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
149 defer_blog = self.host.plugins["XEP-0060"].publish(service, client.jid.userhost(), items=[mblog_item], profile_key=client.profile)
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
150 defer_blog.addErrback(self._mblogPublicationFailed)
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 _mblogPublicationFailed(self, failure):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
153 #TODO
471
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
154 return failure
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
155
471
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
156 def sendGroupBlog(self, access_type, access_list, message, profile_key='@DEFAULT@'):
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
157 """Publish a microblog with given item access
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
158 @param access_type: one of "PUBLIC", "GROUP", "JID"
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
159 @param access_list: list of authorized entity (empty list for PUBLIC ACCESS,
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
160 list of groups or list of jids) for this item
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
161 @param message: microblog
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
162 @profile_key: %(doc_profile)s
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
163 """
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
164 print "sendGroupBlog"
465
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
165 def initialised(result):
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
166 profile, client = result
471
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
167 if access_type == "PUBLIC":
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
168 if access_list:
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
169 raise Exception("Publishers list must be empty when getting microblogs for all contacts")
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
170 self._publishMblog(client.item_access_pubsub, client, "PUBLIC", [], message)
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
171 elif access_type == "GROUP":
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
172 _groups = set(access_list).intersection(client.roster.getGroups()) #We only keep group which actually exist
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
173 if not _groups:
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
174 raise BadAccessListError("No valid group")
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
175 self._publishMblog(client.item_access_pubsub, client, "GROUP", _groups, message)
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
176 elif access_type == "JID":
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
177 raise NotImplementedError
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
178 else:
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
179 error(_("Unknown access type"))
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
180 raise BadAccessTypeError
465
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
181
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
182 self.initialise(profile_key).addCallback(initialised)
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
183
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
184
308
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
185
470
5c916b99d0f6 plugin groupblog, D-Bus frontend: added getLastGroupBlogs and getMassiveLastGroupBlogs
Goffi <goffi@goffi.org>
parents: 467
diff changeset
186 def getLastGroupBlogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@'):
465
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
187 """Get the last published microblogs
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
188 @param pub_jid: jid of the publisher
471
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
189 @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7)
465
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
190 @param profile_key: profile key
471
6cd04adddaea core: exceptions moved to core
Goffi <goffi@goffi.org>
parents: 470
diff changeset
191 @return: list of microblog data (dict)
465
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
192 """
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
193
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
194 def initialised(result):
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
195 profile, client = result
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
196 d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, jid.JID(pub_jid).userhost(),
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
197 max_items=max_items, profile_key=profile_key)
470
5c916b99d0f6 plugin groupblog, D-Bus frontend: added getLastGroupBlogs and getMassiveLastGroupBlogs
Goffi <goffi@goffi.org>
parents: 467
diff changeset
198 d.addCallback(lambda items: map(self.host.plugins["XEP-0277"].item2mbdata, items))
308
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
199
465
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
200 self.initialise(profile_key).addCallback(initialised)
78e67a59d51d plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents: 462
diff changeset
201 #TODO: we need to use the server corresponding the the host of the jid
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
202
470
5c916b99d0f6 plugin groupblog, D-Bus frontend: added getLastGroupBlogs and getMassiveLastGroupBlogs
Goffi <goffi@goffi.org>
parents: 467
diff changeset
203 def getMassiveLastGroupBlogs(self, publishers_type, publishers, max_items=10, profile_key='@DEFAULT@'):
467
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
204 """Get the last published microblogs for a list of groups or jids
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
205 @param publishers_type: type of the list of publishers (one of "GROUP" or "JID" or "ALL")
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
206 @param publishers: list of publishers, according to "publishers_type" (list of groups or list of jids)
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
207 @param max_items: how many microblogs we want to get
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
208 @param profile_key: profile key
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
209 """
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
210 def sendResult(result):
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
211 """send result of DeferredList (list of microblogs to the calling method"""
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
212
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
213 ret = {}
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
214
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
215 for (success, value) in result:
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
216 if success:
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
217 source_jid, data = value
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
218 ret[source_jid] = data
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
219
470
5c916b99d0f6 plugin groupblog, D-Bus frontend: added getLastGroupBlogs and getMassiveLastGroupBlogs
Goffi <goffi@goffi.org>
parents: 467
diff changeset
220 return ret
467
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
221
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
222 def initialised(result):
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
223 profile, client = result
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
224
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
225 if publishers_type == "ALL":
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
226 contacts = client.roster.getItems()
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
227 jids = [contact.jid.userhost() for contact in contacts]
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
228 mblogs = []
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
229 for _jid in jids:
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
230 d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, jid.JID(_jid).userhost(),
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
231 max_items=max_items, profile_key=profile_key)
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
232 d.addCallback(lambda items, source_jid: (source_jid, map(self.host.plugins["XEP-0277"].item2mbdata, items)), _jid)
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
233 mblogs.append(d)
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
234 dlist = defer.DeferredList(mblogs)
470
5c916b99d0f6 plugin groupblog, D-Bus frontend: added getLastGroupBlogs and getMassiveLastGroupBlogs
Goffi <goffi@goffi.org>
parents: 467
diff changeset
235 dlist.addCallback(sendResult)
5c916b99d0f6 plugin groupblog, D-Bus frontend: added getLastGroupBlogs and getMassiveLastGroupBlogs
Goffi <goffi@goffi.org>
parents: 467
diff changeset
236 return dlist
467
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
237
470
5c916b99d0f6 plugin groupblog, D-Bus frontend: added getLastGroupBlogs and getMassiveLastGroupBlogs
Goffi <goffi@goffi.org>
parents: 467
diff changeset
238 return defer.fail("Unknown type")
467
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
239
470
5c916b99d0f6 plugin groupblog, D-Bus frontend: added getLastGroupBlogs and getMassiveLastGroupBlogs
Goffi <goffi@goffi.org>
parents: 467
diff changeset
240
467
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
241 #TODO: custom exception
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
242 if publishers_type not in ["GROUP", "JID", "ALL"]:
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
243 raise Exception("Bad call, unknown publishers_type")
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
244 if publishers_type=="ALL" and publishers:
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
245 raise Exception("Publishers list must be empty when getting microblogs for all contacts")
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
246 return self.initialise(profile_key).addCallback(initialised)
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
247 #TODO: we need to use the server corresponding the the host of the jid
47af60767013 plugin group blog: getMassiveGroupBlog first draft
Goffi <goffi@goffi.org>
parents: 465
diff changeset
248