def getUserTimeline(context, request): """ /people/{username}/timeline Retorna totes les activitats d'un usuari """ actor = request.actor is_context_resource = 'timeline/contexts' in request.path is_follows_resource = 'timeline/follows' in request.path mmdb = MADMaxDB(context.db) actor_query = {'actor._id': actor['_id']} # Add the activity of the people that the user follows actors_followings = [] for following in actor['following']['items']: followed_person = mmdb.users.getItemsByusername( following['username'])[0] if followed_person: actors_followings.append({'actor._id': followed_person['_id']}) # Add the activity of the people that posts to a particular context contexts_followings = [] for subscribed in actor['subscribedTo']['items']: contexts_followings.append({'contexts.url': subscribed['url']}) query_items = [] if not is_follows_resource and not is_context_resource: query_items.append(actor_query) query_items += actors_followings query_items += contexts_followings if is_context_resource: query_items += contexts_followings if is_follows_resource: query_items += contexts_followings if query_items: query = {'$or': query_items} query['verb'] = 'post' activities = mmdb.activity.search(query, sort="_id", flatten=1, **searchParams(request)) else: activities = [] handler = JSONResourceRoot(activities) return handler.buildResponse()
def getActivities(context, request): """ /activities Retorna all activities, optionaly filtered by context """ urlhash = request.params.get('context', None) if not urlhash: raise MissingField, 'You have to specify one context' mmdb = MADMaxDB(context.db) # subscribed contexts with read permission subscribed = [context.get('url') for context in request.actor.subscribedTo.get('items', []) if 'read' in context.get('permissions', [])] # get the defined read context rcontext = mmdb.contexts.getItemsByurlHash(urlhash)[0] url = rcontext.url # regex query to find all contexts within url escaped = re.escape(url) url_regex = {'$regex': '^%s' % escaped} # search all contexts with public read permissions within url query = {'permissions.read': 'public', 'url': url_regex} public = [result.url for result in mmdb.contexts.search(query, show_fields=['url'])] query = {} # Search query.update({'verb': 'post'}) # 'post' activities query.update({'contexts.url': url_regex}) # equal or child of url contexts_query = [] if subscribed: subscribed_query = {'contexts.url': {'$in': subscribed}} # that are subscribed contexts contexts_query.append(subscribed_query) # with read permission if public: # OR public_query = {'contexts.url': {'$in': public}} contexts_query.append(public_query) # pubic contexts if contexts_query: query.update({'$or': contexts_query}) activities = mmdb.activity.search(query, sort="_id", flatten=1, **searchParams(request)) else: # we have no public contexts and we are not subscribed to any context, so we # won't get anything activities = [] # pass the read context as a extension to the resource handler = JSONResourceRoot(activities, extension=dict(context=rcontext.flatten())) return handler.buildResponse()
def getUserTimeline(context, request): """ /people/{username}/timeline Retorna totes les activitats d'un usuari """ actor = request.actor is_context_resource = 'timeline/contexts' in request.path is_follows_resource = 'timeline/follows' in request.path mmdb = MADMaxDB(context.db) actor_query = {'actor._id': actor['_id']} # Add the activity of the people that the user follows actors_followings = [] for following in actor['following']['items']: followed_person = mmdb.users.getItemsByusername(following['username'])[0] if followed_person: actors_followings.append({'actor._id': followed_person['_id']}) # Add the activity of the people that posts to a particular context contexts_followings = [] for subscribed in actor['subscribedTo']['items']: contexts_followings.append({'contexts.url': subscribed['url']}) query_items = [] if not is_follows_resource and not is_context_resource: query_items.append(actor_query) query_items += actors_followings query_items += contexts_followings if is_context_resource: query_items += contexts_followings if is_follows_resource: query_items += contexts_followings if query_items: query = {'$or': query_items} query['verb'] = 'post' activities = mmdb.activity.search(query, sort="_id", flatten=1, **searchParams(request)) else: activities = [] handler = JSONResourceRoot(activities) return handler.buildResponse()
def getActivities(context, request): """ /activities Retorna all activities, optionaly filtered by context """ urlhash = request.params.get('context', None) if not urlhash: raise MissingField, 'You have to specify one context' mmdb = MADMaxDB(context.db) # subscribed contexts with read permission subscribed = [ context.get('url') for context in request.actor.subscribedTo.get('items', []) if 'read' in context.get('permissions', []) ] # get the defined read context rcontext = mmdb.contexts.getItemsByurlHash(urlhash)[0] url = rcontext.url # regex query to find all contexts within url escaped = re.escape(url) url_regex = {'$regex': '^%s' % escaped} # search all contexts with public read permissions within url query = {'permissions.read': 'public', 'url': url_regex} public = [ result.url for result in mmdb.contexts.search(query, show_fields=['url']) ] query = {} # Search query.update({'verb': 'post'}) # 'post' activities query.update({'contexts.url': url_regex}) # equal or child of url contexts_query = [] if subscribed: subscribed_query = { 'contexts.url': { '$in': subscribed } } # that are subscribed contexts contexts_query.append(subscribed_query) # with read permission if public: # OR public_query = {'contexts.url': {'$in': public}} contexts_query.append(public_query) # pubic contexts if contexts_query: query.update({'$or': contexts_query}) activities = mmdb.activity.search(query, sort="_id", flatten=1, **searchParams(request)) else: # we have no public contexts and we are not subscribed to any context, so we # won't get anything activities = [] # pass the read context as a extension to the resource handler = JSONResourceRoot(activities, extension=dict(context=rcontext.flatten())) return handler.buildResponse()