| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Threading; |
| | 4 | | using Cysharp.Threading.Tasks; |
| | 5 | | using DCl.Social.Friends; |
| | 6 | | using UnityEngine; |
| | 7 | | using Random = UnityEngine.Random; |
| | 8 | |
|
| | 9 | | namespace DCL.Social.Friends |
| | 10 | | { |
| | 11 | | public class NewFriendRequestsApiBridgeMock : IFriendsApiBridge |
| | 12 | | { |
| | 13 | | private readonly WebInterfaceFriendsApiBridge apiBridge; |
| | 14 | | private readonly IUserProfileBridge userProfileBridge; |
| 0 | 15 | | private readonly CancellationTokenSource addFriendRequestByUserInputCancellationToken = new CancellationTokenSou |
| 0 | 16 | | private readonly Dictionary<string, FriendRequestPayload> friendRequests = new (); |
| | 17 | |
|
| | 18 | | public event Action<FriendshipInitializationMessage> OnInitialized |
| | 19 | | { |
| 0 | 20 | | add => apiBridge.OnInitialized += value; |
| 0 | 21 | | remove => apiBridge.OnInitialized -= value; |
| | 22 | | } |
| | 23 | |
|
| | 24 | | public event Action<string> OnFriendNotFound |
| | 25 | | { |
| 0 | 26 | | add => apiBridge.OnFriendNotFound += value; |
| 0 | 27 | | remove => apiBridge.OnFriendNotFound -= value; |
| | 28 | | } |
| | 29 | |
|
| | 30 | | public event Action<AddFriendsPayload> OnFriendsAdded |
| | 31 | | { |
| 0 | 32 | | add => apiBridge.OnFriendsAdded += value; |
| 0 | 33 | | remove => apiBridge.OnFriendsAdded -= value; |
| | 34 | | } |
| | 35 | |
|
| | 36 | | public event Action<AddFriendsWithDirectMessagesPayload> OnFriendWithDirectMessagesAdded |
| | 37 | | { |
| 0 | 38 | | add => apiBridge.OnFriendWithDirectMessagesAdded += value; |
| 0 | 39 | | remove => apiBridge.OnFriendWithDirectMessagesAdded -= value; |
| | 40 | | } |
| | 41 | |
|
| | 42 | | public event Action<UserStatus> OnUserPresenceUpdated |
| | 43 | | { |
| 0 | 44 | | add => apiBridge.OnUserPresenceUpdated += value; |
| 0 | 45 | | remove => apiBridge.OnUserPresenceUpdated -= value; |
| | 46 | | } |
| | 47 | |
|
| | 48 | | public event Action<FriendshipUpdateStatusMessage> OnFriendshipStatusUpdated; |
| | 49 | |
|
| | 50 | | public event Action<UpdateTotalFriendRequestsPayload> OnTotalFriendRequestCountUpdated |
| | 51 | | { |
| 0 | 52 | | add => apiBridge.OnTotalFriendRequestCountUpdated += value; |
| 0 | 53 | | remove => apiBridge.OnTotalFriendRequestCountUpdated -= value; |
| | 54 | | } |
| | 55 | |
|
| | 56 | | public event Action<UpdateTotalFriendsPayload> OnTotalFriendCountUpdated |
| | 57 | | { |
| 0 | 58 | | add => apiBridge.OnTotalFriendCountUpdated += value; |
| 0 | 59 | | remove => apiBridge.OnTotalFriendCountUpdated -= value; |
| | 60 | | } |
| | 61 | |
|
| | 62 | | public event Action<FriendRequestPayload> OnFriendRequestAdded; |
| | 63 | |
|
| | 64 | | public event Action<AddFriendRequestsPayload> OnFriendRequestsAdded |
| | 65 | | { |
| 0 | 66 | | add => apiBridge.OnFriendRequestsAdded += value; |
| 0 | 67 | | remove => apiBridge.OnFriendRequestsAdded -= value; |
| | 68 | | } |
| | 69 | |
|
| 0 | 70 | | public NewFriendRequestsApiBridgeMock(WebInterfaceFriendsApiBridge apiBridge, |
| | 71 | | IUserProfileBridge userProfileBridge) |
| | 72 | | { |
| 0 | 73 | | this.apiBridge = apiBridge; |
| 0 | 74 | | this.userProfileBridge = userProfileBridge; |
| | 75 | |
|
| 0 | 76 | | apiBridge.OnFriendshipStatusUpdated += message => OnFriendshipStatusUpdated?.Invoke(message); |
| 0 | 77 | | apiBridge.OnFriendRequestAdded += message => OnFriendRequestAdded?.Invoke(message); |
| | 78 | |
|
| 0 | 79 | | AddFriendRequestByUserInputAsync(addFriendRequestByUserInputCancellationToken.Token).Forget(); |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | public void RejectFriendship(string userId) |
| | 83 | | { |
| 0 | 84 | | apiBridge.RejectFriendship(userId); |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | public void RemoveFriend(string userId) |
| | 88 | | { |
| 0 | 89 | | apiBridge.RemoveFriend(userId); |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | public void GetFriends(int limit, int skip) |
| | 93 | | { |
| 0 | 94 | | apiBridge.GetFriends(limit, skip); |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | public void GetFriends(string usernameOrId, int limit) |
| | 98 | | { |
| 0 | 99 | | apiBridge.GetFriends(usernameOrId, limit); |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | public void GetFriendRequests(int sentLimit, int sentSkip, int receivedLimit, int receivedSkip) => |
| 0 | 103 | | apiBridge.GetFriendRequests(sentLimit, sentSkip, receivedLimit, receivedSkip); |
| | 104 | |
|
| | 105 | | public async UniTask<AddFriendRequestsV2Payload> GetFriendRequestsAsync(int sentLimit, int sentSkip, int receive |
| | 106 | | { |
| 0 | 107 | | await UniTask.Delay(Random.Range(100, 1000)); |
| | 108 | |
|
| | 109 | | // FAKE RECEIVED REQUESTS |
| 0 | 110 | | int amountOfReceivedRequests = Random.Range(1, 5); |
| 0 | 111 | | List<FriendRequestPayload> requestedFromList = new List<FriendRequestPayload>(); |
| | 112 | |
|
| 0 | 113 | | for (var i = 0; i < amountOfReceivedRequests; i++) |
| | 114 | | { |
| 0 | 115 | | var fakeUserId = $"fake_from_user_{i + 1}"; |
| | 116 | |
|
| 0 | 117 | | userProfileBridge.AddUserProfileToCatalog(new UserProfileModel |
| | 118 | | { |
| | 119 | | userId = fakeUserId, |
| | 120 | | name = $"fake from user {i + 1}", |
| | 121 | | snapshots = new UserProfileModel.Snapshots { face256 = $"https://picsum.photos/50?{i}" } |
| | 122 | | }); |
| | 123 | |
|
| 0 | 124 | | var friendRequestId = Guid.NewGuid().ToString("N"); |
| | 125 | |
|
| 0 | 126 | | var friendRequest = new FriendRequestPayload |
| | 127 | | { |
| | 128 | | from = fakeUserId, |
| | 129 | | to = userProfileBridge.GetOwn().userId, |
| | 130 | | friendRequestId = friendRequestId, |
| | 131 | | messageBody = Random.Range(0, 2) == 0 ? $"Test message from {fakeUserId}..." : string.Empty, |
| | 132 | | timestamp = DateTimeOffset.UtcNow.AddDays(-i).ToUnixTimeMilliseconds() |
| | 133 | | }; |
| | 134 | |
|
| 0 | 135 | | friendRequests[friendRequestId] = friendRequest; |
| | 136 | |
|
| 0 | 137 | | requestedFromList.Add(friendRequest); |
| | 138 | | } |
| | 139 | |
|
| | 140 | | // FAKE SENT REQUESTS |
| 0 | 141 | | int amountOfSentRequests = Random.Range(1, 5); |
| 0 | 142 | | List<FriendRequestPayload> requestedToList = new List<FriendRequestPayload>(); |
| | 143 | |
|
| 0 | 144 | | for (var i = 0; i < amountOfSentRequests; i++) |
| | 145 | | { |
| 0 | 146 | | var fakeUserId = $"fake_to_user_{i + 1}"; |
| | 147 | |
|
| 0 | 148 | | userProfileBridge.AddUserProfileToCatalog(new UserProfileModel |
| | 149 | | { |
| | 150 | | userId = fakeUserId, |
| | 151 | | name = $"fake to user {i + 1}", |
| | 152 | | snapshots = new UserProfileModel.Snapshots { face256 = $"https://picsum.photos/50?{i + 100}" } |
| | 153 | | }); |
| | 154 | |
|
| 0 | 155 | | var friendRequestId = Guid.NewGuid().ToString("N"); |
| | 156 | |
|
| 0 | 157 | | var friendRequest = new FriendRequestPayload |
| | 158 | | { |
| | 159 | | from = userProfileBridge.GetOwn().userId, |
| | 160 | | to = fakeUserId, |
| | 161 | | friendRequestId = friendRequestId, |
| | 162 | | messageBody = Random.Range(0, 2) == 0 ? $"Test message to {fakeUserId}..." : string.Empty, |
| | 163 | | timestamp = DateTimeOffset.UtcNow.AddDays(-i).ToUnixTimeMilliseconds() |
| | 164 | | }; |
| | 165 | |
|
| 0 | 166 | | friendRequests[friendRequestId] = friendRequest; |
| | 167 | |
|
| 0 | 168 | | requestedToList.Add(friendRequest); |
| | 169 | | } |
| | 170 | |
|
| 0 | 171 | | var response = new AddFriendRequestsV2Payload |
| | 172 | | { |
| | 173 | | requestedFrom = requestedFromList.ToArray(), |
| | 174 | | requestedTo = requestedToList.ToArray(), |
| | 175 | | totalReceivedFriendRequests = amountOfReceivedRequests, |
| | 176 | | totalSentFriendRequests = amountOfSentRequests |
| | 177 | | }; |
| | 178 | |
|
| 0 | 179 | | return response; |
| 0 | 180 | | } |
| | 181 | |
|
| | 182 | | public void GetFriendsWithDirectMessages(string usernameOrId, int limit, int skip) |
| | 183 | | { |
| 0 | 184 | | apiBridge.GetFriendsWithDirectMessages(usernameOrId, limit, skip); |
| 0 | 185 | | } |
| | 186 | |
|
| | 187 | | public void RequestFriendship(string friendUserId) => |
| 0 | 188 | | apiBridge.RequestFriendship(friendUserId); |
| | 189 | |
|
| | 190 | | public async UniTask<RequestFriendshipConfirmationPayload> RequestFriendshipAsync(string userId, string messageB |
| | 191 | | { |
| 0 | 192 | | await UniTask.Delay(Random.Range(100, 16000)); |
| | 193 | |
|
| | 194 | | // TODO: add user profile to catalog if necessary |
| | 195 | |
|
| | 196 | | // if (Random.Range(0, 2) == 0) |
| | 197 | | // throw new FriendshipException(FriendRequestErrorCodes.Unknown); |
| | 198 | |
|
| 0 | 199 | | var friendRequestId = Guid.NewGuid().ToString("N"); |
| | 200 | |
|
| 0 | 201 | | var friendRequest = new FriendRequestPayload |
| | 202 | | { |
| | 203 | | from = userProfileBridge.GetOwn().userId, |
| | 204 | | friendRequestId = friendRequestId, |
| | 205 | | messageBody = messageBody, |
| | 206 | | timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), |
| | 207 | | to = userId |
| | 208 | | }; |
| | 209 | |
|
| 0 | 210 | | friendRequests[friendRequestId] = friendRequest; |
| | 211 | |
|
| 0 | 212 | | var response = new RequestFriendshipConfirmationPayload |
| | 213 | | { |
| | 214 | | friendRequest = friendRequest, |
| | 215 | | messageId = Guid.NewGuid().ToString("N") |
| | 216 | | }; |
| | 217 | |
|
| 0 | 218 | | OnFriendshipStatusUpdated?.Invoke(new FriendshipUpdateStatusMessage |
| | 219 | | { |
| | 220 | | action = FriendshipAction.REQUESTED_TO, |
| | 221 | | userId = userId |
| | 222 | | }); |
| | 223 | |
|
| 0 | 224 | | return response; |
| 0 | 225 | | } |
| | 226 | |
|
| | 227 | | public async UniTask<CancelFriendshipConfirmationPayload> CancelRequestAsync(string friendRequestId) |
| | 228 | | { |
| 0 | 229 | | await UniTask.Delay(Random.Range(100, 16000)); |
| | 230 | |
|
| | 231 | | // if (Random.Range(0, 2) == 0) |
| | 232 | | // throw new FriendshipException(FriendRequestErrorCodes.Unknown); |
| | 233 | |
|
| 0 | 234 | | var response = new CancelFriendshipConfirmationPayload |
| | 235 | | { |
| | 236 | | messageId = Guid.NewGuid().ToString("N"), |
| | 237 | | friendRequest = friendRequests[friendRequestId], |
| | 238 | | }; |
| | 239 | |
|
| 0 | 240 | | OnFriendshipStatusUpdated?.Invoke(new FriendshipUpdateStatusMessage |
| | 241 | | { |
| | 242 | | action = FriendshipAction.CANCELLED, |
| | 243 | | userId = friendRequests[friendRequestId].to |
| | 244 | | }); |
| | 245 | |
|
| 0 | 246 | | return response; |
| 0 | 247 | | } |
| | 248 | |
|
| | 249 | | public UniTask CancelRequestByUserIdAsync(string userId) => |
| 0 | 250 | | apiBridge.CancelRequestByUserIdAsync(userId); |
| | 251 | |
|
| | 252 | | public void CancelRequestByUserId(string userId) => |
| 0 | 253 | | apiBridge.CancelRequestByUserId(userId); |
| | 254 | |
|
| | 255 | | public void AcceptFriendship(string userId) |
| | 256 | | { |
| 0 | 257 | | apiBridge.AcceptFriendship(userId); |
| 0 | 258 | | } |
| | 259 | |
|
| | 260 | | public void Dispose() |
| | 261 | | { |
| 0 | 262 | | addFriendRequestByUserInputCancellationToken.Cancel(); |
| 0 | 263 | | addFriendRequestByUserInputCancellationToken.Dispose(); |
| 0 | 264 | | } |
| | 265 | |
|
| | 266 | | private async UniTaskVoid AddFriendRequestByUserInputAsync(CancellationToken ct = default) |
| | 267 | | { |
| 0 | 268 | | while (!ct.IsCancellationRequested) |
| | 269 | | { |
| 0 | 270 | | await UniTask.NextFrame(ct); |
| | 271 | |
|
| 0 | 272 | | if (Input.GetKeyDown(KeyCode.R)) |
| | 273 | | { |
| 0 | 274 | | long currentTicks = DateTimeOffset.UtcNow.Ticks; |
| 0 | 275 | | string fakeUserId = $"new_user_{currentTicks.ToString().Substring(currentTicks.ToString().Length - 5 |
| | 276 | |
|
| 0 | 277 | | userProfileBridge.AddUserProfileToCatalog(new UserProfileModel |
| | 278 | | { |
| | 279 | | userId = fakeUserId, |
| | 280 | | name = fakeUserId.Replace("_", " "), |
| | 281 | | snapshots = new UserProfileModel.Snapshots { face256 = $"https://picsum.photos/50?{DateTimeOffse |
| | 282 | | }); |
| | 283 | |
|
| 0 | 284 | | OnFriendRequestAdded?.Invoke(new FriendRequestPayload |
| | 285 | | { |
| | 286 | | friendRequestId = Guid.NewGuid().ToString("N"), |
| | 287 | | from = fakeUserId, |
| | 288 | | to = userProfileBridge.GetOwn().userId, |
| | 289 | | messageBody = Random.Range(0, 2) == 0 ? $"Test message from {fakeUserId}..." : string.Empty, |
| | 290 | | timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() |
| | 291 | | }); |
| | 292 | | } |
| | 293 | | } |
| 0 | 294 | | } |
| | 295 | | } |
| | 296 | | } |