< Summary

Class:UserProfileController
Assembly:UserProfile
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/UserProfile/UserProfileController.cs
Covered lines:19
Uncovered lines:54
Coverable lines:73
Total lines:214
Line coverage:26% (19 of 73)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:21
Method coverage:33.3% (7 of 21)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UserProfileController()0%110100%
Awake()0%110100%
LoadProfile(...)0%12300%
AddUserProfileToCatalog(...)0%2100%
AddUserProfilesToCatalog(...)0%6200%
RemoveUserProfilesFromCatalog(...)0%6200%
AddUserProfileToCatalog(...)0%5.25080%
GetProfileByUserId(...)0%2100%
RemoveUserProfileFromCatalog(...)0%6200%
ClearProfilesCatalog()0%6200%
RequestFullUserProfileAsync(...)0%6200%
SaveLinks(...)0%2100%
SaveVerifiedName(...)0%2100%
SaveUnverifiedName(...)0%2100%
SaveDescription(...)0%2100%
SaveAdditionalInfo(...)0%2100%
SaveUserProfile(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/UserProfile/UserProfileController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL.Interface;
 3using DCL.UserProfiles;
 4using System;
 5using JetBrains.Annotations;
 6using System.Collections.Generic;
 7using System.Threading;
 8using UnityEngine;
 9
 10public class UserProfileController : MonoBehaviour
 11{
 12    private const int REQUEST_TIMEOUT = 30;
 13
 32614    public static UserProfileController i { get; private set; }
 15
 16    public event Action OnBaseWereablesFail;
 17
 18    private static UserProfileDictionary userProfilesCatalogValue;
 19
 2220    private readonly Dictionary<string, UniTaskCompletionSource<UserProfile>> pendingUserProfileTasks = new (StringCompa
 2221    private readonly Dictionary<string, UniTaskCompletionSource<UserProfile>> saveProfileTask = new ();
 2222    private readonly List<WebInterface.SaveLinksPayload.Link> linkList = new ();
 23
 24    public static UserProfileDictionary userProfilesCatalog
 25    {
 26        get
 27        {
 10528            if (userProfilesCatalogValue == null)
 29            {
 130                userProfilesCatalogValue = Resources.Load<UserProfileDictionary>("UserProfilesCatalog");
 31            }
 32
 10533            return userProfilesCatalogValue;
 34        }
 35    }
 36
 37    [NonSerialized] public UserProfile ownUserProfile;
 38
 939    public UserProfileDictionary AllProfiles => userProfilesCatalog;
 40
 41    public void Awake()
 42    {
 2243        i = this;
 2244        ownUserProfile = UserProfile.GetOwnUserProfile();
 2245    }
 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
 053        if (payload == null)
 054            return;
 55
 056        var model = JsonUtility.FromJson<UserProfileModelDTO>(payload).ToUserProfileModel();
 57
 058        ownUserProfile.UpdateData(model);
 059        userProfilesCatalog.Add(model.userId, ownUserProfile);
 60
 061        foreach (var task in saveProfileTask.Values)
 062            task.TrySetResult(ownUserProfile);
 063        saveProfileTask.Clear();
 064    }
 65
 66    [PublicAPI]
 067    public void AddUserProfileToCatalog(string payload) { AddUserProfileToCatalog(JsonUtility.FromJson<UserProfileModelD
 68
 69    [PublicAPI]
 70    public void AddUserProfilesToCatalog(string payload)
 71    {
 072        var usersPayload = JsonUtility.FromJson<AddUserProfilesToCatalogPayload>(payload);
 073        var users = usersPayload.users;
 074        var count = users.Length;
 75
 076        for (var i = 0; i < count; ++i)
 077            AddUserProfileToCatalog(users[i]);
 078    }
 79
 80    [PublicAPI]
 81    public void RemoveUserProfilesFromCatalog(string payload)
 82    {
 083        string[] usernames = JsonUtility.FromJson<string[]>(payload);
 084        for (int index = 0; index < usernames.Length; index++)
 85        {
 086            RemoveUserProfileFromCatalog(userProfilesCatalog.Get(usernames[index]));
 87        }
 088    }
 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
 4093        model.userId = model.userId.ToLower();
 4094        model.ethAddress = model.ethAddress?.ToLower();
 95
 4096        if (!userProfilesCatalog.TryGetValue(model.userId, out UserProfile userProfile))
 697            userProfile = ScriptableObject.CreateInstance<UserProfile>();
 98
 4099        userProfile.UpdateData(model);
 40100        userProfilesCatalog.Add(model.userId, userProfile);
 101
 40102        if (pendingUserProfileTasks.TryGetValue(userProfile.userId, out var existingTask))
 103        {
 0104            existingTask.TrySetResult(userProfile);
 0105            pendingUserProfileTasks.Remove(userProfile.userId);
 106        }
 40107    }
 108
 0109    public static UserProfile GetProfileByUserId(string targetUserId) { return userProfilesCatalog.Get(targetUserId); }
 110
 111    public void RemoveUserProfileFromCatalog(UserProfile userProfile)
 112    {
 0113        if (userProfile == null)
 0114            return;
 115
 0116        userProfilesCatalog.Remove(userProfile.userId);
 0117        Destroy(userProfile);
 0118    }
 119
 0120    public void ClearProfilesCatalog() { userProfilesCatalog?.Clear(); }
 121
 122    public UniTask<UserProfile> RequestFullUserProfileAsync(string userId, CancellationToken cancellationToken = default
 123    {
 0124        cancellationToken.ThrowIfCancellationRequested();
 125
 0126        if (pendingUserProfileTasks.TryGetValue(userId, out var existingTask))
 0127            return existingTask.Task;
 128
 0129        var task = new UniTaskCompletionSource<UserProfile>();
 0130        cancellationToken.RegisterWithoutCaptureExecutionContext(() => task.TrySetCanceled());
 0131        pendingUserProfileTasks[userId] = task;
 132
 0133        WebInterface.SendRequestUserProfile(userId);
 134
 0135        return task.Task.Timeout(TimeSpan.FromSeconds(REQUEST_TIMEOUT));
 136    }
 137
 138    public UniTask<UserProfile> SaveLinks(List<UserProfileModel.Link> links, CancellationToken cancellationToken)
 139    {
 0140        return SaveUserProfile(() =>
 141            {
 0142                linkList.Clear();
 143
 0144                foreach (UserProfileModel.Link link in links)
 145                {
 0146                    linkList.Add(new WebInterface.SaveLinksPayload.Link
 147                    {
 148                        title = link.title,
 149                        url = link.url,
 150                    });
 151                }
 152
 0153                WebInterface.SaveProfileLinks(new WebInterface.SaveLinksPayload
 154                {
 155                    links = linkList,
 156                });
 0157            },
 158            "links", cancellationToken);
 159    }
 160
 161    public UniTask<UserProfile> SaveVerifiedName(string name, CancellationToken cancellationToken)
 162    {
 0163        return SaveUserProfile(() => WebInterface.SendSaveUserVerifiedName(name),
 164            "verified_name", cancellationToken);
 165    }
 166
 167    public UniTask<UserProfile> SaveUnverifiedName(string name, CancellationToken cancellationToken)
 168    {
 0169        return SaveUserProfile(() => WebInterface.SendSaveUserUnverifiedName(name),
 170            "unverified_name", cancellationToken);
 171    }
 172
 173    public UniTask<UserProfile> SaveDescription(string description, CancellationToken cancellationToken)
 174    {
 0175        return SaveUserProfile(() => WebInterface.SendSaveUserDescription(description),
 176            "description", cancellationToken);
 177    }
 178
 179    public UniTask<UserProfile> SaveAdditionalInfo(AdditionalInfo additionalInfo, CancellationToken cancellationToken)
 180    {
 0181        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    {
 0200        cancellationToken.ThrowIfCancellationRequested();
 201
 0202        if (saveProfileTask.ContainsKey(operationType))
 0203            return saveProfileTask[operationType].Task.AttachExternalCancellation(cancellationToken);
 204
 0205        var task = new UniTaskCompletionSource<UserProfile>();
 0206        saveProfileTask[operationType] = task;
 207
 0208        webInterfaceCall.Invoke();
 209
 0210        return task.Task
 211                   .Timeout(TimeSpan.FromSeconds(REQUEST_TIMEOUT))
 212                   .AttachExternalCancellation(cancellationToken);
 213    }
 214}