def subscribe(context, request): """ Subscribe user to context """ actor = request.actor rest_params = {'object': context, 'verb': 'subscribe'} # Initialize a Activity object from the request newactivity = Activity.from_request(request, rest_params=rest_params) # Check if user is already subscribed subscribed_contexts_hashes = [a['hash'] for a in actor['subscribedTo']] if newactivity['object'].getHash() in subscribed_contexts_hashes: # If user already subscribed, send a 200 code and retrieve the original subscribe activity # post when user was subscribed. This way in th return data we'll have the date of subscription code = 200 activities = MADMaxCollection(request, 'activity') query = {'verb': 'subscribe', 'object.url': newactivity['object']['url'], 'actor.username': actor['username']} newactivity = activities.last(query) # Pick the last one, so we get the last time user subscribed (in cas a unsbuscription occured sometime...) else: actor.addSubscription(context) # If user wasn't created, 201 will show that the subscription has just been added code = 201 newactivity_oid = newactivity.insert() # Insert a subscribe activity newactivity['_id'] = newactivity_oid handler = JSONResourceEntity(request, newactivity.flatten(), status_code=code) return handler.buildResponse()
def like(activity, request): """ Like activity """ if activity.has_like_from(request.actor): code = 200 activities = MADMaxCollection(request, 'activity') query = {'verb': 'like', 'object._id': activity['_id'], 'actor.username': request.actor['username']} newactivity = activities.last(query) # Pick the last one, so we get the last time user liked this activity else: code = 201 # Prepare rest parameters to be merged with post data rest_params = { 'verb': 'like', 'object': { '_id': ObjectId(activity['_id']), 'objectType': activity['objectType'], } } # Initialize a Activity object from the request newactivity = Activity.from_request(request, rest_params=rest_params) newactivity_oid = newactivity.insert() newactivity['_id'] = newactivity_oid activity.add_like_from(request.actor) newactivity['object']['likes'] = activity['likes'] # Return the current likes of the activity newactivity['object']['likesCount'] = activity['likesCount'] # Return the current likes of the activity newactivity['object']['liked'] = activity.has_like_from(request.actor) handler = JSONResourceEntity(request, newactivity.flatten(), status_code=code) return handler.buildResponse()
def joinConversation(conversation, request): """ Join conversation """ actor = request.actor cid = request.matchdict['id'] # Check if user is already subscribed if conversation.subscription: # If user already subscribed, send a 200 code and retrieve the original subscribe activity # post when user was subscribed. This way in th return data we'll have the date of subscription code = 200 activities = MADMaxCollection(request, 'activity') query = {'verb': 'subscribe', 'object.id': cid, 'actor.username': actor['username']} newactivity = activities.last(query) # Pick the last one, so we get the last time user subscribed (in cas a unsbuscription occured sometime...) else: if len(conversation['participants']) == CONVERSATION_PARTICIPANTS_LIMIT: raise Forbidden('This conversation is full, no more of {} participants allowed'.format(CONVERSATION_PARTICIPANTS_LIMIT)) if 'group' not in conversation.get('tags', []): raise Forbidden('This is not a group conversation, so no one else is allowed'.format(CONVERSATION_PARTICIPANTS_LIMIT)) if not request.creator.is_allowed_to_see(actor): raise Forbidden('User {} is not allowed to have a conversation with {}'.format(request.creator['username'], actor['username'])) conversation['participants'].append(actor.flatten(preserve=['displayName', 'objectType', 'username'])) actor.addSubscription(conversation) # If we add anyone to a conversation, we remove the archive tag, no matter how many participants have if 'archive' in conversation.get('tags', []): conversation['tags'].remove('archive') conversation.save() # If user wasn't created, 201 will show that the subscription has just been added code = 201 # Initialize a Activity object from the request rest_params = {'actor': actor, 'verb': 'subscribe', 'object': {'objectType': 'conversation', 'id': cid, 'participants': conversation['participants']} } newactivity = Activity.from_request(request, rest_params=rest_params) newactivity_oid = newactivity.insert() # Insert a subscribe activity newactivity['_id'] = newactivity_oid handler = JSONResourceEntity(request, newactivity.flatten(), status_code=code) return handler.buildResponse()