| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCl.Social.Friends; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | |
|
| | 7 | | namespace DCL.Social.Friends |
| | 8 | | { |
| | 9 | | public class FriendsController : IFriendsController |
| | 10 | | { |
| 146 | 11 | | public static FriendsController i { get; private set; } |
| | 12 | |
|
| | 13 | | private readonly IFriendsApiBridge apiBridge; |
| 47 | 14 | | private readonly Dictionary<string, FriendRequest> friendRequests = new (); |
| | 15 | |
|
| | 16 | | public event Action<int> OnTotalFriendsUpdated; |
| 0 | 17 | | public int AllocatedFriendCount => friends.Count(f => f.Value.friendshipStatus == FriendshipStatus.FRIEND); |
| 6 | 18 | | public bool IsInitialized { get; private set; } |
| | 19 | |
|
| 1 | 20 | | public int ReceivedRequestCount => friends.Values.Count(status => status.friendshipStatus == FriendshipStatus.RE |
| | 21 | |
|
| 7 | 22 | | public int TotalFriendCount { get; private set; } |
| 1 | 23 | | public int TotalFriendRequestCount => TotalReceivedFriendRequestCount + TotalSentFriendRequestCount; |
| 41 | 24 | | public int TotalReceivedFriendRequestCount { get; private set; } |
| 6 | 25 | | public int TotalSentFriendRequestCount { get; private set; } |
| 1 | 26 | | public int TotalFriendsWithDirectMessagesCount { get; private set; } |
| | 27 | |
|
| 47 | 28 | | public readonly Dictionary<string, UserStatus> friends = new Dictionary<string, UserStatus>(); |
| | 29 | |
|
| | 30 | | public event Action<string, UserStatus> OnUpdateUserStatus; |
| | 31 | | public event Action<string, FriendshipAction> OnUpdateFriendship; |
| | 32 | | public event Action<string> OnFriendNotFound; |
| | 33 | | public event Action OnInitialized; |
| | 34 | | public event Action<List<FriendWithDirectMessages>> OnAddFriendsWithDirectMessages; |
| | 35 | | public event Action<int, int> OnTotalFriendRequestUpdated; |
| | 36 | | public event Action<FriendRequest> OnAddFriendRequest; |
| | 37 | |
|
| | 38 | | public static void CreateSharedInstance(IFriendsApiBridge apiBridge) |
| | 39 | | { |
| 33 | 40 | | i = new FriendsController(apiBridge); |
| 33 | 41 | | } |
| | 42 | |
|
| 47 | 43 | | public FriendsController(IFriendsApiBridge apiBridge) |
| | 44 | | { |
| 47 | 45 | | this.apiBridge = apiBridge; |
| 47 | 46 | | apiBridge.OnInitialized += Initialize; |
| 47 | 47 | | apiBridge.OnFriendNotFound += FriendNotFound; |
| 47 | 48 | | apiBridge.OnFriendsAdded += AddFriends; |
| 47 | 49 | | apiBridge.OnFriendWithDirectMessagesAdded += AddFriendsWithDirectMessages; |
| 47 | 50 | | apiBridge.OnUserPresenceUpdated += UpdateUserPresence; |
| 47 | 51 | | apiBridge.OnFriendshipStatusUpdated += UpdateFriendshipStatus; |
| 47 | 52 | | apiBridge.OnTotalFriendRequestCountUpdated += UpdateTotalFriendRequests; |
| 47 | 53 | | apiBridge.OnTotalFriendCountUpdated += UpdateTotalFriends; |
| 47 | 54 | | apiBridge.OnFriendRequestsAdded += AddFriendRequests; // TODO (NEW FRIEND REQUESTS): remove when we don't ne |
| 47 | 55 | | apiBridge.OnFriendRequestAdded += AddFriendRequest; |
| 47 | 56 | | } |
| | 57 | |
|
| | 58 | | private void Initialize(FriendshipInitializationMessage msg) |
| | 59 | | { |
| 1 | 60 | | if (IsInitialized) return; |
| | 61 | |
|
| 1 | 62 | | IsInitialized = true; |
| | 63 | |
|
| 1 | 64 | | TotalReceivedFriendRequestCount = msg.totalReceivedRequests; |
| 1 | 65 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| 1 | 66 | | OnInitialized?.Invoke(); |
| 1 | 67 | | } |
| | 68 | |
|
| | 69 | | public UserStatus GetUserStatus(string userId) |
| | 70 | | { |
| 0 | 71 | | if (!friends.ContainsKey(userId)) |
| 0 | 72 | | return new UserStatus { userId = userId, friendshipStatus = FriendshipStatus.NOT_FRIEND }; |
| | 73 | |
|
| 0 | 74 | | return friends[userId]; |
| | 75 | | } |
| | 76 | |
|
| | 77 | | public bool ContainsStatus(string friendId, FriendshipStatus status) |
| | 78 | | { |
| 0 | 79 | | if (!friends.ContainsKey(friendId)) return false; |
| 0 | 80 | | return friends[friendId].friendshipStatus == status; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | public async UniTask<FriendRequest> RequestFriendshipAsync(string friendUserId, string messageBody) |
| | 84 | | { |
| 0 | 85 | | var payload = await apiBridge.RequestFriendshipAsync(friendUserId, messageBody); |
| 0 | 86 | | var friendRequestPayload = payload.friendRequest; |
| | 87 | |
|
| 0 | 88 | | var friendRequest = new FriendRequest(friendRequestPayload.friendRequestId, |
| | 89 | | friendRequestPayload.timestamp, |
| | 90 | | friendRequestPayload.from, |
| | 91 | | friendRequestPayload.to, |
| | 92 | | friendRequestPayload.messageBody); |
| | 93 | |
|
| 0 | 94 | | friendRequests[friendRequest.FriendRequestId] = friendRequest; |
| | 95 | |
|
| 0 | 96 | | return friendRequest; |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | public void RequestFriendship(string friendUserId) |
| | 100 | | { |
| 0 | 101 | | apiBridge.RequestFriendship(friendUserId); |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | public Dictionary<string, UserStatus> GetAllocatedFriends() |
| | 105 | | { |
| 0 | 106 | | return new Dictionary<string, UserStatus>(friends); |
| | 107 | | } |
| | 108 | |
|
| | 109 | | public void RejectFriendship(string friendUserId) => |
| 0 | 110 | | apiBridge.RejectFriendship(friendUserId); |
| | 111 | |
|
| | 112 | | public bool IsFriend(string userId) => |
| 1 | 113 | | friends.ContainsKey(userId) && friends[userId].friendshipStatus == FriendshipStatus.FRIEND; |
| | 114 | |
|
| | 115 | | public void RemoveFriend(string friendId) => |
| 0 | 116 | | apiBridge.RemoveFriend(friendId); |
| | 117 | |
|
| | 118 | | public void GetFriends(int limit, int skip) => |
| 0 | 119 | | apiBridge.GetFriends(limit, skip); |
| | 120 | |
|
| | 121 | | public void GetFriends(string usernameOrId, int limit) => |
| 0 | 122 | | apiBridge.GetFriends(usernameOrId, limit); |
| | 123 | |
|
| | 124 | | // TODO (NEW FRIEND REQUESTS): remove when we don't need to keep the retro-compatibility with the old version |
| | 125 | | public void GetFriendRequests(int sentLimit, int sentSkip, int receivedLimit, int receivedSkip) => |
| 0 | 126 | | apiBridge.GetFriendRequests(sentLimit, sentSkip, receivedLimit, receivedSkip); |
| | 127 | |
|
| | 128 | | public async UniTask<List<FriendRequest>> GetFriendRequestsAsync(int sentLimit, int sentSkip, int receivedLimit, |
| | 129 | | { |
| 1 | 130 | | var payload = await apiBridge.GetFriendRequestsAsync(sentLimit, sentSkip, receivedLimit, receivedSkip); |
| | 131 | |
|
| 1 | 132 | | TotalReceivedFriendRequestCount = payload.totalReceivedFriendRequests; |
| 1 | 133 | | TotalSentFriendRequestCount = payload.totalSentFriendRequests; |
| 1 | 134 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| | 135 | |
|
| 1 | 136 | | List<FriendRequest> receivedFriendRequestsToAdd = new List<FriendRequest>(); |
| | 137 | |
|
| 8 | 138 | | foreach (var friendRequest in payload.requestedFrom) |
| | 139 | | { |
| 3 | 140 | | var request = ToFriendRequest(friendRequest); |
| 3 | 141 | | friendRequests[request.FriendRequestId] = request; |
| 3 | 142 | | receivedFriendRequestsToAdd.Add(request); |
| | 143 | |
|
| 3 | 144 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 145 | | { action = FriendshipAction.REQUESTED_FROM, userId = friendRequest.from }); |
| | 146 | | } |
| | 147 | |
|
| 6 | 148 | | foreach (var friendRequest in payload.requestedTo) |
| | 149 | | { |
| 2 | 150 | | var request = ToFriendRequest(friendRequest); |
| 2 | 151 | | friendRequests[request.FriendRequestId] = request; |
| 2 | 152 | | receivedFriendRequestsToAdd.Add(request); |
| | 153 | |
|
| 2 | 154 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 155 | | { action = FriendshipAction.REQUESTED_TO, userId = friendRequest.to }); |
| | 156 | | } |
| | 157 | |
|
| 1 | 158 | | return receivedFriendRequestsToAdd; |
| 1 | 159 | | } |
| | 160 | |
|
| | 161 | | public void GetFriendsWithDirectMessages(int limit, int skip) => |
| 0 | 162 | | apiBridge.GetFriendsWithDirectMessages("", limit, skip); |
| | 163 | |
|
| | 164 | | public void GetFriendsWithDirectMessages(string userNameOrId, int limit) => |
| 0 | 165 | | apiBridge.GetFriendsWithDirectMessages(userNameOrId, limit, 0); |
| | 166 | |
|
| | 167 | | public FriendRequest GetAllocatedFriendRequest(string friendRequestId) => |
| 0 | 168 | | friendRequests.ContainsKey(friendRequestId) ? friendRequests[friendRequestId] : null; |
| | 169 | |
|
| | 170 | | public FriendRequest GetAllocatedFriendRequestByUser(string userId) => |
| 0 | 171 | | friendRequests.Values.FirstOrDefault(request => request.From == userId || request.To == userId); |
| | 172 | |
|
| | 173 | | public async UniTask<FriendRequest> CancelRequestByUserIdAsync(string friendUserId) |
| | 174 | | { |
| 0 | 175 | | FriendRequest request = GetAllocatedFriendRequestByUser(friendUserId); |
| | 176 | |
|
| 0 | 177 | | if (request != null) |
| | 178 | | { |
| 0 | 179 | | await apiBridge.CancelRequestAsync(request.FriendRequestId); |
| 0 | 180 | | friendRequests.Remove(request.FriendRequestId); |
| 0 | 181 | | return request; |
| | 182 | | } |
| | 183 | |
|
| 0 | 184 | | await apiBridge.CancelRequestByUserIdAsync(friendUserId); |
| | 185 | |
|
| 0 | 186 | | return new FriendRequest("", 0, "", friendUserId, ""); |
| 0 | 187 | | } |
| | 188 | |
|
| | 189 | | public void CancelRequestByUserId(string friendUserId) => |
| 0 | 190 | | apiBridge.CancelRequestByUserId(friendUserId); |
| | 191 | |
|
| | 192 | | public async UniTask<FriendRequest> CancelRequestAsync(string friendRequestId) |
| | 193 | | { |
| 0 | 194 | | CancelFriendshipConfirmationPayload payload = await apiBridge.CancelRequestAsync(friendRequestId); |
| 0 | 195 | | var friendRequest = ToFriendRequest(payload.friendRequest); |
| 0 | 196 | | friendRequestId = friendRequest.FriendRequestId; |
| 0 | 197 | | friendRequests.Remove(friendRequestId); |
| 0 | 198 | | return friendRequest; |
| 0 | 199 | | } |
| | 200 | |
|
| | 201 | | public void AcceptFriendship(string friendUserId) => |
| 0 | 202 | | apiBridge.AcceptFriendship(friendUserId); |
| | 203 | |
|
| | 204 | | private void FriendNotFound(string name) => |
| 0 | 205 | | OnFriendNotFound?.Invoke(name); |
| | 206 | |
|
| | 207 | | private void AddFriends(AddFriendsPayload msg) |
| | 208 | | { |
| 2 | 209 | | TotalFriendCount = msg.totalFriends; |
| 2 | 210 | | OnTotalFriendsUpdated?.Invoke(TotalFriendCount); |
| 2 | 211 | | AddFriends(msg.friends); |
| 2 | 212 | | } |
| | 213 | |
|
| | 214 | | private void AddFriendsWithDirectMessages(AddFriendsWithDirectMessagesPayload friendsWithDMs) |
| | 215 | | { |
| 1 | 216 | | TotalFriendsWithDirectMessagesCount = friendsWithDMs.totalFriendsWithDirectMessages; |
| 4 | 217 | | AddFriends(friendsWithDMs.currentFriendsWithDirectMessages.Select(messages => messages.userId)); |
| 1 | 218 | | OnAddFriendsWithDirectMessages?.Invoke(friendsWithDMs.currentFriendsWithDirectMessages.ToList()); |
| 1 | 219 | | } |
| | 220 | |
|
| | 221 | | private void UpdateUserPresence(UserStatus newUserStatus) |
| | 222 | | { |
| 1 | 223 | | if (!friends.ContainsKey(newUserStatus.userId)) return; |
| | 224 | |
|
| | 225 | | // Kernel doesn't send the friendship status on this call, we have to keep it or it gets defaulted |
| 1 | 226 | | newUserStatus.friendshipStatus = friends[newUserStatus.userId].friendshipStatus; |
| 1 | 227 | | UpdateUserStatus(newUserStatus); |
| 1 | 228 | | } |
| | 229 | |
|
| | 230 | | private void UpdateTotalFriendRequests(UpdateTotalFriendRequestsPayload msg) |
| | 231 | | { |
| 1 | 232 | | TotalReceivedFriendRequestCount = msg.totalReceivedRequests; |
| 1 | 233 | | TotalSentFriendRequestCount = msg.totalSentRequests; |
| 1 | 234 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| 1 | 235 | | } |
| | 236 | |
|
| | 237 | | private void UpdateTotalFriends(UpdateTotalFriendsPayload msg) |
| | 238 | | { |
| 1 | 239 | | TotalFriendCount = msg.totalFriends; |
| 1 | 240 | | OnTotalFriendsUpdated?.Invoke(TotalFriendCount); |
| 1 | 241 | | } |
| | 242 | |
|
| | 243 | | private void UpdateUserStatus(UserStatus newUserStatus) |
| | 244 | | { |
| 1 | 245 | | if (!friends.ContainsKey(newUserStatus.userId)) |
| | 246 | | { |
| 0 | 247 | | friends.Add(newUserStatus.userId, newUserStatus); |
| 0 | 248 | | OnUpdateUserStatus?.Invoke(newUserStatus.userId, newUserStatus); |
| | 249 | | } |
| | 250 | | else |
| | 251 | | { |
| 1 | 252 | | if (!friends[newUserStatus.userId].Equals(newUserStatus)) |
| | 253 | | { |
| 1 | 254 | | friends[newUserStatus.userId] = newUserStatus; |
| 1 | 255 | | OnUpdateUserStatus?.Invoke(newUserStatus.userId, newUserStatus); |
| | 256 | | } |
| | 257 | | } |
| 1 | 258 | | } |
| | 259 | |
|
| | 260 | | private void UpdateFriendshipStatus(FriendshipUpdateStatusMessage msg) |
| | 261 | | { |
| 24 | 262 | | var friendshipStatus = ToFriendshipStatus(msg.action); |
| 24 | 263 | | var userId = msg.userId; |
| | 264 | |
|
| 24 | 265 | | if (friends.ContainsKey(userId) && friends[userId].friendshipStatus == friendshipStatus) |
| 0 | 266 | | return; |
| | 267 | |
|
| 24 | 268 | | if (!friends.ContainsKey(userId)) |
| 20 | 269 | | friends.Add(userId, new UserStatus { userId = userId }); |
| | 270 | |
|
| 24 | 271 | | if (ItsAnOutdatedUpdate(userId, friendshipStatus)) |
| 0 | 272 | | return; |
| | 273 | |
|
| 24 | 274 | | friends[userId].friendshipStatus = friendshipStatus; |
| | 275 | |
|
| 24 | 276 | | if (friendshipStatus == FriendshipStatus.NOT_FRIEND) |
| 6 | 277 | | friends.Remove(userId); |
| | 278 | |
|
| 24 | 279 | | OnUpdateFriendship?.Invoke(userId, msg.action); |
| 23 | 280 | | } |
| | 281 | |
|
| | 282 | | private bool ItsAnOutdatedUpdate(string userId, FriendshipStatus friendshipStatus) |
| | 283 | | { |
| 24 | 284 | | return friendshipStatus == FriendshipStatus.REQUESTED_FROM |
| | 285 | | && friends[userId].friendshipStatus == FriendshipStatus.FRIEND; |
| | 286 | | } |
| | 287 | |
|
| | 288 | | private static FriendshipStatus ToFriendshipStatus(FriendshipAction action) |
| | 289 | | { |
| | 290 | | switch (action) |
| | 291 | | { |
| | 292 | | case FriendshipAction.NONE: |
| | 293 | | break; |
| | 294 | | case FriendshipAction.APPROVED: |
| 9 | 295 | | return FriendshipStatus.FRIEND; |
| | 296 | | case FriendshipAction.REJECTED: |
| 1 | 297 | | return FriendshipStatus.NOT_FRIEND; |
| | 298 | | case FriendshipAction.CANCELLED: |
| 1 | 299 | | return FriendshipStatus.NOT_FRIEND; |
| | 300 | | case FriendshipAction.REQUESTED_FROM: |
| 4 | 301 | | return FriendshipStatus.REQUESTED_FROM; |
| | 302 | | case FriendshipAction.REQUESTED_TO: |
| 5 | 303 | | return FriendshipStatus.REQUESTED_TO; |
| | 304 | | case FriendshipAction.DELETED: |
| 3 | 305 | | return FriendshipStatus.NOT_FRIEND; |
| | 306 | | } |
| | 307 | |
|
| 1 | 308 | | return FriendshipStatus.NOT_FRIEND; |
| | 309 | | } |
| | 310 | |
|
| | 311 | | private void AddFriends(IEnumerable<string> friendIds) |
| | 312 | | { |
| 18 | 313 | | foreach (var friendId in friendIds) |
| | 314 | | { |
| 6 | 315 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 316 | | { action = FriendshipAction.APPROVED, userId = friendId }); |
| | 317 | | } |
| 3 | 318 | | } |
| | 319 | |
|
| | 320 | | private FriendRequest ToFriendRequest(FriendRequestPayload friendRequest) => |
| 5 | 321 | | new FriendRequest( |
| | 322 | | friendRequest.friendRequestId, |
| | 323 | | friendRequest.timestamp, |
| | 324 | | friendRequest.@from, |
| | 325 | | friendRequest.to, |
| | 326 | | friendRequest.messageBody); |
| | 327 | |
|
| | 328 | | // TODO (NEW FRIEND REQUESTS): remove when we don't need to keep the retro-compatibility with the old version |
| | 329 | | private void AddFriendRequests(AddFriendRequestsPayload msg) |
| | 330 | | { |
| 0 | 331 | | TotalReceivedFriendRequestCount = msg.totalReceivedFriendRequests; |
| 0 | 332 | | TotalSentFriendRequestCount = msg.totalSentFriendRequests; |
| 0 | 333 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| | 334 | |
|
| 0 | 335 | | foreach (var userId in msg.requestedFrom) |
| | 336 | | { |
| 0 | 337 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 338 | | { action = FriendshipAction.REQUESTED_FROM, userId = userId }); |
| | 339 | | } |
| | 340 | |
|
| 0 | 341 | | foreach (var userId in msg.requestedTo) |
| | 342 | | { |
| 0 | 343 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 344 | | { action = FriendshipAction.REQUESTED_TO, userId = userId }); |
| | 345 | | } |
| 0 | 346 | | } |
| | 347 | |
|
| | 348 | | private void AddFriendRequest(FriendRequestPayload friendRequest) |
| | 349 | | { |
| 0 | 350 | | OnAddFriendRequest?.Invoke(new FriendRequest( |
| | 351 | | friendRequest.friendRequestId, |
| | 352 | | friendRequest.timestamp, |
| | 353 | | friendRequest.from, |
| | 354 | | friendRequest.to, |
| | 355 | | friendRequest.messageBody)); |
| 0 | 356 | | } |
| | 357 | | } |
| | 358 | | } |