Mercurial > libervia-web
comparison src/server/blog.py @ 746:25984ca4aef2
server side: special handling of Connection/JabberID and Connection/autoconnect parameter which has needed by QuickApp but restricted by security limit
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 23 Nov 2015 14:19:25 +0100 |
parents | 916075bd0356 |
children | efbd28b3438e |
comparison
equal
deleted
inserted
replaced
745:ad733b670cc3 | 746:25984ca4aef2 |
---|---|
89 @param dummy (str): that would be C.SERVICE_PROFILE | 89 @param dummy (str): that would be C.SERVICE_PROFILE |
90 """ | 90 """ |
91 if key != "avatar": | 91 if key != "avatar": |
92 return | 92 return |
93 log.debug(_(u"Received a new avatar for entity %s") % entity_s) | 93 log.debug(_(u"Received a new avatar for entity %s") % entity_s) |
94 | 94 |
95 url = os.path.join(C.AVATARS_DIR, value) | 95 url = os.path.join(C.AVATARS_DIR, value) |
96 self.avatars_cache[entity_s] = url | 96 self.avatars_cache[entity_s] = url |
97 self.waiting_deferreds[entity_s].callback(url) | 97 self.waiting_deferreds[entity_s].callback(url) |
98 del self.waiting_deferreds[entity_s] | 98 del self.waiting_deferreds[entity_s] |
99 | 99 |
100 def getAvatarURL(self, pub_jid): | 100 def getAvatarURL(self, pub_jid): |
101 """Return avatar of a jid if in cache, else ask for it. | 101 """Return avatar of a jid if in cache, else ask for it. |
102 | 102 |
103 @param pub_jid (JID): publisher JID | 103 @param pub_jid (JID): publisher JID |
104 @return: deferred avatar URL (unicode) | 104 @return: deferred avatar URL (unicode) |
126 prof_found = None | 126 prof_found = None |
127 if not prof_found or prof_found == C.SERVICE_PROFILE: | 127 if not prof_found or prof_found == C.SERVICE_PROFILE: |
128 return self.useTemplate(request, "static_blog_error", {'message': "Invalid nickname"}) | 128 return self.useTemplate(request, "static_blog_error", {'message': "Invalid nickname"}) |
129 | 129 |
130 d = defer.Deferred() | 130 d = defer.Deferred() |
131 JID(self.host.bridge.asyncGetParamA('JabberID', 'Connection', 'value', C.SERVER_SECURITY_LIMIT, prof_found, callback=d.callback, errback=d.errback)) | 131 JID(self.host.bridge.asyncGetParamA('JabberID', 'Connection', 'value', profile_key=prof_found, callback=d.callback, errback=d.errback)) |
132 d.addCallbacks(lambda pub_jid_s: self.gotJID(pub_jid_s, request, prof_found)) | 132 d.addCallbacks(lambda pub_jid_s: self.gotJID(pub_jid_s, request, prof_found)) |
133 return server.NOT_DONE_YET | 133 return server.NOT_DONE_YET |
134 | 134 |
135 def gotJID(self, pub_jid_s, request, profile): | 135 def gotJID(self, pub_jid_s, request, profile): |
136 pub_jid = JID(pub_jid_s) | 136 pub_jid = JID(pub_jid_s) |
152 self.getItemById(pub_jid, request.item_id, request.extra_dict, | 152 self.getItemById(pub_jid, request.item_id, request.extra_dict, |
153 request.extra_comments_dict, request, profile) | 153 request.extra_comments_dict, request, profile) |
154 else: | 154 else: |
155 self.getItems(pub_jid, max_items, request.extra_dict, | 155 self.getItems(pub_jid, max_items, request.extra_dict, |
156 request.extra_comments_dict, request, profile) | 156 request.extra_comments_dict, request, profile) |
157 | 157 |
158 def parseURLParams(self, request): | 158 def parseURLParams(self, request): |
159 """Parse the request URL parameters. | 159 """Parse the request URL parameters. |
160 | 160 |
161 @param request: HTTP request | 161 @param request: HTTP request |
162 """ | 162 """ |
200 except KeyError: | 200 except KeyError: |
201 pass | 201 pass |
202 | 202 |
203 def parseURLParamsCommentsRSM(self, request): | 203 def parseURLParamsCommentsRSM(self, request): |
204 """Parse RSM request data from the URL parameters for comments | 204 """Parse RSM request data from the URL parameters for comments |
205 | 205 |
206 @param request: HTTP request | 206 @param request: HTTP request |
207 """ | 207 """ |
208 request.extra_comments_dict = {} | 208 request.extra_comments_dict = {} |
209 if request.display_single: | 209 if request.display_single: |
210 try: | 210 try: |
214 else: | 214 else: |
215 request.extra_comments_dict['rsm_max'] = "0" | 215 request.extra_comments_dict['rsm_max'] = "0" |
216 | 216 |
217 def getItemById(self, pub_jid, item_id, extra_dict, extra_comments_dict, request, profile): | 217 def getItemById(self, pub_jid, item_id, extra_dict, extra_comments_dict, request, profile): |
218 """ | 218 """ |
219 | 219 |
220 @param pub_jid (jid.JID): publisher JID | 220 @param pub_jid (jid.JID): publisher JID |
221 @param item_id(unicode): ID of the item to retrieve | 221 @param item_id(unicode): ID of the item to retrieve |
222 @param extra_dict (dict): extra configuration for initial items only | 222 @param extra_dict (dict): extra configuration for initial items only |
223 @param extra_comments_dict (dict): extra configuration for comments only | 223 @param extra_comments_dict (dict): extra configuration for comments only |
224 @param request: HTTP request | 224 @param request: HTTP request |
233 metadata_bis = items_bis[1] | 233 metadata_bis = items_bis[1] |
234 metadata['rsm_count'] = metadata_bis['rsm_count'] | 234 metadata['rsm_count'] = metadata_bis['rsm_count'] |
235 index_key = "rsm_index" if metadata_bis.get("rsm_index") else "rsm_count" | 235 index_key = "rsm_index" if metadata_bis.get("rsm_index") else "rsm_count" |
236 metadata['rsm_index'] = unicode(int(metadata_bis[index_key]) - 1) | 236 metadata['rsm_index'] = unicode(int(metadata_bis[index_key]) - 1) |
237 metadata['rsm_first'] = metadata['rsm_last'] = item["id"] | 237 metadata['rsm_first'] = metadata['rsm_last'] = item["id"] |
238 | 238 |
239 def gotComments(comments): | 239 def gotComments(comments): |
240 # build the items as self.getItems would do it (and as self.render_html_blog expects them to be) | 240 # build the items as self.getItems would do it (and as self.render_html_blog expects them to be) |
241 comments = [(item['comments_service'], item['comments_node'], "", comments[0], comments[1])] | 241 comments = [(item['comments_service'], item['comments_node'], "", comments[0], comments[1])] |
242 self.render_html_blog([(item, comments)], metadata, request, pub_jid, profile) | 242 self.render_html_blog([(item, comments)], metadata, request, pub_jid, profile) |
243 | 243 |
255 self.host.bridge.mbGet(pub_jid.userhost(), NS_MICROBLOG, 1, [item_id], | 255 self.host.bridge.mbGet(pub_jid.userhost(), NS_MICROBLOG, 1, [item_id], |
256 extra_dict, C.SERVICE_PROFILE, callback=gotItems) | 256 extra_dict, C.SERVICE_PROFILE, callback=gotItems) |
257 | 257 |
258 def getItems(self, pub_jid, max_items, extra_dict, extra_comments_dict, request, profile): | 258 def getItems(self, pub_jid, max_items, extra_dict, extra_comments_dict, request, profile): |
259 """ | 259 """ |
260 | 260 |
261 @param pub_jid (jid.JID): publisher JID | 261 @param pub_jid (jid.JID): publisher JID |
262 @param max_items(int): maximum number of item to get, C.NO_LIMIT for no limit | 262 @param max_items(int): maximum number of item to get, C.NO_LIMIT for no limit |
263 @param extra_dict (dict): extra configuration for initial items only | 263 @param extra_dict (dict): extra configuration for initial items only |
264 @param extra_comments_dict (dict): extra configuration for comments only | 264 @param extra_comments_dict (dict): extra configuration for comments only |
265 @param request: HTTP request | 265 @param request: HTTP request |
269 remaining, results = data | 269 remaining, results = data |
270 for result in results: | 270 for result in results: |
271 service, node, failure, items, metadata = result | 271 service, node, failure, items, metadata = result |
272 if not failure: | 272 if not failure: |
273 self.render_html_blog(items, metadata, request, pub_jid, profile) | 273 self.render_html_blog(items, metadata, request, pub_jid, profile) |
274 | 274 |
275 if remaining: | 275 if remaining: |
276 self._getResults(rt_session) | 276 self._getResults(rt_session) |
277 | 277 |
278 def getResult(rt_session): | 278 def getResult(rt_session): |
279 self.host.bridge.mbGetFromManyWithCommentsRTResult(rt_session, C.SERVICE_PROFILE, | 279 self.host.bridge.mbGetFromManyWithCommentsRTResult(rt_session, C.SERVICE_PROFILE, |
280 callback=lambda data: getResultCb(data, rt_session), | 280 callback=lambda data: getResultCb(data, rt_session), |
281 errback=lambda failure: self.render_error_blog(failure, request, pub_jid)) | 281 errback=lambda failure: self.render_error_blog(failure, request, pub_jid)) |
282 | 282 |
283 max_comments = int(extra_comments_dict['rsm_max']) | 283 max_comments = int(extra_comments_dict['rsm_max']) |
284 self.host.bridge.mbGetFromManyWithComments(C.JID, [pub_jid.userhost()], max_items, | 284 self.host.bridge.mbGetFromManyWithComments(C.JID, [pub_jid.userhost()], max_items, |
285 max_comments, extra_dict, extra_comments_dict, | 285 max_comments, extra_dict, extra_comments_dict, |
286 C.SERVICE_PROFILE, callback=getResult) | 286 C.SERVICE_PROFILE, callback=getResult) |
287 | 287 |
288 def render_html_blog(self, items, metadata, request, pub_jid, profile): | 288 def render_html_blog(self, items, metadata, request, pub_jid, profile): |
289 """Retrieve the user parameters before actually rendering the static blog | 289 """Retrieve the user parameters before actually rendering the static blog |
290 | 290 |
291 @param items(list[tuple(dict, list)]): same as in self.__render_html_blog | 291 @param items(list[tuple(dict, list)]): same as in self.__render_html_blog |
292 @param metadata(dict): original node metadata | 292 @param metadata(dict): original node metadata |
312 cb = lambda dummy: self.__render_html_blog(items, metadata, options, request, pub_jid) | 312 cb = lambda dummy: self.__render_html_blog(items, metadata, options, request, pub_jid) |
313 defer.DeferredList(d_list).addCallback(cb) | 313 defer.DeferredList(d_list).addCallback(cb) |
314 | 314 |
315 def __render_html_blog(self, items, metadata, options, request, pub_jid): | 315 def __render_html_blog(self, items, metadata, options, request, pub_jid): |
316 """Actually render the static blog. | 316 """Actually render the static blog. |
317 | 317 |
318 If mblog_data is a list of dict, we are missing the comments items so we just | 318 If mblog_data is a list of dict, we are missing the comments items so we just |
319 display the main items. If mblog_data is a list of couple, each couple is | 319 display the main items. If mblog_data is a list of couple, each couple is |
320 associating a main item data with the list of its comments, so we render all. | 320 associating a main item data with the list of its comments, so we render all. |
321 | 321 |
322 @param items(list[tuple(dict, list)]): list of 2-tuple with | 322 @param items(list[tuple(dict, list)]): list of 2-tuple with |
452 @param entry(dict): item microblog data | 452 @param entry(dict): item microblog data |
453 @param comments(list[dict]): list of microblog data | 453 @param comments(list[dict]): list of microblog data |
454 @param comments_count (int): total number of comments | 454 @param comments_count (int): total number of comments |
455 """ | 455 """ |
456 timestamp = float(entry.get('published', 0)) | 456 timestamp = float(entry.get('published', 0)) |
457 | 457 |
458 # FIXME: for now we assume that the comments' depth is only 1 | 458 # FIXME: for now we assume that the comments' depth is only 1 |
459 is_comment = not entry.get('comments', False) | 459 is_comment = not entry.get('comments', False) |
460 | 460 |
461 self.date = datetime.fromtimestamp(timestamp) | 461 self.date = datetime.fromtimestamp(timestamp) |
462 self.type = "comment" if is_comment else "main_item" | 462 self.type = "comment" if is_comment else "main_item" |