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