< Summary

Class:DCL.Social.Friends.WebInterfaceFriendsApiBridge
Assembly:FriendsController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/FriendsController/WebInterfaceFriendsApiBridge.cs
/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/FriendsController/WebInterfaceFriendsApiBridgeUtilies.cs
Covered lines:0
Uncovered lines:81
Coverable lines:81
Total lines:274
Line coverage:0% (0 of 81)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WebInterfaceFriendsApiBridge()0%2100%
InitializeFriends(...)0%6200%
FriendNotFound(...)0%6200%
AddFriends(...)0%6200%
AddFriendRequests(...)0%6200%
AddFriendRequest(...)0%6200%
AddFriendsWithDirectMessages(...)0%6200%
UpdateUserPresence(...)0%6200%
UpdateFriendshipStatus(...)0%12300%
UpdateTotalFriendRequests(...)0%6200%
UpdateTotalFriends(...)0%6200%
RequestFriendshipConfirmation(...)0%6200%
RequestFriendshipError(...)0%6200%
CancelFriendshipConfirmation(...)0%12300%
CancelFriendshipError(...)0%6200%
RejectFriendship(...)0%2100%
RemoveFriend(...)0%2100%
GetFriends(...)0%2100%
GetFriends(...)0%2100%
GetFriendRequests(...)0%2100%
GetFriendRequestsAsync(...)0%2100%
GetFriendsWithDirectMessages(...)0%2100%
RequestFriendship(...)0%2100%
RequestFriendshipAsync(...)0%2100%
CancelRequestAsync(...)0%2100%
CancelRequestByUserIdAsync(...)0%6200%
CancelRequestByUserId(...)0%2100%
AcceptFriendship(...)0%2100%
ForceInitialization()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/FriendsController/WebInterfaceFriendsApiBridge.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using Cysharp.Threading.Tasks;
 4using DCL.Interface;
 5using DCl.Social.Friends;
 6using JetBrains.Annotations;
 7using UnityEngine;
 8
 9namespace DCL.Social.Friends
 10{
 11    public partial class WebInterfaceFriendsApiBridge : MonoBehaviour, IFriendsApiBridge
 12    {
 013        private readonly Dictionary<string, IUniTaskSource> pendingRequests = new ();
 014        private readonly Dictionary<string, UniTaskCompletionSource<FriendshipUpdateStatusMessage>> updatedFriendshipPen
 15
 16        public event Action<FriendshipInitializationMessage> OnInitialized;
 17        public event Action<string> OnFriendNotFound;
 18        public event Action<AddFriendsPayload> OnFriendsAdded;
 19        public event Action<AddFriendRequestsPayload> OnFriendRequestsAdded; // TODO (NEW FRIEND REQUESTS): remove when 
 20        public event Action<AddFriendsWithDirectMessagesPayload> OnFriendWithDirectMessagesAdded;
 21        public event Action<UserStatus> OnUserPresenceUpdated;
 22        public event Action<FriendshipUpdateStatusMessage> OnFriendshipStatusUpdated;
 23        public event Action<UpdateTotalFriendRequestsPayload> OnTotalFriendRequestCountUpdated;
 24        public event Action<UpdateTotalFriendsPayload> OnTotalFriendCountUpdated;
 25        public event Action<FriendRequestPayload> OnFriendRequestAdded;
 26
 27        [PublicAPI]
 28        public void InitializeFriends(string json) =>
 029            OnInitialized?.Invoke(JsonUtility.FromJson<FriendshipInitializationMessage>(json));
 30
 31        [PublicAPI]
 32        public void FriendNotFound(string name) =>
 033            OnFriendNotFound?.Invoke(name);
 34
 35        [PublicAPI]
 36        public void AddFriends(string json) =>
 037            OnFriendsAdded?.Invoke(JsonUtility.FromJson<AddFriendsPayload>(json));
 38
 39        // TODO (NEW FRIEND REQUESTS): remove when we don't need to keep the retro-compatibility with the old version
 40        [PublicAPI]
 41        public void AddFriendRequests(string json) =>
 042            OnFriendRequestsAdded?.Invoke(JsonUtility.FromJson<AddFriendRequestsPayload>(json));
 43
 44        [PublicAPI]
 45        public void AddFriendRequest(string json)
 46        {
 047            var payload = JsonUtility.FromJson<FriendRequestPayload>(json);
 048            OnFriendRequestAdded?.Invoke(payload);
 049        }
 50
 51        [PublicAPI]
 52        public void AddFriendsWithDirectMessages(string json) =>
 053            OnFriendWithDirectMessagesAdded?.Invoke(JsonUtility.FromJson<AddFriendsWithDirectMessagesPayload>(json));
 54
 55        [PublicAPI]
 56        public void UpdateUserPresence(string json) =>
 057            OnUserPresenceUpdated?.Invoke(JsonUtility.FromJson<UserStatus>(json));
 58
 59        [PublicAPI]
 60        public void UpdateFriendshipStatus(string json)
 61        {
 062            FriendshipUpdateStatusMessage msg = JsonUtility.FromJson<FriendshipUpdateStatusMessage>(json);
 063            string userId = msg.userId;
 64
 065            if (updatedFriendshipPendingRequests.ContainsKey(userId))
 066                updatedFriendshipPendingRequests[userId].TrySetResult(msg);
 67
 068            updatedFriendshipPendingRequests.Remove(userId);
 69
 070            OnFriendshipStatusUpdated?.Invoke(msg);
 071        }
 72
 73        [PublicAPI]
 74        public void UpdateTotalFriendRequests(string json) =>
 075            OnTotalFriendRequestCountUpdated?.Invoke(JsonUtility.FromJson<UpdateTotalFriendRequestsPayload>(json));
 76
 77        [PublicAPI]
 78        public void UpdateTotalFriends(string json) =>
 079            OnTotalFriendCountUpdated?.Invoke(JsonUtility.FromJson<UpdateTotalFriendsPayload>(json));
 80
 81        [PublicAPI]
 82        public void RequestFriendshipConfirmation(string json)
 83        {
 084            var payload = JsonUtility.FromJson<RequestFriendshipConfirmationPayload>(json);
 085            var messageId = payload.messageId;
 086            if (!pendingRequests.ContainsKey(messageId)) return;
 87
 088            var task = (UniTaskCompletionSource<RequestFriendshipConfirmationPayload>)pendingRequests[messageId];
 89
 090            pendingRequests.Remove(messageId);
 091            task.TrySetResult(payload);
 092        }
 93
 94        [PublicAPI]
 95        public void RequestFriendshipError(string json)
 96        {
 097            var payload = JsonUtility.FromJson<RequestFriendshipErrorPayload>(json);
 098            var messageId = payload.messageId;
 099            if (!pendingRequests.ContainsKey(messageId)) return;
 100
 0101            var task = (UniTaskCompletionSource<RequestFriendshipConfirmationPayload>)pendingRequests[messageId];
 102
 0103            pendingRequests.Remove(messageId);
 0104            task.TrySetException(new FriendshipException((FriendRequestErrorCodes)payload.errorCode));
 0105        }
 106
 107        [PublicAPI]
 108        public void CancelFriendshipConfirmation(string json)
 109        {
 0110            var payload = JsonUtility.FromJson<CancelFriendshipConfirmationPayload>(json);
 0111            string messageId = payload.messageId;
 0112            if (!pendingRequests.ContainsKey(messageId)) return;
 113
 0114            var task = (UniTaskCompletionSource<CancelFriendshipConfirmationPayload>)pendingRequests[messageId];
 115
 0116            pendingRequests.Remove(messageId);
 0117            task.TrySetResult(payload);
 118
 0119            OnFriendshipStatusUpdated?.Invoke(new FriendshipUpdateStatusMessage
 120            {
 121                action = FriendshipAction.CANCELLED,
 122                userId = payload.friendRequest.to
 123            });
 0124        }
 125
 126        [PublicAPI]
 127        public void CancelFriendshipError(string json)
 128        {
 0129            var payload = JsonUtility.FromJson<CancelFriendshipErrorPayload>(json);
 0130            var messageId = payload.messageId;
 0131            if (!pendingRequests.ContainsKey(messageId)) return;
 132
 0133            var task = (UniTaskCompletionSource<CancelFriendshipConfirmationPayload>)pendingRequests[messageId];
 134
 0135            pendingRequests.Remove(messageId);
 0136            task.TrySetException(new FriendshipException((FriendRequestErrorCodes)payload.errorCode));
 0137        }
 138
 139        public void RejectFriendship(string userId)
 140        {
 0141            WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage
 142            {
 143                action = WebInterface.FriendshipAction.REJECTED,
 144                userId = userId
 145            });
 0146        }
 147
 148        public void RemoveFriend(string userId)
 149        {
 0150            WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage
 151            {
 152                action = WebInterface.FriendshipAction.DELETED,
 153                userId = userId
 154            });
 0155        }
 156
 157        public void GetFriends(int limit, int skip) =>
 0158            WebInterface.GetFriends(limit, skip);
 159
 160        public void GetFriends(string usernameOrId, int limit) =>
 0161            WebInterface.GetFriends(usernameOrId, limit);
 162
 163        // TODO (NEW FRIEND REQUESTS): remove when we don't need to keep the retro-compatibility with the old version
 164        public void GetFriendRequests(int sentLimit, int sentSkip, int receivedLimit, int receivedSkip)
 165        {
 0166            WebInterface.SendMessage("GetFriendRequests", new GetFriendRequestsPayload
 167            {
 168                receivedSkip = receivedSkip,
 169                receivedLimit = receivedLimit,
 170                sentSkip = sentSkip,
 171                sentLimit = sentLimit
 172            });
 0173        }
 174
 175        public UniTask<AddFriendRequestsV2Payload> GetFriendRequestsAsync(int sentLimit, int sentSkip, int receivedLimit
 0176            throw new NotImplementedException("Already implemented in RPCFriendsApiBridge");
 177
 178        public void GetFriendsWithDirectMessages(string usernameOrId, int limit, int skip) =>
 0179            WebInterface.GetFriendsWithDirectMessages(usernameOrId, limit, skip);
 180
 181        public void RequestFriendship(string friendUserId)
 182        {
 0183            WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage
 184            {
 185                action = WebInterface.FriendshipAction.REQUESTED_TO,
 186                userId = friendUserId,
 187            });
 0188        }
 189
 190        public UniTask<RequestFriendshipConfirmationPayload> RequestFriendshipAsync(string userId, string messageBody)
 191        {
 0192            var task = new UniTaskCompletionSource<RequestFriendshipConfirmationPayload>();
 193
 194            // TODO: optimize unique id length for performance reasons
 0195            var messageId = Guid.NewGuid().ToString("N");
 0196            pendingRequests[messageId] = task;
 197
 0198            WebInterface.SendMessage("RequestFriendship", new RequestFriendshipPayload
 199            {
 200                messageId = messageId,
 201                messageBody = messageBody,
 202                userId = userId
 203            });
 204
 0205            return task.Task;
 206        }
 207
 208        public UniTask<CancelFriendshipConfirmationPayload> CancelRequestAsync(string friendRequestId)
 209        {
 0210            var task = new UniTaskCompletionSource<CancelFriendshipConfirmationPayload>();
 211
 212            // TODO: optimize unique id length for performance reasons
 0213            var messageId = Guid.NewGuid().ToString("N");
 0214            pendingRequests[messageId] = task;
 215
 0216            WebInterface.SendMessage("CancelFriendship",
 217                new CancelFriendshipPayload
 218                {
 219                    messageId = messageId,
 220                    friendRequestId = friendRequestId
 221                });
 222
 0223            return task.Task;
 224        }
 225
 226        public UniTask CancelRequestByUserIdAsync(string userId)
 227        {
 0228            var task = updatedFriendshipPendingRequests.ContainsKey(userId)
 229                ? updatedFriendshipPendingRequests[userId]
 230                : new UniTaskCompletionSource<FriendshipUpdateStatusMessage>();
 231
 0232            updatedFriendshipPendingRequests[userId] = task;
 233
 0234            WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage
 235            {
 236                userId = userId,
 237                action = WebInterface.FriendshipAction.CANCELLED
 238            });
 239
 0240            return task.Task;
 241        }
 242
 243        public void CancelRequestByUserId(string userId)
 244        {
 0245            WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage
 246            {
 247                userId = userId,
 248                action = WebInterface.FriendshipAction.CANCELLED
 249            });
 0250        }
 251
 252        public void AcceptFriendship(string userId)
 253        {
 0254            WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage
 255            {
 256                userId = userId,
 257                action = WebInterface.FriendshipAction.APPROVED
 258            });
 0259        }
 260    }
 261}

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/FriendsController/WebInterfaceFriendsApiBridgeUtilies.cs

#LineLine coverage
 1using UnityEngine;
 2
 3namespace DCL.Social.Friends
 4{
 5    public partial class WebInterfaceFriendsApiBridge
 6    {
 7        [ContextMenu("Force initialization")]
 8        public void ForceInitialization()
 9        {
 010            InitializeFriends(JsonUtility.ToJson(new FriendshipInitializationMessage()));
 011        }
 12    }
 13}