public FollowUserReturn UnFollowUser(string unFollowedUsername, string followingId) { FollowUserReturn ret = new FollowUserReturn(); if (string.IsNullOrEmpty(unFollowedUsername) || string.IsNullOrEmpty(followingId)) { ret.LastFollowState = FollowState.Unfollowed; return(ret); } UserInfo uInfo = _context.Set <UserInfo>().AsNoTracking().Include(f => f.Followers).FirstOrDefault(f => f.UName == unFollowedUsername); FollowInfo finf = uInfo.Followers.FirstOrDefault(f => f.FollowerId == followingId && f.FollowedId == uInfo.AppUserId); if (finf == null) { ret.IsActionSucceed = false; ret.ErrorInformation.UserInformation = "You are not following this user"; ret.ErrorInformation.ErrorType = ErrorType.NoAction; ret.LastFollowState = FollowState.Unfollowed; return(ret); } finf.FollowState = FollowState.Unfollowed; ret.LastFollowState = FollowState.Unfollowed; _context.SetChild <FollowInfo>().Update(finf); if (_context.SaveChanges() != 0) { _userFollowCacheService.RemoveAllUserCache(followingId); ret.IsActionSucceed = true; } ; return(ret); }
public IActionResult UnFollowUser([FromBody] UserNameModel model) { FollowUserReturn ret = new FollowUserReturn(); //try //{ var user = HttpContext.User; Claim idClaim = User.FindFirst("sub"); ret = _userInfoDataService.UnFollowUser(model.Username, idClaim.Value); return(Ok(Json(ret))); //} //catch (System.Exception ex) //{ // ret.IsActionSucceed = false; // ret.ErrorInformation.UserInformation = ex.Message; // return Ok(Json(ret)); //} }
public IActionResult FollowUser([FromBody] UserNameModel model) { FollowUserReturn ret = new FollowUserReturn(); var user = HttpContext.User; Claim idClaim = User.FindFirst("sub"); Claim usernameClaim = User.FindFirst("nickname"); ret = _userInfoDataService.FollowUser(model.Username, idClaim.Value); if (ret.IsActionSucceed && !string.IsNullOrEmpty(ret.FollowedUserId)) { _bus.Publish(new UserFollowedUserAction() { DateUtcAction = DateTime.UtcNow, FollowedUserId = ret.FollowedUserId, FollowingUsername = usernameClaim.Value, FollowingUserId = idClaim.Value }); } return(Ok(Json(ret))); }
public FollowUserReturn FollowUser(string followedUsername, string followingId) { FollowUserReturn ret = new FollowUserReturn(); if (string.IsNullOrEmpty(followedUsername) || string.IsNullOrEmpty(followingId)) { ret.LastFollowState = FollowState.Unfollowed; ret.IsActionSucceed = false; return(ret); } UserInfo followedUser = _context.Set <UserInfo>().AsNoTracking().FirstOrDefault(f => f.UName == followedUsername); FollowInfo fInfo = _context.SetChild <FollowInfo>().FirstOrDefault(p => p.FollowerId == followingId && p.FollowedId == followedUser.AppUserId); if (followedUser.AppUserId == followingId) { ret.LastFollowState = FollowState.Unfollowed; ret.IsActionSucceed = false; return(ret); } if (fInfo == null) { FollowInfo newFollow = new FollowInfo() { FollowerId = followingId, FollowedId = followedUser.AppUserId, DateUtcFollowed = DateTime.UtcNow }; if (followedUser.FollowSetting == UserFollowSetting.Ask_All) { newFollow.FollowState = FollowState.Pending; ret.LastFollowState = FollowState.Pending; } else if (followedUser.FollowSetting == UserFollowSetting.Confirm_All) { newFollow.FollowState = FollowState.Confirmed; ret.LastFollowState = FollowState.Confirmed; } else if (followedUser.FollowSetting == UserFollowSetting.Deny_All) { ret.LastFollowState = FollowState.Unfollowed; return(ret); } ret.FollowedUserId = followedUser.AppUserId; _context.SetChild <FollowInfo>().Add(newFollow); } else { if (fInfo.FollowState == FollowState.FollowerBlocked_Followed || fInfo.FollowState == FollowState.FollowedBlocked_Follower) { ret.IsActionSucceed = false; ret.ErrorInformation.UserInformation = "Cannot follow blocked users."; ret.LastFollowState = FollowState.FollowedBlocked_Follower; } else if (fInfo.FollowState == FollowState.Unfollowed) { if (followedUser.FollowSetting == UserFollowSetting.Ask_All) { fInfo.FollowState = FollowState.Pending; ret.LastFollowState = FollowState.Pending; } else if (followedUser.FollowSetting == UserFollowSetting.Confirm_All) { fInfo.FollowState = FollowState.Confirmed; ret.LastFollowState = FollowState.Confirmed; } else if (followedUser.FollowSetting == UserFollowSetting.Deny_All) { ret.LastFollowState = FollowState.Unfollowed; return(ret); } } _context.SetChild <FollowInfo>().Update(fInfo); } if (_context.SaveChanges() != 0) { _userFollowCacheService.RemoveAllUserCache(followingId); ret.IsActionSucceed = true; } ; return(ret); }