| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Interface; |
| | 3 | | using DCL.UserProfiles; |
| | 4 | | using System; |
| | 5 | | using JetBrains.Annotations; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | public class UserProfileController : MonoBehaviour |
| | 11 | | { |
| | 12 | | private const int REQUEST_TIMEOUT = 30; |
| | 13 | |
|
| 326 | 14 | | public static UserProfileController i { get; private set; } |
| | 15 | |
|
| | 16 | | public event Action OnBaseWereablesFail; |
| | 17 | |
|
| | 18 | | private static UserProfileDictionary userProfilesCatalogValue; |
| | 19 | |
|
| 22 | 20 | | private readonly Dictionary<string, UniTaskCompletionSource<UserProfile>> pendingUserProfileTasks = new (StringCompa |
| 22 | 21 | | private readonly Dictionary<string, UniTaskCompletionSource<UserProfile>> saveProfileTask = new (); |
| 22 | 22 | | private readonly List<WebInterface.SaveLinksPayload.Link> linkList = new (); |
| | 23 | |
|
| | 24 | | public static UserProfileDictionary userProfilesCatalog |
| | 25 | | { |
| | 26 | | get |
| | 27 | | { |
| 105 | 28 | | if (userProfilesCatalogValue == null) |
| | 29 | | { |
| 1 | 30 | | userProfilesCatalogValue = Resources.Load<UserProfileDictionary>("UserProfilesCatalog"); |
| | 31 | | } |
| | 32 | |
|
| 105 | 33 | | return userProfilesCatalogValue; |
| | 34 | | } |
| | 35 | | } |
| | 36 | |
|
| | 37 | | [NonSerialized] public UserProfile ownUserProfile; |
| | 38 | |
|
| 9 | 39 | | public UserProfileDictionary AllProfiles => userProfilesCatalog; |
| | 40 | |
|
| | 41 | | public void Awake() |
| | 42 | | { |
| 22 | 43 | | i = this; |
| 22 | 44 | | ownUserProfile = UserProfile.GetOwnUserProfile(); |
| 22 | 45 | | } |
| | 46 | |
|
| | 47 | | [PublicAPI] |
| | 48 | | public void LoadProfile(string payload) |
| | 49 | | { |
| | 50 | | // We used to request base wearables here but it raises race condition issues |
| | 51 | | // The current realm is not set yet thus ends up requesting wearables to an incorrect catalyst's content url |
| | 52 | | // Resolving inconsistent wearables information |
| 0 | 53 | | if (payload == null) |
| 0 | 54 | | return; |
| | 55 | |
|
| 0 | 56 | | var model = JsonUtility.FromJson<UserProfileModelDTO>(payload).ToUserProfileModel(); |
| | 57 | |
|
| 0 | 58 | | ownUserProfile.UpdateData(model); |
| 0 | 59 | | userProfilesCatalog.Add(model.userId, ownUserProfile); |
| | 60 | |
|
| 0 | 61 | | foreach (var task in saveProfileTask.Values) |
| 0 | 62 | | task.TrySetResult(ownUserProfile); |
| 0 | 63 | | saveProfileTask.Clear(); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | [PublicAPI] |
| 0 | 67 | | public void AddUserProfileToCatalog(string payload) { AddUserProfileToCatalog(JsonUtility.FromJson<UserProfileModelD |
| | 68 | |
|
| | 69 | | [PublicAPI] |
| | 70 | | public void AddUserProfilesToCatalog(string payload) |
| | 71 | | { |
| 0 | 72 | | var usersPayload = JsonUtility.FromJson<AddUserProfilesToCatalogPayload>(payload); |
| 0 | 73 | | var users = usersPayload.users; |
| 0 | 74 | | var count = users.Length; |
| | 75 | |
|
| 0 | 76 | | for (var i = 0; i < count; ++i) |
| 0 | 77 | | AddUserProfileToCatalog(users[i]); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | [PublicAPI] |
| | 81 | | public void RemoveUserProfilesFromCatalog(string payload) |
| | 82 | | { |
| 0 | 83 | | string[] usernames = JsonUtility.FromJson<string[]>(payload); |
| 0 | 84 | | for (int index = 0; index < usernames.Length; index++) |
| | 85 | | { |
| 0 | 86 | | RemoveUserProfileFromCatalog(userProfilesCatalog.Get(usernames[index])); |
| | 87 | | } |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | public void AddUserProfileToCatalog(UserProfileModel model) |
| | 91 | | { |
| | 92 | | // TODO: the renderer should not alter the userId nor ethAddress, this is just a patch derived from a kernel iss |
| 40 | 93 | | model.userId = model.userId.ToLower(); |
| 40 | 94 | | model.ethAddress = model.ethAddress?.ToLower(); |
| | 95 | |
|
| 40 | 96 | | if (!userProfilesCatalog.TryGetValue(model.userId, out UserProfile userProfile)) |
| 6 | 97 | | userProfile = ScriptableObject.CreateInstance<UserProfile>(); |
| | 98 | |
|
| 40 | 99 | | userProfile.UpdateData(model); |
| 40 | 100 | | userProfilesCatalog.Add(model.userId, userProfile); |
| | 101 | |
|
| 40 | 102 | | if (pendingUserProfileTasks.TryGetValue(userProfile.userId, out var existingTask)) |
| | 103 | | { |
| 0 | 104 | | existingTask.TrySetResult(userProfile); |
| 0 | 105 | | pendingUserProfileTasks.Remove(userProfile.userId); |
| | 106 | | } |
| 40 | 107 | | } |
| | 108 | |
|
| 0 | 109 | | public static UserProfile GetProfileByUserId(string targetUserId) { return userProfilesCatalog.Get(targetUserId); } |
| | 110 | |
|
| | 111 | | public void RemoveUserProfileFromCatalog(UserProfile userProfile) |
| | 112 | | { |
| 0 | 113 | | if (userProfile == null) |
| 0 | 114 | | return; |
| | 115 | |
|
| 0 | 116 | | userProfilesCatalog.Remove(userProfile.userId); |
| 0 | 117 | | Destroy(userProfile); |
| 0 | 118 | | } |
| | 119 | |
|
| 0 | 120 | | public void ClearProfilesCatalog() { userProfilesCatalog?.Clear(); } |
| | 121 | |
|
| | 122 | | public UniTask<UserProfile> RequestFullUserProfileAsync(string userId, CancellationToken cancellationToken = default |
| | 123 | | { |
| 0 | 124 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 125 | |
|
| 0 | 126 | | if (pendingUserProfileTasks.TryGetValue(userId, out var existingTask)) |
| 0 | 127 | | return existingTask.Task; |
| | 128 | |
|
| 0 | 129 | | var task = new UniTaskCompletionSource<UserProfile>(); |
| 0 | 130 | | cancellationToken.RegisterWithoutCaptureExecutionContext(() => task.TrySetCanceled()); |
| 0 | 131 | | pendingUserProfileTasks[userId] = task; |
| | 132 | |
|
| 0 | 133 | | WebInterface.SendRequestUserProfile(userId); |
| | 134 | |
|
| 0 | 135 | | return task.Task.Timeout(TimeSpan.FromSeconds(REQUEST_TIMEOUT)); |
| | 136 | | } |
| | 137 | |
|
| | 138 | | public UniTask<UserProfile> SaveLinks(List<UserProfileModel.Link> links, CancellationToken cancellationToken) |
| | 139 | | { |
| 0 | 140 | | return SaveUserProfile(() => |
| | 141 | | { |
| 0 | 142 | | linkList.Clear(); |
| | 143 | |
|
| 0 | 144 | | foreach (UserProfileModel.Link link in links) |
| | 145 | | { |
| 0 | 146 | | linkList.Add(new WebInterface.SaveLinksPayload.Link |
| | 147 | | { |
| | 148 | | title = link.title, |
| | 149 | | url = link.url, |
| | 150 | | }); |
| | 151 | | } |
| | 152 | |
|
| 0 | 153 | | WebInterface.SaveProfileLinks(new WebInterface.SaveLinksPayload |
| | 154 | | { |
| | 155 | | links = linkList, |
| | 156 | | }); |
| 0 | 157 | | }, |
| | 158 | | "links", cancellationToken); |
| | 159 | | } |
| | 160 | |
|
| | 161 | | public UniTask<UserProfile> SaveVerifiedName(string name, CancellationToken cancellationToken) |
| | 162 | | { |
| 0 | 163 | | return SaveUserProfile(() => WebInterface.SendSaveUserVerifiedName(name), |
| | 164 | | "verified_name", cancellationToken); |
| | 165 | | } |
| | 166 | |
|
| | 167 | | public UniTask<UserProfile> SaveUnverifiedName(string name, CancellationToken cancellationToken) |
| | 168 | | { |
| 0 | 169 | | return SaveUserProfile(() => WebInterface.SendSaveUserUnverifiedName(name), |
| | 170 | | "unverified_name", cancellationToken); |
| | 171 | | } |
| | 172 | |
|
| | 173 | | public UniTask<UserProfile> SaveDescription(string description, CancellationToken cancellationToken) |
| | 174 | | { |
| 0 | 175 | | return SaveUserProfile(() => WebInterface.SendSaveUserDescription(description), |
| | 176 | | "description", cancellationToken); |
| | 177 | | } |
| | 178 | |
|
| | 179 | | public UniTask<UserProfile> SaveAdditionalInfo(AdditionalInfo additionalInfo, CancellationToken cancellationToken) |
| | 180 | | { |
| 0 | 181 | | return SaveUserProfile(() => WebInterface.SaveAdditionalInfo(new WebInterface.SaveAdditionalInfoPayload |
| | 182 | | { |
| | 183 | | country = additionalInfo.Country, |
| | 184 | | gender = additionalInfo.Gender, |
| | 185 | | pronouns = additionalInfo.Pronouns, |
| | 186 | | relationshipStatus = additionalInfo.RelationshipStatus, |
| | 187 | | sexualOrientation = additionalInfo.SexualOrientation, |
| | 188 | | language = additionalInfo.Language, |
| | 189 | | profession = additionalInfo.Profession, |
| | 190 | | birthdate = additionalInfo.BirthDate != null ? new DateTimeOffset(additionalInfo.BirthDate.Value).ToUnix |
| | 191 | | realName = additionalInfo.RealName, |
| | 192 | | hobbies = additionalInfo.Hobbies, |
| | 193 | | employmentStatus = additionalInfo.EmploymentStatus, |
| | 194 | | }), |
| | 195 | | "additional_info", cancellationToken); |
| | 196 | | } |
| | 197 | |
|
| | 198 | | private UniTask<UserProfile> SaveUserProfile(Action webInterfaceCall, string operationType, CancellationToken cancel |
| | 199 | | { |
| 0 | 200 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 201 | |
|
| 0 | 202 | | if (saveProfileTask.ContainsKey(operationType)) |
| 0 | 203 | | return saveProfileTask[operationType].Task.AttachExternalCancellation(cancellationToken); |
| | 204 | |
|
| 0 | 205 | | var task = new UniTaskCompletionSource<UserProfile>(); |
| 0 | 206 | | saveProfileTask[operationType] = task; |
| | 207 | |
|
| 0 | 208 | | webInterfaceCall.Invoke(); |
| | 209 | |
|
| 0 | 210 | | return task.Task |
| | 211 | | .Timeout(TimeSpan.FromSeconds(REQUEST_TIMEOUT)) |
| | 212 | | .AttachExternalCancellation(cancellationToken); |
| | 213 | | } |
| | 214 | | } |