< Summary

Class:FriendsController
Assembly:FriendsController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/FriendsController/FriendsController.cs
Covered lines:30
Uncovered lines:94
Coverable lines:124
Total lines:348
Line coverage:24.1% (30 of 124)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
FriendsController()0%110100%
GetUserStatus(...)0%6200%
ContainsStatus(...)0%6200%
GetAllocatedFriends()0%2100%
RejectFriendship(...)0%2100%
IsFriend(...)0%110100%
RemoveFriend(...)0%2100%
GetFriends(...)0%2100%
GetFriends(...)0%2100%
GetFriendRequests(...)0%2100%
GetFriendsWithDirectMessages(...)0%2100%
GetFriendsWithDirectMessages(...)0%2100%
RequestFriendship(...)0%2100%
CancelRequest(...)0%2100%
AcceptFriendship(...)0%2100%
FriendNotFound(...)0%6200%
InitializeFriends(...)0%20400%
AddFriends(...)0%6200%
AddFriendRequests(...)0%20400%
AddFriendsWithDirectMessages(...)0%12300%
UpdateUserPresence(...)0%6200%
UpdateFriendshipStatus(...)0%110100%
UpdateTotalFriendRequests(...)0%6200%
UpdateTotalFriends(...)0%6200%
UpdateUserStatus(...)0%30500%
UpdateFriendshipStatus(...)0%8.158086.67%
ItsAnOutdatedUpdate(...)0%330100%
ToFriendshipStatus(...)0%19.948042.86%
AddFriends(...)0%12300%
FakeOnlineFriend()0%2100%
ForceInitialization()0%2100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using JetBrains.Annotations;
 5using DCL.Friends.WebApi;
 6using DCL.Interface;
 7using UnityEngine;
 8
 9public class FriendsController : MonoBehaviour, IFriendsController
 10{
 11    private const bool VERBOSE = false;
 1212    public static FriendsController i { get; private set; }
 13
 14    public event Action<int> OnTotalFriendsUpdated;
 015    public int AllocatedFriendCount => friends.Count(f => f.Value.friendshipStatus == FriendshipStatus.FRIEND);
 16
 17    private void Awake()
 18    {
 3319        i = this;
 3320    }
 21
 422    public bool IsInitialized { get; private set; }
 23
 24    public int ReceivedRequestCount =>
 125        friends.Values.Count(status => status.friendshipStatus == FriendshipStatus.REQUESTED_FROM);
 26
 127    public int TotalFriendCount { get; private set; }
 128    public int TotalFriendRequestCount => TotalReceivedFriendRequestCount + TotalSentFriendRequestCount;
 3329    public int TotalReceivedFriendRequestCount { get; private set; }
 030    public int TotalSentFriendRequestCount { get; private set; }
 031    public int TotalFriendsWithDirectMessagesCount { get; private set; }
 32
 3333    public readonly Dictionary<string, UserStatus> friends = new Dictionary<string, UserStatus>();
 34
 35    public UserStatus GetUserStatus(string userId)
 36    {
 037        if (!friends.ContainsKey(userId))
 038            return new UserStatus {userId = userId, friendshipStatus = FriendshipStatus.NOT_FRIEND};
 39
 040        return friends[userId];
 41    }
 42
 43    public bool ContainsStatus(string friendId, FriendshipStatus status)
 44    {
 045        if (!friends.ContainsKey(friendId)) return false;
 046        return friends[friendId].friendshipStatus == status;
 47    }
 48
 49    public event Action<string, UserStatus> OnUpdateUserStatus;
 50    public event Action<string, FriendshipAction> OnUpdateFriendship;
 51    public event Action<string> OnFriendNotFound;
 52    public event Action OnInitialized;
 53    public event Action<List<FriendWithDirectMessages>> OnAddFriendsWithDirectMessages;
 54    public event Action<int, int> OnTotalFriendRequestUpdated;
 55
 56    public Dictionary<string, UserStatus> GetAllocatedFriends()
 57    {
 058        return new Dictionary<string, UserStatus>(friends);
 59    }
 60
 61    public void RejectFriendship(string friendUserId)
 62    {
 063        WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage
 64        {
 65            action = WebInterface.FriendshipAction.REJECTED,
 66            userId = friendUserId
 67        });
 068    }
 69
 170    public bool IsFriend(string userId) => friends.ContainsKey(userId);
 71
 72    public void RemoveFriend(string friendId)
 73    {
 074        WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage
 75        {
 76            action = WebInterface.FriendshipAction.DELETED,
 77            userId = friendId
 78        });
 079    }
 80
 81    public void GetFriends(int limit, int skip) =>
 082        WebInterface.GetFriends(limit, skip);
 83
 84    public void GetFriends(string usernameOrId, int limit)
 85    {
 086        WebInterface.GetFriends(usernameOrId, limit);
 087    }
 88
 89    public void GetFriendRequests(int sentLimit, int sentSkip, int receivedLimit,
 90        int receivedSkip)
 91    {
 092        WebInterface.GetFriendRequests(sentLimit, sentSkip, receivedLimit,
 93            receivedSkip);
 094    }
 95
 96    public void GetFriendsWithDirectMessages(int limit, int skip)
 97    {
 098        WebInterface.GetFriendsWithDirectMessages("", limit, skip);
 099    }
 100
 101    public void GetFriendsWithDirectMessages(string userNameOrId, int limit)
 102    {
 0103        WebInterface.GetFriendsWithDirectMessages(userNameOrId, limit, 0);
 0104    }
 105
 106    public void RequestFriendship(string friendUserId)
 107    {
 0108        WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage
 109        {
 110            userId = friendUserId,
 111            action = WebInterface.FriendshipAction.REQUESTED_TO
 112        });
 0113    }
 114
 115    public void CancelRequest(string friendUserId)
 116    {
 0117        WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage
 118        {
 119            userId = friendUserId,
 120            action = WebInterface.FriendshipAction.CANCELLED
 121        });
 0122    }
 123
 124    public void AcceptFriendship(string friendUserId)
 125    {
 0126        WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage
 127        {
 128            userId = friendUserId,
 129            action = WebInterface.FriendshipAction.APPROVED
 130        });
 0131    }
 132
 133    // called by kernel
 134    [UsedImplicitly]
 135    public void FriendNotFound(string name)
 136    {
 0137        OnFriendNotFound?.Invoke(name);
 0138    }
 139
 140    // called by kernel
 141    [UsedImplicitly]
 142    public void InitializeFriends(string json)
 143    {
 0144        if (IsInitialized)
 0145            return;
 146
 0147        IsInitialized = true;
 148
 0149        var msg = JsonUtility.FromJson<FriendshipInitializationMessage>(json);
 150
 0151        TotalReceivedFriendRequestCount = msg.totalReceivedRequests;
 0152        OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount);
 0153        OnInitialized?.Invoke();
 0154    }
 155
 156    // called by kernel
 157    [UsedImplicitly]
 158    public void AddFriends(string json)
 159    {
 0160        var msg = JsonUtility.FromJson<AddFriendsPayload>(json);
 161
 0162        TotalFriendCount = msg.totalFriends;
 0163        OnTotalFriendsUpdated?.Invoke(TotalFriendCount);
 164
 0165        AddFriends(msg.friends);
 0166    }
 167
 168    // called by kernel
 169    [UsedImplicitly]
 170    public void AddFriendRequests(string json)
 171    {
 0172        var msg = JsonUtility.FromJson<AddFriendRequestsPayload>(json);
 173
 0174        TotalReceivedFriendRequestCount = msg.totalReceivedFriendRequests;
 0175        TotalSentFriendRequestCount = msg.totalSentFriendRequests;
 0176        OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount);
 177
 0178        foreach (var userId in msg.requestedFrom)
 179        {
 0180            UpdateFriendshipStatus(new FriendshipUpdateStatusMessage
 181                {action = FriendshipAction.REQUESTED_FROM, userId = userId});
 182        }
 183
 0184        foreach (var userId in msg.requestedTo)
 185        {
 0186            UpdateFriendshipStatus(new FriendshipUpdateStatusMessage
 187                {action = FriendshipAction.REQUESTED_TO, userId = userId});
 188        }
 0189    }
 190
 191    // called by kernel
 192    [UsedImplicitly]
 193    public void AddFriendsWithDirectMessages(string json)
 194    {
 0195        var friendsWithDMs = JsonUtility.FromJson<AddFriendsWithDirectMessagesPayload>(json);
 0196        TotalFriendsWithDirectMessagesCount = friendsWithDMs.totalFriendsWithDirectMessages;
 197
 0198        AddFriends(friendsWithDMs.currentFriendsWithDirectMessages.Select(messages => messages.userId));
 199
 0200        OnAddFriendsWithDirectMessages?.Invoke(friendsWithDMs.currentFriendsWithDirectMessages.ToList());
 0201    }
 202
 203    // called by kernel
 204    [UsedImplicitly]
 205    public void UpdateUserPresence(string json)
 206    {
 0207        UserStatus newUserStatus = JsonUtility.FromJson<UserStatus>(json);
 208
 0209        if (!friends.ContainsKey(newUserStatus.userId))
 0210            return;
 211
 212        // Kernel doesn't send the friendship status on this call, we have to keep it or it gets defaulted
 0213        newUserStatus.friendshipStatus = friends[newUserStatus.userId].friendshipStatus;
 214
 0215        UpdateUserStatus(newUserStatus);
 0216    }
 217
 218    // called by kernel
 219    [UsedImplicitly]
 220    public void UpdateFriendshipStatus(string json)
 221    {
 6222        FriendshipUpdateStatusMessage msg = JsonUtility.FromJson<FriendshipUpdateStatusMessage>(json);
 6223        UpdateFriendshipStatus(msg);
 6224    }
 225
 226    // called by kernel
 227    [UsedImplicitly]
 228    public void UpdateTotalFriendRequests(string json)
 229    {
 0230        var msg = JsonUtility.FromJson<UpdateTotalFriendRequestsPayload>(json);
 0231        TotalReceivedFriendRequestCount = msg.totalReceivedRequests;
 0232        TotalSentFriendRequestCount = msg.totalSentRequests;
 0233        OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount);
 0234    }
 235
 236    // called by kernel
 237    [UsedImplicitly]
 238    public void UpdateTotalFriends(string json)
 239    {
 0240        var msg = JsonUtility.FromJson<UpdateTotalFriendsPayload>(json);
 0241        TotalFriendCount = msg.totalFriends;
 0242        OnTotalFriendsUpdated?.Invoke(TotalFriendCount);
 0243    }
 244
 245    private void UpdateUserStatus(UserStatus newUserStatus)
 246    {
 0247        if (!friends.ContainsKey(newUserStatus.userId))
 248        {
 0249            friends.Add(newUserStatus.userId, newUserStatus);
 0250            OnUpdateUserStatus?.Invoke(newUserStatus.userId, newUserStatus);
 0251        }
 252        else
 253        {
 0254            if (!friends[newUserStatus.userId].Equals(newUserStatus))
 255            {
 0256                friends[newUserStatus.userId] = newUserStatus;
 0257                OnUpdateUserStatus?.Invoke(newUserStatus.userId, newUserStatus);
 258            }
 259        }
 0260    }
 261
 262    private void UpdateFriendshipStatus(FriendshipUpdateStatusMessage msg)
 263    {
 6264        var friendshipStatus = ToFriendshipStatus(msg.action);
 6265        var userId = msg.userId;
 266
 6267        if (friends.ContainsKey(userId) && friends[userId].friendshipStatus == friendshipStatus)
 0268            return;
 269
 6270        if (!friends.ContainsKey(userId))
 2271            friends.Add(userId, new UserStatus { userId = userId });
 272
 6273        if (ItsAnOutdatedUpdate(userId, friendshipStatus))
 0274            return;
 275
 6276        friends[userId].friendshipStatus = friendshipStatus;
 277
 6278        if (friendshipStatus == FriendshipStatus.FRIEND)
 2279            friends[userId].friendshipStartedTime = DateTime.UtcNow;
 280
 281        if (VERBOSE)
 282            Debug.Log($"Change friend status of {userId} to {friends[userId].friendshipStatus}");
 283
 6284        if (friendshipStatus == FriendshipStatus.NOT_FRIEND)
 2285            friends.Remove(userId);
 286
 6287        OnUpdateFriendship?.Invoke(userId, msg.action);
 6288    }
 289
 290    private bool ItsAnOutdatedUpdate(string userId, FriendshipStatus friendshipStatus)
 291    {
 6292        return friendshipStatus == FriendshipStatus.REQUESTED_FROM
 293               && friends[userId].friendshipStatus == FriendshipStatus.FRIEND
 294               && (DateTime.UtcNow - friends[userId].friendshipStartedTime).TotalSeconds < 5;
 295    }
 296
 297    private static FriendshipStatus ToFriendshipStatus(FriendshipAction action)
 298    {
 299        switch (action)
 300        {
 301            case FriendshipAction.NONE:
 302                break;
 303            case FriendshipAction.APPROVED:
 2304                return FriendshipStatus.FRIEND;
 305            case FriendshipAction.REJECTED:
 0306                return FriendshipStatus.NOT_FRIEND;
 307            case FriendshipAction.CANCELLED:
 0308                return FriendshipStatus.NOT_FRIEND;
 309            case FriendshipAction.REQUESTED_FROM:
 0310                return FriendshipStatus.REQUESTED_FROM;
 311            case FriendshipAction.REQUESTED_TO:
 2312                return FriendshipStatus.REQUESTED_TO;
 313            case FriendshipAction.DELETED:
 2314                return FriendshipStatus.NOT_FRIEND;
 315        }
 316
 0317        return FriendshipStatus.NOT_FRIEND;
 318    }
 319
 320    private void AddFriends(IEnumerable<string> friendIds)
 321    {
 0322        foreach (var friendId in friendIds)
 323        {
 0324            UpdateFriendshipStatus(new FriendshipUpdateStatusMessage
 325                {action = FriendshipAction.APPROVED, userId = friendId});
 326        }
 0327    }
 328
 329    [ContextMenu("Change user stats to online")]
 330    public void FakeOnlineFriend()
 331    {
 0332        var friend = friends.Values.First();
 0333        UpdateUserStatus(new UserStatus
 334        {
 335            userId = friend.userId,
 336            position = friend.position,
 337            presence = PresenceStatus.ONLINE,
 338            friendshipStatus = friend.friendshipStatus,
 339            friendshipStartedTime = friend.friendshipStartedTime
 340        });
 0341    }
 342
 343    [ContextMenu("Force initialization")]
 344    public void ForceInitialization()
 345    {
 0346        InitializeFriends(JsonUtility.ToJson(new FriendshipInitializationMessage()));
 0347    }
 348}

Methods/Properties

i()
i(FriendsController)
AllocatedFriendCount()
Awake()
IsInitialized()
IsInitialized(System.Boolean)
ReceivedRequestCount()
TotalFriendCount()
TotalFriendCount(System.Int32)
TotalFriendRequestCount()
TotalReceivedFriendRequestCount()
TotalReceivedFriendRequestCount(System.Int32)
TotalSentFriendRequestCount()
TotalSentFriendRequestCount(System.Int32)
TotalFriendsWithDirectMessagesCount()
TotalFriendsWithDirectMessagesCount(System.Int32)
FriendsController()
GetUserStatus(System.String)
ContainsStatus(System.String, FriendshipStatus)
GetAllocatedFriends()
RejectFriendship(System.String)
IsFriend(System.String)
RemoveFriend(System.String)
GetFriends(System.Int32, System.Int32)
GetFriends(System.String, System.Int32)
GetFriendRequests(System.Int32, System.Int32, System.Int32, System.Int32)
GetFriendsWithDirectMessages(System.Int32, System.Int32)
GetFriendsWithDirectMessages(System.String, System.Int32)
RequestFriendship(System.String)
CancelRequest(System.String)
AcceptFriendship(System.String)
FriendNotFound(System.String)
InitializeFriends(System.String)
AddFriends(System.String)
AddFriendRequests(System.String)
AddFriendsWithDirectMessages(System.String)
UpdateUserPresence(System.String)
UpdateFriendshipStatus(System.String)
UpdateTotalFriendRequests(System.String)
UpdateTotalFriends(System.String)
UpdateUserStatus(UserStatus)
UpdateFriendshipStatus(FriendshipUpdateStatusMessage)
ItsAnOutdatedUpdate(System.String, FriendshipStatus)
ToFriendshipStatus(FriendshipAction)
AddFriends(System.Collections.Generic.IEnumerable[String])
FakeOnlineFriend()
ForceInitialization()