| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Social.Friends; |
| | 3 | | using DCL.Tasks; |
| | 4 | | using SocialFeaturesAnalytics; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Threading; |
| | 8 | |
|
| | 9 | | namespace DCL.MyAccount |
| | 10 | | { |
| | 11 | | public class BlockedListController |
| | 12 | | { |
| | 13 | | private readonly IBlockedListComponentView view; |
| | 14 | | private readonly DataStore dataStore; |
| | 15 | | private readonly IUserProfileBridge userProfileBridge; |
| | 16 | |
|
| | 17 | | private readonly IBlockedListApiBridge blockedListApiBridge; |
| | 18 | | private readonly ISocialAnalytics socialAnalytics; |
| | 19 | | private readonly IFriendsController friendsController; |
| | 20 | |
|
| | 21 | | private CancellationTokenSource lifeTimeCancellationToken; |
| | 22 | |
|
| 0 | 23 | | private UserProfile ownUserProfile => userProfileBridge.GetOwn(); |
| | 24 | |
|
| 0 | 25 | | public BlockedListController( |
| | 26 | | IBlockedListComponentView view, |
| | 27 | | DataStore dataStore, |
| | 28 | | IUserProfileBridge userProfileBridge, |
| | 29 | | IBlockedListApiBridge blockedListApiBridge, |
| | 30 | | ISocialAnalytics socialAnalytics, |
| | 31 | | IFriendsController friendsController) |
| | 32 | | { |
| 0 | 33 | | this.view = view; |
| 0 | 34 | | this.dataStore = dataStore; |
| 0 | 35 | | this.userProfileBridge = userProfileBridge; |
| 0 | 36 | | this.blockedListApiBridge = blockedListApiBridge; |
| 0 | 37 | | this.socialAnalytics = socialAnalytics; |
| 0 | 38 | | this.friendsController = friendsController; |
| | 39 | |
|
| 0 | 40 | | dataStore.myAccount.isMyAccountSectionVisible.OnChange += OnMyAccountSectionVisibleChanged; |
| 0 | 41 | | dataStore.myAccount.openSection.OnChange += OnMyAccountSectionTabChanged; |
| | 42 | |
|
| 0 | 43 | | ownUserProfile.OnUpdate += OnOwnUserProfileUpdated; |
| 0 | 44 | | view.OnUnblockUser += UnblockUser; |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | private void OnMyAccountSectionVisibleChanged(bool isVisible, bool _) |
| | 48 | | { |
| 0 | 49 | | if (isVisible) |
| 0 | 50 | | OpenSection(); |
| | 51 | | else |
| 0 | 52 | | CloseSection(); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | private void OpenSection() |
| | 56 | | { |
| 0 | 57 | | lifeTimeCancellationToken = lifeTimeCancellationToken.SafeRestart(); |
| 0 | 58 | | UpdateBlockedUserList(ownUserProfile.blocked, lifeTimeCancellationToken.Token); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | private void OnMyAccountSectionTabChanged(string currentOpenSection, string _) |
| | 62 | | { |
| 0 | 63 | | if (currentOpenSection != MyAccountSection.BlockedList.ToString()) |
| 0 | 64 | | return; |
| | 65 | | } |
| | 66 | |
|
| | 67 | | private void CloseSection() |
| | 68 | | { |
| 0 | 69 | | lifeTimeCancellationToken.SafeCancelAndDispose(); |
| | 70 | |
|
| 0 | 71 | | view.ClearAllEntries(); |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | |
|
| | 75 | | private void OnOwnUserProfileUpdated(UserProfile userProfile) |
| | 76 | | { |
| 0 | 77 | | if (userProfile == null) |
| 0 | 78 | | return; |
| | 79 | | } |
| | 80 | |
|
| | 81 | | private void UnblockUser(string userId) |
| | 82 | | { |
| 0 | 83 | | if (!ownUserProfile.IsBlocked(userId)) return; |
| | 84 | |
|
| 0 | 85 | | dataStore.notifications.GenericConfirmation.Set(GenericConfirmationNotificationData.CreateUnBlockUserData( |
| | 86 | | userProfileBridge.Get(userId)?.userName, |
| | 87 | | () => |
| | 88 | | { |
| 0 | 89 | | ownUserProfile.Unblock(userId); |
| 0 | 90 | | view.Remove(userId); |
| 0 | 91 | | blockedListApiBridge.SendUnblockPlayer(userId); |
| 0 | 92 | | socialAnalytics.SendPlayerUnblocked(friendsController.IsFriend(userId), PlayerActionSource.MyProfile |
| | 93 | |
|
| 0 | 94 | | }), true); |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | public void Dispose() |
| | 98 | | { |
| 0 | 99 | | dataStore.myAccount.isMyAccountSectionVisible.OnChange -= OnMyAccountSectionVisibleChanged; |
| 0 | 100 | | dataStore.myAccount.openSection.OnChange -= OnMyAccountSectionTabChanged; |
| 0 | 101 | | ownUserProfile.OnUpdate -= OnOwnUserProfileUpdated; |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | private async void UpdateBlockedUserList(List<string> blockedUsersList, CancellationToken token) |
| | 105 | | { |
| 0 | 106 | | view.SetupBlockedList(); |
| | 107 | |
|
| | 108 | | async UniTaskVoid UpdateChannelMembersAsync(IEnumerable<string> blockedUsers, |
| | 109 | | CancellationToken cancellationToken) |
| | 110 | | { |
| 0 | 111 | | view.SetLoadingActive(false); |
| | 112 | |
|
| 0 | 113 | | foreach (string member in blockedUsers) |
| | 114 | | { |
| 0 | 115 | | UserProfile memberProfile = userProfileBridge.Get(member); |
| | 116 | |
|
| 0 | 117 | | try { memberProfile ??= await userProfileBridge.RequestFullUserProfileAsync(member, cancellationToke |
| 0 | 118 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 119 | | { |
| 0 | 120 | | var fallbackBlockedUserEntry = new BlockedUserEntryModel |
| | 121 | | { |
| | 122 | | thumbnailUrl = "", |
| | 123 | | userId = member, |
| | 124 | | userName = member, |
| | 125 | | }; |
| | 126 | |
|
| 0 | 127 | | view.Set(fallbackBlockedUserEntry); |
| 0 | 128 | | } |
| | 129 | |
|
| 0 | 130 | | if (memberProfile != null) |
| | 131 | | { |
| 0 | 132 | | var userToAdd = new BlockedUserEntryModel |
| | 133 | | { |
| | 134 | | thumbnailUrl = memberProfile.face256SnapshotURL, |
| | 135 | | userId = memberProfile.userId, |
| | 136 | | userName = memberProfile.userName, |
| | 137 | | }; |
| | 138 | |
|
| 0 | 139 | | view.Set(userToAdd); |
| | 140 | | } |
| 0 | 141 | | } |
| | 142 | |
|
| 0 | 143 | | view.SetLoadingActive(false); |
| 0 | 144 | | } |
| | 145 | |
|
| 0 | 146 | | UpdateChannelMembersAsync(blockedUsersList, token).Forget(); |
| 0 | 147 | | } |
| | 148 | | } |
| | 149 | | } |