Beispiel #1
0
        /// <summary>
        /// Gets a list of the most reported users
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> GetUsers(ActualRequest actualRequest)
        {
            List <int> paginationInts     = JsonSerializer.Deserialize <List <int> >(actualRequest.Request.Argument.ToString());
            List <UserShortVersion> users = await adminRepo.GetAdminUsersAsync(paginationInts[0], paginationInts[1]);

            Request request = new Request
            {
                ActionType = ActionType.ADMIN_GET_USERS.ToString(),
                Argument   = JsonSerializer.Serialize(users)
            };
            List <byte[]> userAvatars = new List <byte[]>();

            if (users != null && users.Count > 0)
            {
                foreach (var user in users)
                {
                    try {
                        var readAvatarFile = File.ReadAllBytes($"{FILE_PATH}/Users/{user.UserId}/avatar.jpg");
                        userAvatars.Add(ImagesUtil.ResizeImage(readAvatarFile, 50, 50));
                    }
                    catch (Exception e) {
                        Console.WriteLine("No avatar found for user " + user.UserId);
                    }
                }
            }
            return(new ActualRequest
            {
                Request = request,
                Images = userAvatars
            });
        }
Beispiel #2
0
        /// <summary>
        /// Retrieves a user short version instance with the given user id
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private ActualRequest GetUserShortVersionById(ActualRequest actualRequest)
        {
            Request          request  = actualRequest.Request;
            int              userId   = Convert.ToInt32(request.Argument.ToString());
            UserShortVersion user     = userRepo.GetUserShortVersionById(userId);
            Request          response = new Request
            {
                ActionType = ActionType.USER_GET_SV_BY_ID.ToString(),
                Argument   = JsonSerializer.Serialize(user)
            };
            List <byte[]> userAvatars = new List <byte[]>();

            if (user != null)
            {
                try {
                    var readAvatarFile = File.ReadAllBytes($"{FILE_PATH}/Users/{user.UserId}/avatar.jpg");
                    userAvatars.Add(ImagesUtil.ResizeImage(readAvatarFile, 20, 20));
                }
                catch (Exception e) {
                    Console.WriteLine("No avatar found for user " + user.UserId);
                }
            }
            return(new ActualRequest
            {
                Request = response,
                Images = userAvatars
            });
        }
Beispiel #3
0
        /// <summary>
        /// Retrieves the last messages for a user with his friends/following gyms for unit testing
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private ActualRequest GetLastMessagesForUser(ActualRequest actualRequest)
        {
            List <int> integers = JsonSerializer.Deserialize <List <int> >(actualRequest.Request.Argument.ToString());
            List <UserShortVersionWithMessage> users = chatRepo.GetLastMessagesForUser(integers[0], integers[1]);
            Request response = new Request
            {
                ActionType = ActionType.MESSAGE_GET_LATEST.ToString(),
                Argument   = JsonSerializer.Serialize(users)
            };
            List <byte[]> userAvatars = new List <byte[]>();

            if (users != null && users.Count > 0)
            {
                foreach (var user in users)
                {
                    if (user.Message.HasImage)
                    {
                        user.Message.Content = "<Image>";
                    }
                    try {
                        var readAvatarFile = File.ReadAllBytes($"{FILE_PATH}/Users/{user.UserId}/avatar.jpg");
                        userAvatars.Add(ImagesUtil.ResizeImage(readAvatarFile, 20, 20));
                    }
                    catch (Exception e) {
                        Console.WriteLine("No avatar found for user " + user.UserId);
                    }
                }
            }
            return(new ActualRequest
            {
                Request = response,
                Images = userAvatars
            });
        }
Beispiel #4
0
        /// <summary>
        /// Deletes a user with a given id
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> DeleteUserAsync(ActualRequest actualRequest)
        {
            Request request = actualRequest.Request;
            int     userId  = Convert.ToInt32(request.Argument.ToString());

            Console.WriteLine("Deleting user with id " + userId);
            List <int> postIdsForUser = userRepo.GetPostIdsForUser(userId);
            bool       response       = await userRepo.DeleteUserAsync(userId);

            if (response)
            {
                ImagesUtil.DeleteUserFolder($"{FILE_PATH}/Users/{userId}");
                foreach (var postId in postIdsForUser)
                {
                    ImagesUtil.DeleteFile($"{FILE_PATH}/Posts", $"{postId}.jpg");
                }
            }

            Request responseRequest = new Request
            {
                ActionType = ActionType.USER_DELETE.ToString(),
                Argument   = JsonSerializer.Serialize(response)
            };

            return(new ActualRequest
            {
                Request = responseRequest,
                Images = null
            });
        }
Beispiel #5
0
        /// <summary>
        /// Edits a given user
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> UpdateUserAsync(ActualRequest actualRequest)
        {
            Request request = actualRequest.Request;

            UserSocketsModel user   = JsonSerializer.Deserialize <UserSocketsModel>(request.Argument.ToString());
            bool             result = await userRepo.EditUserAsync(user);

            Request requestResponse = new Request
            {
                ActionType = ActionType.USER_EDIT.ToString(),
                Argument   = JsonSerializer.Serialize(result)
            };

            if (actualRequest.Images != null && actualRequest.Images.Any())
            {
                if (user.Email != null)
                {
                    ImagesUtil.WriteImageToPath(actualRequest.Images[0], $"{FILE_PATH}/Users/{user.Id}", "/avatar.jpg");
                }
                else
                {
                    ImagesUtil.WriteImageToPath(actualRequest.Images[0], $"{FILE_PATH}/Users/{user.Id}",
                                                "/background.jpg");
                }
            }

            return(new ActualRequest
            {
                Request = requestResponse,
                Images = null
            });
        }
Beispiel #6
0
        /// <summary>
        /// Retrieves a list of the target user's friends, or common friends with the sender
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private ActualRequest GetFriendsForUser(ActualRequest actualRequest)
        {
            List <int> integers = JsonSerializer.Deserialize <List <int> >(actualRequest.Request.Argument.ToString());
            List <UserShortVersionWithStatus> friends = userRepo.GetFriendsForUser(integers[0], integers[1], integers[2]);
            Request response = new Request
            {
                ActionType = ActionType.USER_GET_FRIENDS.ToString(),
                Argument   = JsonSerializer.Serialize(friends)
            };
            List <byte[]> userAvatars = new List <byte[]>();

            if (friends != null && friends.Count > 0)
            {
                foreach (var friend in friends)
                {
                    try {
                        var readAvatarFile = File.ReadAllBytes($"{FILE_PATH}/Users/{friend.UserId}/avatar.jpg");
                        userAvatars.Add(ImagesUtil.ResizeImage(readAvatarFile, 20, 20));
                    }
                    catch (Exception e) {
                        Console.WriteLine("No avatar found for user " + friend.UserId);
                    }
                }
            }
            return(new ActualRequest
            {
                Request = response,
                Images = userAvatars
            });
        }
Beispiel #7
0
        /// <summary>
        /// Returns a list with all the gyms in a given city
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private ActualRequest GetGymsByCity(ActualRequest actualRequest)
        {
            string city = actualRequest.Request.Argument.ToString();
            List <UserShortVersion> users = userRepo.GetAllGymsInCity(city);
            Request response = new Request
            {
                ActionType = ActionType.USER_GET_GYMS.ToString(),
                Argument   = JsonSerializer.Serialize(users)
            };
            List <byte[]> userAvatars = new List <byte[]>();

            if (users != null && users.Count > 0)
            {
                foreach (var user in users)
                {
                    try {
                        var readAvatarFile = File.ReadAllBytes($"{FILE_PATH}/Users/{user.UserId}/avatar.jpg");
                        userAvatars.Add(ImagesUtil.ResizeImage(readAvatarFile, 20, 20));
                    }
                    catch (Exception e) {
                        Console.WriteLine("No avatar found for user " + user.UserId);
                    }
                }
            }
            return(new ActualRequest
            {
                Request = response,
                Images = userAvatars
            });
        }
Beispiel #8
0
        /// <summary>
        /// Persists a given message to the database and its corresponding notification
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> AddMessageAsync(ActualRequest actualRequest)
        {
            Request             request = actualRequest.Request;
            MessageSocketsModel message = JsonSerializer.Deserialize <MessageSocketsModel>(request.Argument.ToString());
            List <int>          ints    = await chatRepo.AddMessageAsync(message);

            int     messageId       = ints[0];
            Request requestResponse = new Request
            {
                ActionType = ActionType.MESSAGE_CREATE.ToString(),
                Argument   = JsonSerializer.Serialize(ints)
            };

            if (messageId > 0)
            {
                if (message.HasImage && actualRequest.Images != null)
                {
                    try {
                        ImagesUtil.WriteImageToPath(actualRequest.Images[0], $"{FILE_PATH}/Messages", $"/{messageId}.jpg");
                    }
                    catch (Exception e) {
                        Console.WriteLine("Could not add image to created message " + messageId);
                    }
                }
            }

            return(new ActualRequest
            {
                Request = requestResponse,
                Images = null
            });
        }
Beispiel #9
0
        /// <summary>
        /// Gets a list with all the online friends belonging to a given user
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private ActualRequest GetOnlineFriendsForUser(ActualRequest actualRequest)
        {
            Request request = actualRequest.Request;
            int     userId  = Convert.ToInt32(request.Argument.ToString());
            List <UserShortVersion> onlineFriends = userRepo.GetOnlineFriendsForUser(userId);
            Request response = new Request
            {
                ActionType = ActionType.USER_GET_ONLINE_FRIENDS.ToString(),
                Argument   = JsonSerializer.Serialize(onlineFriends)
            };
            List <byte[]> userAvatars = new List <byte[]>();

            if (onlineFriends != null && onlineFriends.Count > 0)
            {
                foreach (var friend in onlineFriends)
                {
                    try {
                        var readAvatarFile = File.ReadAllBytes($"{FILE_PATH}/Users/{friend.UserId}/avatar.jpg");
                        userAvatars.Add(ImagesUtil.ResizeImage(readAvatarFile, 20, 20));
                    }
                    catch (Exception e) {
                        Console.WriteLine("No avatar found for user " + friend.UserId);
                    }
                }
            }
            return(new ActualRequest
            {
                Request = response,
                Images = userAvatars
            });
        }
Beispiel #10
0
        /// <summary>
        /// Returns a list with all the comments belonging to a given post, in reverse chronological order
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> GetAllCommentsForPostAsync(ActualRequest actualRequest)
        {
            Request request = actualRequest.Request;
            int     postId  = Convert.ToInt32(request.Argument.ToString());
            List <CommentSockets> comments = await postRepo.GetAllCommentsForPost(postId);

            Request response = new Request
            {
                ActionType = ActionType.POST_GET_COMMENTS.ToString(),
                Argument   = JsonSerializer.Serialize(comments)
            };
            List <byte[]> userAvatars = new List <byte[]>();

            if (comments != null && comments.Count > 0)
            {
                foreach (var comment in comments)
                {
                    try {
                        var readAvatarFile = File.ReadAllBytes($"{FILE_PATH}/Users/{comment.Owner.UserId}/avatar.jpg");
                        userAvatars.Add(ImagesUtil.ResizeImage(readAvatarFile, 20, 20));
                    }
                    catch (Exception e) {
                        Console.WriteLine("No avatar found for user ");
                    }
                }
            }
            return(new ActualRequest
            {
                Request = response,
                Images = userAvatars
            });
        }
Beispiel #11
0
        /// <summary>
        /// Persists a given post to the database
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> AddPostAsync(ActualRequest actualRequest)
        {
            Request          request = actualRequest.Request;
            PostShortVersion post    = JsonSerializer.Deserialize <PostShortVersion>(request.Argument.ToString());

            post.HasImage = actualRequest.Images != null;
            Console.WriteLine("Post Sockets adding post " + post.Title);
            int result = await postRepo.AddPostAsync(post);

            Request responseRequest = new Request
            {
                ActionType = ActionType.POST_CREATE.ToString(),
                Argument   = JsonSerializer.Serialize(result)
            };

            if (result > 0)
            {
                if (post.HasImage)
                {
                    try {
                        ImagesUtil.WriteImageToPath(actualRequest.Images[0], $"{FILE_PATH}/Posts", $"/{result}.jpg");
                    }
                    catch (Exception e) {
                        Console.WriteLine("Could not add image to created post " + result);
                    }
                }
            }
            return(new ActualRequest
            {
                Request = responseRequest,
                Images = null
            });
        }
Beispiel #12
0
        /// <summary>
        /// Returns a list with all the users who reacted to a given post, in alphabetical order
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private ActualRequest GetAllLikesForPostAsync(ActualRequest actualRequest)
        {
            Request request = actualRequest.Request;
            int     postId  = Convert.ToInt32(request.Argument.ToString());
            List <UserShortVersion> users = postRepo.GetAllLikesForPost(postId);
            Request response = new Request
            {
                ActionType = ActionType.POST_GET_LIKES.ToString(),
                Argument   = JsonSerializer.Serialize(users)
            };
            List <byte[]> userAvatars = new List <byte[]>();

            if (users != null && users.Count > 0)
            {
                foreach (var user in users)
                {
                    try {
                        var readAvatarFile = File.ReadAllBytes($"{FILE_PATH}/Users/{user.UserId}/avatar.jpg");
                        userAvatars.Add(ImagesUtil.ResizeImage(readAvatarFile, 20, 20));
                    }
                    catch (Exception e) {
                        Console.WriteLine("No avatar found for user " + user.UserId);
                    }
                }
            }
            return(new ActualRequest
            {
                Request = response,
                Images = userAvatars
            });
        }
Beispiel #13
0
        /// <summary>
        /// Deletes a post with a given id
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> DeletePostAsync(ActualRequest actualRequest)
        {
            Request request = actualRequest.Request;
            int     postId  = Convert.ToInt32(request.Argument.ToString());

            Console.WriteLine("Deleting post with id " + postId);
            bool response = await postRepo.DeletePostAsync(postId);

            if (response)
            {
                ImagesUtil.DeleteFile($"{FILE_PATH}/Posts", $"{postId}.jpg");
            }

            Request responseRequest = new Request
            {
                ActionType = ActionType.POST_DELETE.ToString(),
                Argument   = JsonSerializer.Serialize(response)
            };

            return(new ActualRequest
            {
                Request = responseRequest,
                Images = null
            });
        }
Beispiel #14
0
        /// <summary>
        /// Edits a given post
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> UpdatePostAsync(ActualRequest actualRequest)
        {
            Request          request     = actualRequest.Request;
            PostShortVersion updatedPost = JsonSerializer.Deserialize <PostShortVersion>(request.Argument.ToString());
            bool             result      = await postRepo.EditPostAsync(updatedPost);

            Request responseRequest = new Request
            {
                ActionType = ActionType.POST_EDIT.ToString(),
                Argument   = JsonSerializer.Serialize(result)
            };

            if (!result)
            {
                return new ActualRequest {
                           Request = responseRequest,
                           Images  = null
                }
            }
            ;

            if (actualRequest.Images != null && actualRequest.Images.Any())
            {
                ImagesUtil.WriteImageToPath(actualRequest.Images[0], $"{FILE_PATH}/Posts", $"/{updatedPost.Id}.jpg");
            }
            return(new ActualRequest {
                Request = responseRequest,
                Images = null
            });
        }
Beispiel #15
0
        /// <summary>
        /// Retrieves a post with its owner, by id and with the sender status regarding it
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> GetPostByIdAsync(ActualRequest actualRequest)
        {
            Request          request = actualRequest.Request;
            List <int>       ints    = JsonSerializer.Deserialize <List <int> >(request.Argument.ToString());
            PostShortVersion post    = await postRepo.GetPostByIdAsync(ints[0], ints[1]);

            Request responseRequest = new Request
            {
                ActionType = ActionType.POST_GET_BY_ID.ToString(),
                Argument   = JsonSerializer.Serialize(post)
            };
            List <byte[]> images = new List <byte[]>();

            if (post != null)
            {
                var readOwnerAvatar = File.ReadAllBytes($"{FILE_PATH}/Users/{post.Owner.UserId}/avatar.jpg");
                images.Add(ImagesUtil.ResizeImage(readOwnerAvatar, 20, 20));

                if (post.HasImage)
                {
                    var readAvatarFile = File.ReadAllBytes($"{FILE_PATH}/Posts/{post.Id}.jpg");
                    images.Add(readAvatarFile);
                }
            }
            return(new ActualRequest
            {
                Request = responseRequest,
                Images = images
            });
        }
Beispiel #16
0
        /// <summary>
        /// Retrieves a user by id, and his status regarding the sender
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> GetUserByIdAsync(ActualRequest actualRequest)
        {
            Request request = actualRequest.Request;

            Console.WriteLine("Get user argument " + request.Argument);
            List <int> userIds = JsonSerializer.Deserialize <List <int> >(request.Argument.ToString());

            Console.WriteLine("Retrieving user with id " + userIds[1] + " by " + userIds[0]);
            UserSocketsModel user = await userRepo.GetUserByIdAsync(userIds[0], userIds[1]);

            Request requestResponse = new Request
            {
                ActionType = ActionType.USER_GET_BY_ID.ToString(),
                Argument   = JsonSerializer.Serialize(user)
            };
            List <byte[]> images = new List <byte[]>();

            try
            {
                // /Images/users/{userId}/....
                // /Images/posts/{postId}/....
                var readAvatarFile = File.ReadAllBytes($"{FILE_PATH}/Users/{user.Id}/avatar.jpg");

                images.Add(ImagesUtil.ResizeImage(readAvatarFile, 200, 200));
                var readBackgroundFile = File.ReadAllBytes($"{FILE_PATH}/Users/{user.Id}/background.jpg");
                images.Add(readBackgroundFile);
            }
            catch (Exception e)
            {
                Console.WriteLine("No avatar found for user " + user.Id);
            }

            return(new ActualRequest
            {
                Request = requestResponse,
                Images = images
            });
        }
Beispiel #17
0
        /// <summary>
        /// Deletes a message with a given id by setting its content to null and deleting its picture
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> DeleteMessageAsync(ActualRequest actualRequest)
        {
            Request request   = actualRequest.Request;
            int     messageId = Convert.ToInt32(request.Argument.ToString());
            bool    response  = await chatRepo.DeleteMessageAsync(messageId);

            if (response)
            {
                ImagesUtil.DeleteFile($"{FILE_PATH}/Messages", $"{messageId}.jpg");
            }

            Request responseRequest = new Request
            {
                ActionType = ActionType.MESSAGE_DELETE.ToString(),
                Argument   = JsonSerializer.Serialize(response)
            };

            return(new ActualRequest
            {
                Request = responseRequest,
                Images = null
            });
        }
Beispiel #18
0
        /// <summary>
        /// Persists a given user to the database
        /// </summary>
        /// <param name="actualRequest">the client request to be handled</param>
        /// <returns>the response to the given request</returns>
        private async Task <ActualRequest> AddUserAsync(ActualRequest actualRequest)
        {
            Request          request    = actualRequest.Request;
            string           userAsJson = request.Argument.ToString();
            UserSocketsModel user       = JsonSerializer.Deserialize <UserSocketsModel>(userAsJson);

            Console.WriteLine("Server got register user " + JsonSerializer.Serialize(user));
            int result = await userRepo.AddUserAsync(user);

            bool    resultBool      = result >= 0;
            Request requestResponse = new Request
            {
                ActionType = ActionType.USER_REGISTER.ToString(),
                Argument   = JsonSerializer.Serialize(resultBool)
            };

            if (result >= 0)
            {
                try
                {
                    byte[] readDefaultAvatar = File.ReadAllBytes($"{FILE_PATH}/Users/defaultAvatar.jpg");
                    byte[] readDefaultBg     = File.ReadAllBytes($"{FILE_PATH}/Users/defaultBg.jpg");
                    ImagesUtil.WriteImageToPath(readDefaultAvatar, $"{FILE_PATH}/Users/{result}", "/avatar.jpg");
                    ImagesUtil.WriteImageToPath(readDefaultBg, $"{FILE_PATH}/Users/{result}", "/background.jpg");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Default avatar not found");
                }
            }

            return(new ActualRequest
            {
                Request = requestResponse,
                Images = null
            });
        }