def getUserTimeline(user, request): """ Get user timeline """ query = timelineQuery(user) activities = sorted_query(request, request.db.activity, query, flatten=1) handler = JSONResourceRoot(request, activities) return handler.buildResponse()
def getContextActivities(context, request): """ Get context activities Returns all the activities posted on a context :rest hash The hash of the context url where the activties where posted """ url = context['url'] # regex query to find all contexts within url escaped = re.escape(url) url_regex = {'$regex': '^%s' % escaped} # Search posts associated with contexts that have this context's # url as prefix a.k.a "recursive contexts" query = {} # Search query.update({'verb': 'post'}) # 'post' activities query.update({'contexts.url': url_regex}) # equal or child of url # Check if we have permission to unrestrictely view activities from recursive contexts: can_list_activities_unsubscribed = isinstance(request.has_permission(list_activities_unsubscribed), ACLAllowed) # If we can't view unsubcribed contexts, filter from which contexts we get activities by listing # the contexts that the user has read permission on his subscriptions. Public contexts are only searched here # because if we can list_activities_unsubscribed, main query already includes them. readable_contexts_urls = [] if not can_list_activities_unsubscribed: # Include all urls from subscriptions to contexts whose url # is a child of of main context url, for subscription in request.actor['subscribedTo']: if 'read' in subscription.get('permissions', []) \ and subscription['objectType'] == 'context'\ and subscription['url'].startswith(url): readable_contexts_urls.append(subscription['url']) # We'll include also all contexts that are public whitin the url public_query = {'permissions.read': 'public', 'url': url_regex} for result in request.db.contexts.search(public_query, show_fields=['url']): readable_contexts_urls.append(result['url']) # if any url collected, include it on the query if readable_contexts_urls: query['contexts.url'] = {'$in': readable_contexts_urls} activities = [] # Execute search only if we have read permision on some contexts or we have usubscribed access to activities. if readable_contexts_urls or can_list_activities_unsubscribed: activities = sorted_query(request, request.db.activity, query, flatten=1) is_head = request.method == 'HEAD' handler = JSONResourceRoot(request, activities, stats=is_head) return handler.buildResponse()
def feed_activities(): """ Keep feeding a continuous flow of activitesm making as many queries as needed, while not reaching defined limit """ while feed_parameters['queries'] <= AUTHORS_SEARCH_MAX_QUERIES_LIMIT: activities = sorted_query(request, request.db.activity, query, count=False, **extra_params()) activity = None for activity in activities: yield activity # Once exhausted the first query, prepare for the next round if activity: feed_parameters['before'] = activity['_id'] feed_parameters['queries'] += 1 else: raise StopIteration