def UploadMessage(requestData): TAG = Const.Tags.Urls.UPLOAD_MESSAGE securityProperties = RunThroughSecurityLayer(TAG, requestData) if (not securityProperties.isSecure): return securityProperties.httpResponse try: clientUser = securityProperties.clientUserObject clientRecipientUserUUID = securityProperties.jsonRequestData[ Const.Views.UploadMessage.JsonRequestKey.TO_USER_ID] clientMessageText = securityProperties.jsonRequestData[ Const.Views.UploadMessage.JsonRequestKey.TEXT] clientMessageURL = securityProperties.jsonRequestData[ Const.Views.UploadMessage.JsonRequestKey.URL] # Find the recipient user in the DB try: recipientUser = User.objects.get( uuid=Utils.ConvertUUIDToBinary(clientRecipientUserUUID)) except ObjectDoesNotExist: DataCollector.logURL( TAG, { Const.DataCollection.ParamNames.RESPONSE_CODE: Const.HttpResponseFactory.ResponseCodes.ClientError. CODE_UNPROCESSABLE_ENTITY, Const.DataCollection.ParamNames.MESSAGE_CODE: Const.DataCollection.MessageCodes.UploadMessage. RECIPIENT_NOT_FOUND, Const.DataCollection.ParamNames.FROM_USER: Utils.ConvertBinaryToUUID(clientUser.uuid), Const.DataCollection.ParamNames.TO_USER: Utils.ConvertBinaryToUUID(recipientUser.uuid), Const.DataCollection.ParamNames.HAS_TEXT: (not Utils.StringIsEmpty(clientMessageText)) }) return HttpResponseFactory.MakeHttpResponse( Const.HttpResponseFactory.ResponseCodes.ClientError. CODE_UNPROCESSABLE_ENTITY, Const.DataCollection.MessageCodes. UploadMessage.RECIPIENT_NOT_FOUND) # Save the message in the DB newMessage = Message(toUser=recipientUser, fromUser=clientUser, text=clientMessageText, url=clientMessageURL, contentType=Const.Tags.ContentTypes.MESSAGE) # If there is an exception, roll back this db transaction with transaction.atomic(): newMessage.save() # log and return on success DataCollector.logURL( TAG, { Const.DataCollection.ParamNames.RESPONSE_CODE: Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK, Const.DataCollection.ParamNames.MESSAGE_CODE: Const.DataCollection.MessageCodes.UploadMessage. POST_SUCCESSFUL, Const.DataCollection.ParamNames.FROM_USER: Utils.ConvertBinaryToUUID(clientUser.uuid), Const.DataCollection.ParamNames.TO_USER: Utils.ConvertBinaryToUUID(recipientUser.uuid), Const.DataCollection.ParamNames.HAS_TEXT: (not Utils.StringIsEmpty(clientMessageText)) }) return HttpResponseFactory.MakeHttpResponse( Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK, Const.DataCollection.MessageCodes.UploadMessage.POST_SUCCESSFUL) except Exception as e: DataCollector.logServerError(e) DataCollector.logURL( TAG, { Const.DataCollection.ParamNames.RESPONSE_CODE: Const.HttpResponseFactory.ResponseCodes.ServerError. CODE_INTERNAL_SERVER_ERROR, Const.DataCollection.ParamNames.MESSAGE_CODE: Const.DataCollection.MessageCodes.UploadMessage. POST_FAILED_SERVER_ERROR, Const.DataCollection.ParamNames.FROM_USER: Utils.ConvertBinaryToUUID(clientUser.uuid), Const.DataCollection.ParamNames.TO_USER: Utils.ConvertBinaryToUUID(recipientUser.uuid), Const.DataCollection.ParamNames.HAS_TEXT: (not Utils.StringIsEmpty(clientMessageText)) }) return HttpResponseFactory.MakeHttpResponse( Const.HttpResponseFactory.ResponseCodes.ServerError. CODE_INTERNAL_SERVER_ERROR, Const.DataCollection.MessageCodes. UploadMessage.POST_FAILED_SERVER_ERROR)
def GetMessage(requestData): TAG = Const.Tags.Urls.GET_MESSAGE securityProperties = RunThroughSecurityLayer(TAG, requestData) if (not securityProperties.isSecure): return securityProperties.httpResponse try: clientUser = securityProperties.clientUserObject # Retrieve all this user's messages from the DB messages = Message.objects.filter(toUser=clientUser) clientMessageListToReturn = [] for lm in messages: clientMessageToReturn = _GetMessageClientObject( id=lm.id, time=lm.timeCreated, fromUserId=str(ConvertBinaryToUUID(lm.fromUser.uuid)), text=lm.text, url=lm.url) clientMessageListToReturn.append( clientMessageToReturn.getOrderedDict()) jsonString = json.dumps(clientMessageListToReturn) # Delete all this user's messages from the DB, since we are about to give # them their messages (this is temporary) for m in messages: m.delete() # log and return on success DataCollector.logURL( TAG, { Const.DataCollection.ParamNames.RESPONSE_CODE: Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK, Const.DataCollection.ParamNames.MESSAGE_CODE: Const.DataCollection.MessageCodes.GetMessage. REQUEST_SUCCESSFUL, Const.DataCollection.ParamNames.FROM_USER: Utils.ConvertBinaryToUUID(clientUser.uuid), Const.DataCollection.ParamNames.NUM_MESSAGES_RECEIVED: len(list(messages)) }) return HttpResponseFactory.MakeHttpResponse( Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK, jsonString, 'application/json') except Exception as e: DataCollector.logServerError(e) DataCollector.logURL( TAG, { Const.DataCollection.ParamNames.RESPONSE_CODE: Const.HttpResponseFactory.ResponseCodes.ServerError. CODE_INTERNAL_SERVER_ERROR, Const.DataCollection.ParamNames.MESSAGE_CODE: Const.DataCollection.MessageCodes.GetMessage. REQUEST_FAILED_SERVER_ERROR, Const.DataCollection.ParamNames.FROM_USER: Utils.ConvertBinaryToUUID(clientUser.uuid), Const.DataCollection.ParamNames.NUM_MESSAGES_RECEIVED: len(list(messages)) }) return HttpResponseFactory.MakeHttpResponse( Const.HttpResponseFactory.ResponseCodes.ServerError. CODE_INTERNAL_SERVER_ERROR, Const.DataCollection.MessageCodes. GetMessage.REQUEST_FAILED_SERVER_ERROR)
def GetBanInfo(requestData): TAG = Const.Tags.Urls.SECURITY_GETBANINFO securityProperties = RunThroughSecurityLayer(TAG, requestData) if (not securityProperties.isSecure): return securityProperties.httpResponse try: clientUser = securityProperties.clientUserObject banLength = '' banTimeCreated = '' # Check if the user has any bans userBan = Ban.objects.filter( bannedUser=clientUser).order_by('-timeCreated')[:1] # If there is a ban, check to make sure it is still active if (userBan): userBan = userBan[0] banExpires = userBan.timeCreated + (userBan.banLengthHours * Const.SECONDS_IN_HOUR) # If the ban expir. time is past the current time, then the user is # still currently under a ban if (banExpires > time.time()): banTimeCreated = userBan.timeCreated banLength = userBan.banLengthHours clientObject = _BanInfoClientObject( banStartTime=banTimeCreated, banEndTime=(banTimeCreated + (banLength * Const.SECONDS_IN_HOUR))) jsonString = json.dumps(clientObject.getOrderedDict()) DataCollector.logURL( TAG, { Const.DataCollection.ParamNames.RESPONSE_CODE: Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK, Const.DataCollection.ParamNames.MESSAGE_CODE: Const.DataCollection.MessageCodes.SecurityGetBanInfo. REQUEST_SUCCESSFUL, Const.DataCollection.ParamNames.FROM_USER: Utils.ConvertBinaryToUUID(clientUser.uuid) }) return HttpResponseFactory.MakeHttpResponse( Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK, jsonString, 'application/json') except Exception as e: DataCollector.logServerError(e) DataCollector.logURL( TAG, { Const.DataCollection.ParamNames.RESPONSE_CODE: Const.HttpResponseFactory.ResponseCodes.ServerError. CODE_INTERNAL_SERVER_ERROR, Const.DataCollection.ParamNames.MESSAGE_CODE: Const.DataCollection.MessageCodes.SecurityGetBanInfo. REQUEST_FAILED_SERVER_ERROR, Const.DataCollection.ParamNames.NEW_USER: '' }) return HttpResponseFactory.MakeHttpResponse( Const.HttpResponseFactory.ResponseCodes.ServerError. CODE_INTERNAL_SERVER_ERROR, Const.DataCollection.MessageCodes. SecurityGetBanInfo.REQUEST_FAILED_SERVER_ERROR)
def UploadLocalPost(requestData): TAG = Const.Tags.Urls.UPLOAD_LOCAL securityProperties = RunThroughSecurityLayer(TAG, requestData) if (not securityProperties.isSecure): return securityProperties.httpResponse try: clientUser = securityProperties.clientUserObject clientLatitude = securityProperties.jsonRequestData[Const.Views.UploadLocalPost.JsonRequestKey.LATITUDE] clientLongitude = securityProperties.jsonRequestData[Const.Views.UploadLocalPost.JsonRequestKey.LONGITUDE] clientPostText = securityProperties.jsonRequestData[Const.Views.UploadLocalPost.JsonRequestKey.TEXT] clientPostURL = securityProperties.jsonRequestData[Const.Views.UploadLocalPost.JsonRequestKey.URL] clientARN = securityProperties.jsonRequestData[Const.Views.UploadLocalPost.JsonRequestKey.ARN] # Moderation - check if this user is posting too fast if (settings.RATE_LIMIT_LOCAL and _UserLocalRateLimitExceeded(clientUser.id)): DataCollector.logURL(TAG, { Const.DataCollection.ParamNames.RESPONSE_CODE: Const.HttpResponseFactory.ResponseCodes.ClientError.CODE_TOO_MANY_REQUESTS, Const.DataCollection.ParamNames.MESSAGE_CODE: Const.DataCollection.MessageCodes.UploadLocal.RATE_LIMIT_EXCEEDED, Const.DataCollection.ParamNames.FROM_USER: Utils.ConvertBinaryToUUID(clientUser.uuid), Const.DataCollection.ParamNames.LATITUDE: clientLatitude, Const.DataCollection.ParamNames.LONGITUDE: clientLongitude, Const.DataCollection.ParamNames.HAS_TEXT: (not Utils.StringIsEmpty(clientPostText)) }) return HttpResponseFactory.MakeHttpResponse(Const.HttpResponseFactory.ResponseCodes.ClientError.CODE_TOO_MANY_REQUESTS, Const.DataCollection.MessageCodes.UploadLocal.RATE_LIMIT_EXCEEDED) # Creating a localPost and saving it in the DB # Create a new LocalPost and populate the fields from the Json newPost = LocalPost(fromUser=clientUser, latitude=clientLatitude, longitude=clientLongitude, text=clientPostText, url=clientPostURL, contentType=Const.Tags.ContentTypes.LOCALPOST, arn=clientARN) # If there is an exception, roll back this db transaction # Save the post in the database with transaction.atomic(): newPost.save() # log and return on success DataCollector.logURL(TAG, { Const.DataCollection.ParamNames.RESPONSE_CODE: Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK, Const.DataCollection.ParamNames.MESSAGE_CODE: Const.DataCollection.MessageCodes.UploadLocal.POST_SUCCESSFUL, Const.DataCollection.ParamNames.FROM_USER: Utils.ConvertBinaryToUUID(clientUser.uuid), Const.DataCollection.ParamNames.LATITUDE: clientLatitude, Const.DataCollection.ParamNames.LONGITUDE: clientLongitude, Const.DataCollection.ParamNames.HAS_TEXT: (not Utils.StringIsEmpty(clientPostText)) }) return HttpResponseFactory.MakeHttpResponse(Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK, Const.DataCollection.MessageCodes.UploadLocal.POST_SUCCESSFUL) except Exception as e: # log and return on error DataCollector.logServerError(e) DataCollector.logURL(TAG, { Const.DataCollection.ParamNames.RESPONSE_CODE: Const.HttpResponseFactory.ResponseCodes.ServerError.CODE_INTERNAL_SERVER_ERROR, Const.DataCollection.ParamNames.MESSAGE_CODE: Const.DataCollection.MessageCodes.UploadLocal.POST_FAILED_SERVER_ERROR, Const.DataCollection.ParamNames.FROM_USER: Utils.ConvertBinaryToUUID(clientUser.uuid), Const.DataCollection.ParamNames.LATITUDE: clientLatitude, Const.DataCollection.ParamNames.LONGITUDE: clientLongitude, Const.DataCollection.ParamNames.HAS_TEXT: (not Utils.StringIsEmpty(clientPostText)) }) return HttpResponseFactory.MakeHttpResponse(Const.HttpResponseFactory.ResponseCodes.ServerError.CODE_INTERNAL_SERVER_ERROR, Const.DataCollection.MessageCodes.UploadLocal.POST_FAILED_SERVER_ERROR)
def GetLocalPost(requestData): TAG = Const.Tags.Urls.GET_LOCAL securityProperties = RunThroughSecurityLayer(TAG, requestData) if (not securityProperties.isSecure): return securityProperties.httpResponse try: clientUser = securityProperties.clientUserObject clientNumPostsRequested = securityProperties.jsonRequestData[Const.Views.GetLocalPost.JsonRequestKey.COUNT] clientSeenPosts = securityProperties.jsonRequestData[Const.Views.GetLocalPost.JsonRequestKey.SEEN] clientLatitude = securityProperties.jsonRequestData[Const.Views.GetLocalPost.JsonRequestKey.LATITUDE] clientLongitude = securityProperties.jsonRequestData[Const.Views.GetLocalPost.JsonRequestKey.LONGITUDE] localPosts = None # If the list images the client has seen is empty, # set the list to be non-empty with a dummy value of 0. # This prevents the query from breaking. if not clientSeenPosts: clientSeenPosts = [0] # Get a list of users that this user has blocked on local blockedUserList = _GetBlockedUsersList(clientUser) # Run the local algo. localPosts = _RunLocalAlgorithm(clientID=clientUser.id, latitude=clientLatitude, longitude=clientLongitude, numOfPostsRequested=clientNumPostsRequested, postsToExclude=clientSeenPosts, blockedUsers=blockedUserList) # Package the localPosts # Iterate over the results set. Make 'LocalPostClientObject' out of each result # Add these objects to a list as a dictionary. Json stringify the whole list, # using simplejson. clientPostListToReturn = [] for lp in localPosts: clientPostToReturn = _GetLocalPostClientObject(postID=lp.id, fromUser=str(ConvertBinaryToUUID(lp.fromUser_uuid)), timeCreated=lp.timeCreated, latitude=lp.latitude, longitude=lp.longitude, weight=lp.weight, text=lp.text, url=lp.url, arn=lp.arn) clientPostListToReturn.append(clientPostToReturn.getOrderedDict()) jsonString = json.dumps(clientPostListToReturn) # log and return on success DataCollector.logURL(TAG, { Const.DataCollection.ParamNames.RESPONSE_CODE: Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK, Const.DataCollection.ParamNames.MESSAGE_CODE: Const.DataCollection.MessageCodes.GetLocal.REQUEST_SUCCESSFUL, Const.DataCollection.ParamNames.FROM_USER: Utils.ConvertBinaryToUUID(clientUser.uuid), Const.DataCollection.ParamNames.LATITUDE: clientLatitude, Const.DataCollection.ParamNames.LONGITUDE: clientLongitude, Const.DataCollection.ParamNames.NUM_IMAGES_REQUESTED: clientNumPostsRequested, Const.DataCollection.ParamNames.NUM_IMAGES_SERVED: len(list(localPosts)) }) return HttpResponseFactory.MakeHttpResponse(Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK, jsonString, 'application/json') except Exception as e: # log and return on error DataCollector.logServerError(e) DataCollector.logURL(TAG, { Const.DataCollection.ParamNames.RESPONSE_CODE: Const.HttpResponseFactory.ResponseCodes.ServerError.CODE_INTERNAL_SERVER_ERROR, Const.DataCollection.ParamNames.MESSAGE_CODE: Const.DataCollection.MessageCodes.GetLocal.REQUEST_FAILED_SERVER_ERROR, Const.DataCollection.ParamNames.FROM_USER: Utils.ConvertBinaryToUUID(clientUser.uuid), Const.DataCollection.ParamNames.LATITUDE: clientLatitude, Const.DataCollection.ParamNames.LONGITUDE: clientLongitude, Const.DataCollection.ParamNames.NUM_IMAGES_REQUESTED: clientNumPostsRequested, Const.DataCollection.ParamNames.NUM_IMAGES_SERVED: 0 }) return HttpResponseFactory.MakeHttpResponse(Const.HttpResponseFactory.ResponseCodes.ServerError.CODE_INTERNAL_SERVER_ERROR, Const.DataCollection.MessageCodes.GetLocal.REQUEST_FAILED_SERVER_ERROR)
def Block(requestData): TAG = Const.Tags.Urls.MODERATION_BLOCK securityProperties = RunThroughSecurityLayer(TAG, requestData) if (not securityProperties.isSecure): return securityProperties.httpResponse try: clientUser = securityProperties.clientUserObject clientBlockUser = securityProperties.jsonRequestData[ Const.Views.Block.JsonRequestKey.USER_TO_BLOCK_ID] # Try to find the user to block in the Db try: blockedUser = User.objects.get( uuid=Utils.ConvertUUIDToBinary(clientBlockUser)) except ObjectDoesNotExist: DataCollector.logURL( TAG, { Const.DataCollection.ParamNames.RESPONSE_CODE: Const.HttpResponseFactory.ResponseCodes.ClientError. CODE_UNPROCESSABLE_ENTITY, Const.DataCollection.ParamNames.MESSAGE_CODE: Const.DataCollection.MessageCodes.ModerationBlock. TARGET_USER_NOT_FOUND, Const.DataCollection.ParamNames.BLOCKED_USER: clientBlockUser, Const.DataCollection.ParamNames.BLOCKER_USER: Utils.ConvertBinaryToUUID(clientUser.uuid) }) return HttpResponseFactory.MakeHttpResponse( Const.HttpResponseFactory.ResponseCodes.ClientError. CODE_UNPROCESSABLE_ENTITY, Const.DataCollection.MessageCodes. ModerationBlock.TARGET_USER_NOT_FOUND) # Make sure that this block does not already exist block = Block.objects.filter(blockerUser=clientUser, blockedUser=blockedUser) if block: DataCollector.logURL( TAG, { Const.DataCollection.ParamNames.RESPONSE_CODE: Const.HttpResponseFactory.ResponseCodes.ClientError. CODE_CONFLICT, Const.DataCollection.ParamNames.MESSAGE_CODE: Const.DataCollection.MessageCodes.ModerationBlock. BLOCK_EXISTS, Const.DataCollection.ParamNames.BLOCKED_USER: clientBlockUser, Const.DataCollection.ParamNames.BLOCKER_USER: Utils.ConvertBinaryToUUID(clientUser.uuid) }) return HttpResponseFactory.MakeHttpResponse( Const.HttpResponseFactory.ResponseCodes.ClientError. CODE_CONFLICT, Const.DataCollection.MessageCodes.ModerationBlock.BLOCK_EXISTS) # Save the block in the DB # If there is an exception, roll back this db transaction with transaction.atomic(): Block.objects.create(blockerUser=clientUser, blockedUser=blockedUser) # log and return on success DataCollector.logURL( TAG, { Const.DataCollection.ParamNames.RESPONSE_CODE: Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK, Const.DataCollection.ParamNames.MESSAGE_CODE: Const.DataCollection.MessageCodes.ModerationBlock. REQUEST_SUCCESSFUL, Const.DataCollection.ParamNames.BLOCKED_USER: clientBlockUser, Const.DataCollection.ParamNames.BLOCKER_USER: Utils.ConvertBinaryToUUID(clientUser.uuid) }) return HttpResponseFactory.MakeHttpResponse( Const.HttpResponseFactory.ResponseCodes.Success.CODE_OK, Const. DataCollection.MessageCodes.ModerationBlock.REQUEST_SUCCESSFUL) except Exception as e: DataCollector.logServerError(e) DataCollector.logURL( TAG, { Const.DataCollection.ParamNames.RESPONSE_CODE: Const.HttpResponseFactory.ResponseCodes.ServerError. CODE_INTERNAL_SERVER_ERROR, Const.DataCollection.ParamNames.MESSAGE_CODE: Const.DataCollection.MessageCodes.ModerationBlock. REQUEST_FAILED_SERVER_ERROR, Const.DataCollection.ParamNames.BLOCKED_USER: clientBlockUser, Const.DataCollection.ParamNames.BLOCKER_USER: Utils.ConvertBinaryToUUID(clientUser.uuid) }) return HttpResponseFactory.MakeHttpResponse( Const.HttpResponseFactory.ResponseCodes.ServerError. CODE_INTERNAL_SERVER_ERROR, Const.DataCollection.MessageCodes. ModerationBlock.REQUEST_FAILED_SERVER_ERROR)