Mercurial > libervia-backend
comparison src/plugins/plugin_misc_groupblog.py @ 465:78e67a59d51d
plugin groupblog: added getLastGroupBlogs method
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 24 Mar 2012 17:48:12 +0100 |
parents | d9456d94cd12 |
children | 47af60767013 |
comparison
equal
deleted
inserted
replaced
464:794f60e2da27 | 465:78e67a59d51d |
---|---|
57 "main": "GroupBlog", | 57 "main": "GroupBlog", |
58 "handler": "no", | 58 "handler": "no", |
59 "description": _("""Implementation of microblogging with roster access""") | 59 "description": _("""Implementation of microblogging with roster access""") |
60 } | 60 } |
61 | 61 |
62 class NodeCreationError(Exception): | 62 class NoCompatiblePubSubServerFound(Exception): |
63 pass | |
64 | |
65 class NodeDeletionError(Exception): | |
66 pass | 63 pass |
67 | 64 |
68 class GroupBlog(): | 65 class GroupBlog(): |
69 """This class use a SàT PubSub Service to manage access on microblog""" | 66 """This class use a SàT PubSub Service to manage access on microblog""" |
70 | 67 |
77 doc = { 'summary':"Send a microblog to a list of groups", | 74 doc = { 'summary':"Send a microblog to a list of groups", |
78 'param_0':'list of groups which can read the microblog', | 75 'param_0':'list of groups which can read the microblog', |
79 'param_1':'text to send', | 76 'param_1':'text to send', |
80 'param_2':'%(doc_profile)s' | 77 'param_2':'%(doc_profile)s' |
81 }) | 78 }) |
79 | |
80 host.bridge.addMethod("getLastGroupBlogs", ".plugin", | |
81 in_sign='sis', out_sign='aa{ss}', | |
82 method=self.getLastGroupBlogs, | |
83 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 | |
91 @defer.inlineCallbacks | |
92 def initialise(self, profile_key): | |
93 """Check that this data for this profile are initialised, and do it else | |
94 @param client: client of the profile | |
95 @profile_key: %(doc_profile)s""" | |
96 profile = self.host.memory.getProfileName(profile_key) | |
97 if not profile: | |
98 error(_("Unknown profile")) | |
99 raise Exception("Unknown profile") | |
82 | 100 |
101 client = self.host.getClient(profile) | |
102 if not client: | |
103 error(_('No client for this profile key: %s') % profile_key) | |
104 raise Exception("Unknown profile") | |
105 yield client.client_initialized #we want to be sure that the client is initialized | |
106 | |
107 #we first check that we have a item-access pubsub server | |
108 if not hasattr(client,"item_access_pubsub"): | |
109 debug(_('Looking for item-access power pubsub server')) | |
110 #we don't have any pubsub server featuring item access yet | |
111 test = self.host.memory.getServerServiceEntities("pubsub", "service", profile) | |
112 client.item_access_pubsub = None | |
113 for entity in self.host.memory.getServerServiceEntities("pubsub", "service", profile): | |
114 disco = yield client.disco.requestInfo(entity) | |
115 if set([NS_PUBSUB_ITEM_ACCESS, NS_PUBSUB_AUTO_CREATE, NS_PUBSUB_CREATOR_JID_CHECK]).issubset(disco.features): | |
116 info(_("item-access powered pubsub service found: [%s]") % entity.full()) | |
117 client.item_access_pubsub = entity | |
118 | |
119 if not client.item_access_pubsub: | |
120 error(_("No item-access powered pubsub server found, can't post group blog")) | |
121 raise NoCompatiblePubSubServerFound | |
122 | |
123 defer.returnValue((profile, client)) | |
124 | |
125 | |
83 def _publishMblog(self, service, groups, message, client): | 126 def _publishMblog(self, service, groups, message, client): |
84 """Actually publish the message on the group blog | 127 """Actually publish the message on the group blog |
85 @param service: jid of the item-access pubsub service | 128 @param service: jid of the item-access pubsub service |
86 @param groups: set of groups allowed to see the item | 129 @param groups: set of groups allowed to see the item |
87 @param message: message to publish | 130 @param message: message to publish |
96 | 139 |
97 def _mblogPublicationFailed(self, failure): | 140 def _mblogPublicationFailed(self, failure): |
98 #TODO | 141 #TODO |
99 pass | 142 pass |
100 | 143 |
101 @defer.inlineCallbacks | |
102 def sendGroupBlog(self, groups, message, profile_key='@DEFAULT@'): | 144 def sendGroupBlog(self, groups, message, profile_key='@DEFAULT@'): |
103 """Publish a microblog to the node associated to the groups | 145 """Publish a microblog to the node associated to the groups |
104 If the node doesn't exist, it is created, then the message is posted | 146 If the node doesn't exist, it is created, then the message is posted |
105 @param groups: list of groups allowed to retrieve the microblog | 147 @param groups: list of groups allowed to retrieve the microblog |
106 @param message: microblog | 148 @param message: microblog |
107 @profile_key: %(doc_profile)s | 149 @profile_key: %(doc_profile)s |
108 """ | 150 """ |
109 print "sendGroupBlog" | 151 print "sendGroupBlog" |
110 profile = self.host.memory.getProfileName(profile_key) | 152 |
111 if not profile: | 153 def initialised(result): |
112 error(_("Unknown profile")) | 154 profile, client = result |
113 return | 155 _groups = set(groups).intersection(client.roster.getGroups()) #We only keep group which actually exist |
156 #TODO: send an error signal if user want to post to non existant groups | |
157 | |
158 self._publishMblog(client.item_access_pubsub, _groups, message, client) | |
159 | |
160 self.initialise(profile_key).addCallback(initialised) | |
161 | |
162 | |
114 | 163 |
115 client = self.host.getClient(profile) | 164 def getLastGroupBlogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@', callback=None, errback=None): |
116 if not client: | 165 """Get the last published microblogs |
117 error(_('No client for this profile key: %s') % profile_key) | 166 @param pub_jid: jid of the publisher |
118 return | 167 @param max_items: how many microblogs we want to get |
168 @param profile_key: profile key | |
169 @param callback: used for the async answer | |
170 @param errback: used for the async answer | |
171 """ | |
119 | 172 |
120 yield client.client_initialized #we want to be sure that the client is initialized | 173 def initialised(result): |
121 | 174 profile, client = result |
122 #we first check that we have a item-access pubsub server | 175 d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, jid.JID(pub_jid).userhost(), |
123 if not hasattr(client,"item_access_pubsub"): | 176 max_items=max_items, profile_key=profile_key) |
124 debug(_('Looking for item-access power pubsub server')) | 177 d.addCallbacks(lambda items: callback(map(self.host.plugins["XEP-0277"].item2mbdata, items)), errback) |
125 #we don't have any pubsub server featuring item access yet | |
126 test = self.host.memory.getServerServiceEntities("pubsub", "service", profile) | |
127 client.item_access_pubsub = None | |
128 for entity in self.host.memory.getServerServiceEntities("pubsub", "service", profile): | |
129 disco = yield client.disco.requestInfo(entity) | |
130 if set([NS_PUBSUB_ITEM_ACCESS, NS_PUBSUB_AUTO_CREATE, NS_PUBSUB_CREATOR_JID_CHECK]).issubset(disco.features): | |
131 info(_("item-access powered pubsub service found: [%s]") % entity.full()) | |
132 client.item_access_pubsub = entity | |
133 | 178 |
134 if not client.item_access_pubsub: | 179 assert(callback) |
135 error(_("No item-access powered pubsub server found, can't post group blog")) | 180 self.initialise(profile_key).addCallback(initialised) |
136 return | 181 #TODO: we need to use the server corresponding the the host of the jid |
137 | 182 |
138 _groups = set(groups).intersection(client.roster.getGroups()) #We only keep group which actually exist | |
139 #TODO: send an error signal if user want to post to non existant groups | |
140 | |
141 self._publishMblog(client.item_access_pubsub, _groups, message, client) | |
142 | |
143 |