| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCl.Social.Friends; |
| | 5 | |
|
| | 6 | | namespace DCL.Social.Friends |
| | 7 | | { |
| | 8 | | public class FriendsController : IFriendsController |
| | 9 | | { |
| 150 | 10 | | public static FriendsController i { get; private set; } |
| | 11 | |
|
| | 12 | | private readonly IFriendsApiBridge apiBridge; |
| | 13 | |
|
| | 14 | | public event Action<int> OnTotalFriendsUpdated; |
| 0 | 15 | | public int AllocatedFriendCount => friends.Count(f => f.Value.friendshipStatus == FriendshipStatus.FRIEND); |
| 6 | 16 | | public bool IsInitialized { get; private set; } |
| | 17 | |
|
| | 18 | | public int ReceivedRequestCount => |
| 1 | 19 | | friends.Values.Count(status => status.friendshipStatus == FriendshipStatus.REQUESTED_FROM); |
| | 20 | |
|
| 7 | 21 | | public int TotalFriendCount { get; private set; } |
| 1 | 22 | | public int TotalFriendRequestCount => TotalReceivedFriendRequestCount + TotalSentFriendRequestCount; |
| 41 | 23 | | public int TotalReceivedFriendRequestCount { get; private set; } |
| 6 | 24 | | public int TotalSentFriendRequestCount { get; private set; } |
| 1 | 25 | | public int TotalFriendsWithDirectMessagesCount { get; private set; } |
| | 26 | |
|
| 47 | 27 | | public readonly Dictionary<string, UserStatus> friends = new Dictionary<string, UserStatus>(); |
| | 28 | |
|
| | 29 | | public event Action<string, UserStatus> OnUpdateUserStatus; |
| | 30 | | public event Action<string, FriendshipAction> OnUpdateFriendship; |
| | 31 | | public event Action<string> OnFriendNotFound; |
| | 32 | | public event Action OnInitialized; |
| | 33 | | public event Action<List<FriendWithDirectMessages>> OnAddFriendsWithDirectMessages; |
| | 34 | | public event Action<int, int> OnTotalFriendRequestUpdated; |
| | 35 | |
|
| | 36 | | public static void CreateSharedInstance(IFriendsApiBridge apiBridge) |
| | 37 | | { |
| 33 | 38 | | i = new FriendsController(apiBridge); |
| 33 | 39 | | } |
| | 40 | |
|
| 47 | 41 | | public FriendsController(IFriendsApiBridge apiBridge) |
| | 42 | | { |
| 47 | 43 | | this.apiBridge = apiBridge; |
| 47 | 44 | | apiBridge.OnInitialized += Initialize; |
| 47 | 45 | | apiBridge.OnFriendNotFound += FriendNotFound; |
| 47 | 46 | | apiBridge.OnFriendsAdded += AddFriends; |
| 47 | 47 | | apiBridge.OnFriendRequestsAdded += AddFriendRequests; |
| 47 | 48 | | apiBridge.OnFriendWithDirectMessagesAdded += AddFriendsWithDirectMessages; |
| 47 | 49 | | apiBridge.OnUserPresenceUpdated += UpdateUserPresence; |
| 47 | 50 | | apiBridge.OnFriendshipStatusUpdated += UpdateFriendshipStatus; |
| 47 | 51 | | apiBridge.OnTotalFriendRequestCountUpdated += UpdateTotalFriendRequests; |
| 47 | 52 | | apiBridge.OnTotalFriendCountUpdated += UpdateTotalFriends; |
| 47 | 53 | | } |
| | 54 | |
|
| | 55 | | private void Initialize(FriendshipInitializationMessage msg) |
| | 56 | | { |
| 1 | 57 | | if (IsInitialized) return; |
| | 58 | |
|
| 1 | 59 | | IsInitialized = true; |
| | 60 | |
|
| 1 | 61 | | TotalReceivedFriendRequestCount = msg.totalReceivedRequests; |
| 1 | 62 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| 1 | 63 | | OnInitialized?.Invoke(); |
| 1 | 64 | | } |
| | 65 | |
|
| | 66 | | public UserStatus GetUserStatus(string userId) |
| | 67 | | { |
| 0 | 68 | | if (!friends.ContainsKey(userId)) |
| 0 | 69 | | return new UserStatus {userId = userId, friendshipStatus = FriendshipStatus.NOT_FRIEND}; |
| | 70 | |
|
| 0 | 71 | | return friends[userId]; |
| | 72 | | } |
| | 73 | |
|
| | 74 | | public bool ContainsStatus(string friendId, FriendshipStatus status) |
| | 75 | | { |
| 0 | 76 | | if (!friends.ContainsKey(friendId)) return false; |
| 0 | 77 | | return friends[friendId].friendshipStatus == status; |
| | 78 | | } |
| | 79 | |
|
| | 80 | | public Dictionary<string, UserStatus> GetAllocatedFriends() |
| | 81 | | { |
| 0 | 82 | | return new Dictionary<string, UserStatus>(friends); |
| | 83 | | } |
| | 84 | |
|
| 0 | 85 | | public void RejectFriendship(string friendUserId) => apiBridge.RejectFriendship(friendUserId); |
| | 86 | |
|
| | 87 | | public bool IsFriend(string userId) => |
| 1 | 88 | | friends.ContainsKey(userId) && friends[userId].friendshipStatus == FriendshipStatus.FRIEND; |
| | 89 | |
|
| 0 | 90 | | public void RemoveFriend(string friendId) => apiBridge.RemoveFriend(friendId); |
| | 91 | |
|
| 0 | 92 | | public void GetFriends(int limit, int skip) => apiBridge.GetFriends(limit, skip); |
| | 93 | |
|
| 0 | 94 | | public void GetFriends(string usernameOrId, int limit) =>apiBridge.GetFriends(usernameOrId, limit); |
| | 95 | |
|
| | 96 | | public void GetFriendRequests(int sentLimit, int sentSkip, int receivedLimit, |
| | 97 | | int receivedSkip) => |
| 0 | 98 | | apiBridge.GetFriendRequests(sentLimit, sentSkip, receivedLimit, receivedSkip); |
| | 99 | |
|
| | 100 | | public void GetFriendsWithDirectMessages(int limit, int skip) => |
| 0 | 101 | | apiBridge.GetFriendsWithDirectMessages("", limit, skip); |
| | 102 | |
|
| | 103 | | public void GetFriendsWithDirectMessages(string userNameOrId, int limit) => |
| 0 | 104 | | apiBridge.GetFriendsWithDirectMessages(userNameOrId, limit, 0); |
| | 105 | |
|
| | 106 | | public void RequestFriendship(string friendUserId) => |
| 0 | 107 | | apiBridge.RequestFriendship(friendUserId); |
| | 108 | |
|
| | 109 | | public void CancelRequest(string friendUserId) => |
| 0 | 110 | | apiBridge.CancelRequest(friendUserId); |
| | 111 | |
|
| | 112 | | public void AcceptFriendship(string friendUserId) => |
| 0 | 113 | | apiBridge.AcceptFriendship(friendUserId); |
| | 114 | |
|
| 0 | 115 | | private void FriendNotFound(string name) => OnFriendNotFound?.Invoke(name); |
| | 116 | |
|
| | 117 | | private void AddFriends(AddFriendsPayload msg) |
| | 118 | | { |
| 2 | 119 | | TotalFriendCount = msg.totalFriends; |
| 2 | 120 | | OnTotalFriendsUpdated?.Invoke(TotalFriendCount); |
| 2 | 121 | | AddFriends(msg.friends); |
| 2 | 122 | | } |
| | 123 | |
|
| | 124 | | private void AddFriendRequests(AddFriendRequestsPayload msg) |
| | 125 | | { |
| 1 | 126 | | TotalReceivedFriendRequestCount = msg.totalReceivedFriendRequests; |
| 1 | 127 | | TotalSentFriendRequestCount = msg.totalSentFriendRequests; |
| 1 | 128 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| | 129 | |
|
| 8 | 130 | | foreach (var userId in msg.requestedFrom) |
| | 131 | | { |
| 3 | 132 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 133 | | {action = FriendshipAction.REQUESTED_FROM, userId = userId}); |
| | 134 | | } |
| | 135 | |
|
| 6 | 136 | | foreach (var userId in msg.requestedTo) |
| | 137 | | { |
| 2 | 138 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 139 | | {action = FriendshipAction.REQUESTED_TO, userId = userId}); |
| | 140 | | } |
| 1 | 141 | | } |
| | 142 | |
|
| | 143 | | private void AddFriendsWithDirectMessages(AddFriendsWithDirectMessagesPayload friendsWithDMs) |
| | 144 | | { |
| 1 | 145 | | TotalFriendsWithDirectMessagesCount = friendsWithDMs.totalFriendsWithDirectMessages; |
| 4 | 146 | | AddFriends(friendsWithDMs.currentFriendsWithDirectMessages.Select(messages => messages.userId)); |
| 1 | 147 | | OnAddFriendsWithDirectMessages?.Invoke(friendsWithDMs.currentFriendsWithDirectMessages.ToList()); |
| 1 | 148 | | } |
| | 149 | |
|
| | 150 | | private void UpdateUserPresence(UserStatus newUserStatus) |
| | 151 | | { |
| 1 | 152 | | if (!friends.ContainsKey(newUserStatus.userId)) return; |
| | 153 | | // Kernel doesn't send the friendship status on this call, we have to keep it or it gets defaulted |
| 1 | 154 | | newUserStatus.friendshipStatus = friends[newUserStatus.userId].friendshipStatus; |
| 1 | 155 | | UpdateUserStatus(newUserStatus); |
| 1 | 156 | | } |
| | 157 | |
|
| | 158 | | private void UpdateTotalFriendRequests(UpdateTotalFriendRequestsPayload msg) |
| | 159 | | { |
| 1 | 160 | | TotalReceivedFriendRequestCount = msg.totalReceivedRequests; |
| 1 | 161 | | TotalSentFriendRequestCount = msg.totalSentRequests; |
| 1 | 162 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| 1 | 163 | | } |
| | 164 | |
|
| | 165 | | private void UpdateTotalFriends(UpdateTotalFriendsPayload msg) |
| | 166 | | { |
| 1 | 167 | | TotalFriendCount = msg.totalFriends; |
| 1 | 168 | | OnTotalFriendsUpdated?.Invoke(TotalFriendCount); |
| 1 | 169 | | } |
| | 170 | |
|
| | 171 | | private void UpdateUserStatus(UserStatus newUserStatus) |
| | 172 | | { |
| 1 | 173 | | if (!friends.ContainsKey(newUserStatus.userId)) |
| | 174 | | { |
| 0 | 175 | | friends.Add(newUserStatus.userId, newUserStatus); |
| 0 | 176 | | OnUpdateUserStatus?.Invoke(newUserStatus.userId, newUserStatus); |
| | 177 | | } |
| | 178 | | else |
| | 179 | | { |
| 1 | 180 | | if (!friends[newUserStatus.userId].Equals(newUserStatus)) |
| | 181 | | { |
| 1 | 182 | | friends[newUserStatus.userId] = newUserStatus; |
| 1 | 183 | | OnUpdateUserStatus?.Invoke(newUserStatus.userId, newUserStatus); |
| | 184 | | } |
| | 185 | | } |
| 1 | 186 | | } |
| | 187 | |
|
| | 188 | | private void UpdateFriendshipStatus(FriendshipUpdateStatusMessage msg) |
| | 189 | | { |
| 24 | 190 | | var friendshipStatus = ToFriendshipStatus(msg.action); |
| 24 | 191 | | var userId = msg.userId; |
| | 192 | |
|
| 24 | 193 | | if (friends.ContainsKey(userId) && friends[userId].friendshipStatus == friendshipStatus) |
| 0 | 194 | | return; |
| | 195 | |
|
| 24 | 196 | | if (!friends.ContainsKey(userId)) |
| 20 | 197 | | friends.Add(userId, new UserStatus {userId = userId}); |
| | 198 | |
|
| 24 | 199 | | if (ItsAnOutdatedUpdate(userId, friendshipStatus)) |
| 0 | 200 | | return; |
| | 201 | |
|
| 24 | 202 | | friends[userId].friendshipStatus = friendshipStatus; |
| | 203 | |
|
| 24 | 204 | | if (friendshipStatus == FriendshipStatus.NOT_FRIEND) |
| 6 | 205 | | friends.Remove(userId); |
| | 206 | |
|
| 24 | 207 | | OnUpdateFriendship?.Invoke(userId, msg.action); |
| 23 | 208 | | } |
| | 209 | |
|
| | 210 | | private bool ItsAnOutdatedUpdate(string userId, FriendshipStatus friendshipStatus) |
| | 211 | | { |
| 24 | 212 | | return friendshipStatus == FriendshipStatus.REQUESTED_FROM |
| | 213 | | && friends[userId].friendshipStatus == FriendshipStatus.FRIEND; |
| | 214 | | } |
| | 215 | |
|
| | 216 | | private static FriendshipStatus ToFriendshipStatus(FriendshipAction action) |
| | 217 | | { |
| | 218 | | switch (action) |
| | 219 | | { |
| | 220 | | case FriendshipAction.NONE: |
| | 221 | | break; |
| | 222 | | case FriendshipAction.APPROVED: |
| 9 | 223 | | return FriendshipStatus.FRIEND; |
| | 224 | | case FriendshipAction.REJECTED: |
| 1 | 225 | | return FriendshipStatus.NOT_FRIEND; |
| | 226 | | case FriendshipAction.CANCELLED: |
| 1 | 227 | | return FriendshipStatus.NOT_FRIEND; |
| | 228 | | case FriendshipAction.REQUESTED_FROM: |
| 4 | 229 | | return FriendshipStatus.REQUESTED_FROM; |
| | 230 | | case FriendshipAction.REQUESTED_TO: |
| 5 | 231 | | return FriendshipStatus.REQUESTED_TO; |
| | 232 | | case FriendshipAction.DELETED: |
| 3 | 233 | | return FriendshipStatus.NOT_FRIEND; |
| | 234 | | } |
| | 235 | |
|
| 1 | 236 | | return FriendshipStatus.NOT_FRIEND; |
| | 237 | | } |
| | 238 | |
|
| | 239 | | private void AddFriends(IEnumerable<string> friendIds) |
| | 240 | | { |
| 18 | 241 | | foreach (var friendId in friendIds) |
| | 242 | | { |
| 6 | 243 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 244 | | {action = FriendshipAction.APPROVED, userId = friendId}); |
| | 245 | | } |
| 3 | 246 | | } |
| | 247 | | } |
| | 248 | | } |