| | 1 | | using AvatarSystem; |
| | 2 | | using Cysharp.Threading.Tasks; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.ProfanityFiltering; |
| | 5 | | using DCLServices.Lambdas.LandsService; |
| | 6 | | using DCLServices.Lambdas.NamesService; |
| | 7 | | using System; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Threading; |
| | 11 | | using UnityEngine; |
| | 12 | |
|
| | 13 | | namespace DCL.Social.Passports |
| | 14 | | { |
| | 15 | | public class PassportNavigationComponentController : IDisposable |
| | 16 | | { |
| | 17 | | private const int MAX_NFT_COUNT = 40; |
| | 18 | |
|
| | 19 | | private readonly IProfanityFilter profanityFilter; |
| | 20 | | private readonly IWearableItemResolver wearableItemResolver; |
| | 21 | | private readonly IWearableCatalogBridge wearableCatalogBridge; |
| | 22 | | private readonly IEmotesCatalogService emotesCatalogService; |
| | 23 | | private readonly INamesService namesService; |
| | 24 | | private readonly ILandsService landsService; |
| | 25 | | private readonly IUserProfileBridge userProfileBridge; |
| | 26 | | private readonly DataStore dataStore; |
| | 27 | | private string currentUserId; |
| | 28 | |
|
| 0 | 29 | | private UserProfile ownUserProfile => userProfileBridge.GetOwn(); |
| | 30 | | private readonly IPassportNavigationComponentView view; |
| 0 | 31 | | private HashSet<string> cachedAvatarEquippedWearables = new (); |
| 0 | 32 | | private readonly List<string> loadedWearables = new List<string>(); |
| | 33 | | public event Action<string, string> OnClickBuyNft; |
| | 34 | | public event Action OnClickedLink; |
| | 35 | | public event Action OnClickCollectibles; |
| 0 | 36 | | private CancellationTokenSource cts = new CancellationTokenSource(); |
| | 37 | | private Promise<WearableItem[]> wearablesPromise; |
| | 38 | | private Promise<WearableItem[]> emotesPromise; |
| | 39 | |
|
| 0 | 40 | | public PassportNavigationComponentController( |
| | 41 | | IPassportNavigationComponentView view, |
| | 42 | | IProfanityFilter profanityFilter, |
| | 43 | | IWearableItemResolver wearableItemResolver, |
| | 44 | | IWearableCatalogBridge wearableCatalogBridge, |
| | 45 | | IEmotesCatalogService emotesCatalogService, |
| | 46 | | INamesService namesService, |
| | 47 | | ILandsService landsService, |
| | 48 | | IUserProfileBridge userProfileBridge, |
| | 49 | | DataStore dataStore) |
| | 50 | | { |
| 0 | 51 | | this.view = view; |
| 0 | 52 | | this.profanityFilter = profanityFilter; |
| 0 | 53 | | this.wearableItemResolver = wearableItemResolver; |
| 0 | 54 | | this.wearableCatalogBridge = wearableCatalogBridge; |
| 0 | 55 | | this.emotesCatalogService = emotesCatalogService; |
| 0 | 56 | | this.namesService = namesService; |
| 0 | 57 | | this.landsService = landsService; |
| 0 | 58 | | this.userProfileBridge = userProfileBridge; |
| 0 | 59 | | this.dataStore = dataStore; |
| 0 | 60 | | view.OnClickBuyNft += (wearableId, wearableType) => OnClickBuyNft?.Invoke(wearableType is "name" or "parcel" |
| 0 | 61 | | view.OnClickCollectibles += () => OnClickCollectibles?.Invoke(); |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | public void UpdateWithUserProfile(UserProfile userProfile) |
| | 65 | | { |
| | 66 | | async UniTaskVoid UpdateWithUserProfileAsync() |
| | 67 | | { |
| 0 | 68 | | var ct = cts.Token; |
| 0 | 69 | | currentUserId = userProfile.userId; |
| 0 | 70 | | wearableCatalogBridge.RemoveWearablesInUse(loadedWearables); |
| 0 | 71 | | string filteredName = await FilterContentAsync(userProfile.userName).AttachExternalCancellation(ct); |
| 0 | 72 | | view.SetGuestUser(userProfile.isGuest); |
| 0 | 73 | | view.SetName(filteredName); |
| 0 | 74 | | view.SetOwnUserTexts(userProfile.userId == ownUserProfile.userId); |
| | 75 | |
|
| 0 | 76 | | if (!userProfile.isGuest) |
| | 77 | | { |
| 0 | 78 | | string filteredDescription = await FilterContentAsync(userProfile.description).AttachExternalCancell |
| 0 | 79 | | view.SetDescription(filteredDescription); |
| 0 | 80 | | view.SetHasBlockedOwnUser(userProfile.IsBlocked(ownUserProfile.userId)); |
| 0 | 81 | | LoadAndShowOwnedNamesAsync(userProfile, ct).Forget(); |
| 0 | 82 | | LoadAndShowOwnedLandsAsync(userProfile, ct).Forget(); |
| 0 | 83 | | LoadAndDisplayEquippedWearablesAsync(userProfile, ct).Forget(); |
| | 84 | | } |
| 0 | 85 | | } |
| | 86 | |
|
| 0 | 87 | | cts?.Cancel(); |
| 0 | 88 | | cts?.Dispose(); |
| 0 | 89 | | cts = new CancellationTokenSource(); |
| 0 | 90 | | UpdateWithUserProfileAsync().Forget(); |
| 0 | 91 | | } |
| | 92 | |
|
| 0 | 93 | | public void CloseAllNFTItemInfos() => view.CloseAllNFTItemInfos(); |
| | 94 | |
|
| | 95 | | public void SetViewInitialPage() => |
| 0 | 96 | | view.SetInitialPage(); |
| | 97 | |
|
| | 98 | | public void Dispose() |
| | 99 | | { |
| 0 | 100 | | cts?.Cancel(); |
| 0 | 101 | | cts?.Dispose(); |
| 0 | 102 | | cts = null; |
| | 103 | |
|
| 0 | 104 | | wearablesPromise?.Dispose(); |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | private async UniTask LoadAndDisplayEquippedWearablesAsync(UserProfile userProfile, CancellationToken ct) |
| | 108 | | { |
| 0 | 109 | | foreach (var t in userProfile.avatar.wearables) |
| | 110 | | { |
| 0 | 111 | | if (!cachedAvatarEquippedWearables.Contains(t)) |
| | 112 | | { |
| 0 | 113 | | view.InitializeView(); |
| 0 | 114 | | cachedAvatarEquippedWearables = new HashSet<string>(userProfile.avatar.wearables); |
| 0 | 115 | | LoadAndShowOwnedWearables(userProfile); |
| 0 | 116 | | LoadAndShowOwnedEmotes(userProfile).Forget(); |
| | 117 | |
|
| 0 | 118 | | WearableItem[] wearableItems = await wearableItemResolver.Resolve(userProfile.avatar.wearables, ct) |
| 0 | 119 | | view.SetEquippedWearables(wearableItems, userProfile.avatar.bodyShape); |
| 0 | 120 | | return; |
| | 121 | | } |
| | 122 | | } |
| 0 | 123 | | } |
| | 124 | |
|
| | 125 | | private void LoadAndShowOwnedWearables(UserProfile userProfile) |
| | 126 | | { |
| 0 | 127 | | view.SetCollectibleWearablesLoadingActive(true); |
| 0 | 128 | | wearablesPromise = wearableCatalogBridge.RequestOwnedWearables(userProfile.userId).Then(wearables => |
| | 129 | | { |
| 0 | 130 | | IGrouping<string, WearableItem>[] wearableItems = wearables.GroupBy(i => i.id).ToArray(); |
| 0 | 131 | | string[] wearableIds = wearableItems.Select(g => g.First().id).Take(MAX_NFT_COUNT).ToArray(); |
| 0 | 132 | | userProfile.SetInventory(wearableIds); |
| 0 | 133 | | loadedWearables.AddRange(wearableIds); |
| | 134 | |
|
| 0 | 135 | | var containedWearables = wearableItems |
| 0 | 136 | | .Select(g => g.First()) |
| | 137 | | .Take(MAX_NFT_COUNT) |
| 0 | 138 | | .Where(wearable => wearableCatalogBridge.IsValidWearable(wearable.id)); |
| | 139 | |
|
| 0 | 140 | | view.SetCollectibleWearables(containedWearables.ToArray()); |
| 0 | 141 | | view.SetCollectibleWearablesLoadingActive(false); |
| 0 | 142 | | }); |
| | 143 | |
|
| 0 | 144 | | wearablesPromise.Catch(Debug.LogError); |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | private async UniTask LoadAndShowOwnedEmotes(UserProfile userProfile) |
| | 148 | | { |
| 0 | 149 | | view.SetCollectibleEmotesLoadingActive(true); |
| | 150 | |
|
| 0 | 151 | | WearableItem[] emotes = await emotesCatalogService.RequestOwnedEmotesAsync(userProfile.userId, cts.Token); |
| 0 | 152 | | WearableItem[] emoteItems = emotes.GroupBy(i => i.id).Select(g => g.First()).Take(MAX_NFT_COUNT).ToArray(); |
| 0 | 153 | | view.SetCollectibleEmotes(emoteItems); |
| 0 | 154 | | view.SetCollectibleEmotesLoadingActive(false); |
| 0 | 155 | | } |
| | 156 | |
|
| | 157 | | private async UniTask LoadAndShowOwnedNamesAsync(UserProfile userProfile, CancellationToken ct) |
| | 158 | | { |
| 0 | 159 | | view.SetCollectibleNamesLoadingActive(true); |
| 0 | 160 | | using var pagePointer = namesService.GetPaginationPointer(userProfile.userId, MAX_NFT_COUNT, CancellationTok |
| 0 | 161 | | var response = await pagePointer.GetPageAsync(1, ct); |
| 0 | 162 | | var namesResult = Array.Empty<NamesResponse.NameEntry>(); |
| | 163 | |
|
| 0 | 164 | | if (response.success) |
| 0 | 165 | | namesResult = response.response.Names.ToArray(); |
| | 166 | | else |
| 0 | 167 | | Debug.LogError("Error requesting names lambdas!"); |
| | 168 | |
|
| 0 | 169 | | view.SetCollectibleNames(namesResult); |
| 0 | 170 | | view.SetCollectibleNamesLoadingActive(false); |
| 0 | 171 | | } |
| | 172 | |
|
| | 173 | | private async UniTask LoadAndShowOwnedLandsAsync(UserProfile userProfile, CancellationToken ct) |
| | 174 | | { |
| 0 | 175 | | view.SetCollectibleLandsLoadingActive(true); |
| | 176 | | // TODO (Santi): Use userProfile.userId here!! |
| 0 | 177 | | using var pagePointer = landsService.GetPaginationPointer(userProfile.userId, MAX_NFT_COUNT, CancellationTok |
| 0 | 178 | | var response = await pagePointer.GetPageAsync(1, ct); |
| 0 | 179 | | var landsResult = Array.Empty<LandsResponse.LandEntry>(); |
| | 180 | |
|
| 0 | 181 | | if (response.success) |
| 0 | 182 | | landsResult = response.response.Lands.ToArray(); |
| | 183 | | else |
| 0 | 184 | | Debug.LogError("Error requesting lands lambdas!"); |
| | 185 | |
|
| 0 | 186 | | view.SetCollectibleLands(landsResult); |
| 0 | 187 | | view.SetCollectibleLandsLoadingActive(false); |
| 0 | 188 | | } |
| | 189 | |
|
| | 190 | | private async UniTask<string> FilterContentAsync(string filterContent) => |
| 0 | 191 | | IsProfanityFilteringEnabled() |
| | 192 | | ? await profanityFilter.Filter(filterContent) |
| | 193 | | : filterContent; |
| | 194 | |
|
| | 195 | | private bool IsProfanityFilteringEnabled() => |
| 0 | 196 | | dataStore.settings.profanityChatFilteringEnabled.Get(); |
| | 197 | | } |
| | 198 | | } |