| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Social.Friends; |
| | 5 | | using Decentraland.Renderer.KernelServices; |
| | 6 | | using Decentraland.Renderer.RendererServices; |
| | 7 | | using Decentraland.Renderer.Common; |
| | 8 | | using JetBrains.Annotations; |
| | 9 | | using RPC; |
| | 10 | | using rpc_csharp; |
| | 11 | | using System; |
| | 12 | | using System.Linq; |
| | 13 | | using System.Threading; |
| | 14 | | using FriendshipStatus = DCL.Social.Friends.FriendshipStatus; |
| | 15 | | using KernelGetFriendRequestsPayload = Decentraland.Renderer.KernelServices.GetFriendRequestsPayload; |
| | 16 | | using KernelRejectFriendRequestReply = Decentraland.Renderer.KernelServices.RejectFriendRequestReply; |
| | 17 | | using KernelRejectFriendRequestPayload = Decentraland.Renderer.KernelServices.RejectFriendRequestPayload; |
| | 18 | | using KernelCancelFriendRequestReply = Decentraland.Renderer.KernelServices.CancelFriendRequestReply; |
| | 19 | | using KernelCancelFriendRequestPayload = Decentraland.Renderer.KernelServices.CancelFriendRequestPayload; |
| | 20 | | using RendererRejectFriendRequestReply = Decentraland.Renderer.RendererServices.RejectFriendRequestReply; |
| | 21 | | using RendererRejectFriendRequestPayload = Decentraland.Renderer.RendererServices.RejectFriendRequestPayload; |
| | 22 | | using RendererCancelFriendRequestReply = Decentraland.Renderer.RendererServices.CancelFriendRequestReply; |
| | 23 | | using RendererCancelFriendRequestPayload = Decentraland.Renderer.RendererServices.CancelFriendRequestPayload; |
| | 24 | | using RPCFriendshipStatus = Decentraland.Renderer.KernelServices.FriendshipStatus; |
| | 25 | |
|
| | 26 | | namespace DCl.Social.Friends |
| | 27 | | { |
| | 28 | | public class RPCFriendsApiBridge : IFriendsApiBridge, IFriendRequestRendererService<RPCContext> |
| | 29 | | { |
| | 30 | | private const int REQUEST_TIMEOUT = 30; |
| | 31 | |
|
| | 32 | | private static RPCFriendsApiBridge i; |
| | 33 | |
|
| | 34 | | private readonly IRPC rpc; |
| | 35 | | private readonly IFriendsApiBridge fallbackApiBridge; |
| | 36 | |
|
| | 37 | | public event Action<FriendshipInitializationMessage> OnInitialized |
| | 38 | | { |
| 19 | 39 | | add => fallbackApiBridge.OnInitialized += value; |
| 423 | 40 | | remove => fallbackApiBridge.OnInitialized -= value; |
| | 41 | | } |
| | 42 | |
|
| | 43 | | public event Action<string> OnFriendNotFound |
| | 44 | | { |
| 19 | 45 | | add => fallbackApiBridge.OnFriendNotFound += value; |
| 423 | 46 | | remove => fallbackApiBridge.OnFriendNotFound -= value; |
| | 47 | | } |
| | 48 | |
|
| | 49 | | public event Action<AddFriendRequestsPayload> OnFriendRequestsAdded |
| | 50 | | { |
| 19 | 51 | | add => fallbackApiBridge.OnFriendRequestsAdded += value; |
| 423 | 52 | | remove => fallbackApiBridge.OnFriendRequestsAdded -= value; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | public event Action<AddFriendsWithDirectMessagesPayload> OnFriendWithDirectMessagesAdded |
| | 56 | | { |
| 423 | 57 | | add => fallbackApiBridge.OnFriendWithDirectMessagesAdded += value; |
| 423 | 58 | | remove => fallbackApiBridge.OnFriendWithDirectMessagesAdded -= value; |
| | 59 | | } |
| | 60 | |
|
| | 61 | | public event Action<UserStatus> OnUserPresenceUpdated |
| | 62 | | { |
| 423 | 63 | | add => fallbackApiBridge.OnUserPresenceUpdated += value; |
| 423 | 64 | | remove => fallbackApiBridge.OnUserPresenceUpdated -= value; |
| | 65 | | } |
| | 66 | |
|
| | 67 | | public event Action<FriendshipUpdateStatusMessage> OnFriendshipStatusUpdated; |
| | 68 | |
|
| | 69 | | public event Action<UpdateTotalFriendRequestsPayload> OnTotalFriendRequestCountUpdated |
| | 70 | | { |
| 19 | 71 | | add => fallbackApiBridge.OnTotalFriendRequestCountUpdated += value; |
| 423 | 72 | | remove => fallbackApiBridge.OnTotalFriendRequestCountUpdated -= value; |
| | 73 | | } |
| | 74 | |
|
| | 75 | | public event Action<UpdateTotalFriendsPayload> OnTotalFriendCountUpdated |
| | 76 | | { |
| 19 | 77 | | add => fallbackApiBridge.OnTotalFriendCountUpdated += value; |
| 423 | 78 | | remove => fallbackApiBridge.OnTotalFriendCountUpdated -= value; |
| | 79 | | } |
| | 80 | |
|
| | 81 | | public event Action<FriendRequestPayload> OnFriendRequestReceived; |
| | 82 | |
|
| | 83 | | public static RPCFriendsApiBridge CreateSharedInstance(IRPC rpc, IFriendsApiBridge fallbackApiBridge) |
| | 84 | | { |
| 423 | 85 | | i = new RPCFriendsApiBridge(rpc, fallbackApiBridge); |
| 423 | 86 | | return i; |
| | 87 | | } |
| | 88 | |
|
| | 89 | | public static void RegisterService(RpcServerPort<RPCContext> port) |
| | 90 | | { |
| 0 | 91 | | FriendRequestRendererServiceCodeGen.RegisterService(port, i); |
| 0 | 92 | | } |
| | 93 | |
|
| 423 | 94 | | public RPCFriendsApiBridge(IRPC rpc, IFriendsApiBridge fallbackApiBridge) |
| | 95 | | { |
| 423 | 96 | | this.rpc = rpc; |
| 423 | 97 | | this.fallbackApiBridge = fallbackApiBridge; |
| 423 | 98 | | } |
| | 99 | |
|
| | 100 | | public async UniTask<RejectFriendshipPayload> RejectFriendshipAsync(string friendRequestId, CancellationToken ca |
| | 101 | | { |
| | 102 | | try |
| | 103 | | { |
| 0 | 104 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 105 | |
|
| | 106 | | // TODO: pass cancellation token to rpc client when is supported |
| 0 | 107 | | KernelRejectFriendRequestReply response = await rpc.FriendRequests() |
| | 108 | | .RejectFriendRequest(new KernelRejectFriendRequestPay |
| | 109 | | { |
| | 110 | | FriendRequestId = friendRequestId |
| | 111 | | }).Timeout(TimeSpan.FromSeconds(REQUEST_TIMEOUT)); |
| | 112 | |
|
| 0 | 113 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 114 | |
|
| 0 | 115 | | RejectFriendshipPayload payload = response.MessageCase == KernelRejectFriendRequestReply.MessageOneofCas |
| | 116 | | ? new RejectFriendshipPayload |
| | 117 | | { |
| | 118 | | FriendRequestPayload = ToFriendRequestPayload(response.Reply.FriendRequest), |
| | 119 | | } |
| | 120 | | : throw new FriendshipException(ToErrorCode(response.Error)); |
| | 121 | |
|
| 0 | 122 | | await UniTask.SwitchToMainThread(cancellationToken); |
| | 123 | |
|
| 0 | 124 | | return payload; |
| | 125 | | } |
| | 126 | | finally |
| | 127 | | { |
| 0 | 128 | | await UniTask.SwitchToMainThread(); |
| | 129 | | } |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | public void RemoveFriend(string userId) => |
| 0 | 133 | | fallbackApiBridge.RemoveFriend(userId); |
| | 134 | |
|
| | 135 | | public UniTask<AddFriendsPayload> GetFriendsAsync(int limit, int skip, CancellationToken cancellationToken = def |
| 0 | 136 | | fallbackApiBridge.GetFriendsAsync(limit, skip, cancellationToken); |
| | 137 | |
|
| | 138 | | public UniTask<AddFriendsPayload> GetFriendsAsync(string usernameOrId, int limit, CancellationToken cancellation |
| 0 | 139 | | fallbackApiBridge.GetFriendsAsync(usernameOrId, limit, cancellationToken); |
| | 140 | |
|
| | 141 | | public async UniTask<AddFriendRequestsV2Payload> GetFriendRequestsAsync(int sentLimit, int sentSkip, |
| | 142 | | int receivedLimit, int receivedSkip, |
| | 143 | | CancellationToken cancellationToken) |
| | 144 | | { |
| | 145 | | try |
| | 146 | | { |
| 0 | 147 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 148 | |
|
| | 149 | | // TODO: pass cancellation token to rpc client when is supported |
| 0 | 150 | | GetFriendRequestsReply response = await rpc.FriendRequests() |
| | 151 | | .GetFriendRequests(new KernelGetFriendRequestsPayload |
| | 152 | | { |
| | 153 | | SentLimit = sentLimit, |
| | 154 | | SentSkip = sentSkip, |
| | 155 | | ReceivedLimit = receivedLimit, |
| | 156 | | ReceivedSkip = receivedSkip |
| | 157 | | }) |
| | 158 | | .Timeout(TimeSpan.FromSeconds(REQUEST_TIMEOUT)); |
| | 159 | |
|
| 0 | 160 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 161 | |
|
| 0 | 162 | | AddFriendRequestsV2Payload payload = response.MessageCase == GetFriendRequestsReply.MessageOneofCase.Err |
| | 163 | | ? throw new FriendshipException(ToErrorCode(response.Error)) |
| | 164 | | : new AddFriendRequestsV2Payload |
| | 165 | | { |
| | 166 | | requestedTo = response.Reply.RequestedTo.Select(ToFriendRequestPayload).ToArray(), |
| | 167 | | requestedFrom = response.Reply.RequestedFrom.Select(ToFriendRequestPayload).ToArray(), |
| | 168 | | totalReceivedFriendRequests = response.Reply.TotalReceivedFriendRequests, |
| | 169 | | totalSentFriendRequests = response.Reply.TotalSentFriendRequests, |
| | 170 | | }; |
| | 171 | |
|
| 0 | 172 | | await UniTask.SwitchToMainThread(cancellationToken); |
| | 173 | |
|
| 0 | 174 | | return payload; |
| | 175 | | } |
| | 176 | | finally |
| | 177 | | { |
| 0 | 178 | | await UniTask.SwitchToMainThread(); |
| | 179 | | } |
| 0 | 180 | | } |
| | 181 | |
|
| | 182 | | public void GetFriendsWithDirectMessages(string usernameOrId, int limit, int skip) => |
| 0 | 183 | | fallbackApiBridge.GetFriendsWithDirectMessages(usernameOrId, limit, skip); |
| | 184 | |
|
| | 185 | | public async UniTask<RequestFriendshipConfirmationPayload> RequestFriendshipAsync(string userId, string messageB |
| | 186 | | CancellationToken cancellationToken) |
| | 187 | | { |
| | 188 | | try |
| | 189 | | { |
| 0 | 190 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 191 | |
|
| | 192 | | // TODO: pass cancellation token to rpc client when is supported |
| 0 | 193 | | SendFriendRequestReply reply = await rpc.FriendRequests() |
| | 194 | | .SendFriendRequest(new SendFriendRequestPayload |
| | 195 | | { |
| | 196 | | MessageBody = messageBody, |
| | 197 | | UserId = userId, |
| | 198 | | }) |
| | 199 | | .Timeout(TimeSpan.FromSeconds(REQUEST_TIMEOUT)); |
| | 200 | |
|
| 0 | 201 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 202 | |
|
| 0 | 203 | | RequestFriendshipConfirmationPayload payload = reply.MessageCase == SendFriendRequestReply.MessageOneofC |
| | 204 | | ? new RequestFriendshipConfirmationPayload |
| | 205 | | { |
| | 206 | | friendRequest = ToFriendRequestPayload(reply.Reply.FriendRequest), |
| | 207 | | } |
| | 208 | | : throw new FriendshipException(ToErrorCode(reply.Error)); |
| | 209 | |
|
| 0 | 210 | | await UniTask.SwitchToMainThread(cancellationToken); |
| | 211 | |
|
| 0 | 212 | | return payload; |
| | 213 | | } |
| | 214 | | finally |
| | 215 | | { |
| 0 | 216 | | await UniTask.SwitchToMainThread(); |
| | 217 | | } |
| 0 | 218 | | } |
| | 219 | |
|
| | 220 | | public async UniTask<CancelFriendshipConfirmationPayload> CancelRequestAsync(string friendRequestId, |
| | 221 | | CancellationToken cancellationToken) |
| | 222 | | { |
| | 223 | | try |
| | 224 | | { |
| 0 | 225 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 226 | |
|
| | 227 | | // TODO: pass cancellation token to rpc client when is supported |
| 0 | 228 | | KernelCancelFriendRequestReply reply = await rpc.FriendRequests() |
| | 229 | | .CancelFriendRequest(new KernelCancelFriendRequestPayloa |
| | 230 | | { |
| | 231 | | FriendRequestId = friendRequestId |
| | 232 | | }).Timeout(TimeSpan.FromSeconds(REQUEST_TIMEOUT)); |
| | 233 | |
|
| 0 | 234 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 235 | |
|
| 0 | 236 | | CancelFriendshipConfirmationPayload payload = reply.MessageCase == KernelCancelFriendRequestReply.Messag |
| | 237 | | ? new CancelFriendshipConfirmationPayload |
| | 238 | | { |
| | 239 | | friendRequest = ToFriendRequestPayload(reply.Reply.FriendRequest), |
| | 240 | | } |
| | 241 | | : throw new FriendshipException(ToErrorCode(reply.Error)); |
| | 242 | |
|
| 0 | 243 | | await UniTask.SwitchToMainThread(cancellationToken); |
| | 244 | |
|
| 0 | 245 | | return payload; |
| | 246 | | } |
| | 247 | | finally |
| | 248 | | { |
| 0 | 249 | | await UniTask.SwitchToMainThread(cancellationToken); |
| | 250 | | } |
| 0 | 251 | | } |
| | 252 | |
|
| | 253 | | public UniTask<ApproveFriendRequestReply> ApproveFriendRequest(ApproveFriendRequestPayload request, RPCContext c |
| | 254 | | { |
| 0 | 255 | | OnFriendshipStatusUpdated?.InvokeOnMainThread(new FriendshipUpdateStatusMessage |
| | 256 | | { |
| | 257 | | action = FriendshipAction.APPROVED, |
| | 258 | | userId = request.UserId |
| | 259 | | }); |
| | 260 | |
|
| 0 | 261 | | return UniTask.FromResult(new ApproveFriendRequestReply()); |
| | 262 | | } |
| | 263 | |
|
| | 264 | | public UniTask<RendererRejectFriendRequestReply> RejectFriendRequest(RendererRejectFriendRequestPayload request, |
| | 265 | | { |
| 0 | 266 | | OnFriendshipStatusUpdated?.InvokeOnMainThread(new FriendshipUpdateStatusMessage |
| | 267 | | { |
| | 268 | | action = FriendshipAction.REJECTED, |
| | 269 | | userId = request.UserId |
| | 270 | | }); |
| | 271 | |
|
| 0 | 272 | | return UniTask.FromResult(new RendererRejectFriendRequestReply()); |
| | 273 | | } |
| | 274 | |
|
| | 275 | | public async UniTask<RendererCancelFriendRequestReply> CancelFriendRequest(RendererCancelFriendRequestPayload re |
| | 276 | | { |
| 0 | 277 | | await UniTask.SwitchToMainThread(ct); |
| | 278 | |
|
| 0 | 279 | | OnFriendshipStatusUpdated?.Invoke(new FriendshipUpdateStatusMessage |
| | 280 | | { |
| | 281 | | action = FriendshipAction.CANCELLED, |
| | 282 | | userId = request.UserId |
| | 283 | | }); |
| | 284 | |
|
| 0 | 285 | | return new RendererCancelFriendRequestReply(); |
| 0 | 286 | | } |
| | 287 | |
|
| | 288 | | public async UniTask<ReceiveFriendRequestReply> ReceiveFriendRequest(ReceiveFriendRequestPayload request, RPCCon |
| | 289 | | { |
| 0 | 290 | | await UniTask.SwitchToMainThread(ct); |
| | 291 | |
|
| 0 | 292 | | OnFriendRequestReceived?.Invoke(ToFriendRequestPayload(request.FriendRequest)); |
| | 293 | |
|
| 0 | 294 | | OnFriendshipStatusUpdated?.Invoke(new FriendshipUpdateStatusMessage |
| | 295 | | { |
| | 296 | | action = FriendshipAction.REQUESTED_FROM, |
| | 297 | | userId = request.FriendRequest.From |
| | 298 | | }); |
| | 299 | |
|
| 0 | 300 | | return new ReceiveFriendRequestReply(); |
| 0 | 301 | | } |
| | 302 | |
|
| | 303 | | public async UniTask<AcceptFriendshipPayload> AcceptFriendshipAsync(string friendRequestId, CancellationToken ca |
| | 304 | | { |
| | 305 | | try |
| | 306 | | { |
| 0 | 307 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 308 | |
|
| | 309 | | // TODO: pass cancellation token to rpc client when is supported |
| 0 | 310 | | AcceptFriendRequestReply response = await rpc.FriendRequests() |
| | 311 | | .AcceptFriendRequest(new AcceptFriendRequestPayload |
| | 312 | | { |
| | 313 | | FriendRequestId = friendRequestId |
| | 314 | | }).Timeout(TimeSpan.FromSeconds(REQUEST_TIMEOUT)); |
| | 315 | |
|
| 0 | 316 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 317 | |
|
| 0 | 318 | | AcceptFriendshipPayload payload = response.MessageCase == AcceptFriendRequestReply.MessageOneofCase.Repl |
| | 319 | | ? new AcceptFriendshipPayload |
| | 320 | | { |
| | 321 | | FriendRequest = ToFriendRequestPayload(response.Reply.FriendRequest) |
| | 322 | | } |
| | 323 | | : throw new FriendshipException(ToErrorCode(response.Error)); |
| | 324 | |
|
| 0 | 325 | | await UniTask.SwitchToMainThread(cancellationToken); |
| | 326 | |
|
| 0 | 327 | | return payload; |
| | 328 | | } |
| | 329 | | finally |
| | 330 | | { |
| 0 | 331 | | await UniTask.SwitchToMainThread(); |
| | 332 | | } |
| 0 | 333 | | } |
| | 334 | |
|
| | 335 | | public async UniTask<FriendshipStatus> GetFriendshipStatus(string userId, CancellationToken cancellationToken) |
| | 336 | | { |
| | 337 | | try |
| | 338 | | { |
| 0 | 339 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 340 | |
|
| | 341 | | // TODO: pass cancellation token to rpc client when is supported |
| 0 | 342 | | GetFriendshipStatusResponse response = await rpc.Friends() |
| | 343 | | .GetFriendshipStatus(new GetFriendshipStatusRequest |
| | 344 | | { |
| | 345 | | UserId = userId |
| | 346 | | }).Timeout(TimeSpan.FromSeconds(REQUEST_TIMEOUT)); |
| | 347 | |
|
| 0 | 348 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 349 | |
|
| 0 | 350 | | FriendshipStatus status = ToFriendshipStatus(response.Status); |
| | 351 | |
|
| 0 | 352 | | await UniTask.SwitchToMainThread(cancellationToken); |
| | 353 | |
|
| 0 | 354 | | return status; |
| | 355 | | } |
| | 356 | | finally |
| | 357 | | { |
| 0 | 358 | | await UniTask.SwitchToMainThread(); |
| | 359 | | } |
| 0 | 360 | | } |
| | 361 | |
|
| | 362 | | private FriendshipStatus ToFriendshipStatus(RPCFriendshipStatus status) |
| | 363 | | { |
| | 364 | | switch (status) |
| | 365 | | { |
| | 366 | | case RPCFriendshipStatus.Approved: |
| 0 | 367 | | return FriendshipStatus.FRIEND; |
| | 368 | | case RPCFriendshipStatus.None: |
| | 369 | | default: |
| 0 | 370 | | return FriendshipStatus.NOT_FRIEND; |
| | 371 | | case RPCFriendshipStatus.RequestedFrom: |
| 0 | 372 | | return FriendshipStatus.REQUESTED_FROM; |
| | 373 | | case RPCFriendshipStatus.RequestedTo: |
| 0 | 374 | | return FriendshipStatus.REQUESTED_TO; |
| | 375 | | } |
| | 376 | | } |
| | 377 | |
|
| | 378 | | private static FriendRequestPayload ToFriendRequestPayload(FriendRequestInfo request) => |
| 0 | 379 | | new() |
| | 380 | | { |
| | 381 | | from = request.From, |
| | 382 | | timestamp = (long)request.Timestamp, |
| | 383 | | to = request.To, |
| | 384 | | messageBody = request.MessageBody, |
| | 385 | | friendRequestId = request.FriendRequestId |
| | 386 | | }; |
| | 387 | |
|
| | 388 | | private FriendRequestErrorCodes ToErrorCode(FriendshipErrorCode code) |
| | 389 | | { |
| | 390 | | switch (code) |
| | 391 | | { |
| | 392 | | default: |
| | 393 | | case FriendshipErrorCode.FecUnknown: |
| 0 | 394 | | return FriendRequestErrorCodes.Unknown; |
| | 395 | | case FriendshipErrorCode.FecBlockedUser: |
| 0 | 396 | | return FriendRequestErrorCodes.BlockedUser; |
| | 397 | | case FriendshipErrorCode.FecInvalidRequest: |
| 0 | 398 | | return FriendRequestErrorCodes.InvalidRequest; |
| | 399 | | case FriendshipErrorCode.FecNonExistingUser: |
| 0 | 400 | | return FriendRequestErrorCodes.NonExistingUser; |
| | 401 | | case FriendshipErrorCode.FecNotEnoughTimePassed: |
| 0 | 402 | | return FriendRequestErrorCodes.NotEnoughTimePassed; |
| | 403 | | case FriendshipErrorCode.FecTooManyRequestsSent: |
| 0 | 404 | | return FriendRequestErrorCodes.TooManyRequestsSent; |
| | 405 | | } |
| | 406 | | } |
| | 407 | | } |
| | 408 | | } |