| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.ProfanityFiltering; |
| | 3 | | using DCL.Social.Friends; |
| | 4 | | using DCL.Tasks; |
| | 5 | | using SocialFeaturesAnalytics; |
| | 6 | | using System; |
| | 7 | | using System.Threading; |
| | 8 | |
|
| | 9 | | namespace DCL.Social.Passports |
| | 10 | | { |
| | 11 | | public class PassportPlayerInfoComponentController : IDisposable |
| | 12 | | { |
| | 13 | | private readonly IPassportPlayerInfoComponentView view; |
| | 14 | | private readonly DataStore dataStore; |
| | 15 | | private readonly IProfanityFilter profanityFilter; |
| | 16 | | private readonly IFriendsController friendsController; |
| | 17 | | private readonly IUserProfileBridge userProfileBridge; |
| | 18 | | private readonly ISocialAnalytics socialAnalytics; |
| | 19 | | private readonly BaseVariable<(string playerId, string source)> currentPlayerId; |
| | 20 | | private readonly IClipboard clipboard; |
| | 21 | | private readonly IPassportApiBridge passportApiBridge; |
| | 22 | |
|
| | 23 | | private CancellationTokenSource cancellationTokenSource; |
| | 24 | | private CancellationTokenSource removeFriendCancellationToken; |
| 0 | 25 | | private UserProfile ownUserProfile => userProfileBridge.GetOwn(); |
| | 26 | | private string name; |
| 0 | 27 | | private bool isFriendsEnabled => dataStore.featureFlags.flags.Get().IsFeatureEnabled("friends_enabled"); |
| | 28 | | public event Action OnClosePassport; |
| | 29 | |
|
| 0 | 30 | | public PassportPlayerInfoComponentController( |
| | 31 | | IPassportPlayerInfoComponentView view, |
| | 32 | | DataStore dataStore, |
| | 33 | | IProfanityFilter profanityFilter, |
| | 34 | | IFriendsController friendsController, |
| | 35 | | IUserProfileBridge userProfileBridge, |
| | 36 | | ISocialAnalytics socialAnalytics, |
| | 37 | | IClipboard clipboard, |
| | 38 | | IPassportApiBridge passportApiBridge) |
| | 39 | | { |
| 0 | 40 | | this.view = view; |
| 0 | 41 | | this.dataStore = dataStore; |
| 0 | 42 | | this.profanityFilter = profanityFilter; |
| 0 | 43 | | this.friendsController = friendsController; |
| 0 | 44 | | this.userProfileBridge = userProfileBridge; |
| 0 | 45 | | this.socialAnalytics = socialAnalytics; |
| 0 | 46 | | this.clipboard = clipboard; |
| 0 | 47 | | this.passportApiBridge = passportApiBridge; |
| 0 | 48 | | this.currentPlayerId = dataStore.HUDs.currentPlayerId; |
| | 49 | |
|
| 0 | 50 | | view.OnAddFriend += AddPlayerAsFriend; |
| 0 | 51 | | view.OnRemoveFriend += RemoveFriend; |
| 0 | 52 | | view.OnCancelFriendRequest += CancelFriendRequest; |
| 0 | 53 | | view.OnAcceptFriendRequest += AcceptFriendRequest; |
| 0 | 54 | | view.OnBlockUser += BlockUser; |
| 0 | 55 | | view.OnUnblockUser += UnblockUser; |
| 0 | 56 | | view.OnReportUser += ReportUser; |
| 0 | 57 | | view.OnWhisperUser += WhisperUser; |
| 0 | 58 | | view.OnJumpInUser += JumpInUser; |
| 0 | 59 | | view.OnWalletCopy += WalletCopy; |
| 0 | 60 | | view.OnUsernameCopy += UsernameCopy; |
| | 61 | |
|
| 0 | 62 | | friendsController.OnUpdateFriendship += UpdateFriendshipStatus; |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public void Dispose() |
| | 66 | | { |
| 0 | 67 | | view.Dispose(); |
| 0 | 68 | | cancellationTokenSource?.Cancel(); |
| 0 | 69 | | cancellationTokenSource?.Dispose(); |
| 0 | 70 | | cancellationTokenSource = null; |
| 0 | 71 | | friendsController.OnUpdateFriendship -= UpdateFriendshipStatus; |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | private void WalletCopy(string address) |
| | 75 | | { |
| 0 | 76 | | clipboard.WriteText(address); |
| 0 | 77 | | socialAnalytics.SendCopyWallet(PlayerActionSource.Passport); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | private void UsernameCopy(string username) |
| | 81 | | { |
| 0 | 82 | | clipboard.WriteText(username); |
| 0 | 83 | | socialAnalytics.SendCopyUsername(PlayerActionSource.Passport); |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | private void JumpInUser() |
| | 87 | | { |
| 0 | 88 | | socialAnalytics.SendJumpInToPlayer(PlayerActionSource.Passport, currentPlayerId.Get().playerId); |
| 0 | 89 | | OnClosePassport?.Invoke(); |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | public void UpdateWithUserProfile(UserProfile userProfile) |
| | 93 | | { |
| 0 | 94 | | cancellationTokenSource?.Cancel(); |
| 0 | 95 | | cancellationTokenSource?.Dispose(); |
| 0 | 96 | | cancellationTokenSource = new CancellationTokenSource(); |
| | 97 | |
|
| 0 | 98 | | UpdateWithUserProfileAsync(userProfile, cancellationTokenSource.Token).Forget(); |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public void ClosePassport() => |
| 0 | 102 | | view.ResetCopyToast(); |
| | 103 | |
|
| | 104 | | private async UniTask UpdateWithUserProfileAsync(UserProfile userProfile, CancellationToken cancellationToken) |
| | 105 | | { |
| 0 | 106 | | name = userProfile.name; |
| 0 | 107 | | string filteredName = await FilterName(userProfile); |
| | 108 | | PlayerPassportModel playerPassportModel; |
| | 109 | |
|
| 0 | 110 | | if (userProfile.isGuest) |
| | 111 | | { |
| 0 | 112 | | playerPassportModel = new PlayerPassportModel |
| | 113 | | { |
| | 114 | | name = filteredName, |
| | 115 | | isGuest = userProfile.isGuest, |
| | 116 | | userId = userProfile.userId, |
| | 117 | | isBlocked = ownUserProfile.IsBlocked(userProfile.userId), |
| | 118 | | hasBlocked = userProfile.IsBlocked(ownUserProfile.userId), |
| | 119 | | }; |
| | 120 | | } |
| | 121 | | else |
| | 122 | | { |
| 0 | 123 | | playerPassportModel = new PlayerPassportModel |
| | 124 | | { |
| | 125 | | name = filteredName, |
| | 126 | | userId = userProfile.userId, |
| | 127 | | presenceStatus = friendsController.GetUserStatus(userProfile.userId).presence, |
| | 128 | | isGuest = userProfile.isGuest, |
| | 129 | | isBlocked = ownUserProfile.IsBlocked(userProfile.userId), |
| | 130 | | hasBlocked = userProfile.IsBlocked(ownUserProfile.userId), |
| | 131 | | friendshipStatus = await friendsController.GetFriendshipStatus(userProfile.userId, cancellationToken |
| | 132 | | isFriendshipVisible = isFriendsEnabled && friendsController.IsInitialized, |
| | 133 | | }; |
| | 134 | | } |
| | 135 | |
|
| 0 | 136 | | view.SetModel(playerPassportModel); |
| 0 | 137 | | view.InitializeJumpInButton(friendsController, userProfile.userId, socialAnalytics); |
| 0 | 138 | | view.SetActionsActive(userProfile.userId != ownUserProfile.userId); |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | private async UniTask<string> FilterName(UserProfile userProfile) |
| | 142 | | { |
| 0 | 143 | | return IsProfanityFilteringEnabled() |
| | 144 | | ? await profanityFilter.Filter(userProfile.userName) |
| | 145 | | : userProfile.userName; |
| 0 | 146 | | } |
| | 147 | |
|
| | 148 | | private bool IsProfanityFilteringEnabled() |
| | 149 | | { |
| 0 | 150 | | return dataStore.settings.profanityChatFilteringEnabled.Get(); |
| | 151 | | } |
| | 152 | |
|
| | 153 | | private void AddPlayerAsFriend() |
| | 154 | | { |
| 0 | 155 | | if (userProfileBridge.GetOwn().isGuest) |
| | 156 | | { |
| 0 | 157 | | dataStore.HUDs.connectWalletModalVisible.Set(true); |
| 0 | 158 | | return; |
| | 159 | | } |
| | 160 | |
|
| 0 | 161 | | string userId = currentPlayerId.Get().playerId; |
| 0 | 162 | | dataStore.HUDs.sendFriendRequest.Set(userId, true); |
| 0 | 163 | | } |
| | 164 | |
|
| | 165 | | private void RemoveFriend() |
| | 166 | | { |
| 0 | 167 | | string userId = currentPlayerId.Get().playerId; |
| | 168 | |
|
| 0 | 169 | | dataStore.notifications.GenericConfirmation.Set(GenericConfirmationNotificationData.CreateUnFriendData( |
| | 170 | | UserProfileController.userProfilesCatalog.Get(userId)?.userName, |
| | 171 | | () => |
| | 172 | | { |
| 0 | 173 | | removeFriendCancellationToken = removeFriendCancellationToken.SafeRestart(); |
| 0 | 174 | | friendsController.RemoveFriendAsync(userId, removeFriendCancellationToken.Token); |
| 0 | 175 | | socialAnalytics.SendFriendDeleted(UserProfile.GetOwnUserProfile().userId, userId, PlayerActionSource |
| 0 | 176 | | }), true); |
| 0 | 177 | | } |
| | 178 | |
|
| | 179 | | private void CancelFriendRequest() |
| | 180 | | { |
| 0 | 181 | | cancellationTokenSource?.Cancel(); |
| 0 | 182 | | cancellationTokenSource?.Dispose(); |
| 0 | 183 | | cancellationTokenSource = new CancellationTokenSource(); |
| 0 | 184 | | CancelFriendRequestAsync(cancellationTokenSource.Token).Forget(); |
| 0 | 185 | | } |
| | 186 | |
|
| | 187 | | private async UniTaskVoid CancelFriendRequestAsync(CancellationToken cancellationToken) |
| | 188 | | { |
| 0 | 189 | | string userId = currentPlayerId.Get().playerId; |
| | 190 | |
|
| | 191 | | try |
| | 192 | | { |
| 0 | 193 | | var friendRequest = await friendsController.CancelRequestByUserIdAsync(userId, cancellationToken); |
| 0 | 194 | | dataStore.HUDs.openSentFriendRequestDetail.Set(null, true); |
| | 195 | |
|
| 0 | 196 | | socialAnalytics.SendFriendRequestCancelled(ownUserProfile.userId, userId, |
| | 197 | | PlayerActionSource.Passport.ToString(), friendRequest.FriendRequestId); |
| 0 | 198 | | } |
| 0 | 199 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 200 | | { |
| 0 | 201 | | e.ReportFriendRequestErrorToAnalyticsByUserId(userId, "modal", |
| | 202 | | friendsController, socialAnalytics); |
| | 203 | |
|
| 0 | 204 | | throw; |
| | 205 | | } |
| 0 | 206 | | } |
| | 207 | |
|
| | 208 | | private void AcceptFriendRequest() |
| | 209 | | { |
| 0 | 210 | | cancellationTokenSource?.Cancel(); |
| 0 | 211 | | cancellationTokenSource?.Dispose(); |
| 0 | 212 | | cancellationTokenSource = new CancellationTokenSource(); |
| 0 | 213 | | AcceptFriendRequestAsync(cancellationTokenSource.Token).Forget(); |
| 0 | 214 | | } |
| | 215 | |
|
| | 216 | | private async UniTaskVoid AcceptFriendRequestAsync(CancellationToken cancellationToken) |
| | 217 | | { |
| 0 | 218 | | string userId = currentPlayerId.Get().playerId; |
| | 219 | |
|
| | 220 | | try |
| | 221 | | { |
| 0 | 222 | | FriendRequest request = friendsController.GetAllocatedFriendRequestByUser(userId); |
| 0 | 223 | | await friendsController.AcceptFriendshipAsync(request.FriendRequestId, cancellationToken); |
| 0 | 224 | | dataStore.HUDs.openReceivedFriendRequestDetail.Set(null, true); |
| | 225 | |
|
| 0 | 226 | | socialAnalytics.SendFriendRequestApproved(ownUserProfile.userId, userId, PlayerActionSource.Passport.ToS |
| | 227 | | request.HasBodyMessage, request.FriendRequestId); |
| 0 | 228 | | } |
| 0 | 229 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 230 | | { |
| 0 | 231 | | e.ReportFriendRequestErrorToAnalyticsByUserId(userId, "modal", |
| | 232 | | friendsController, socialAnalytics); |
| | 233 | |
|
| 0 | 234 | | throw; |
| | 235 | | } |
| 0 | 236 | | } |
| | 237 | |
|
| | 238 | | private void BlockUser() |
| | 239 | | { |
| 0 | 240 | | string userId = currentPlayerId.Get().playerId; |
| 0 | 241 | | if (ownUserProfile.IsBlocked(userId)) return; |
| | 242 | |
|
| 0 | 243 | | dataStore.notifications.GenericConfirmation.Set(GenericConfirmationNotificationData.CreateBlockUserData( |
| | 244 | | userProfileBridge.Get(userId)?.userName, |
| | 245 | | () => |
| | 246 | | { |
| 0 | 247 | | ownUserProfile.Block(userId); |
| 0 | 248 | | view.SetIsBlocked(true); |
| 0 | 249 | | passportApiBridge.SendBlockPlayer(userId); |
| 0 | 250 | | socialAnalytics.SendPlayerBlocked(friendsController.IsFriend(userId), PlayerActionSource.Passport, c |
| 0 | 251 | | }), true); |
| 0 | 252 | | } |
| | 253 | |
|
| | 254 | | private void UnblockUser() |
| | 255 | | { |
| 0 | 256 | | string userId = currentPlayerId.Get().playerId; |
| 0 | 257 | | if (!ownUserProfile.IsBlocked(userId)) return; |
| 0 | 258 | | ownUserProfile.Unblock(userId); |
| 0 | 259 | | view.SetIsBlocked(false); |
| 0 | 260 | | passportApiBridge.SendUnblockPlayer(userId); |
| 0 | 261 | | socialAnalytics.SendPlayerUnblocked(friendsController.IsFriend(userId), PlayerActionSource.Passport, current |
| 0 | 262 | | } |
| | 263 | |
|
| | 264 | | private void ReportUser() |
| | 265 | | { |
| 0 | 266 | | passportApiBridge.SendReportPlayer(currentPlayerId.Get().playerId, name); |
| 0 | 267 | | socialAnalytics.SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.Passport, currentPlayerId |
| 0 | 268 | | } |
| | 269 | |
|
| | 270 | | private void WhisperUser(string userId) |
| | 271 | | { |
| 0 | 272 | | dataStore.HUDs.openChat.Set(userId, true); |
| 0 | 273 | | socialAnalytics.SendStartedConversation(PlayerActionSource.Passport, currentPlayerId.Get().playerId); |
| 0 | 274 | | OnClosePassport?.Invoke(); |
| 0 | 275 | | } |
| | 276 | |
|
| | 277 | | private void UpdateFriendshipStatus(string userId, FriendshipAction action) |
| | 278 | | { |
| 0 | 279 | | if (userId != currentPlayerId.Get().playerId) return; |
| 0 | 280 | | view.SetFriendStatus(ToFriendshipStatus(action)); |
| 0 | 281 | | } |
| | 282 | |
|
| | 283 | | private FriendshipStatus ToFriendshipStatus(FriendshipAction action) |
| | 284 | | { |
| | 285 | | switch (action) |
| | 286 | | { |
| | 287 | | case FriendshipAction.APPROVED: |
| 0 | 288 | | return FriendshipStatus.FRIEND; |
| | 289 | | case FriendshipAction.REQUESTED_TO: |
| 0 | 290 | | return FriendshipStatus.REQUESTED_TO; |
| | 291 | | case FriendshipAction.REQUESTED_FROM: |
| 0 | 292 | | return FriendshipStatus.REQUESTED_FROM; |
| | 293 | | case FriendshipAction.NONE: |
| | 294 | | case FriendshipAction.DELETED: |
| | 295 | | case FriendshipAction.REJECTED: |
| | 296 | | case FriendshipAction.CANCELLED: |
| | 297 | | default: |
| 0 | 298 | | return FriendshipStatus.NOT_FRIEND; |
| | 299 | | } |
| | 300 | | } |
| | 301 | | } |
| | 302 | | } |