Beispiel #1
0
        public string FriendControl(string targetUserId, int type)
        {
            string result = "";
            var since = DateTime.UtcNow;
            var curId = CurrentUserID;
            var myFriend = db.Friends.FirstOrDefault(x => x.FriendsListId == curId && x.FriendId == targetUserId);
            var hisFriend = db.Friends.FirstOrDefault(x => x.FriendsListId == targetUserId && x.FriendId == curId);

            var f = new Friend()
            {
                FriendsListId = curId,
                FriendId = targetUserId,
                FriendStatus = false
            };
            switch ((FriendType)type)
            {
                case FriendType.NoneFriend:
                    try
                    {
                        if (myFriend == null && hisFriend == null)
                        {
                            db.Friends.Add(f);
                            db.SaveChanges();
                            result = "Pending";
                            FriendToast(targetUserId);
                        }
                        else
                        {
                            result = ActionResults.Failed.ToString();
                        }
                    }
                    catch (Exception exception)
                    {
                        Helper.WriteLog(exception);
                    }
                    break;
                case FriendType.HisPendingFriend:
                    try
                    {
                        if (hisFriend != null && myFriend == null)
                        {
                            hisFriend.FriendStatus = true;
                            hisFriend.FriendSince = since;
                            f.FriendStatus = true;
                            f.FriendSince = since;
                            db.Friends.Add(f);
                            db.SaveChanges();
                            result = "Friend";
                        }
                        else
                        {
                            result = ActionResults.Failed.ToString();
                        }
                    }
                    catch (Exception exception)
                    {
                        Helper.WriteLog(exception);
                    }
                    break;

                case FriendType.MyPendingFriend:
                case FriendType.Friend:
                    try
                    {
                        result = myFriend != null ? "Remove" : ActionResults.Failed.ToString();
                    }
                    catch (Exception exception)
                    {
                        Helper.WriteLog(exception);
                    }
                    break;
            }
            return result;
        }
Beispiel #2
0
        public string ProcessFriendRequest(string friendId, string isAccept)
        {
            try
            {
                var hisFriend =
                    db.Friends.First(
                        x => x.FriendsListId.Equals(friendId, StringComparison.InvariantCultureIgnoreCase)
                        && x.FriendId == CurrentUserID);

                if (hisFriend != null)
                {
                    if (isAccept.Equals("1"))
                    {
                        var since = DateTime.UtcNow;
                        var hisUsername = db.Users.First(x => x.UserID == friendId).UserName;

                        hisFriend.FriendStatus = true;
                        hisFriend.FriendSince = since;

                        var entry = db.Entry(hisFriend);
                        entry.Property(x => x.FriendStatus).IsModified = true;
                        entry.Property(x => x.FriendSince).IsModified = true;

                        var myFriend = new Friend
                        {
                            FriendsListId = CurrentUserID,
                            FriendStatus = true,
                            FriendId = friendId,
                            FriendSince = since
                        };
                        db.Friends.Add(myFriend);
                        db.SaveChanges();

                        PushNotification(friendId, CurrentUserName, (int)NotificationType.AcceptFriendRequest);
                        PushNotification(CurrentUserID, hisUsername, (int)NotificationType.AcceptFriendRequest);
                    }
                    else
                    {
                        db.Friends.Remove(hisFriend);
                        db.SaveChanges();

                        PushNotification(friendId, CurrentUserName, (int)NotificationType.CancelFriendRequest);
                    }
                }
                else
                {
                    return ActionResults.Deleted.ToString();
                }

                //create sub hub context
                var hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();

                var data = db.Friends.Where(x => x.FriendId == CurrentUserID && !x.FriendStatus).Select(item => new FriendViewModel
                {
                    FriendId = item.FriendsListId,
                    Name = item.FriendList.User.FirstName + " " + item.FriendList.User.LastName,
                    ProfileImgUrl = UrlImage("avatar", item.FriendsListId),
                    UserName = item.FriendList.User.UserName
                }).ToList();

                var html = RenderPartialViewToString("FriendRequest", data);
                hubContext.Clients.All.updateRequest(CurrentUserID, html);

                return ActionResults.Succeed.ToString();
            }
            catch (Exception exception)
            {
                Helper.WriteLog(exception);
                return ActionResults.Failed.ToString();
            }
        }