public ActionResult DeleteFriendship(int id) { var otherUser = dbProvider.GetUserByID(id, false); if (otherUser == null) { return(HttpNotFound()); } dbProvider.DeleteFriendship(SessionData.User, new DB.Entities.User { Id = id }); dbProvider.DeleteFriendship(new DB.Entities.User { Id = id }, SessionData.User); MessengerHub.CallRefresh(otherUser.RtID); return(Content("OK")); }
public ActionResult Accept(int?otherId) { if (otherId == null) { return(HttpNotFound()); } var request = dbProvider.GetRequestBetween(new DB.Entities.User { Id = (int)otherId }, SessionData.User); if (request == null) { return(HttpNotFound()); } request.Resolved = true; var group = dbProvider.CreateGroup(new DB.Entities.Group { Binary = true, IsAdmin = false, Name = null, Picture = null, PictureType = null, Timestamp = DateTime.Now }); dbProvider.AddUsersToGroup(new List <DB.Entities.User>() { SessionData.User, new DB.Entities.User { Id = (int)otherId } }, group, false); dbProvider.AddFriendship(SessionData.User, new DB.Entities.User { Id = (int)otherId }); dbProvider.DeleteFriendRequest(request); var other = dbProvider.GetUserByID((int)otherId, false); MessengerHub.CallRefresh(other.RtID); return(Content("OK")); }
public ActionResult SendRequest(int?id) { if (id == null) { return(HttpNotFound()); } if (id == SessionData.User.Id) { return(HttpNotFound()); } var other = dbProvider.GetUserByID((int)id, false); if (other == null) { return(HttpNotFound()); } if (dbProvider.GetSentFriendRequests(SessionData.User).Any(x => x.ReceiverId == id) || dbProvider.GetUnresolvedFriendRequests(SessionData.User).Any(x => x.SenderId == id)) { return(HttpNotFound()); } if (dbProvider.GetFriends(SessionData.User).Any(x => x.Id == id)) { return(HttpNotFound()); } dbProvider.CreateFriendRequest(new DB.Entities.FriendRequest { ReceiverId = (int)id, Resolved = false, Seen = false, SenderId = SessionData.User.Id, Timestamp = DateTime.Now }); MessengerHub.CallRefresh(other.RtID); return(Content("OK")); }
public ActionResult Edit(GroupModel model) { if (ModelState.IsValid) { if (model.Id == null) { if (model.Picture == null) { return(Content("FAIL:Niste poslali sliku")); } var image = ImageManipulation.LoadImage(model.Picture.InputStream); if (image == null) { return(Content("FAIL:Niste poslali sliku")); } image = ImageManipulation.ResizeImage(image, 160, 160); var group = dbProvider.CreateGroup(new DB.Entities.Group { Binary = false, Name = model.Name, Timestamp = DateTime.Now, Picture = ImageManipulation.StoreImage(image), PictureType = ImageManipulation.OutputImageFormat, }); var users = model.GetMembers(); var friends = dbProvider.GetFriends(SessionData.User).Union(dbProvider.GetUsersInGroup(group)); foreach (var user in users) { if (user.Id == SessionData.User.Id || friends.All(x => x.Id != user.Id)) { return(HttpNotFound()); } } dbProvider.AddUsersToGroup(users, group, false); dbProvider.AddUsersToGroup(new List <DB.Entities.User> { SessionData.User }, group, true); dbProvider.CommitIfNecessary(); foreach (var user in users) { MessengerHub.CallRefresh(friends.First(x => x.Id == user.Id).RtID); } return(Content("OK")); } else { Image picture = null; if (model.Picture != null) { picture = ImageManipulation.LoadImage(model.Picture.InputStream); if (picture == null) { return(Content("FAIL:Niste poslali sliku")); } picture = ImageManipulation.ResizeImage(picture, 160, 160); } var group = dbProvider.GetGroupsForUser(SessionData.User).FirstOrDefault(x => x.Id == model.Id); if (group == null || !(bool)group.IsAdmin) { return(HttpNotFound()); } var oldMembers = dbProvider.GetUsersInGroup(group); var newMembers = model.GetMembers(); var friends = dbProvider.GetFriends(SessionData.User).Union(dbProvider.GetUsersInGroup(group)); foreach (var user in newMembers) { if (user.Id == SessionData.User.Id || friends.All(x => x.Id != user.Id)) { return(HttpNotFound()); } } newMembers.Add(SessionData.User); List <User> deletedMembers = new List <User>(); List <User> addedMembers = new List <User>(); foreach (var member in oldMembers) { if (!newMembers.Any(x => x.Id == member.Id)) { deletedMembers.Add(member); } } foreach (var member in newMembers) { if (!oldMembers.Any(x => x.Id == member.Id)) { addedMembers.Add(member); } } dbProvider.RemoveUsersFromGroup(deletedMembers, group); dbProvider.AddUsersToGroup(addedMembers, group, false); if (model.Name != group.Name || model.Picture != null) { if (model.Name != group.Name) { group.Name = model.Name; } if (picture != null) { group.Picture = ImageManipulation.StoreImage(picture); } dbProvider.UpdateGroup(group, picture != null); } dbProvider.CommitIfNecessary(); foreach (var member in deletedMembers) { MessengerHub.CallRefresh(member.RtID); } foreach (var member in addedMembers) { MessengerHub.CallRefresh(friends.First(x => x.Id == member.Id).RtID); } return(Content("OK")); } } else { var x = ModelValidationErrors; return(Content("FAIL:" + string.Join(", ", ModelValidationErrors))); } }