| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Interface; |
| | 6 | | using DCl.Social.Friends; |
| | 7 | | using JetBrains.Annotations; |
| | 8 | | using System.Threading; |
| | 9 | | using UnityEngine; |
| | 10 | | using System.Threading; |
| | 11 | |
|
| | 12 | | namespace DCL.Social.Friends |
| | 13 | | { |
| | 14 | | public partial class WebInterfaceFriendsApiBridge : MonoBehaviour, IFriendsApiBridge |
| | 15 | | { |
| | 16 | | private const string GET_FRIENDS_REQUEST_MESSAGE_ID = "GetFriendsRequest"; |
| | 17 | |
|
| | 18 | | public static WebInterfaceFriendsApiBridge GetOrCreate() |
| | 19 | | { |
| 423 | 20 | | var bridgeObj = GameObject.Find("Main"); |
| | 21 | |
|
| 423 | 22 | | return bridgeObj == null |
| | 23 | | ? new GameObject("Main").AddComponent<WebInterfaceFriendsApiBridge>() |
| | 24 | | : bridgeObj.GetOrCreateComponent<WebInterfaceFriendsApiBridge>(); |
| | 25 | | } |
| | 26 | |
|
| 308 | 27 | | private readonly Dictionary<string, IUniTaskSource> pendingRequests = new (); |
| 308 | 28 | | private readonly Dictionary<string, UniTaskCompletionSource<FriendshipUpdateStatusMessage>> updatedFriendshipPen |
| | 29 | |
|
| | 30 | | public event Action<FriendshipInitializationMessage> OnInitialized; |
| | 31 | | public event Action<string> OnFriendNotFound; |
| | 32 | | public event Action<AddFriendRequestsPayload> OnFriendRequestsAdded; // TODO (NEW FRIEND REQUESTS): remove when |
| | 33 | | public event Action<AddFriendsWithDirectMessagesPayload> OnFriendWithDirectMessagesAdded; |
| | 34 | | public event Action<UserStatus> OnUserPresenceUpdated; |
| | 35 | | public event Action<FriendshipUpdateStatusMessage> OnFriendshipStatusUpdated; |
| | 36 | | public event Action<UpdateTotalFriendRequestsPayload> OnTotalFriendRequestCountUpdated; |
| | 37 | | public event Action<UpdateTotalFriendsPayload> OnTotalFriendCountUpdated; |
| | 38 | | public event Action<FriendRequestPayload> OnFriendRequestReceived; |
| | 39 | |
|
| | 40 | | [PublicAPI] |
| | 41 | | public void InitializeFriends(string json) => |
| 0 | 42 | | OnInitialized?.Invoke(JsonUtility.FromJson<FriendshipInitializationMessage>(json)); |
| | 43 | |
|
| | 44 | | [PublicAPI] |
| | 45 | | public void FriendNotFound(string name) => |
| 0 | 46 | | OnFriendNotFound?.Invoke(name); |
| | 47 | |
|
| | 48 | | [PublicAPI] |
| | 49 | | public void AddFriends(string json) |
| | 50 | | { |
| 0 | 51 | | var payload = JsonUtility.FromJson<AddFriendsPayload>(json); |
| 0 | 52 | | string messageId = GET_FRIENDS_REQUEST_MESSAGE_ID; |
| | 53 | |
|
| 0 | 54 | | if (!pendingRequests.ContainsKey(messageId)) |
| 0 | 55 | | return; |
| | 56 | |
|
| 0 | 57 | | var task = (UniTaskCompletionSource<AddFriendsPayload>)pendingRequests[messageId]; |
| | 58 | |
|
| 0 | 59 | | pendingRequests.Remove(messageId); |
| 0 | 60 | | task.TrySetResult(payload); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | // TODO (NEW FRIEND REQUESTS): remove when we don't need to keep the retro-compatibility with the old version |
| | 64 | | [PublicAPI] |
| | 65 | | public void AddFriendRequests(string json) => |
| 0 | 66 | | OnFriendRequestsAdded?.Invoke(JsonUtility.FromJson<AddFriendRequestsPayload>(json)); |
| | 67 | |
|
| | 68 | | [PublicAPI] |
| | 69 | | public void AddFriendRequest(string json) |
| | 70 | | { |
| 0 | 71 | | var payload = JsonUtility.FromJson<FriendRequestPayload>(json); |
| 0 | 72 | | OnFriendRequestReceived?.Invoke(payload); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | [PublicAPI] |
| | 76 | | public void AddFriendsWithDirectMessages(string json) => |
| 0 | 77 | | OnFriendWithDirectMessagesAdded?.Invoke(JsonUtility.FromJson<AddFriendsWithDirectMessagesPayload>(json)); |
| | 78 | |
|
| | 79 | | [PublicAPI] |
| | 80 | | public void UpdateUserPresence(string json) => |
| 0 | 81 | | OnUserPresenceUpdated?.Invoke(JsonUtility.FromJson<UserStatus>(json)); |
| | 82 | |
|
| | 83 | | [PublicAPI] |
| | 84 | | public void UpdateFriendshipStatus(string json) |
| | 85 | | { |
| 0 | 86 | | FriendshipUpdateStatusMessage msg = JsonUtility.FromJson<FriendshipUpdateStatusMessage>(json); |
| 0 | 87 | | string userId = msg.userId; |
| | 88 | |
|
| 0 | 89 | | if (updatedFriendshipPendingRequests.ContainsKey(userId)) |
| 0 | 90 | | updatedFriendshipPendingRequests[userId].TrySetResult(msg); |
| | 91 | |
|
| 0 | 92 | | updatedFriendshipPendingRequests.Remove(userId); |
| | 93 | |
|
| 0 | 94 | | OnFriendshipStatusUpdated?.Invoke(msg); |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | [PublicAPI] |
| | 98 | | public void UpdateTotalFriendRequests(string json) => |
| 0 | 99 | | OnTotalFriendRequestCountUpdated?.Invoke(JsonUtility.FromJson<UpdateTotalFriendRequestsPayload>(json)); |
| | 100 | |
|
| | 101 | | [PublicAPI] |
| | 102 | | public void UpdateTotalFriends(string json) => |
| 0 | 103 | | OnTotalFriendCountUpdated?.Invoke(JsonUtility.FromJson<UpdateTotalFriendsPayload>(json)); |
| | 104 | |
|
| | 105 | | [PublicAPI] |
| | 106 | | public void RequestFriendshipConfirmation(string json) |
| | 107 | | { |
| 0 | 108 | | var payload = JsonUtility.FromJson<RequestFriendshipConfirmationPayload>(json); |
| 0 | 109 | | var messageId = payload.messageId; |
| 0 | 110 | | if (!pendingRequests.ContainsKey(messageId)) return; |
| | 111 | |
|
| 0 | 112 | | var task = (UniTaskCompletionSource<RequestFriendshipConfirmationPayload>)pendingRequests[messageId]; |
| | 113 | |
|
| 0 | 114 | | pendingRequests.Remove(messageId); |
| 0 | 115 | | task.TrySetResult(payload); |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | [PublicAPI] |
| | 119 | | public void RequestFriendshipError(string json) |
| | 120 | | { |
| 0 | 121 | | var payload = JsonUtility.FromJson<RequestFriendshipErrorPayload>(json); |
| 0 | 122 | | var messageId = payload.messageId; |
| 0 | 123 | | if (!pendingRequests.ContainsKey(messageId)) return; |
| | 124 | |
|
| 0 | 125 | | var task = (UniTaskCompletionSource<RequestFriendshipConfirmationPayload>)pendingRequests[messageId]; |
| | 126 | |
|
| 0 | 127 | | pendingRequests.Remove(messageId); |
| 0 | 128 | | task.TrySetException(new FriendshipException((FriendRequestErrorCodes)payload.errorCode)); |
| 0 | 129 | | } |
| | 130 | |
|
| | 131 | | [PublicAPI] |
| | 132 | | public void CancelFriendshipConfirmation(string json) |
| | 133 | | { |
| 0 | 134 | | var payload = JsonUtility.FromJson<CancelFriendshipConfirmationPayload>(json); |
| 0 | 135 | | string messageId = payload.messageId; |
| 0 | 136 | | if (!pendingRequests.ContainsKey(messageId)) return; |
| | 137 | |
|
| 0 | 138 | | var task = (UniTaskCompletionSource<CancelFriendshipConfirmationPayload>)pendingRequests[messageId]; |
| | 139 | |
|
| 0 | 140 | | pendingRequests.Remove(messageId); |
| 0 | 141 | | task.TrySetResult(payload); |
| | 142 | |
|
| 0 | 143 | | OnFriendshipStatusUpdated?.Invoke(new FriendshipUpdateStatusMessage |
| | 144 | | { |
| | 145 | | action = FriendshipAction.CANCELLED, |
| | 146 | | userId = payload.friendRequest.to |
| | 147 | | }); |
| 0 | 148 | | } |
| | 149 | |
|
| | 150 | | [PublicAPI] |
| | 151 | | public void CancelFriendshipError(string json) |
| | 152 | | { |
| 0 | 153 | | var payload = JsonUtility.FromJson<CancelFriendshipErrorPayload>(json); |
| 0 | 154 | | var messageId = payload.messageId; |
| 0 | 155 | | if (!pendingRequests.ContainsKey(messageId)) return; |
| | 156 | |
|
| 0 | 157 | | var task = (UniTaskCompletionSource<CancelFriendshipConfirmationPayload>)pendingRequests[messageId]; |
| | 158 | |
|
| 0 | 159 | | pendingRequests.Remove(messageId); |
| 0 | 160 | | task.TrySetException(new FriendshipException((FriendRequestErrorCodes)payload.errorCode)); |
| 0 | 161 | | } |
| | 162 | |
|
| | 163 | | public void RejectFriendship(string userId) |
| | 164 | | { |
| 0 | 165 | | WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage |
| | 166 | | { |
| | 167 | | action = WebInterface.FriendshipAction.REJECTED, |
| | 168 | | userId = userId |
| | 169 | | }); |
| 0 | 170 | | } |
| | 171 | |
|
| | 172 | | public UniTask<RejectFriendshipPayload> RejectFriendshipAsync(string friendRequestId, CancellationToken cancella |
| 0 | 173 | | throw new NotImplementedException("Already implemented in RPCFriendsApiBridge"); |
| | 174 | |
|
| | 175 | | public void RemoveFriend(string userId) |
| | 176 | | { |
| 0 | 177 | | WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage |
| | 178 | | { |
| | 179 | | action = WebInterface.FriendshipAction.DELETED, |
| | 180 | | userId = userId |
| | 181 | | }); |
| 0 | 182 | | } |
| | 183 | |
|
| | 184 | | public UniTask<AddFriendsPayload> GetFriendsAsync(int limit, int skip, CancellationToken cancellationToken = def |
| | 185 | | { |
| 0 | 186 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 187 | |
|
| 0 | 188 | | var task = new UniTaskCompletionSource<AddFriendsPayload>(); |
| | 189 | |
|
| 0 | 190 | | pendingRequests[GET_FRIENDS_REQUEST_MESSAGE_ID] = task; |
| 0 | 191 | | WebInterface.GetFriends(limit, skip); |
| | 192 | |
|
| 0 | 193 | | return task.Task.AttachExternalCancellation(cancellationToken); |
| | 194 | | } |
| | 195 | |
|
| | 196 | | public UniTask<AddFriendsPayload> GetFriendsAsync(string usernameOrId, int limit, CancellationToken cancellation |
| | 197 | | { |
| 0 | 198 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 199 | |
|
| 0 | 200 | | var task = new UniTaskCompletionSource<AddFriendsPayload>(); |
| | 201 | |
|
| 0 | 202 | | pendingRequests[GET_FRIENDS_REQUEST_MESSAGE_ID] = task; |
| 0 | 203 | | WebInterface.GetFriends(usernameOrId, limit); |
| | 204 | |
|
| 0 | 205 | | return task.Task.AttachExternalCancellation(cancellationToken); |
| | 206 | | } |
| | 207 | |
|
| | 208 | | // TODO (NEW FRIEND REQUESTS): remove when we don't need to keep the retro-compatibility with the old version |
| | 209 | | public void GetFriendRequests(int sentLimit, int sentSkip, int receivedLimit, int receivedSkip) |
| | 210 | | { |
| 0 | 211 | | WebInterface.SendMessage("GetFriendRequests", new GetFriendRequestsPayload |
| | 212 | | { |
| | 213 | | receivedSkip = receivedSkip, |
| | 214 | | receivedLimit = receivedLimit, |
| | 215 | | sentSkip = sentSkip, |
| | 216 | | sentLimit = sentLimit |
| | 217 | | }); |
| 0 | 218 | | } |
| | 219 | |
|
| | 220 | | public UniTask<AddFriendRequestsV2Payload> GetFriendRequestsAsync(int sentLimit, int sentSkip, int receivedLimit |
| 0 | 221 | | throw new NotImplementedException("Already implemented in RPCFriendsApiBridge"); |
| | 222 | |
|
| | 223 | | public void GetFriendsWithDirectMessages(string usernameOrId, int limit, int skip) => |
| 0 | 224 | | WebInterface.GetFriendsWithDirectMessages(usernameOrId, limit, skip); |
| | 225 | |
|
| | 226 | | public void RequestFriendship(string friendUserId) |
| | 227 | | { |
| 0 | 228 | | WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage |
| | 229 | | { |
| | 230 | | action = WebInterface.FriendshipAction.REQUESTED_TO, |
| | 231 | | userId = friendUserId, |
| | 232 | | }); |
| 0 | 233 | | } |
| | 234 | |
|
| | 235 | | public UniTask<RequestFriendshipConfirmationPayload> RequestFriendshipAsync(string userId, string messageBody, C |
| | 236 | | { |
| 0 | 237 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 238 | |
|
| 0 | 239 | | var task = new UniTaskCompletionSource<RequestFriendshipConfirmationPayload>(); |
| | 240 | |
|
| | 241 | | // TODO: optimize unique id length for performance reasons |
| 0 | 242 | | var messageId = Guid.NewGuid().ToString("N"); |
| 0 | 243 | | pendingRequests[messageId] = task; |
| | 244 | |
|
| 0 | 245 | | WebInterface.SendMessage("RequestFriendship", new RequestFriendshipPayload |
| | 246 | | { |
| | 247 | | messageId = messageId, |
| | 248 | | messageBody = messageBody, |
| | 249 | | userId = userId |
| | 250 | | }); |
| | 251 | |
|
| 0 | 252 | | return task.Task.AttachExternalCancellation(cancellationToken); |
| | 253 | | } |
| | 254 | |
|
| | 255 | | public UniTask<CancelFriendshipConfirmationPayload> CancelRequestAsync(string friendRequestId, CancellationToken |
| | 256 | | { |
| 0 | 257 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 258 | |
|
| 0 | 259 | | var task = new UniTaskCompletionSource<CancelFriendshipConfirmationPayload>(); |
| | 260 | |
|
| | 261 | | // TODO: optimize unique id length for performance reasons |
| 0 | 262 | | var messageId = Guid.NewGuid().ToString("N"); |
| 0 | 263 | | pendingRequests[messageId] = task; |
| | 264 | |
|
| 0 | 265 | | WebInterface.SendMessage("CancelFriendship", |
| | 266 | | new CancelFriendshipPayload |
| | 267 | | { |
| | 268 | | messageId = messageId, |
| | 269 | | friendRequestId = friendRequestId |
| | 270 | | }); |
| | 271 | |
|
| 0 | 272 | | return task.Task.AttachExternalCancellation(cancellationToken); |
| | 273 | | } |
| | 274 | |
|
| | 275 | | public UniTask CancelRequestByUserIdAsync(string userId, CancellationToken cancellationToken) |
| | 276 | | { |
| 0 | 277 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 278 | |
|
| 0 | 279 | | var task = updatedFriendshipPendingRequests.ContainsKey(userId) |
| | 280 | | ? updatedFriendshipPendingRequests[userId] |
| | 281 | | : new UniTaskCompletionSource<FriendshipUpdateStatusMessage>(); |
| | 282 | |
|
| 0 | 283 | | updatedFriendshipPendingRequests[userId] = task; |
| | 284 | |
|
| 0 | 285 | | WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage |
| | 286 | | { |
| | 287 | | userId = userId, |
| | 288 | | action = WebInterface.FriendshipAction.CANCELLED |
| | 289 | | }); |
| | 290 | |
|
| 0 | 291 | | return task.Task.AttachExternalCancellation(cancellationToken); |
| | 292 | | } |
| | 293 | |
|
| | 294 | | public void CancelRequestByUserId(string userId) |
| | 295 | | { |
| 0 | 296 | | WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage |
| | 297 | | { |
| | 298 | | userId = userId, |
| | 299 | | action = WebInterface.FriendshipAction.CANCELLED |
| | 300 | | }); |
| 0 | 301 | | } |
| | 302 | |
|
| | 303 | | public void AcceptFriendship(string userId) |
| | 304 | | { |
| 0 | 305 | | WebInterface.UpdateFriendshipStatus(new WebInterface.FriendshipUpdateStatusMessage |
| | 306 | | { |
| | 307 | | userId = userId, |
| | 308 | | action = WebInterface.FriendshipAction.APPROVED |
| | 309 | | }); |
| 0 | 310 | | } |
| | 311 | |
|
| | 312 | | public UniTask<AcceptFriendshipPayload> AcceptFriendshipAsync(string friendRequestId, CancellationToken cancella |
| 0 | 313 | | throw new NotImplementedException("Already implemented in RPCFriendsApiBridge"); |
| | 314 | |
|
| | 315 | | public UniTask<FriendshipStatus> GetFriendshipStatus(string userId, CancellationToken cancellationToken) => |
| 0 | 316 | | throw new NotImplementedException("Already implemented in RPCFriendsApiBridge"); |
| | 317 | | } |
| | 318 | | } |