comparison src/plugins/plugin_misc_groupblog.py @ 471:6cd04adddaea

core: exceptions moved to core plugin group blog: group blog now manage public microblogs
author Goffi <goffi@goffi.org>
date Wed, 04 Apr 2012 00:06:44 +0200
parents 5c916b99d0f6
children b9fd32b46306
comparison
equal deleted inserted replaced
470:5c916b99d0f6 471:6cd04adddaea
60 } 60 }
61 61
62 class NoCompatiblePubSubServerFound(Exception): 62 class NoCompatiblePubSubServerFound(Exception):
63 pass 63 pass
64 64
65 class BadAccessTypeError(Exception):
66 pass
67
68 class BadAccessListError(Exception):
69 pass
70
65 class GroupBlog(): 71 class GroupBlog():
66 """This class use a SàT PubSub Service to manage access on microblog""" 72 """This class use a SàT PubSub Service to manage access on microblog"""
67 73
68 def __init__(self, host): 74 def __init__(self, host):
69 info(_("Group blog plugin initialization")) 75 info(_("Group blog plugin initialization"))
70 self.host = host 76 self.host = host
71 77
72 host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='asss', out_sign='', 78 host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='sasss', out_sign='',
73 method=self.sendGroupBlog, 79 method=self.sendGroupBlog)
74 doc = { 'summary':"Send a microblog to a list of groups",
75 'param_0':'list of groups which can read the microblog',
76 'param_1':'text to send',
77 'param_2':'%(doc_profile)s'
78 })
79 80
80 host.bridge.addMethod("getLastGroupBlogs", ".plugin", 81 host.bridge.addMethod("getLastGroupBlogs", ".plugin",
81 in_sign='sis', out_sign='aa{ss}', 82 in_sign='sis', out_sign='aa{ss}',
82 method=self.getLastGroupBlogs, 83 method=self.getLastGroupBlogs,
83 async = True, 84 async = True)
84 doc = { 'summary':'retrieve items',
85 'param_0':'jid: publisher of wanted microblog',
86 'param_1':'max_items: see XEP-0060 #6.5.7',
87 'param_2':'%(doc_profile)s',
88 'return':'list of microblog data (dict)'
89 })
90 85
91 host.bridge.addMethod("getMassiveLastGroupBlogs", ".plugin", 86 host.bridge.addMethod("getMassiveLastGroupBlogs", ".plugin",
92 in_sign='sasis', out_sign='a{saa{ss}}', 87 in_sign='sasis', out_sign='a{saa{ss}}',
93 method=self.getMassiveLastGroupBlogs, 88 method=self.getMassiveLastGroupBlogs,
94 async = True) 89 async = True)
127 raise NoCompatiblePubSubServerFound 122 raise NoCompatiblePubSubServerFound
128 123
129 defer.returnValue((profile, client)) 124 defer.returnValue((profile, client))
130 125
131 126
132 def _publishMblog(self, service, groups, message, client): 127 def _publishMblog(self, service, client, access_type, access_list, message):
133 """Actually publish the message on the group blog 128 """Actually publish the message on the group blog
134 @param service: jid of the item-access pubsub service 129 @param service: jid of the item-access pubsub service
135 @param groups: set of groups allowed to see the item 130 @param client: SatXMPPClient of the published
131 @param access_type: one of "PUBLIC", "GROUP", "JID"
132 @param access_list: set of entities (empty list for all, groups or jids) allowed to see the item
136 @param message: message to publish 133 @param message: message to publish
137 @param client: SatXMPPClient of the published""" 134 """
138 mblog_item = self.host.plugins["XEP-0277"].data2entry({'content':message}, client.profile) 135 mblog_item = self.host.plugins["XEP-0277"].data2entry({'content':message}, client.profile)
139 form = data_form.Form('submit', formNamespace=NS_PUBSUB_ITEM_CONFIG) 136 form = data_form.Form('submit', formNamespace=NS_PUBSUB_ITEM_CONFIG)
140 field = data_form.Field('list-multi', OPT_ROSTER_GROUPS_ALLOWED, values=groups) 137 if access_type == "PUBLIC":
141 form.addField(field) 138 if access_list:
142 mblog_item.addChild(form.toElement()) 139 raise BadAccessListError("access_list must be empty for PUBLIC access")
140 elif access_type == "GROUP":
141 field = data_form.Field('list-multi', OPT_ROSTER_GROUPS_ALLOWED, values=access_list)
142 form.addField(field)
143 mblog_item.addChild(form.toElement())
144 elif access_type == "JID":
145 raise NotImplementedError
146 else:
147 error(_("Unknown access_type"))
148 raise BadAccessTypeError
143 defer_blog = self.host.plugins["XEP-0060"].publish(service, client.jid.userhost(), items=[mblog_item], profile_key=client.profile) 149 defer_blog = self.host.plugins["XEP-0060"].publish(service, client.jid.userhost(), items=[mblog_item], profile_key=client.profile)
144 defer_blog.addErrback(self._mblogPublicationFailed) 150 defer_blog.addErrback(self._mblogPublicationFailed)
145 151
146 def _mblogPublicationFailed(self, failure): 152 def _mblogPublicationFailed(self, failure):
147 #TODO 153 #TODO
148 pass 154 return failure
149 155
150 def sendGroupBlog(self, groups, message, profile_key='@DEFAULT@'): 156 def sendGroupBlog(self, access_type, access_list, message, profile_key='@DEFAULT@'):
151 """Publish a microblog to the node associated to the groups 157 """Publish a microblog with given item access
152 If the node doesn't exist, it is created, then the message is posted 158 @param access_type: one of "PUBLIC", "GROUP", "JID"
153 @param groups: list of groups allowed to retrieve the microblog 159 @param access_list: list of authorized entity (empty list for PUBLIC ACCESS,
160 list of groups or list of jids) for this item
154 @param message: microblog 161 @param message: microblog
155 @profile_key: %(doc_profile)s 162 @profile_key: %(doc_profile)s
156 """ 163 """
157 print "sendGroupBlog" 164 print "sendGroupBlog"
158
159 def initialised(result): 165 def initialised(result):
160 profile, client = result 166 profile, client = result
161 _groups = set(groups).intersection(client.roster.getGroups()) #We only keep group which actually exist 167 if access_type == "PUBLIC":
162 #TODO: send an error signal if user want to post to non existant groups 168 if access_list:
169 raise Exception("Publishers list must be empty when getting microblogs for all contacts")
170 self._publishMblog(client.item_access_pubsub, client, "PUBLIC", [], message)
171 elif access_type == "GROUP":
172 _groups = set(access_list).intersection(client.roster.getGroups()) #We only keep group which actually exist
173 if not _groups:
174 raise BadAccessListError("No valid group")
175 self._publishMblog(client.item_access_pubsub, client, "GROUP", _groups, message)
176 elif access_type == "JID":
177 raise NotImplementedError
178 else:
179 error(_("Unknown access type"))
180 raise BadAccessTypeError
163 181
164 self._publishMblog(client.item_access_pubsub, _groups, message, client)
165
166 self.initialise(profile_key).addCallback(initialised) 182 self.initialise(profile_key).addCallback(initialised)
167 183
168 184
169 185
170 def getLastGroupBlogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@'): 186 def getLastGroupBlogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@'):
171 """Get the last published microblogs 187 """Get the last published microblogs
172 @param pub_jid: jid of the publisher 188 @param pub_jid: jid of the publisher
173 @param max_items: how many microblogs we want to get 189 @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7)
174 @param profile_key: profile key 190 @param profile_key: profile key
175 @param callback: used for the async answer 191 @return: list of microblog data (dict)
176 @param errback: used for the async answer
177 """ 192 """
178 193
179 def initialised(result): 194 def initialised(result):
180 profile, client = result 195 profile, client = result
181 d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, jid.JID(pub_jid).userhost(), 196 d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, jid.JID(pub_jid).userhost(),