| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Tasks; |
| | 3 | | using Decentraland.Social.Friendships; |
| | 4 | | using MainScripts.DCL.Controllers.FriendsController; |
| | 5 | | using NUnit.Framework; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Threading; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | namespace DCL.Social.Friends |
| | 12 | | { |
| | 13 | | public class RPCSocialApiBridge : ISocialApiBridge |
| | 14 | | { |
| | 15 | | private const int REQUEST_TIMEOUT = 30; |
| | 16 | | private const int MAX_MINUTES_WAIT_TIME = 3; |
| | 17 | | private const int MAX_RECONNECT_RETRIES = 3; |
| | 18 | |
|
| | 19 | | private readonly IMatrixInitializationBridge matrixInitializationBridge; |
| | 20 | | private readonly IUserProfileBridge userProfileWebInterfaceBridge; |
| | 21 | | private readonly ISocialClientProvider socialClientProvider; |
| | 22 | |
|
| | 23 | | private string accessToken; |
| | 24 | | private IClientFriendshipsService socialClient; |
| | 25 | | private UniTaskCompletionSource<AllFriendsInitializationMessage> initializationInformationTask; |
| 431 | 26 | | private CancellationTokenSource initializationCancellationToken = new (); |
| 431 | 27 | | private CancellationTokenSource incomingEventsSubscriptionCancellationToken = new (); |
| | 28 | |
|
| | 29 | | public event Action<FriendRequest> OnIncomingFriendRequestAdded; |
| | 30 | | public event Action<FriendRequest> OnOutgoingFriendRequestAdded; |
| | 31 | | public event Action<string> OnFriendRequestAccepted; |
| | 32 | | public event Action<string> OnFriendRequestRejected; |
| | 33 | | public event Action<string> OnFriendRequestCanceled; |
| | 34 | | public event Action<string> OnDeletedByFriend; |
| | 35 | |
|
| | 36 | | private int transportFailures = 0; |
| | 37 | |
|
| 431 | 38 | | public RPCSocialApiBridge(IMatrixInitializationBridge matrixInitializationBridge, |
| | 39 | | IUserProfileBridge userProfileWebInterfaceBridge, |
| | 40 | | ISocialClientProvider socialClientProvider) |
| | 41 | | { |
| 431 | 42 | | this.matrixInitializationBridge = matrixInitializationBridge; |
| 431 | 43 | | this.userProfileWebInterfaceBridge = userProfileWebInterfaceBridge; |
| 431 | 44 | | this.socialClientProvider = socialClientProvider; |
| 431 | 45 | | this.socialClientProvider.OnTransportError += OnTransportError; |
| 431 | 46 | | } |
| | 47 | |
|
| | 48 | | public void Dispose() |
| | 49 | | { |
| 0 | 50 | | initializationInformationTask?.TrySetCanceled(); |
| 0 | 51 | | initializationCancellationToken.SafeCancelAndDispose(); |
| 0 | 52 | | incomingEventsSubscriptionCancellationToken.SafeCancelAndDispose(); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | public void Initialize() |
| | 56 | | { |
| 8 | 57 | | accessToken = matrixInitializationBridge.AccessToken; |
| 8 | 58 | | matrixInitializationBridge.OnReceiveMatrixAccessToken += token => accessToken = token; |
| | 59 | |
|
| | 60 | | async UniTaskVoid InitializeAsync(CancellationToken cancellationToken) |
| | 61 | | { |
| 8 | 62 | | await InitializeClient(cancellationToken); |
| 24 | 63 | | await WaitForAccessTokenAsync(cancellationToken); |
| | 64 | |
|
| 8 | 65 | | incomingEventsSubscriptionCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellati |
| | 66 | |
|
| | 67 | | // this is an endless task that's why is forgotten |
| 8 | 68 | | SubscribeToIncomingFriendshipEvents(incomingEventsSubscriptionCancellationToken.Token).Forget(); |
| 8 | 69 | | } |
| | 70 | |
|
| 8 | 71 | | initializationCancellationToken = initializationCancellationToken.SafeRestart(); |
| 8 | 72 | | InitializeAsync(initializationCancellationToken.Token).Forget(); |
| 8 | 73 | | } |
| | 74 | |
|
| | 75 | | private async void OnTransportError() |
| | 76 | | { |
| 0 | 77 | | socialClient = null; |
| | 78 | |
|
| 0 | 79 | | incomingEventsSubscriptionCancellationToken = incomingEventsSubscriptionCancellationToken.SafeRestartLinked( |
| | 80 | |
|
| 0 | 81 | | if (transportFailures >= MAX_RECONNECT_RETRIES) |
| | 82 | | { |
| 0 | 83 | | Debug.LogError("Max reconnect retries reached"); |
| 0 | 84 | | return; |
| | 85 | | } |
| | 86 | |
|
| 0 | 87 | | while (transportFailures < MAX_RECONNECT_RETRIES) |
| | 88 | | { |
| 0 | 89 | | Debug.Log("Reconnecting to Social service"); |
| | 90 | |
|
| 0 | 91 | | transportFailures++; |
| | 92 | |
|
| 0 | 93 | | CancellationTokenSource cancellationTokenSource = new CancellationTokenSource( |
| | 94 | | TimeSpan.FromSeconds(Math.Pow(5, transportFailures) + REQUEST_TIMEOUT) |
| | 95 | | ); |
| | 96 | |
|
| 0 | 97 | | await InitializeClient(cancellationTokenSource.Token); |
| | 98 | |
|
| 0 | 99 | | if (socialClient != null) |
| | 100 | | { |
| | 101 | | // this is an endless task that's why is forgotten |
| 0 | 102 | | SubscribeToIncomingFriendshipEvents(incomingEventsSubscriptionCancellationToken.Token).Forget(); |
| 0 | 103 | | transportFailures = 0; |
| | 104 | |
|
| 0 | 105 | | return; |
| | 106 | | } |
| | 107 | | } |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | private async UniTask InitializeClient(CancellationToken cancellationToken = default) |
| | 111 | | { |
| 16 | 112 | | try { socialClient = await socialClientProvider.Provide(cancellationToken); } |
| 0 | 113 | | catch (Exception e) { Debug.LogException(e); } |
| 8 | 114 | | } |
| | 115 | |
|
| | 116 | | public async UniTask<AllFriendsInitializationMessage> GetInitializationInformationAsync(CancellationToken cancel |
| | 117 | | { |
| 1 | 118 | | if (initializationInformationTask != null) return await initializationInformationTask.Task.AttachExternalCan |
| | 119 | |
|
| 1 | 120 | | initializationInformationTask = new UniTaskCompletionSource<AllFriendsInitializationMessage>(); |
| | 121 | |
|
| 1 | 122 | | if (socialClient == null) { await WaitForSocialClient(cancellationToken); } |
| | 123 | |
|
| 3 | 124 | | await WaitForAccessTokenAsync(cancellationToken); |
| | 125 | |
|
| | 126 | | // TODO: the bridge should not fetch all friends at start, its a responsibility/design issue. |
| | 127 | | // It should be fetched by its request function accordingly |
| 1 | 128 | | List<string> allFriends = await GetAllFriends(cancellationToken); |
| | 129 | |
|
| | 130 | | // TODO: the bridge should not fetch all friend requests at start, its a responsibility/design issue. |
| | 131 | | // It should be fetched by its request function accordingly |
| 1 | 132 | | (List<FriendRequest> incoming, List<FriendRequest> outgoing) = await GetAllFriendRequests(cancellationToken) |
| | 133 | |
|
| 1 | 134 | | await UniTask.SwitchToMainThread(cancellationToken); |
| | 135 | |
|
| 1 | 136 | | initializationInformationTask.TrySetResult(new AllFriendsInitializationMessage( |
| | 137 | | allFriends, incoming, outgoing)); |
| | 138 | |
|
| 1 | 139 | | return await initializationInformationTask.Task.AttachExternalCancellation(cancellationToken); |
| 1 | 140 | | } |
| | 141 | |
|
| | 142 | | private async UniTask<(List<FriendRequest> incoming, List<FriendRequest> outgoing)> GetAllFriendRequests( |
| | 143 | | CancellationToken cancellationToken = default) |
| | 144 | | { |
| 1 | 145 | | List<FriendRequest> incoming = new (); |
| 1 | 146 | | List<FriendRequest> outgoing = new (); |
| | 147 | |
|
| 1 | 148 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 149 | |
|
| 1 | 150 | | if (socialClient == null) { await WaitForSocialClient(cancellationToken); } |
| | 151 | |
|
| 1 | 152 | | var requestEvents = await socialClient.GetRequestEvents(new Payload |
| | 153 | | { SynapseToken = accessToken }); |
| | 154 | |
|
| 1 | 155 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 156 | |
|
| 8 | 157 | | foreach (var friendRequest in requestEvents.Events.Incoming.Items) |
| | 158 | | { |
| 3 | 159 | | incoming.Add(new FriendRequest( |
| | 160 | | GetFriendRequestId(friendRequest.User.Address, friendRequest.CreatedAt), |
| | 161 | |
|
| | 162 | | // timestamps comes in seconds instead of milliseconds, so do the conversion |
| | 163 | | DateTimeOffset.FromUnixTimeMilliseconds(friendRequest.CreatedAt * 1000L).DateTime, |
| | 164 | | friendRequest.User.Address, |
| | 165 | | userProfileWebInterfaceBridge.GetOwn().userId, |
| | 166 | | friendRequest.Message)); |
| | 167 | | } |
| | 168 | |
|
| 10 | 169 | | foreach (var friendRequest in requestEvents.Events.Outgoing.Items) |
| | 170 | | { |
| 4 | 171 | | outgoing.Add(new FriendRequest( |
| | 172 | | GetFriendRequestId(friendRequest.User.Address, friendRequest.CreatedAt), |
| | 173 | |
|
| | 174 | | // timestamps comes in seconds instead of milliseconds, so do the conversion |
| | 175 | | DateTimeOffset.FromUnixTimeMilliseconds(friendRequest.CreatedAt * 1000L).DateTime, |
| | 176 | | userProfileWebInterfaceBridge.GetOwn().userId, |
| | 177 | | friendRequest.User.Address, |
| | 178 | | friendRequest.Message)); |
| | 179 | | } |
| | 180 | |
|
| 1 | 181 | | return (incoming: incoming, outgoing: outgoing); |
| 1 | 182 | | } |
| | 183 | |
|
| | 184 | | private async UniTask<List<string>> GetAllFriends(CancellationToken cancellationToken) |
| | 185 | | { |
| 1 | 186 | | List<string> result = new (); |
| | 187 | |
|
| 1 | 188 | | if (socialClient == null) { await WaitForSocialClient(cancellationToken); } |
| | 189 | |
|
| 1 | 190 | | var friendsStream = socialClient.GetFriends(new Payload |
| | 191 | | { SynapseToken = accessToken }); |
| | 192 | |
|
| 6 | 193 | | await foreach (var friends in friendsStream.WithCancellation(cancellationToken)) |
| | 194 | | { |
| 12 | 195 | | foreach (User friend in friends.Users.Users_) |
| 4 | 196 | | result.Add(friend.Address); |
| | 197 | | } |
| | 198 | |
|
| 1 | 199 | | return result; |
| 1 | 200 | | } |
| | 201 | |
|
| | 202 | | private async UniTask WaitForAccessTokenAsync(CancellationToken cancellationToken) |
| | 203 | | { |
| 27 | 204 | | await UniTask.WaitUntil(() => |
| 9 | 205 | | !string.IsNullOrEmpty(accessToken), |
| | 206 | | PlayerLoopTiming.Update, |
| | 207 | | cancellationToken); |
| 9 | 208 | | } |
| | 209 | |
|
| | 210 | | public async UniTask RejectFriendshipAsync(string friendId, CancellationToken cancellationToken = default) |
| | 211 | | { |
| 1 | 212 | | var updateFriendshipPayload = new UpdateFriendshipPayload |
| | 213 | | { |
| | 214 | | Event = |
| | 215 | | new FriendshipEventPayload |
| | 216 | | { |
| | 217 | | Reject = new RejectPayload |
| | 218 | | { |
| | 219 | | User = new User |
| | 220 | | { Address = friendId }, |
| | 221 | | } |
| | 222 | | }, |
| | 223 | | AuthToken = new Payload |
| | 224 | | { |
| | 225 | | SynapseToken = accessToken, |
| | 226 | | } |
| | 227 | | }; |
| | 228 | |
|
| 1 | 229 | | await this.UpdateFriendship(updateFriendshipPayload, friendId, cancellationToken); |
| 1 | 230 | | } |
| | 231 | |
|
| | 232 | | public async UniTask CancelFriendshipAsync(string friendId, CancellationToken cancellationToken = default) |
| | 233 | | { |
| 1 | 234 | | var updateFriendshipPayload = new UpdateFriendshipPayload |
| | 235 | | { |
| | 236 | | Event = |
| | 237 | | new FriendshipEventPayload |
| | 238 | | { |
| | 239 | | Cancel = new CancelPayload |
| | 240 | | { |
| | 241 | | User = new User |
| | 242 | | { Address = friendId } |
| | 243 | | } |
| | 244 | | }, |
| | 245 | | AuthToken = new Payload |
| | 246 | | { |
| | 247 | | SynapseToken = accessToken, |
| | 248 | | } |
| | 249 | | }; |
| | 250 | |
|
| 1 | 251 | | await this.UpdateFriendship(updateFriendshipPayload, friendId, cancellationToken); |
| 1 | 252 | | } |
| | 253 | |
|
| | 254 | | public async UniTask AcceptFriendshipAsync(string friendId, CancellationToken cancellationToken = default) |
| | 255 | | { |
| 1 | 256 | | var updateFriendshipPayload = new UpdateFriendshipPayload |
| | 257 | | { |
| | 258 | | Event = |
| | 259 | | new FriendshipEventPayload |
| | 260 | | { |
| | 261 | | Accept = new AcceptPayload |
| | 262 | | { |
| | 263 | | User = new User |
| | 264 | | { Address = friendId } |
| | 265 | | } |
| | 266 | | }, |
| | 267 | | AuthToken = new Payload |
| | 268 | | { |
| | 269 | | SynapseToken = accessToken, |
| | 270 | | } |
| | 271 | | }; |
| | 272 | |
|
| 1 | 273 | | await this.UpdateFriendship(updateFriendshipPayload, friendId, cancellationToken); |
| 1 | 274 | | } |
| | 275 | |
|
| | 276 | | public async UniTask DeleteFriendshipAsync(string friendId, CancellationToken cancellationToken = default) |
| | 277 | | { |
| 1 | 278 | | var updateFriendshipPayload = new UpdateFriendshipPayload |
| | 279 | | { |
| | 280 | | Event = |
| | 281 | | new FriendshipEventPayload |
| | 282 | | { |
| | 283 | | Delete = new DeletePayload |
| | 284 | | { |
| | 285 | | User = new User |
| | 286 | | { Address = friendId } |
| | 287 | | } |
| | 288 | | }, |
| | 289 | | AuthToken = new Payload |
| | 290 | | { |
| | 291 | | SynapseToken = accessToken, |
| | 292 | | } |
| | 293 | | }; |
| | 294 | |
|
| 1 | 295 | | await this.UpdateFriendship(updateFriendshipPayload, friendId, cancellationToken); |
| 1 | 296 | | } |
| | 297 | |
|
| | 298 | | public async UniTask<FriendRequest> RequestFriendshipAsync(string friendId, string messageBody, CancellationToke |
| | 299 | | { |
| 1 | 300 | | var updateFriendshipPayload = new UpdateFriendshipPayload |
| | 301 | | { |
| | 302 | | Event = new FriendshipEventPayload |
| | 303 | | { |
| | 304 | | Request = new RequestPayload |
| | 305 | | { |
| | 306 | | Message = messageBody, |
| | 307 | | User = new User |
| | 308 | | { Address = friendId }, |
| | 309 | | } |
| | 310 | | }, |
| | 311 | | AuthToken = new Payload |
| | 312 | | { |
| | 313 | | SynapseToken = accessToken, |
| | 314 | | } |
| | 315 | | }; |
| | 316 | |
|
| 1 | 317 | | FriendshipEventResponse @event = await UpdateFriendship(updateFriendshipPayload, friendId, cancellationToken |
| 1 | 318 | | RequestResponse response = @event.Request; |
| | 319 | |
|
| 1 | 320 | | return new FriendRequest( |
| | 321 | | GetFriendRequestId(response.User.Address, response.CreatedAt), |
| | 322 | |
|
| | 323 | | // timestamps comes in seconds instead of milliseconds, so do the conversion |
| | 324 | | DateTimeOffset.FromUnixTimeMilliseconds(response.CreatedAt * 1000L).DateTime, |
| | 325 | | userProfileWebInterfaceBridge.GetOwn().userId, |
| | 326 | | response.User.Address, |
| | 327 | | response.Message); |
| 1 | 328 | | } |
| | 329 | |
|
| | 330 | | private async UniTask SubscribeToIncomingFriendshipEvents(CancellationToken cancellationToken = default) |
| | 331 | | { |
| | 332 | | try |
| | 333 | | { |
| 8 | 334 | | IUniTaskAsyncEnumerable<SubscribeFriendshipEventsUpdatesResponse> stream = socialClient |
| | 335 | | .SubscribeFriendshipEventsUpdates(new Payload { SynapseToken = accessToken }); |
| | 336 | |
|
| 34 | 337 | | await foreach (var friendshipEventResponse in stream.WithCancellation(cancellationToken)) |
| | 338 | | { |
| 6 | 339 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 340 | |
|
| 6 | 341 | | switch (friendshipEventResponse.ResponseCase) |
| | 342 | | { |
| | 343 | | case SubscribeFriendshipEventsUpdatesResponse.ResponseOneofCase.InternalServerError: |
| | 344 | | case SubscribeFriendshipEventsUpdatesResponse.ResponseOneofCase.UnauthorizedError: |
| | 345 | | case SubscribeFriendshipEventsUpdatesResponse.ResponseOneofCase.ForbiddenError: |
| | 346 | | case SubscribeFriendshipEventsUpdatesResponse.ResponseOneofCase.TooManyRequestsError: |
| 4 | 347 | | LogIncomingFriendshipUpdateEventError(friendshipEventResponse); |
| 4 | 348 | | break; |
| | 349 | | case SubscribeFriendshipEventsUpdatesResponse.ResponseOneofCase.Events: |
| 4 | 350 | | try { await ProcessIncomingFriendshipEvent(friendshipEventResponse.Events, cancellationToken |
| 0 | 351 | | catch (OperationCanceledException) { throw; } |
| | 352 | | catch (Exception e) |
| | 353 | | { |
| | 354 | | // just log the exception so we keep receiving updates in the loop |
| 0 | 355 | | Debug.LogException(e); |
| 0 | 356 | | } |
| | 357 | |
|
| | 358 | | break; |
| | 359 | | default: |
| | 360 | | { |
| 0 | 361 | | Debug.LogErrorFormat("Subscription to friendship events got invalid response {0}", friendshi |
| | 362 | | break; |
| | 363 | | } |
| | 364 | | } |
| | 365 | | } |
| 2 | 366 | | } |
| 0 | 367 | | catch (Exception ex) { Debug.LogException(ex); } |
| 2 | 368 | | } |
| | 369 | |
|
| | 370 | | private async UniTask ProcessIncomingFriendshipEvent(FriendshipEventResponses friendshipEventsUpdatesResponse, |
| | 371 | | CancellationToken cancellationToken = default) |
| | 372 | | { |
| 16 | 373 | | foreach (var friendshipEvent in friendshipEventsUpdatesResponse.Responses) |
| 6 | 374 | | await ProcessIncomingFriendshipEvent(friendshipEvent, cancellationToken); |
| 2 | 375 | | } |
| | 376 | |
|
| | 377 | | private async UniTask ProcessIncomingFriendshipEvent(FriendshipEventResponse friendshipEvent, CancellationToken |
| | 378 | | { |
| 6 | 379 | | await UniTask.SwitchToMainThread(cancellationToken); |
| | 380 | |
|
| 6 | 381 | | switch (friendshipEvent.BodyCase) |
| | 382 | | { |
| | 383 | | case FriendshipEventResponse.BodyOneofCase.Request: |
| 1 | 384 | | var response = friendshipEvent.Request; |
| | 385 | |
|
| 1 | 386 | | var request = new FriendRequest( |
| | 387 | | GetFriendRequestId(response.User.Address, response.CreatedAt), |
| | 388 | |
|
| | 389 | | // timestamps comes in seconds instead of milliseconds, so do the conversion |
| | 390 | | DateTimeOffset.FromUnixTimeMilliseconds(response.CreatedAt * 1000L).DateTime, |
| | 391 | | response.User.Address, |
| | 392 | | userProfileWebInterfaceBridge.GetOwn().userId, |
| | 393 | | response.Message); |
| | 394 | |
|
| 1 | 395 | | OnIncomingFriendRequestAdded?.Invoke(request); |
| 1 | 396 | | break; |
| | 397 | | case FriendshipEventResponse.BodyOneofCase.Accept: |
| 1 | 398 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 399 | |
|
| 1 | 400 | | OnFriendRequestAccepted?.Invoke(friendshipEvent.Accept.User.Address); |
| 1 | 401 | | break; |
| | 402 | | case FriendshipEventResponse.BodyOneofCase.Reject: |
| 2 | 403 | | OnFriendRequestRejected?.Invoke(friendshipEvent.Reject.User.Address); |
| 2 | 404 | | break; |
| | 405 | | case FriendshipEventResponse.BodyOneofCase.Delete: |
| 1 | 406 | | OnDeletedByFriend?.Invoke(friendshipEvent.Delete.User.Address); |
| 1 | 407 | | break; |
| | 408 | | case FriendshipEventResponse.BodyOneofCase.Cancel: |
| 1 | 409 | | OnFriendRequestCanceled?.Invoke(friendshipEvent.Cancel.User.Address); |
| 1 | 410 | | break; |
| | 411 | | default: |
| 0 | 412 | | Debug.LogErrorFormat("Invalid friendship event {0}", friendshipEvent); |
| | 413 | | break; |
| | 414 | | } |
| 6 | 415 | | } |
| | 416 | |
|
| | 417 | | private void LogIncomingFriendshipUpdateEventError(SubscribeFriendshipEventsUpdatesResponse error) |
| | 418 | | { |
| 4 | 419 | | switch (error.ResponseCase) |
| | 420 | | { |
| | 421 | | case SubscribeFriendshipEventsUpdatesResponse.ResponseOneofCase.InternalServerError: |
| 1 | 422 | | Debug.LogErrorFormat("Subscription to friendship events got internal server error {0}", error.Intern |
| 1 | 423 | | break; |
| | 424 | | case SubscribeFriendshipEventsUpdatesResponse.ResponseOneofCase.UnauthorizedError: |
| 1 | 425 | | Debug.LogErrorFormat("Subscription to friendship events got Unauthorized error {0}", error.Unauthori |
| 1 | 426 | | break; |
| | 427 | | case SubscribeFriendshipEventsUpdatesResponse.ResponseOneofCase.ForbiddenError: |
| 1 | 428 | | Debug.LogErrorFormat("Subscription to friendship events got Forbidden error {0}", error.ForbiddenErr |
| 1 | 429 | | break; |
| | 430 | | case SubscribeFriendshipEventsUpdatesResponse.ResponseOneofCase.TooManyRequestsError: |
| 1 | 431 | | Debug.LogErrorFormat("Subscription to friendship events got Too many requests error {0}", error.TooM |
| | 432 | | break; |
| | 433 | | } |
| 1 | 434 | | } |
| | 435 | |
|
| | 436 | | private static string GetFriendRequestId(string userId, long createdAt) => |
| 9 | 437 | | $"{userId}-{createdAt}"; |
| | 438 | |
|
| | 439 | | private async UniTask<FriendshipEventResponse> UpdateFriendship( |
| | 440 | | UpdateFriendshipPayload updateFriendshipPayload, |
| | 441 | | string friendId, |
| | 442 | | CancellationToken cancellationToken = default) |
| | 443 | | { |
| | 444 | | try |
| | 445 | | { |
| 5 | 446 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 447 | |
|
| 5 | 448 | | if (socialClient == null) { await WaitForSocialClient(cancellationToken); } |
| | 449 | |
|
| | 450 | | // TODO: pass cancellation token to rpc client when is supported |
| 5 | 451 | | var response = await socialClient |
| | 452 | | .UpdateFriendshipEvent(updateFriendshipPayload) |
| | 453 | | .Timeout(TimeSpan.FromSeconds(REQUEST_TIMEOUT)); |
| | 454 | |
|
| 5 | 455 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 456 | |
|
| 5 | 457 | | switch (response.ResponseCase) |
| | 458 | | { |
| | 459 | | case UpdateFriendshipResponse.ResponseOneofCase.Event: |
| 5 | 460 | | return response.Event; |
| | 461 | | case UpdateFriendshipResponse.ResponseOneofCase.InternalServerError: |
| | 462 | | case UpdateFriendshipResponse.ResponseOneofCase.UnauthorizedError: |
| | 463 | | case UpdateFriendshipResponse.ResponseOneofCase.ForbiddenError: |
| | 464 | | case UpdateFriendshipResponse.ResponseOneofCase.TooManyRequestsError: |
| | 465 | | case UpdateFriendshipResponse.ResponseOneofCase.BadRequestError: |
| | 466 | | default: |
| 0 | 467 | | throw new Exception(GetFriendshipUpdateErrorMessage(response, friendId)); |
| | 468 | | } |
| | 469 | | } |
| 5 | 470 | | finally { await UniTask.SwitchToMainThread(); } |
| 5 | 471 | | } |
| | 472 | |
|
| | 473 | | private string GetFriendshipUpdateErrorMessage(UpdateFriendshipResponse error, string friendId) |
| | 474 | | { |
| 0 | 475 | | switch (error.ResponseCase) |
| | 476 | | { |
| | 477 | | case UpdateFriendshipResponse.ResponseOneofCase.InternalServerError: |
| 0 | 478 | | return $"Got internal server error while trying to update friendship {friendId} {error.InternalServe |
| | 479 | | case UpdateFriendshipResponse.ResponseOneofCase.UnauthorizedError: |
| 0 | 480 | | return $"Got Unauthorized error while trying to update friendship {friendId} {error.UnauthorizedErro |
| | 481 | | case UpdateFriendshipResponse.ResponseOneofCase.ForbiddenError: |
| 0 | 482 | | return $"Got Forbidden error while trying to update friendship {friendId} {error.ForbiddenError.Mess |
| | 483 | | case UpdateFriendshipResponse.ResponseOneofCase.TooManyRequestsError: |
| 0 | 484 | | return $"Got Too many requests error {friendId} {error.TooManyRequestsError.Message} while trying to |
| | 485 | | case UpdateFriendshipResponse.ResponseOneofCase.BadRequestError: |
| 0 | 486 | | return $"Got Bad request {friendId} {error.BadRequestError.Message} while trying to update friendshi |
| | 487 | | default: |
| 0 | 488 | | return $"Unsupported friendship error {error.ResponseCase}"; |
| | 489 | | } |
| | 490 | | } |
| | 491 | |
|
| | 492 | | private UniTask WaitForSocialClient(CancellationToken cancellationToken) |
| | 493 | | { |
| 0 | 494 | | CancellationTokenSource timeoutCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(can |
| 0 | 495 | | timeoutCancellationTokenSource.CancelAfterSlim(TimeSpan.FromMinutes(MAX_MINUTES_WAIT_TIME)); |
| | 496 | |
|
| 0 | 497 | | return UniTask.WaitUntil(() => socialClient != null, cancellationToken: timeoutCancellationTokenSource.Token |
| | 498 | | } |
| | 499 | | } |
| | 500 | | } |