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