| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Interface; |
| | 4 | | using DCL.Social.Friends; |
| | 5 | | using DCL.Tasks; |
| | 6 | | using SocialFeaturesAnalytics; |
| | 7 | | using System; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Threading; |
| | 11 | | using TMPro; |
| | 12 | | using UnityEngine; |
| | 13 | | using UnityEngine.EventSystems; |
| | 14 | | using UnityEngine.UI; |
| | 15 | | using Environment = DCL.Environment; |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Contextual menu with different options about an user. |
| | 19 | | /// </summary> |
| | 20 | | [RequireComponent(typeof(RectTransform))] |
| | 21 | | // TODO: refactor into MVC |
| | 22 | | public class UserContextMenu : MonoBehaviour |
| | 23 | | { |
| | 24 | | private const string CURRENT_PLAYER_ID = "CurrentPlayerInfoCardId"; |
| | 25 | | private const string BLOCK_BTN_BLOCK_TEXT = "Block"; |
| | 26 | | private const string BLOCK_BTN_UNBLOCK_TEXT = "Unblock"; |
| | 27 | | private const string DELETE_MSG_PATTERN = "Are you sure you want to delete {0} as a friend?"; |
| | 28 | |
|
| | 29 | | [Flags] |
| | 30 | | public enum MenuConfigFlags |
| | 31 | | { |
| | 32 | | Name = 1, |
| | 33 | | Friendship = 2, |
| | 34 | | Message = 4, |
| | 35 | | Passport = 8, |
| | 36 | | Block = 16, |
| | 37 | | Report = 32 |
| | 38 | | } |
| | 39 | |
|
| | 40 | | const MenuConfigFlags headerFlags = MenuConfigFlags.Name | MenuConfigFlags.Friendship; |
| | 41 | | const MenuConfigFlags usesFriendsApiFlags = MenuConfigFlags.Friendship | MenuConfigFlags.Message; |
| | 42 | |
|
| | 43 | | [Header("Optional: Set Confirmation Dialog")] |
| | 44 | | [SerializeField] internal UserContextConfirmationDialog confirmationDialog; |
| | 45 | |
|
| | 46 | | [Header("Enable Actions")] |
| 231 | 47 | | [SerializeField] internal MenuConfigFlags menuConfigFlags = MenuConfigFlags.Passport | MenuConfigFlags.Block | MenuC |
| | 48 | |
|
| | 49 | | [Header("Containers")] |
| | 50 | | [SerializeField] internal GameObject headerContainer; |
| | 51 | | [SerializeField] internal GameObject friendshipContainer; |
| | 52 | | [SerializeField] internal GameObject friendAddContainer; |
| | 53 | | [SerializeField] internal GameObject friendRemoveContainer; |
| | 54 | | [SerializeField] internal GameObject friendRequestedContainer; |
| | 55 | |
|
| | 56 | | [Header("Texts")] |
| | 57 | | [SerializeField] internal TextMeshProUGUI userName; |
| | 58 | | [SerializeField] internal TextMeshProUGUI blockText; |
| | 59 | |
|
| | 60 | | [Header("Buttons")] |
| | 61 | | [SerializeField] internal Button passportButton; |
| | 62 | | [SerializeField] internal Button blockButton; |
| | 63 | | [SerializeField] internal Button reportButton; |
| | 64 | | [SerializeField] internal Button addFriendButton; |
| | 65 | | [SerializeField] internal Button cancelFriendButton; |
| | 66 | | [SerializeField] internal Button deleteFriendButton; |
| | 67 | | [SerializeField] internal Button messageButton; |
| | 68 | |
|
| | 69 | | public static event Action<string> OnOpenPrivateChatRequest; |
| | 70 | |
|
| 23 | 71 | | public bool isVisible => gameObject.activeSelf; |
| 0 | 72 | | public string UserId => userId; |
| | 73 | |
|
| | 74 | | public event Action OnShowMenu; |
| | 75 | | public event Action<string> OnPassport; |
| | 76 | | public event Action<string> OnReport; |
| | 77 | | public event Action<string, bool> OnBlock; |
| | 78 | | public event Action<string> OnUnfriend; |
| | 79 | | public event Action OnHide; |
| | 80 | |
|
| | 81 | | private static StringVariable currentPlayerId; |
| | 82 | | private string userId; |
| | 83 | | private bool isBlocked; |
| | 84 | | private MenuConfigFlags currentConfigFlags; |
| | 85 | | private IConfirmationDialog currentConfirmationDialog; |
| 231 | 86 | | private CancellationTokenSource friendOperationsCancellationToken = new (); |
| 0 | 87 | | private bool isNewFriendRequestsEnabled => DataStore.i.featureFlags.flags.Get().IsFeatureEnabled("new_friend_request |
| 82 | 88 | | private bool isFriendsEnabled => DataStore.i.featureFlags.flags.Get().IsFeatureEnabled("friends_enabled"); |
| | 89 | | internal ISocialAnalytics socialAnalytics; |
| | 90 | |
|
| | 91 | | /// <summary> |
| | 92 | | /// Show context menu |
| | 93 | | /// </summary> |
| | 94 | | /// <param name="userId"> user id</param> |
| | 95 | | public void Show(string userId) |
| | 96 | | { |
| 5 | 97 | | Show(userId, menuConfigFlags); |
| 5 | 98 | | } |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// Show context menu |
| | 102 | | /// </summary> |
| | 103 | | /// <param name="userId"> user id</param> |
| | 104 | | /// <param name="configFlags">set buttons to enable in menu</param> |
| | 105 | | public void Show(string userId, MenuConfigFlags configFlags) |
| | 106 | | { |
| 7 | 107 | | this.userId = userId; |
| 7 | 108 | | ProcessActiveElements(configFlags); |
| 7 | 109 | | Setup(userId, configFlags); |
| | 110 | |
|
| 7 | 111 | | if (currentConfirmationDialog == null && confirmationDialog != null) { SetConfirmationDialog(confirmationDialog) |
| | 112 | |
|
| 7 | 113 | | gameObject.SetActive(true); |
| 7 | 114 | | OnShowMenu?.Invoke(); |
| 1 | 115 | | } |
| | 116 | |
|
| | 117 | | /// <summary> |
| | 118 | | /// Set confirmation popup to reference use |
| | 119 | | /// </summary> |
| | 120 | | /// <param name="confirmationPopup">confirmation popup reference</param> |
| | 121 | | public void SetConfirmationDialog(IConfirmationDialog confirmationPopup) |
| | 122 | | { |
| 0 | 123 | | this.currentConfirmationDialog = confirmationPopup; |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | /// <summary> |
| | 127 | | /// Hides the context menu. |
| | 128 | | /// </summary> |
| | 129 | | public void Hide() |
| | 130 | | { |
| 20 | 131 | | friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart(); |
| 20 | 132 | | gameObject.SetActive(false); |
| 20 | 133 | | OnHide?.Invoke(); |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | /// <summary> |
| | 137 | | /// Shows/Hides the friendship container |
| | 138 | | /// </summary> |
| | 139 | | public void SetFriendshipContentActive(bool isActive) => |
| 2 | 140 | | friendshipContainer.SetActive(isActive); |
| | 141 | |
|
| | 142 | | private void Awake() |
| | 143 | | { |
| 10 | 144 | | if (!currentPlayerId) |
| 1 | 145 | | currentPlayerId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID); |
| | 146 | |
|
| 10 | 147 | | passportButton.onClick.AddListener(OnPassportButtonPressed); |
| 10 | 148 | | blockButton.onClick.AddListener(OnBlockUserButtonPressed); |
| 10 | 149 | | reportButton.onClick.AddListener(OnReportUserButtonPressed); |
| 10 | 150 | | deleteFriendButton.onClick.AddListener(OnDeleteUserButtonPressed); |
| 10 | 151 | | addFriendButton.onClick.AddListener(OnAddFriendButtonPressed); |
| 10 | 152 | | cancelFriendButton.onClick.AddListener(OnCancelFriendRequestButtonPressed); |
| 10 | 153 | | messageButton.onClick.AddListener(OnMessageButtonPressed); |
| 10 | 154 | | } |
| | 155 | |
|
| | 156 | | private void Update() |
| | 157 | | { |
| 0 | 158 | | HideIfClickedOutside(); |
| 0 | 159 | | } |
| | 160 | |
|
| | 161 | | private void OnDisable() |
| | 162 | | { |
| 10 | 163 | | FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate; |
| 10 | 164 | | } |
| | 165 | |
|
| | 166 | | private void OnPassportButtonPressed() |
| | 167 | | { |
| 1 | 168 | | OnPassport?.Invoke(userId); |
| 1 | 169 | | currentPlayerId.Set(userId); |
| 1 | 170 | | Hide(); |
| | 171 | |
|
| 1 | 172 | | AudioScriptableObjects.dialogOpen.Play(true); |
| 1 | 173 | | } |
| | 174 | |
|
| | 175 | | private void OnReportUserButtonPressed() |
| | 176 | | { |
| 1 | 177 | | OnReport?.Invoke(userId); |
| 1 | 178 | | WebInterface.SendReportPlayer(userId, UserProfileController.userProfilesCatalog.Get(userId)?.userName); |
| 1 | 179 | | GetSocialAnalytics().SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.ProfileContextMenu); |
| 1 | 180 | | Hide(); |
| 1 | 181 | | } |
| | 182 | |
|
| | 183 | | private void OnDeleteUserButtonPressed() |
| | 184 | | { |
| 0 | 185 | | DataStore.i.notifications.GenericConfirmation.Set(GenericConfirmationNotificationData.CreateUnFriendData( |
| | 186 | | UserProfileController.userProfilesCatalog.Get(userId)?.userName, |
| | 187 | | () => |
| | 188 | | { |
| 0 | 189 | | FriendsController.i.RemoveFriend(userId); |
| 0 | 190 | | OnUnfriend?.Invoke(userId); |
| 0 | 191 | | }), true); |
| | 192 | |
|
| 0 | 193 | | GetSocialAnalytics().SendFriendDeleted(UserProfile.GetOwnUserProfile().userId, userId, PlayerActionSource.Profil |
| 0 | 194 | | Hide(); |
| 0 | 195 | | } |
| | 196 | |
|
| | 197 | | private void OnAddFriendButtonPressed() |
| | 198 | | { |
| | 199 | | // NOTE: if we don't add this, the friend request has strange behaviors |
| 0 | 200 | | UserProfileController.i.AddUserProfileToCatalog(new UserProfileModel() |
| | 201 | | { |
| | 202 | | userId = userId, |
| | 203 | | name = UserProfileController.userProfilesCatalog.Get(userId)?.userName |
| | 204 | | }); |
| | 205 | |
|
| 0 | 206 | | if (isNewFriendRequestsEnabled) |
| | 207 | | { |
| 0 | 208 | | DataStore.i.HUDs.sendFriendRequest.Set(userId, true); |
| 0 | 209 | | DataStore.i.HUDs.sendFriendRequestSource.Set((int)PlayerActionSource.ProfileContextMenu); |
| | 210 | | } |
| | 211 | | else |
| | 212 | | { |
| 0 | 213 | | FriendsController.i.RequestFriendship(userId); |
| 0 | 214 | | GetSocialAnalytics().SendFriendRequestSent(UserProfile.GetOwnUserProfile().userId, userId, 0, PlayerActionSo |
| | 215 | | } |
| 0 | 216 | | } |
| | 217 | |
|
| | 218 | | private void OnCancelFriendRequestButtonPressed() |
| | 219 | | { |
| 0 | 220 | | friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart(); |
| 0 | 221 | | CancelFriendRequestAsync(userId, friendOperationsCancellationToken.Token).Forget(); |
| 0 | 222 | | } |
| | 223 | |
|
| | 224 | | private async UniTaskVoid CancelFriendRequestAsync(string userId, CancellationToken cancellationToken = default) |
| | 225 | | { |
| 0 | 226 | | if (isNewFriendRequestsEnabled) |
| | 227 | | { |
| | 228 | | try |
| | 229 | | { |
| 0 | 230 | | FriendRequest request = await FriendsController.i.CancelRequestByUserIdAsync(userId, cancellationToken); |
| | 231 | |
|
| 0 | 232 | | GetSocialAnalytics() |
| | 233 | | .SendFriendRequestCancelled(request.From, request.To, |
| | 234 | | PlayerActionSource.ProfileContextMenu.ToString()); |
| 0 | 235 | | } |
| 0 | 236 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 237 | | { |
| 0 | 238 | | e.ReportFriendRequestErrorToAnalyticsByUserId(userId, PlayerActionSource.ProfileContextMenu.ToString(), |
| | 239 | | FriendsController.i, socialAnalytics); |
| | 240 | |
|
| 0 | 241 | | throw; |
| | 242 | | } |
| | 243 | | } |
| | 244 | | else |
| | 245 | | { |
| 0 | 246 | | FriendsController.i.CancelRequestByUserId(userId); |
| | 247 | |
|
| 0 | 248 | | GetSocialAnalytics() |
| | 249 | | .SendFriendRequestCancelled(UserProfile.GetOwnUserProfile().userId, userId, |
| | 250 | | PlayerActionSource.ProfileContextMenu.ToString()); |
| | 251 | | } |
| 0 | 252 | | } |
| | 253 | |
|
| | 254 | | private void OnMessageButtonPressed() |
| | 255 | | { |
| 0 | 256 | | OnOpenPrivateChatRequest?.Invoke(userId); |
| 0 | 257 | | Hide(); |
| 0 | 258 | | } |
| | 259 | |
|
| | 260 | | private void OnBlockUserButtonPressed() |
| | 261 | | { |
| 1 | 262 | | bool blockUser = !isBlocked; |
| | 263 | |
|
| 1 | 264 | | if (blockUser) |
| | 265 | | { |
| 1 | 266 | | DataStore.i.notifications.GenericConfirmation.Set(GenericConfirmationNotificationData.CreateBlockUserData( |
| | 267 | | UserProfileController.userProfilesCatalog.Get(userId)?.userName, |
| | 268 | | () => |
| | 269 | | { |
| 1 | 270 | | WebInterface.SendBlockPlayer(userId); |
| 1 | 271 | | GetSocialAnalytics().SendPlayerBlocked(FriendsController.i.IsFriend(userId), PlayerActionSource.Prof |
| 1 | 272 | | OnBlock?.Invoke(userId, blockUser); |
| 1 | 273 | | }), true); |
| | 274 | | } |
| | 275 | | else |
| | 276 | | { |
| 0 | 277 | | WebInterface.SendUnblockPlayer(userId); |
| 0 | 278 | | GetSocialAnalytics().SendPlayerUnblocked(FriendsController.i.IsFriend(userId), PlayerActionSource.ProfileCon |
| 0 | 279 | | OnBlock?.Invoke(userId, blockUser); |
| | 280 | | } |
| | 281 | |
|
| 1 | 282 | | Hide(); |
| 1 | 283 | | } |
| | 284 | |
|
| | 285 | | private void UpdateBlockButton() |
| | 286 | | { |
| 5 | 287 | | blockText.text = isBlocked ? BLOCK_BTN_UNBLOCK_TEXT : BLOCK_BTN_BLOCK_TEXT; |
| 5 | 288 | | } |
| | 289 | |
|
| | 290 | | private void HideIfClickedOutside() |
| | 291 | | { |
| 0 | 292 | | if (!Input.GetMouseButtonDown(0)) return; |
| | 293 | |
|
| 0 | 294 | | var pointerEventData = new PointerEventData(EventSystem.current) |
| | 295 | | { |
| | 296 | | position = Input.mousePosition |
| | 297 | | }; |
| | 298 | |
|
| 0 | 299 | | var raycastResults = new List<RaycastResult>(); |
| 0 | 300 | | EventSystem.current.RaycastAll(pointerEventData, raycastResults); |
| | 301 | |
|
| 0 | 302 | | if (raycastResults.All(result => result.gameObject != gameObject)) |
| 0 | 303 | | Hide(); |
| 0 | 304 | | } |
| | 305 | |
|
| | 306 | | private void ProcessActiveElements(MenuConfigFlags flags) |
| | 307 | | { |
| 38 | 308 | | headerContainer.SetActive((flags & headerFlags) != 0); |
| 38 | 309 | | userName.gameObject.SetActive((flags & MenuConfigFlags.Name) != 0); |
| 38 | 310 | | friendshipContainer.SetActive((flags & MenuConfigFlags.Friendship) != 0 && isFriendsEnabled); |
| 38 | 311 | | deleteFriendButton.gameObject.SetActive((flags & MenuConfigFlags.Friendship) != 0 && isFriendsEnabled); |
| 38 | 312 | | passportButton.gameObject.SetActive((flags & MenuConfigFlags.Passport) != 0); |
| 38 | 313 | | blockButton.gameObject.SetActive((flags & MenuConfigFlags.Block) != 0); |
| 38 | 314 | | reportButton.gameObject.SetActive((flags & MenuConfigFlags.Report) != 0); |
| 38 | 315 | | messageButton.gameObject.SetActive((flags & MenuConfigFlags.Message) != 0); |
| 38 | 316 | | } |
| | 317 | |
|
| | 318 | | private void Setup(string userId, MenuConfigFlags configFlags) |
| | 319 | | { |
| 7 | 320 | | this.userId = userId; |
| | 321 | |
|
| 7 | 322 | | UserProfile profile = UserProfileController.userProfilesCatalog.Get(userId); |
| 7 | 323 | | bool userHasWallet = profile?.hasConnectedWeb3 ?? false; |
| | 324 | |
|
| 8 | 325 | | if (!userHasWallet || !UserProfile.GetOwnUserProfile().hasConnectedWeb3) { configFlags &= ~usesFriendsApiFlags; |
| | 326 | |
|
| 7 | 327 | | currentConfigFlags = configFlags; |
| 7 | 328 | | ProcessActiveElements(configFlags); |
| | 329 | |
|
| 7 | 330 | | if ((configFlags & MenuConfigFlags.Block) != 0) |
| | 331 | | { |
| 5 | 332 | | isBlocked = UserProfile.GetOwnUserProfile().blocked.Contains(userId); |
| 5 | 333 | | UpdateBlockButton(); |
| | 334 | | } |
| | 335 | |
|
| 7 | 336 | | if ((configFlags & MenuConfigFlags.Name) != 0) |
| | 337 | | { |
| 5 | 338 | | string name = profile?.userName; |
| 5 | 339 | | userName.text = name; |
| | 340 | | } |
| | 341 | |
|
| 7 | 342 | | if ((configFlags & usesFriendsApiFlags) != 0) |
| | 343 | | { |
| 6 | 344 | | UserStatus status = FriendsController.i.GetUserStatus(userId); |
| 6 | 345 | | SetupFriendship(status?.friendshipStatus ?? FriendshipStatus.NOT_FRIEND); |
| 6 | 346 | | FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate; |
| 6 | 347 | | FriendsController.i.OnUpdateFriendship += OnFriendActionUpdate; |
| | 348 | | } |
| 7 | 349 | | } |
| | 350 | |
|
| | 351 | | private void SetupFriendship(FriendshipStatus friendshipStatus) |
| | 352 | | { |
| 12 | 353 | | bool friendshipEnabled = (currentConfigFlags & MenuConfigFlags.Friendship) != 0 && isFriendsEnabled; |
| 12 | 354 | | bool messageEnabled = (currentConfigFlags & MenuConfigFlags.Message) != 0 && isFriendsEnabled; |
| | 355 | |
|
| 12 | 356 | | if (friendshipStatus == FriendshipStatus.FRIEND) |
| | 357 | | { |
| 2 | 358 | | if (friendshipEnabled) |
| | 359 | | { |
| 1 | 360 | | friendAddContainer.SetActive(false); |
| 1 | 361 | | friendRemoveContainer.SetActive(true); |
| 1 | 362 | | friendRequestedContainer.SetActive(false); |
| 1 | 363 | | deleteFriendButton.gameObject.SetActive(true); |
| | 364 | | } |
| | 365 | |
|
| 3 | 366 | | if (messageEnabled) { messageButton.gameObject.SetActive(true); } |
| | 367 | | } |
| 10 | 368 | | else if (friendshipStatus == FriendshipStatus.REQUESTED_TO) |
| | 369 | | { |
| 2 | 370 | | if (friendshipEnabled) |
| | 371 | | { |
| 1 | 372 | | friendAddContainer.SetActive(false); |
| 1 | 373 | | friendRemoveContainer.SetActive(false); |
| 1 | 374 | | friendRequestedContainer.SetActive(true); |
| 1 | 375 | | deleteFriendButton.gameObject.SetActive(false); |
| | 376 | | } |
| | 377 | |
|
| 3 | 378 | | if (messageEnabled) { messageButton.gameObject.SetActive(false); } |
| | 379 | | } |
| 8 | 380 | | else if (friendshipStatus == FriendshipStatus.NOT_FRIEND) |
| | 381 | | { |
| 8 | 382 | | if (friendshipEnabled) |
| | 383 | | { |
| 6 | 384 | | friendAddContainer.SetActive(true); |
| 6 | 385 | | friendRemoveContainer.SetActive(false); |
| 6 | 386 | | friendRequestedContainer.SetActive(false); |
| 6 | 387 | | deleteFriendButton.gameObject.SetActive(false); |
| | 388 | | } |
| | 389 | |
|
| 14 | 390 | | if (messageEnabled) { messageButton.gameObject.SetActive(false); } |
| | 391 | | } |
| 0 | 392 | | else if (friendshipStatus == FriendshipStatus.REQUESTED_FROM) |
| | 393 | | { |
| 0 | 394 | | if (friendshipEnabled) |
| | 395 | | { |
| 0 | 396 | | friendAddContainer.SetActive(true); |
| 0 | 397 | | friendRemoveContainer.SetActive(false); |
| 0 | 398 | | friendRequestedContainer.SetActive(false); |
| 0 | 399 | | deleteFriendButton.gameObject.SetActive(false); |
| | 400 | | } |
| | 401 | |
|
| 0 | 402 | | if (messageEnabled) { messageButton.gameObject.SetActive(false); } |
| | 403 | | } |
| 4 | 404 | | } |
| | 405 | |
|
| | 406 | | private void OnFriendActionUpdate(string userId, FriendshipAction action) |
| | 407 | | { |
| 6 | 408 | | if (this.userId != userId) { return; } |
| | 409 | |
|
| 8 | 410 | | if (action == FriendshipAction.APPROVED) { SetupFriendship(FriendshipStatus.FRIEND); } |
| 6 | 411 | | else if (action == FriendshipAction.REQUESTED_TO) { SetupFriendship(FriendshipStatus.REQUESTED_TO); } |
| 4 | 412 | | else if (action == FriendshipAction.DELETED || action == FriendshipAction.CANCELLED || action == FriendshipActio |
| 2 | 413 | | } |
| | 414 | |
|
| | 415 | | private ISocialAnalytics GetSocialAnalytics() |
| | 416 | | { |
| 2 | 417 | | if (socialAnalytics == null) |
| | 418 | | { |
| 0 | 419 | | socialAnalytics = new SocialAnalytics( |
| | 420 | | Environment.i.platform.serviceProviders.analytics, |
| | 421 | | new UserProfileWebInterfaceBridge()); |
| | 422 | | } |
| | 423 | |
|
| 2 | 424 | | return socialAnalytics; |
| | 425 | | } |
| | 426 | |
|
| | 427 | | #if UNITY_EDITOR |
| | 428 | |
|
| | 429 | | //This is just to process buttons and container visibility on editor |
| | 430 | | private void OnValidate() |
| | 431 | | { |
| 24 | 432 | | if (headerContainer == null) |
| 0 | 433 | | return; |
| | 434 | |
|
| 24 | 435 | | ProcessActiveElements(menuConfigFlags); |
| 24 | 436 | | } |
| | 437 | | #endif |
| | 438 | | } |