| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCl.Social.Friends; |
| | 3 | | using DCL.Tasks; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace DCL.Social.Friends |
| | 11 | | { |
| | 12 | | public class FriendsController : IFriendsController |
| | 13 | | { |
| | 14 | | private const string USE_SOCIAL_CLIENT_FEATURE_FLAG = "use-social-client"; |
| 22 | 15 | | private static readonly Comparer<long> DESCENDING_LONG_COMPARER = Comparer<long>.Create((l, l1) => -l.CompareTo( |
| | 16 | |
|
| | 17 | | private readonly IFriendsApiBridge apiBridge; |
| | 18 | | private readonly ISocialApiBridge socialApiBridge; |
| | 19 | |
|
| | 20 | | // Used only when social client feature flag is false |
| 450 | 21 | | private readonly Dictionary<string, FriendRequest> friendRequests = new (); |
| | 22 | |
|
| 450 | 23 | | private readonly Dictionary<string, FriendRequest> incomingFriendRequestsById = new (); |
| 450 | 24 | | private readonly Dictionary<string, FriendRequest> outgoingFriendRequestsById = new (); |
| 450 | 25 | | private readonly SortedList<long, FriendRequest> incomingFriendRequestsByTimestamp = new (DESCENDING_LONG_COMPAR |
| 450 | 26 | | private readonly SortedList<long, FriendRequest> outgoingFriendRequestsByTimestamp = new (DESCENDING_LONG_COMPAR |
| 450 | 27 | | private readonly Dictionary<string, UserStatus> friends = new (); |
| 450 | 28 | | private readonly SortedList<string, UserStatus> friendsSortedByName = new (); |
| | 29 | | private readonly DataStore dataStore; |
| | 30 | | private readonly IUserProfileBridge userProfileBridge; |
| | 31 | |
|
| 450 | 32 | | private CancellationTokenSource controllerCancellationTokenSource = new (); |
| | 33 | | private UniTaskCompletionSource featureFlagsInitializedTask; |
| | 34 | | private int totalFriendCount; |
| | 35 | | private int totalReceivedFriendRequestCount; |
| | 36 | | private int totalSentFriendRequestCount; |
| | 37 | |
|
| 608 | 38 | | private FeatureFlag featureFlags => dataStore.featureFlags.flags.Get(); |
| | 39 | |
|
| 158 | 40 | | private bool useSocialApiBridge => featureFlags.IsFeatureEnabled(USE_SOCIAL_CLIENT_FEATURE_FLAG); |
| | 41 | |
|
| 0 | 42 | | public int AllocatedFriendCount => friends.Count(f => f.Value.friendshipStatus == FriendshipStatus.FRIEND); |
| 14 | 43 | | public bool IsInitialized { get; private set; } |
| | 44 | |
|
| 0 | 45 | | public int ReceivedRequestCount => friends.Values.Count(status => status.friendshipStatus == FriendshipStatus.RE |
| | 46 | |
|
| | 47 | | public int TotalFriendCount |
| | 48 | | { |
| 2 | 49 | | get => useSocialApiBridge ? friends.Count : totalFriendCount; |
| 3 | 50 | | private set => totalFriendCount = value; |
| | 51 | | } |
| | 52 | |
|
| 0 | 53 | | public int TotalFriendRequestCount => TotalReceivedFriendRequestCount + TotalSentFriendRequestCount; |
| | 54 | |
|
| | 55 | | public int TotalReceivedFriendRequestCount |
| | 56 | | { |
| 4 | 57 | | get => useSocialApiBridge ? incomingFriendRequestsById.Count : totalReceivedFriendRequestCount; |
| 10 | 58 | | private set => totalReceivedFriendRequestCount = value; |
| | 59 | | } |
| | 60 | |
|
| | 61 | | public int TotalSentFriendRequestCount |
| | 62 | | { |
| 3 | 63 | | get => useSocialApiBridge ? outgoingFriendRequestsById.Count : totalSentFriendRequestCount; |
| 3 | 64 | | private set => totalSentFriendRequestCount = value; |
| | 65 | | } |
| | 66 | |
|
| 1 | 67 | | public int TotalFriendsWithDirectMessagesCount { get; private set; } |
| | 68 | |
|
| | 69 | | public event Action<string, UserStatus> OnUpdateUserStatus; |
| | 70 | | public event Action<string, FriendshipAction> OnUpdateFriendship; |
| | 71 | | public event Action<string> OnFriendNotFound; |
| | 72 | | public event Action OnInitialized; |
| | 73 | | public event Action<List<FriendWithDirectMessages>> OnAddFriendsWithDirectMessages; |
| | 74 | | public event Action<int, int> OnTotalFriendRequestUpdated; |
| | 75 | | public event Action<FriendRequest> OnFriendRequestReceived; |
| | 76 | | public event Action<FriendRequest> OnSentFriendRequestApproved; |
| | 77 | |
|
| 450 | 78 | | public FriendsController(IFriendsApiBridge apiBridge, ISocialApiBridge socialApiBridge, DataStore dataStore, |
| | 79 | | IUserProfileBridge userProfileBridge) |
| | 80 | | { |
| 450 | 81 | | this.apiBridge = apiBridge; |
| 450 | 82 | | this.socialApiBridge = socialApiBridge; |
| 450 | 83 | | this.dataStore = dataStore; |
| 450 | 84 | | this.userProfileBridge = userProfileBridge; |
| 450 | 85 | | } |
| | 86 | |
|
| | 87 | | public void Initialize() |
| | 88 | | { |
| 450 | 89 | | controllerCancellationTokenSource = controllerCancellationTokenSource.SafeRestart(); |
| | 90 | |
|
| | 91 | | // TODO: wrap this events into socialApiBridge since it wont be supported by the social service |
| 450 | 92 | | apiBridge.OnUserPresenceUpdated += UpdateUserPresence; |
| 450 | 93 | | apiBridge.OnFriendWithDirectMessagesAdded += AddFriendsWithDirectMessages; |
| | 94 | |
|
| | 95 | | async UniTask InitializeSocialClientAsync(CancellationToken cancellationToken) |
| | 96 | | { |
| 86 | 97 | | if (!useSocialApiBridge) return; |
| | 98 | |
|
| | 99 | | try |
| | 100 | | { |
| | 101 | |
|
| 6 | 102 | | AllFriendsInitializationMessage info = await socialApiBridge.GetInitializationInformationAsync(cance |
| 6 | 103 | | InitializeFriendships(info); |
| | 104 | |
|
| 20 | 105 | | foreach (string friendId in info.Friends) |
| 4 | 106 | | AddFriendToCacheAndTriggerFriendshipUpdate(friendId); |
| | 107 | |
|
| 12 | 108 | | foreach (FriendRequest friendRequest in info.IncomingFriendRequests) |
| 0 | 109 | | AddIncomingFriendRequest(friendRequest, false); |
| | 110 | |
|
| 12 | 111 | | foreach (FriendRequest friendRequest in info.OutgoingFriendRequests) |
| 0 | 112 | | AddOutgoingFriendRequest(friendRequest, false); |
| | 113 | |
|
| 6 | 114 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| 6 | 115 | | } |
| | 116 | | catch (Exception ex) |
| | 117 | | { |
| 0 | 118 | | Debug.LogError("Couldn't initialize friendship information"); |
| 0 | 119 | | Debug.LogException(ex); |
| 0 | 120 | | } |
| 46 | 121 | | } |
| | 122 | |
|
| | 123 | | void SubscribeToCorrespondingBridgeEvents() |
| | 124 | | { |
| 46 | 125 | | if (useSocialApiBridge) |
| | 126 | | { |
| 6 | 127 | | socialApiBridge.OnIncomingFriendRequestAdded += AddIncomingFriendRequest; |
| 6 | 128 | | socialApiBridge.OnOutgoingFriendRequestAdded += AddOutgoingFriendRequest; |
| 6 | 129 | | socialApiBridge.OnFriendRequestAccepted += HandlePeerAcceptedFriendRequest; |
| 6 | 130 | | socialApiBridge.OnDeletedByFriend += HandlePeerDeletedFriendship; |
| 6 | 131 | | socialApiBridge.OnFriendRequestRejected += RemoveFriendRequestFromRejection; |
| 6 | 132 | | socialApiBridge.OnFriendRequestCanceled += RemoveFriendRequestFromCancellation; |
| | 133 | | } |
| | 134 | | else |
| | 135 | | { |
| 40 | 136 | | apiBridge.OnInitialized += InitializeFriendships; |
| | 137 | |
|
| | 138 | | // TODO (NEW FRIEND REQUESTS): remove when we don't need to keep the retro-compatibility with the ol |
| 40 | 139 | | apiBridge.OnFriendRequestsAdded += AddFriendRequests; |
| 40 | 140 | | apiBridge.OnFriendRequestReceived += ReceiveFriendRequest; |
| 40 | 141 | | apiBridge.OnTotalFriendRequestCountUpdated += UpdateTotalFriendRequests; |
| 40 | 142 | | apiBridge.OnTotalFriendCountUpdated += UpdateTotalFriends; |
| 40 | 143 | | apiBridge.OnFriendshipStatusUpdated += HandleUpdateFriendshipStatus; |
| 40 | 144 | | apiBridge.OnFriendNotFound += FriendNotFound; |
| | 145 | | } |
| 40 | 146 | | } |
| | 147 | |
|
| 450 | 148 | | if (featureFlags.IsInitialized) |
| | 149 | | { |
| 27 | 150 | | SubscribeToCorrespondingBridgeEvents(); |
| 27 | 151 | | InitializeSocialClientAsync(controllerCancellationTokenSource.Token).Forget(); |
| | 152 | | } |
| | 153 | | else |
| | 154 | | { |
| 423 | 155 | | WaitForFeatureFlagsToBeInitialized(controllerCancellationTokenSource.Token) |
| | 156 | | .ContinueWith(() => |
| | 157 | | { |
| 19 | 158 | | SubscribeToCorrespondingBridgeEvents(); |
| 19 | 159 | | InitializeSocialClientAsync(controllerCancellationTokenSource.Token).Forget(); |
| 19 | 160 | | }) |
| | 161 | | .Forget(); |
| | 162 | | } |
| 423 | 163 | | } |
| | 164 | |
|
| | 165 | | public void Dispose() |
| | 166 | | { |
| 423 | 167 | | controllerCancellationTokenSource.SafeCancelAndDispose(); |
| | 168 | |
|
| 423 | 169 | | socialApiBridge.OnIncomingFriendRequestAdded -= AddIncomingFriendRequest; |
| 423 | 170 | | socialApiBridge.OnOutgoingFriendRequestAdded -= AddOutgoingFriendRequest; |
| 423 | 171 | | socialApiBridge.OnFriendRequestAccepted -= HandlePeerAcceptedFriendRequest; |
| 423 | 172 | | socialApiBridge.OnDeletedByFriend -= HandlePeerDeletedFriendship; |
| 423 | 173 | | socialApiBridge.OnFriendRequestRejected -= RemoveFriendRequestFromRejection; |
| 423 | 174 | | socialApiBridge.OnFriendRequestCanceled -= RemoveFriendRequestFromCancellation; |
| | 175 | |
|
| 423 | 176 | | apiBridge.OnInitialized -= InitializeFriendships; |
| | 177 | |
|
| | 178 | | // TODO (NEW FRIEND REQUESTS): remove when we don't need to keep the retro-compatibility with the old versio |
| 423 | 179 | | apiBridge.OnFriendRequestsAdded -= AddFriendRequests; |
| 423 | 180 | | apiBridge.OnFriendRequestReceived -= ReceiveFriendRequest; |
| 423 | 181 | | apiBridge.OnFriendNotFound -= FriendNotFound; |
| 423 | 182 | | apiBridge.OnFriendWithDirectMessagesAdded -= AddFriendsWithDirectMessages; |
| 423 | 183 | | apiBridge.OnUserPresenceUpdated -= UpdateUserPresence; |
| 423 | 184 | | apiBridge.OnFriendshipStatusUpdated -= HandleUpdateFriendshipStatus; |
| 423 | 185 | | apiBridge.OnTotalFriendRequestCountUpdated -= UpdateTotalFriendRequests; |
| 423 | 186 | | apiBridge.OnTotalFriendCountUpdated -= UpdateTotalFriends; |
| 423 | 187 | | } |
| | 188 | |
|
| | 189 | | public UserStatus GetUserStatus(string userId) |
| | 190 | | { |
| 0 | 191 | | if (!friends.ContainsKey(userId)) |
| 0 | 192 | | return new UserStatus { userId = userId, friendshipStatus = FriendshipStatus.NOT_FRIEND }; |
| | 193 | |
|
| 0 | 194 | | return friends[userId]; |
| | 195 | | } |
| | 196 | |
|
| | 197 | | public bool ContainsStatus(string friendId, FriendshipStatus status) |
| | 198 | | { |
| 0 | 199 | | if (!friends.ContainsKey(friendId)) return false; |
| 0 | 200 | | return friends[friendId].friendshipStatus == status; |
| | 201 | | } |
| | 202 | |
|
| | 203 | | public async UniTask<FriendRequest> RequestFriendshipAsync(string friendUserId, string messageBody, Cancellation |
| | 204 | | { |
| 2 | 205 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 206 | |
|
| | 207 | | FriendRequest friendRequest; |
| | 208 | |
|
| 2 | 209 | | if (useSocialApiBridge) |
| | 210 | | { |
| 1 | 211 | | friendRequest = await socialApiBridge.RequestFriendshipAsync(friendUserId, messageBody, cancellationToke |
| 1 | 212 | | outgoingFriendRequestsById[friendRequest.FriendRequestId] = friendRequest; |
| | 213 | | // it's impossible to have duplicated timestamps for two different correctly created friend requests |
| 1 | 214 | | outgoingFriendRequestsByTimestamp[friendRequest.Timestamp.Ticks] = friendRequest; |
| | 215 | |
|
| 1 | 216 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| | 217 | | } |
| | 218 | | else |
| | 219 | | { |
| 1 | 220 | | RequestFriendshipConfirmationPayload payload = await apiBridge.RequestFriendshipAsync(friendUserId, mess |
| | 221 | |
|
| 1 | 222 | | FriendRequestPayload friendRequestPayload = payload.friendRequest; |
| | 223 | |
|
| 1 | 224 | | friendRequest = new (friendRequestPayload.friendRequestId, |
| | 225 | | DateTimeOffset.FromUnixTimeMilliseconds(friendRequestPayload.timestamp).DateTime, |
| | 226 | | friendRequestPayload.from, |
| | 227 | | friendRequestPayload.to, |
| | 228 | | friendRequestPayload.messageBody); |
| | 229 | |
|
| 1 | 230 | | friendRequests[friendRequest.FriendRequestId] = friendRequest; |
| | 231 | | } |
| | 232 | |
|
| 2 | 233 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 234 | | { action = FriendshipAction.REQUESTED_TO, userId = friendRequest.To }); |
| | 235 | |
|
| 2 | 236 | | return friendRequest; |
| 2 | 237 | | } |
| | 238 | |
|
| | 239 | | public Dictionary<string, UserStatus> GetAllocatedFriends() => |
| 1 | 240 | | new (friends); |
| | 241 | |
|
| | 242 | | public async UniTask<FriendRequest> AcceptFriendshipAsync(string friendRequestId, CancellationToken cancellation |
| | 243 | | { |
| 2 | 244 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 245 | |
|
| | 246 | | FriendRequest request; |
| | 247 | |
|
| 2 | 248 | | if (useSocialApiBridge) |
| | 249 | | { |
| 1 | 250 | | request = incomingFriendRequestsById[friendRequestId]; |
| 1 | 251 | | await socialApiBridge.AcceptFriendshipAsync(request.From, cancellationToken); |
| | 252 | |
|
| 1 | 253 | | incomingFriendRequestsById.Remove(request.FriendRequestId); |
| 1 | 254 | | incomingFriendRequestsByTimestamp.Remove(request.Timestamp.Ticks); |
| 1 | 255 | | outgoingFriendRequestsById.Remove(request.FriendRequestId); |
| 1 | 256 | | outgoingFriendRequestsByTimestamp.Remove(request.Timestamp.Ticks); |
| | 257 | |
|
| 1 | 258 | | AddFriendToCacheAndTriggerFriendshipUpdate(request.From); |
| | 259 | |
|
| 1 | 260 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| | 261 | |
|
| 1 | 262 | | return request; |
| | 263 | | } |
| | 264 | |
|
| 1 | 265 | | AcceptFriendshipPayload payload = await apiBridge.AcceptFriendshipAsync(friendRequestId, cancellationToken); |
| | 266 | |
|
| 1 | 267 | | FriendRequestPayload requestPayload = payload.FriendRequest; |
| 1 | 268 | | request = ToFriendRequest(requestPayload); |
| 1 | 269 | | friendRequests.Remove(request.FriendRequestId); |
| | 270 | |
|
| 1 | 271 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 272 | | { action = FriendshipAction.APPROVED, userId = request.From }); |
| | 273 | |
|
| 1 | 274 | | return request; |
| 2 | 275 | | } |
| | 276 | |
|
| | 277 | | public async UniTask<FriendRequest> RejectFriendshipAsync(string friendRequestId, CancellationToken cancellation |
| | 278 | | { |
| 2 | 279 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 280 | |
|
| | 281 | | FriendRequest friendRequest; |
| | 282 | |
|
| 2 | 283 | | if (useSocialApiBridge) |
| | 284 | | { |
| 1 | 285 | | friendRequest = incomingFriendRequestsById[friendRequestId]; |
| 1 | 286 | | await socialApiBridge.RejectFriendshipAsync(friendRequest.From, cancellationToken); |
| 1 | 287 | | RemoveFriendRequestAndUpdateFriendshipStatus(friendRequest.From, FriendshipAction.REJECTED); |
| 1 | 288 | | return friendRequest; |
| | 289 | | } |
| | 290 | |
|
| 1 | 291 | | RejectFriendshipPayload payload = await apiBridge.RejectFriendshipAsync(friendRequestId, cancellationToken); |
| | 292 | |
|
| 1 | 293 | | FriendRequestPayload requestPayload = payload.FriendRequestPayload; |
| 1 | 294 | | friendRequest = ToFriendRequest(requestPayload); |
| 1 | 295 | | friendRequests.Remove(friendRequest.FriendRequestId); |
| | 296 | |
|
| 1 | 297 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 298 | | { action = FriendshipAction.REJECTED, userId = friendRequest.From }); |
| | 299 | |
|
| 1 | 300 | | return friendRequest; |
| 2 | 301 | | } |
| | 302 | |
|
| | 303 | | public bool IsFriend(string userId) => |
| 0 | 304 | | friends.ContainsKey(userId) && friends[userId].friendshipStatus == FriendshipStatus.FRIEND; |
| | 305 | |
|
| | 306 | | public async UniTask RemoveFriendAsync(string friendId, CancellationToken cancellationToken) |
| | 307 | | { |
| 0 | 308 | | if (!useSocialApiBridge) |
| | 309 | | { |
| 0 | 310 | | apiBridge.RemoveFriend(friendId); |
| 0 | 311 | | return; |
| | 312 | | } |
| | 313 | |
|
| 0 | 314 | | await socialApiBridge.DeleteFriendshipAsync(friendId, cancellationToken); |
| 0 | 315 | | RemoveFriendFromCacheAndTriggerFriendshipUpdate(friendId); |
| 0 | 316 | | } |
| | 317 | |
|
| | 318 | | public async UniTask<string[]> GetFriendsAsync(int limit, int skip, CancellationToken cancellationToken = defaul |
| | 319 | | { |
| 14 | 320 | | if (useSocialApiBridge) |
| | 321 | | { |
| 12 | 322 | | int startIndex = skip; |
| 12 | 323 | | int endIndex = Math.Min(skip + limit, friendsSortedByName.Count) - 1; |
| 12 | 324 | | int arraySize = endIndex - startIndex + 1; |
| | 325 | |
|
| 12 | 326 | | var friendIds = new string[arraySize]; |
| | 327 | |
|
| 44 | 328 | | for (int i = startIndex; i <= endIndex; i++) |
| | 329 | | { |
| 10 | 330 | | string key = friendsSortedByName.Keys[i]; |
| 10 | 331 | | friendIds[i - startIndex] = friendsSortedByName[key].userId; |
| | 332 | | } |
| | 333 | |
|
| 12 | 334 | | return friendIds; |
| | 335 | | } |
| | 336 | |
|
| 2 | 337 | | var payload = await apiBridge.GetFriendsAsync(limit, skip, cancellationToken); |
| 2 | 338 | | await UniTask.SwitchToMainThread(); |
| | 339 | |
|
| 2 | 340 | | TotalFriendCount = payload.totalFriends; |
| 2 | 341 | | AddFriends(payload.friends); |
| | 342 | |
|
| 2 | 343 | | return payload.friends; |
| 14 | 344 | | } |
| | 345 | |
|
| | 346 | | public async UniTask<IReadOnlyList<string>> GetFriendsAsync(string userNameOrId, int limit, CancellationToken ca |
| | 347 | | { |
| 2 | 348 | | if (useSocialApiBridge) |
| | 349 | | { |
| 2 | 350 | | var friendsToReturn = new List<string>(); |
| | 351 | |
|
| 17 | 352 | | foreach (var friend in friendsSortedByName.Values) |
| | 353 | | { |
| 7 | 354 | | if (friend.userName.IndexOf(userNameOrId, StringComparison.OrdinalIgnoreCase) >= 0 || |
| 3 | 355 | | friend.userId.IndexOf(userNameOrId, StringComparison.OrdinalIgnoreCase) >= 0) { friendsToReturn. |
| | 356 | |
|
| 7 | 357 | | if (friendsToReturn.Count >= limit) { break; } |
| | 358 | | } |
| | 359 | |
|
| 2 | 360 | | return friendsToReturn; |
| | 361 | | } |
| | 362 | |
|
| 0 | 363 | | var payload = await apiBridge.GetFriendsAsync(userNameOrId, limit, cancellationToken); |
| 0 | 364 | | await UniTask.SwitchToMainThread(); |
| | 365 | |
|
| 0 | 366 | | TotalFriendCount = payload.totalFriends; |
| 0 | 367 | | AddFriends(payload.friends); |
| | 368 | |
|
| 0 | 369 | | return payload.friends; |
| 2 | 370 | | } |
| | 371 | |
|
| | 372 | | public async UniTask<IReadOnlyList<FriendRequest>> GetFriendRequestsAsync(int sentLimit, int sentSkip, int recei |
| | 373 | | { |
| 3 | 374 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 375 | |
|
| 3 | 376 | | if (useSocialApiBridge) |
| | 377 | | { |
| 1 | 378 | | int sentCount = outgoingFriendRequestsByTimestamp.Values.Count; |
| 1 | 379 | | int receivedCount = incomingFriendRequestsByTimestamp.Values.Count; |
| | 380 | |
|
| 1 | 381 | | int capacity = sentCount - sentSkip + receivedCount - receivedSkip; |
| 1 | 382 | | if (capacity <= 0) return new List<FriendRequest>(0); |
| | 383 | |
|
| 1 | 384 | | List<FriendRequest> result = new List<FriendRequest>(capacity); |
| | 385 | |
|
| 10 | 386 | | for (int i = receivedSkip; i < receivedSkip + receivedLimit && i < incomingFriendRequestsByTimestamp.Val |
| | 387 | | { |
| 4 | 388 | | var receivedRequest = incomingFriendRequestsByTimestamp.Values[i]; |
| 4 | 389 | | result.Add(receivedRequest); |
| | 390 | | } |
| | 391 | |
|
| 10 | 392 | | for (int i = sentSkip; i < sentSkip + sentLimit && i < outgoingFriendRequestsByTimestamp.Values.Count; i |
| | 393 | | { |
| 4 | 394 | | var sentRequest = outgoingFriendRequestsByTimestamp.Values[i]; |
| 4 | 395 | | result.Add(sentRequest); |
| | 396 | | } |
| | 397 | |
|
| 1 | 398 | | return result; |
| | 399 | | } |
| | 400 | |
|
| 2 | 401 | | var payload = await apiBridge.GetFriendRequestsAsync(sentLimit, sentSkip, receivedLimit, receivedSkip, cance |
| | 402 | |
|
| 2 | 403 | | TotalReceivedFriendRequestCount = payload.totalReceivedFriendRequests; |
| 2 | 404 | | TotalSentFriendRequestCount = payload.totalSentFriendRequests; |
| 2 | 405 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| | 406 | |
|
| 2 | 407 | | List<FriendRequest> receivedFriendRequestsToAdd = new List<FriendRequest>(); |
| | 408 | |
|
| 14 | 409 | | foreach (var friendRequest in payload.requestedFrom) |
| | 410 | | { |
| 5 | 411 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 412 | |
|
| 5 | 413 | | var request = ToFriendRequest(friendRequest); |
| 5 | 414 | | friendRequests[request.FriendRequestId] = request; |
| 5 | 415 | | receivedFriendRequestsToAdd.Add(request); |
| | 416 | |
|
| 5 | 417 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 418 | | { action = FriendshipAction.REQUESTED_FROM, userId = friendRequest.from }); |
| | 419 | | } |
| | 420 | |
|
| 12 | 421 | | foreach (var friendRequest in payload.requestedTo) |
| | 422 | | { |
| 4 | 423 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 424 | |
|
| 4 | 425 | | var request = ToFriendRequest(friendRequest); |
| 4 | 426 | | friendRequests[request.FriendRequestId] = request; |
| 4 | 427 | | receivedFriendRequestsToAdd.Add(request); |
| | 428 | |
|
| 4 | 429 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 430 | | { action = FriendshipAction.REQUESTED_TO, userId = friendRequest.to }); |
| | 431 | | } |
| | 432 | |
|
| 2 | 433 | | return receivedFriendRequestsToAdd; |
| 3 | 434 | | } |
| | 435 | |
|
| | 436 | | // TODO: implement async function and make the new social service support |
| | 437 | | public void GetFriendsWithDirectMessages(int limit, int skip) => |
| 0 | 438 | | apiBridge.GetFriendsWithDirectMessages("", limit, skip); |
| | 439 | |
|
| | 440 | | // TODO: implement async function and make the new social service support |
| | 441 | | public void GetFriendsWithDirectMessages(string userNameOrId, int limit) => |
| 0 | 442 | | apiBridge.GetFriendsWithDirectMessages(userNameOrId, limit, 0); |
| | 443 | |
|
| | 444 | | public bool TryGetAllocatedFriendRequest(string friendRequestId, out FriendRequest result) |
| | 445 | | { |
| 9 | 446 | | if (!useSocialApiBridge) |
| 9 | 447 | | return friendRequests.TryGetValue(friendRequestId, out result); |
| | 448 | |
|
| 0 | 449 | | return incomingFriendRequestsById.TryGetValue(friendRequestId, out result) |
| | 450 | | || outgoingFriendRequestsById.TryGetValue(friendRequestId, out result); |
| | 451 | | } |
| | 452 | |
|
| | 453 | | public FriendRequest GetAllocatedFriendRequestByUser(string userId) |
| | 454 | | { |
| 22 | 455 | | long max = long.MinValue; |
| 22 | 456 | | FriendRequest result = null; |
| | 457 | |
|
| 22 | 458 | | if (useSocialApiBridge) |
| | 459 | | { |
| 21 | 460 | | foreach (var request in incomingFriendRequestsById.Values) |
| | 461 | | { |
| 5 | 462 | | if (request.From != userId && request.To != userId) continue; |
| 1 | 463 | | if (request.Timestamp.Ticks <= max) continue; |
| 1 | 464 | | result = request; |
| 1 | 465 | | max = request.Timestamp.Ticks; |
| 1 | 466 | | break; |
| | 467 | | } |
| | 468 | |
|
| 14 | 469 | | foreach (var request in outgoingFriendRequestsById.Values) |
| | 470 | | { |
| 2 | 471 | | if (request.From != userId && request.To != userId) continue; |
| | 472 | |
|
| | 473 | | // we do check for max here because within a single session there can be a |
| | 474 | | // sent and a received request with the same user and we want to show the latest one |
| 2 | 475 | | if (request.Timestamp.Ticks <= max) continue; |
| 2 | 476 | | return request; |
| | 477 | | } |
| | 478 | | } |
| | 479 | | else |
| | 480 | | { |
| 68 | 481 | | foreach (var request in friendRequests.Values) |
| | 482 | | { |
| 18 | 483 | | if (request.From != userId && request.To != userId) continue; |
| 6 | 484 | | if (request.Timestamp.Ticks <= max) continue; |
| 6 | 485 | | result = request; |
| 6 | 486 | | max = request.Timestamp.Ticks; |
| | 487 | | } |
| | 488 | | } |
| | 489 | |
|
| 20 | 490 | | return result; |
| 2 | 491 | | } |
| | 492 | |
|
| | 493 | | public async UniTask<FriendshipStatus> GetFriendshipStatus(string userId, CancellationToken cancellationToken) |
| | 494 | | { |
| 0 | 495 | | if (useSocialApiBridge) |
| | 496 | | { |
| 0 | 497 | | if (friends.TryGetValue(userId, out UserStatus status)) |
| 0 | 498 | | return status.friendshipStatus; |
| | 499 | |
|
| 0 | 500 | | FriendRequest friendRequest = GetAllocatedFriendRequestByUser(userId); |
| 0 | 501 | | if (friendRequest == null) return FriendshipStatus.NOT_FRIEND; |
| 0 | 502 | | if (outgoingFriendRequestsById.ContainsKey(friendRequest.FriendRequestId)) |
| 0 | 503 | | return FriendshipStatus.REQUESTED_TO; |
| 0 | 504 | | if (incomingFriendRequestsById.ContainsKey(friendRequest.FriendRequestId)) |
| 0 | 505 | | return FriendshipStatus.REQUESTED_FROM; |
| 0 | 506 | | return FriendshipStatus.NOT_FRIEND; |
| | 507 | | } |
| | 508 | | else |
| | 509 | | { |
| 0 | 510 | | FriendshipStatus status = await apiBridge.GetFriendshipStatus(userId, cancellationToken); |
| | 511 | |
|
| 0 | 512 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 513 | | { |
| | 514 | | action = ToFriendshipAction(status), |
| | 515 | | userId = userId, |
| | 516 | | }); |
| | 517 | |
|
| 0 | 518 | | return status; |
| | 519 | | } |
| 0 | 520 | | } |
| | 521 | |
|
| | 522 | | public async UniTask<FriendRequest> CancelRequestByUserIdAsync(string friendUserId, CancellationToken cancellati |
| | 523 | | { |
| 0 | 524 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 525 | |
|
| 0 | 526 | | FriendRequest request = GetAllocatedFriendRequestByUser(friendUserId); |
| | 527 | |
|
| 0 | 528 | | if (request == null) throw new Exception($"No friend request for user: {friendUserId}"); |
| | 529 | |
|
| 0 | 530 | | if (useSocialApiBridge) |
| | 531 | | { |
| 0 | 532 | | await socialApiBridge.CancelFriendshipAsync(request.To, cancellationToken); |
| 0 | 533 | | RemoveFriendRequestAndUpdateFriendshipStatus(request.To, FriendshipAction.CANCELLED); |
| | 534 | | } |
| | 535 | | else |
| | 536 | | { |
| 0 | 537 | | await apiBridge.CancelRequestAsync(request.FriendRequestId, cancellationToken); |
| | 538 | |
|
| 0 | 539 | | friendRequests.Remove(request.FriendRequestId); |
| | 540 | |
|
| 0 | 541 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 542 | | { action = FriendshipAction.CANCELLED, userId = friendUserId }); |
| | 543 | | } |
| | 544 | |
|
| 0 | 545 | | return request; |
| | 546 | |
|
| 0 | 547 | | } |
| | 548 | |
|
| | 549 | | public async UniTask<FriendRequest> CancelRequestAsync(string friendRequestId, CancellationToken cancellationTok |
| | 550 | | { |
| 1 | 551 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 552 | | FriendRequest friendRequest; |
| | 553 | |
|
| 1 | 554 | | if (useSocialApiBridge) |
| | 555 | | { |
| 0 | 556 | | if (TryGetAllocatedFriendRequest(friendRequestId, out friendRequest)) |
| | 557 | | { |
| 0 | 558 | | await socialApiBridge.CancelFriendshipAsync(friendRequest.To, cancellationToken); |
| 0 | 559 | | RemoveFriendRequestAndUpdateFriendshipStatus(friendRequest.To, FriendshipAction.CANCELLED); |
| | 560 | | } |
| | 561 | | else |
| 0 | 562 | | throw new Exception($"Friend request does not exist: {friendRequestId}"); |
| | 563 | | } |
| | 564 | | else |
| | 565 | | { |
| 1 | 566 | | CancelFriendshipConfirmationPayload payload = await apiBridge.CancelRequestAsync(friendRequestId, cancel |
| | 567 | |
|
| 1 | 568 | | friendRequest = ToFriendRequest(payload.friendRequest); |
| | 569 | |
|
| 1 | 570 | | friendRequests.Remove(friendRequest.FriendRequestId); |
| | 571 | |
|
| 1 | 572 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 573 | | { action = FriendshipAction.CANCELLED, userId = friendRequest.To }); |
| | 574 | | } |
| | 575 | |
|
| 1 | 576 | | return friendRequest; |
| 1 | 577 | | } |
| | 578 | |
|
| | 579 | | private void HandlePeerDeletedFriendship(string friendId) |
| | 580 | | { |
| 1 | 581 | | var friendRequest = GetAllocatedFriendRequestByUser(friendId); |
| | 582 | |
|
| 1 | 583 | | if (friendRequest != null) |
| | 584 | | { |
| 0 | 585 | | outgoingFriendRequestsById.Remove(friendRequest.FriendRequestId); |
| 0 | 586 | | outgoingFriendRequestsByTimestamp.Remove(friendRequest.Timestamp.Ticks); |
| | 587 | | } |
| | 588 | |
|
| 1 | 589 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 590 | | { |
| | 591 | | userId = friendId, |
| | 592 | | action = FriendshipAction.DELETED |
| | 593 | | }); |
| 1 | 594 | | } |
| | 595 | |
|
| | 596 | | private void HandlePeerAcceptedFriendRequest(string friendId) |
| | 597 | | { |
| 2 | 598 | | var friendRequest = GetAllocatedFriendRequestByUser(friendId); |
| | 599 | |
|
| 2 | 600 | | if (friendRequest != null) |
| | 601 | | { |
| 0 | 602 | | outgoingFriendRequestsById.Remove(friendRequest.FriendRequestId); |
| 0 | 603 | | outgoingFriendRequestsByTimestamp.Remove(friendRequest.Timestamp.Ticks); |
| | 604 | | } |
| | 605 | |
|
| 2 | 606 | | AddFriendToCacheAndTriggerFriendshipUpdate(friendId); |
| | 607 | |
|
| 2 | 608 | | if (friendRequest != null) |
| 0 | 609 | | OnSentFriendRequestApproved?.Invoke(friendRequest); |
| 2 | 610 | | } |
| | 611 | |
|
| | 612 | | private void RemoveFriendRequestFromRejection(string userId) => |
| 1 | 613 | | RemoveFriendRequestAndUpdateFriendshipStatus(userId, FriendshipAction.REJECTED); |
| | 614 | |
|
| | 615 | | private void RemoveFriendRequestFromCancellation(string userId) => |
| 0 | 616 | | RemoveFriendRequestAndUpdateFriendshipStatus(userId, FriendshipAction.CANCELLED); |
| | 617 | |
|
| | 618 | | private void RemoveFriendRequestAndUpdateFriendshipStatus(string userId, |
| | 619 | | FriendshipAction newFriendshipAction) |
| | 620 | | { |
| 2 | 621 | | var friendRequest = GetAllocatedFriendRequestByUser(userId); |
| | 622 | |
|
| 2 | 623 | | if (friendRequest != null) |
| | 624 | | { |
| 2 | 625 | | incomingFriendRequestsById.Remove(friendRequest.FriendRequestId); |
| 2 | 626 | | incomingFriendRequestsByTimestamp.Remove(friendRequest.Timestamp.Ticks); |
| | 627 | |
|
| 2 | 628 | | outgoingFriendRequestsById.Remove(friendRequest.FriendRequestId); |
| 2 | 629 | | outgoingFriendRequestsByTimestamp.Remove(friendRequest.Timestamp.Ticks); |
| | 630 | | } |
| | 631 | |
|
| 2 | 632 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 633 | | { |
| | 634 | | userId = userId, |
| | 635 | | action = newFriendshipAction |
| | 636 | | }); |
| | 637 | |
|
| 2 | 638 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| 0 | 639 | | } |
| | 640 | |
|
| | 641 | | private void AddFriendToCacheAndTriggerFriendshipUpdate(string friendId) |
| | 642 | | { |
| | 643 | | async UniTaskVoid IndexUserByNameAsync(UserStatus status, CancellationToken cancellationToken) |
| | 644 | | { |
| 7 | 645 | | UserProfile profile = userProfileBridge.Get(status.userId) |
| | 646 | | ?? await userProfileBridge.RequestFullUserProfileAsync(status.userId, cancellation |
| | 647 | |
|
| 7 | 648 | | status.userName = profile.userName; |
| 7 | 649 | | friendsSortedByName[profile.userName] = status; |
| 7 | 650 | | } |
| | 651 | |
|
| | 652 | | UserStatus status; |
| | 653 | |
|
| 7 | 654 | | if (friends.TryGetValue(friendId, out status)) |
| 1 | 655 | | status.friendshipStatus = FriendshipStatus.FRIEND; |
| | 656 | | else |
| | 657 | | { |
| 6 | 658 | | status = new UserStatus |
| | 659 | | { |
| | 660 | | friendshipStatus = FriendshipStatus.FRIEND, |
| | 661 | | userId = friendId, |
| | 662 | | }; |
| | 663 | |
|
| 6 | 664 | | friends[friendId] = status; |
| | 665 | | } |
| | 666 | |
|
| 7 | 667 | | IndexUserByNameAsync(status, controllerCancellationTokenSource.Token).Forget(); |
| | 668 | |
|
| 7 | 669 | | OnUpdateFriendship?.Invoke(friendId, FriendshipAction.APPROVED); |
| 3 | 670 | | } |
| | 671 | |
|
| | 672 | | private void RemoveFriendFromCacheAndTriggerFriendshipUpdate(string userId) |
| | 673 | | { |
| 0 | 674 | | if (!this.friends.ContainsKey(userId)) |
| | 675 | | { |
| 0 | 676 | | Debug.LogWarning($"Tried to remove non existing friend {userId}"); |
| 0 | 677 | | return; |
| | 678 | | } |
| | 679 | |
|
| 0 | 680 | | var friend = this.friends[userId]; |
| 0 | 681 | | this.friends.Remove(userId); |
| | 682 | |
|
| 0 | 683 | | if (string.IsNullOrEmpty(friend.userName)) |
| | 684 | | { |
| 0 | 685 | | string userNameFound = null; |
| | 686 | |
|
| 0 | 687 | | foreach (UserStatus status in friendsSortedByName.Values) |
| | 688 | | { |
| 0 | 689 | | if (status.userId != userId) continue; |
| 0 | 690 | | userNameFound = status.userName; |
| 0 | 691 | | break; |
| | 692 | | } |
| | 693 | |
|
| 0 | 694 | | if (!string.IsNullOrEmpty(userNameFound)) |
| 0 | 695 | | friendsSortedByName.Remove(userNameFound); |
| | 696 | | } |
| | 697 | | else |
| 0 | 698 | | friendsSortedByName.Remove(friend.userName); |
| | 699 | |
|
| 0 | 700 | | OnUpdateFriendship?.Invoke(userId, FriendshipAction.DELETED); |
| 0 | 701 | | } |
| | 702 | |
|
| | 703 | | private void InitializeFriendships(FriendshipInitializationMessage msg) |
| | 704 | | { |
| 7 | 705 | | if (IsInitialized) return; |
| | 706 | |
|
| 7 | 707 | | IsInitialized = true; |
| | 708 | |
|
| 7 | 709 | | TotalReceivedFriendRequestCount = msg.totalReceivedRequests; |
| 7 | 710 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| 7 | 711 | | OnInitialized?.Invoke(); |
| 1 | 712 | | } |
| | 713 | |
|
| | 714 | | private void FriendNotFound(string name) => |
| 0 | 715 | | OnFriendNotFound?.Invoke(name); |
| | 716 | |
|
| | 717 | | private void AddFriendsWithDirectMessages(AddFriendsWithDirectMessagesPayload friendsWithDMs) |
| | 718 | | { |
| 1 | 719 | | TotalFriendsWithDirectMessagesCount = friendsWithDMs.totalFriendsWithDirectMessages; |
| | 720 | |
|
| 4 | 721 | | var friendIds = friendsWithDMs.currentFriendsWithDirectMessages.Select(messages => messages.userId); |
| | 722 | |
|
| 8 | 723 | | foreach (var friendId in friendIds) |
| | 724 | | { |
| 3 | 725 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 726 | | { action = FriendshipAction.APPROVED, userId = friendId }); |
| | 727 | | } |
| | 728 | |
|
| 1 | 729 | | OnAddFriendsWithDirectMessages?.Invoke(friendsWithDMs.currentFriendsWithDirectMessages.ToList()); |
| 1 | 730 | | } |
| | 731 | |
|
| | 732 | | private void UpdateUserPresence(UserStatus newUserStatus) |
| | 733 | | { |
| 1 | 734 | | if (!friends.ContainsKey(newUserStatus.userId)) return; |
| | 735 | |
|
| | 736 | | // Kernel doesn't send the friendship status on this call, we have to keep it or it gets defaulted |
| 1 | 737 | | newUserStatus.friendshipStatus = friends[newUserStatus.userId].friendshipStatus; |
| 1 | 738 | | UpdateUserStatus(newUserStatus); |
| 1 | 739 | | } |
| | 740 | |
|
| | 741 | | // TODO (Joni): this should be called after AddFriendRequests / RemoveFriendRequest |
| | 742 | | private void UpdateTotalFriendRequests(UpdateTotalFriendRequestsPayload msg) |
| | 743 | | { |
| 1 | 744 | | TotalReceivedFriendRequestCount = msg.totalReceivedRequests; |
| 1 | 745 | | TotalSentFriendRequestCount = msg.totalSentRequests; |
| 1 | 746 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| 1 | 747 | | } |
| | 748 | |
|
| | 749 | | private void UpdateTotalFriends(UpdateTotalFriendsPayload msg) |
| | 750 | | { |
| 1 | 751 | | TotalFriendCount = msg.totalFriends; |
| 1 | 752 | | } |
| | 753 | |
|
| | 754 | | private void UpdateUserStatus(UserStatus newUserStatus) |
| | 755 | | { |
| 1 | 756 | | if (!friends.ContainsKey(newUserStatus.userId)) |
| | 757 | | { |
| 0 | 758 | | friends.Add(newUserStatus.userId, newUserStatus); |
| 0 | 759 | | OnUpdateUserStatus?.Invoke(newUserStatus.userId, newUserStatus); |
| | 760 | | } |
| | 761 | | else |
| | 762 | | { |
| 1 | 763 | | if (!friends[newUserStatus.userId].Equals(newUserStatus)) |
| | 764 | | { |
| 1 | 765 | | friends[newUserStatus.userId] = newUserStatus; |
| 1 | 766 | | OnUpdateUserStatus?.Invoke(newUserStatus.userId, newUserStatus); |
| | 767 | | } |
| | 768 | | } |
| 1 | 769 | | } |
| | 770 | |
|
| | 771 | | private void ReceiveFriendRequest(FriendRequestPayload msg) |
| | 772 | | { |
| 2 | 773 | | FriendRequest request = ToFriendRequest(msg); |
| | 774 | |
|
| 2 | 775 | | friendRequests[msg.friendRequestId] = request; |
| 2 | 776 | | OnFriendRequestReceived?.Invoke(request); |
| 0 | 777 | | } |
| | 778 | |
|
| | 779 | | // TODO (Joni): Replace by according friend event from RPC Bridge |
| | 780 | | private void HandleUpdateFriendshipStatus(FriendshipUpdateStatusMessage msg) |
| | 781 | | { |
| 7 | 782 | | UpdateFriendshipStatus(msg); |
| | 783 | |
|
| 7 | 784 | | var friendRequest = GetAllocatedFriendRequestByUser(msg.userId); |
| | 785 | |
|
| 7 | 786 | | if (msg.action == FriendshipAction.APPROVED) |
| 1 | 787 | | OnSentFriendRequestApproved?.Invoke(friendRequest); |
| 6 | 788 | | } |
| | 789 | |
|
| | 790 | | private void UpdateFriendshipStatus(FriendshipUpdateStatusMessage msg) |
| | 791 | | { |
| 46 | 792 | | FriendshipStatus friendshipStatus = ToFriendshipStatus(msg.action); |
| 46 | 793 | | string userId = msg.userId; |
| | 794 | |
|
| 46 | 795 | | if (friends.ContainsKey(userId) && friends[userId].friendshipStatus == friendshipStatus) |
| 1 | 796 | | return; |
| | 797 | |
|
| 45 | 798 | | if (!friends.ContainsKey(userId)) |
| | 799 | | { |
| 42 | 800 | | friends.Add(userId, new UserStatus { userId = userId }); |
| | 801 | |
|
| | 802 | | // Not adding the user to friendsSortedByName because this is only called in the legacy flow of a friend |
| | 803 | | } |
| | 804 | |
|
| 45 | 805 | | friends[userId].friendshipStatus = friendshipStatus; |
| | 806 | |
|
| 45 | 807 | | if (friendshipStatus == FriendshipStatus.NOT_FRIEND) |
| | 808 | | { |
| 9 | 809 | | var friend = this.friends[userId]; |
| 9 | 810 | | friends.Remove(userId); |
| | 811 | |
|
| 9 | 812 | | if (!string.IsNullOrEmpty(friend.userName)) |
| 1 | 813 | | friendsSortedByName.Remove(friend.userName); |
| | 814 | | } |
| | 815 | |
|
| 45 | 816 | | OnUpdateFriendship?.Invoke(userId, msg.action); |
| 21 | 817 | | } |
| | 818 | |
|
| | 819 | | private static FriendshipStatus ToFriendshipStatus(FriendshipAction action) |
| | 820 | | { |
| | 821 | | switch (action) |
| | 822 | | { |
| | 823 | | case FriendshipAction.NONE: |
| | 824 | | break; |
| | 825 | | case FriendshipAction.APPROVED: |
| 8 | 826 | | return FriendshipStatus.FRIEND; |
| | 827 | | case FriendshipAction.REJECTED: |
| 4 | 828 | | return FriendshipStatus.NOT_FRIEND; |
| | 829 | | case FriendshipAction.CANCELLED: |
| 2 | 830 | | return FriendshipStatus.NOT_FRIEND; |
| | 831 | | case FriendshipAction.REQUESTED_FROM: |
| 16 | 832 | | return FriendshipStatus.REQUESTED_FROM; |
| | 833 | | case FriendshipAction.REQUESTED_TO: |
| 16 | 834 | | return FriendshipStatus.REQUESTED_TO; |
| | 835 | | case FriendshipAction.DELETED: |
| 2 | 836 | | return FriendshipStatus.NOT_FRIEND; |
| | 837 | | } |
| | 838 | |
|
| 1 | 839 | | return FriendshipStatus.NOT_FRIEND; |
| | 840 | | } |
| | 841 | |
|
| | 842 | | private void AddFriends(IEnumerable<string> friendIds) |
| | 843 | | { |
| 10 | 844 | | foreach (var friendId in friendIds) |
| | 845 | | { |
| 3 | 846 | | if (!friends.ContainsKey(friendId)) |
| 3 | 847 | | friends.Add(friendId, new UserStatus { userId = friendId }); |
| | 848 | |
|
| 3 | 849 | | friends[friendId].friendshipStatus = ToFriendshipStatus(FriendshipAction.APPROVED); |
| | 850 | | } |
| 2 | 851 | | } |
| | 852 | |
|
| | 853 | | private FriendRequest ToFriendRequest(FriendRequestPayload friendRequest) => |
| 14 | 854 | | new ( |
| | 855 | | friendRequest.friendRequestId, |
| | 856 | | DateTimeOffset.FromUnixTimeMilliseconds(friendRequest.timestamp).DateTime, |
| | 857 | | friendRequest.from, |
| | 858 | | friendRequest.to, |
| | 859 | | friendRequest.messageBody); |
| | 860 | |
|
| | 861 | | // TODO (NEW FRIEND REQUESTS): remove when we don't need to keep the retro-compatibility with the old version |
| | 862 | | private void AddFriendRequests(AddFriendRequestsPayload msg) |
| | 863 | | { |
| 0 | 864 | | TotalReceivedFriendRequestCount = msg.totalReceivedFriendRequests; |
| 0 | 865 | | TotalSentFriendRequestCount = msg.totalSentFriendRequests; |
| 0 | 866 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| | 867 | |
|
| 0 | 868 | | foreach (var userId in msg.requestedFrom) |
| | 869 | | { |
| 0 | 870 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 871 | | { action = FriendshipAction.REQUESTED_FROM, userId = userId }); |
| | 872 | | } |
| | 873 | |
|
| 0 | 874 | | foreach (var userId in msg.requestedTo) |
| | 875 | | { |
| 0 | 876 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 877 | | { action = FriendshipAction.REQUESTED_TO, userId = userId }); |
| | 878 | | } |
| 0 | 879 | | } |
| | 880 | |
|
| | 881 | | private void AddIncomingFriendRequest(FriendRequest friendRequest) => |
| 10 | 882 | | AddIncomingFriendRequest(friendRequest, true); |
| | 883 | |
|
| | 884 | | private void AddIncomingFriendRequest(FriendRequest friendRequest, bool notify) |
| | 885 | | { |
| 10 | 886 | | this.incomingFriendRequestsById[friendRequest.FriendRequestId] = friendRequest; |
| 10 | 887 | | this.incomingFriendRequestsByTimestamp[friendRequest.Timestamp.Ticks] = friendRequest; |
| | 888 | |
|
| 10 | 889 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 890 | | { action = FriendshipAction.REQUESTED_FROM, userId = friendRequest.From }); |
| | 891 | |
|
| 10 | 892 | | if (notify) |
| | 893 | | { |
| 10 | 894 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| 10 | 895 | | OnFriendRequestReceived?.Invoke(friendRequest); |
| | 896 | | } |
| 0 | 897 | | } |
| | 898 | |
|
| | 899 | | private void AddOutgoingFriendRequest(FriendRequest friendRequest) => |
| 9 | 900 | | AddOutgoingFriendRequest(friendRequest, true); |
| | 901 | |
|
| | 902 | | private void AddOutgoingFriendRequest(FriendRequest friendRequest, bool notify) |
| | 903 | | { |
| 9 | 904 | | this.outgoingFriendRequestsById[friendRequest.FriendRequestId] = friendRequest; |
| 9 | 905 | | this.outgoingFriendRequestsByTimestamp[friendRequest.Timestamp.Ticks] = friendRequest; |
| | 906 | |
|
| 9 | 907 | | UpdateFriendshipStatus(new FriendshipUpdateStatusMessage |
| | 908 | | { action = FriendshipAction.REQUESTED_TO, userId = friendRequest.From }); |
| | 909 | |
|
| 9 | 910 | | if (notify) |
| 9 | 911 | | OnTotalFriendRequestUpdated?.Invoke(TotalReceivedFriendRequestCount, TotalSentFriendRequestCount); |
| 0 | 912 | | } |
| | 913 | |
|
| | 914 | | private FriendshipAction ToFriendshipAction(FriendshipStatus status) |
| | 915 | | { |
| | 916 | | switch (status) |
| | 917 | | { |
| | 918 | | case FriendshipStatus.FRIEND: |
| 0 | 919 | | return FriendshipAction.APPROVED; |
| | 920 | | case FriendshipStatus.NOT_FRIEND: |
| | 921 | | default: |
| 0 | 922 | | return FriendshipAction.NONE; |
| | 923 | | case FriendshipStatus.REQUESTED_TO: |
| 0 | 924 | | return FriendshipAction.REQUESTED_TO; |
| | 925 | | case FriendshipStatus.REQUESTED_FROM: |
| 0 | 926 | | return FriendshipAction.REQUESTED_FROM; |
| | 927 | | } |
| | 928 | | } |
| | 929 | |
|
| | 930 | | private async UniTask WaitForFeatureFlagsToBeInitialized(CancellationToken cancellationToken) |
| | 931 | | { |
| 423 | 932 | | if (featureFlagsInitializedTask == null) |
| | 933 | | { |
| 423 | 934 | | featureFlagsInitializedTask = new UniTaskCompletionSource(); |
| | 935 | |
|
| | 936 | | void CompleteTaskAndUnsubscribe(FeatureFlag current, FeatureFlag previous) |
| | 937 | | { |
| 59 | 938 | | dataStore.featureFlags.flags.OnChange -= CompleteTaskAndUnsubscribe; |
| 59 | 939 | | featureFlagsInitializedTask.TrySetResult(); |
| 59 | 940 | | } |
| | 941 | |
|
| 423 | 942 | | dataStore.featureFlags.flags.OnChange += CompleteTaskAndUnsubscribe; |
| | 943 | | } |
| | 944 | |
|
| 1269 | 945 | | await featureFlagsInitializedTask.Task.AttachExternalCancellation(cancellationToken); |
| 19 | 946 | | } |
| | 947 | | } |
| | 948 | | } |