Beispiel #1
0
        void SqlFriendRequestsTableDependencyChanged(object sender, RecordChangedEventArgs <FriendRequest> e)
        {
            if (e.ChangeType == ChangeType.Insert)
            {
                var          userProfileActor  = _userProfileService.GetUserProfile(e.Entity.InitiatorUserProfileId);
                var          friendRequest     = _friendRequestService.GetFriendRequest(e.Entity.Id);
                var          userProfileTarget = _userProfileService.GetUserProfile(friendRequest.TargetUserProfileId);
                Notification notification      = null;

                notification = _notificationService.AddFriendRequestNotification(userProfileTarget.Id, userProfileActor.Name + " wants to be friends.",
                                                                                 "", userProfileActor.Name + " wants to be friends.", "/friend-requests/" + friendRequest.Id, e.Entity.Id.ToString());

                if (notification != null)
                {
                    LikeWatcherHub.PushNotification(notification, userProfileTarget.Id);
                }
            }
        }
        public JsonResult RespondToFriendRequest()
        {
            if (Request.Form["response"] != null && (Request.Form["initiatorProfileId"] != null || Request.Form["notificationId"] != null))
            {
                var           currentUserId  = User.Identity.GetUserId();
                var           currentProfile = _userProfileService.GetUserProfileByUserId(new Guid(currentUserId));
                FriendRequest friendRequest  = null;
                if ((Request.Form["initiatorProfileId"] == null || Request.Form["initiatorProfileId"] == "") && Request.Form["notificationId"] != null)
                {
                    //we get the friend request id from the notification
                    if (Int32.TryParse(Request.Form["notificationId"], out var notifId))
                    {
                        var notification = _notificationService.GetNotification(notifId);
                        if (Int32.TryParse(notification.EntityId, out var frId))
                        {
                            friendRequest = _friendRequestService.GetFriendRequest(frId);
                        }
                        else
                        {
                            return(Json(new { Message = "invalid friend request id" }));
                        }
                    }
                    else
                    {
                        return(Json(new { Message = "invalid notification id" }));
                    }
                }
                else
                {
                    friendRequest = _friendRequestService.CheckIfFriendRequestExists(new Guid(Request.Form["initiatorProfileId"]), currentProfile.Id);
                }
                if (friendRequest != null)
                {
                    var initiatorProfileId = friendRequest.InitiatorUserProfileId;
                    if (!short.TryParse(Request.Form["response"], out var response))
                    {
                        response = 0;
                    }

                    if (response == 1 || response == 2)
                    {
                        //we have a valid response
                        _friendRequestService.MarkFriendRequestAsUsed(friendRequest.Id, response);
                        if (Request.Form["notificationId"] != null)
                        {
                            // in case this request comes from a notification
                            _notificationService.MarkNotificationAsSeen(Request.Form["notificationId"]);
                        }

                        if (response == 1)
                        {
                            //friend request accepted , add user profile as friend
                            _friendsService.AddFriend(currentProfile.Id, initiatorProfileId);
                            return(Json(new { Message = "friend added" }));
                        }
                        else
                        {
                            return(Json(new { Message = "friend request denied" }));
                        }
                    }
                    else
                    {
                        return(Json(new { Message = "invalid response" }));
                    }
                }
                else
                {
                    return(Json(new { Message = "no friend request found" }));
                }
            }
            else
            {
                return(Json(new { Message = "missing response" }));
            }
        }