| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Threading; |
| | 5 | | using Cysharp.Threading.Tasks; |
| | 6 | | using DCL.Chat.WebApi; |
| | 7 | | using DCL.Interface; |
| | 8 | | using UnityEngine; |
| | 9 | | using Random = UnityEngine.Random; |
| | 10 | |
|
| | 11 | | namespace DCL.Chat.Channels |
| | 12 | | { |
| | 13 | | public class ChatChannelsControllerMock : IChatController |
| | 14 | | { |
| | 15 | | private const int CHANNEL_LIMIT = 5; |
| | 16 | | private readonly ChatController controller; |
| | 17 | | private readonly UserProfileController userProfileController; |
| 0 | 18 | | private readonly List<string> joinedChannels = new List<string>(); |
| | 19 | | private string currentChannelId; |
| | 20 | |
|
| | 21 | | private static ChatChannelsControllerMock sharedInstance; |
| | 22 | |
|
| | 23 | | public static ChatChannelsControllerMock i => |
| 0 | 24 | | sharedInstance ??= new ChatChannelsControllerMock(ChatController.i, UserProfileController.i); |
| | 25 | |
|
| | 26 | | public event Action<int> OnTotalUnseenMessagesUpdated |
| | 27 | | { |
| 0 | 28 | | add => controller.OnTotalUnseenMessagesUpdated += value; |
| 0 | 29 | | remove => controller.OnTotalUnseenMessagesUpdated -= value; |
| | 30 | | } |
| | 31 | |
|
| | 32 | | public event Action<string, int> OnUserUnseenMessagesUpdated |
| | 33 | | { |
| 0 | 34 | | add => controller.OnUserUnseenMessagesUpdated += value; |
| 0 | 35 | | remove => controller.OnUserUnseenMessagesUpdated -= value; |
| | 36 | | } |
| | 37 | |
|
| | 38 | | public event Action<string, int> OnChannelUnseenMessagesUpdated |
| | 39 | | { |
| 0 | 40 | | add => controller.OnChannelUnseenMessagesUpdated += value; |
| 0 | 41 | | remove => controller.OnChannelUnseenMessagesUpdated -= value; |
| | 42 | | } |
| | 43 | |
|
| | 44 | | public event Action OnInitialized |
| | 45 | | { |
| 0 | 46 | | add => controller.OnInitialized += value; |
| 0 | 47 | | remove => controller.OnInitialized -= value; |
| | 48 | | } |
| | 49 | |
|
| | 50 | | public event Action<ChatMessage> OnAddMessage |
| | 51 | | { |
| 0 | 52 | | add => controller.OnAddMessage += value; |
| 0 | 53 | | remove => controller.OnAddMessage -= value; |
| | 54 | | } |
| | 55 | |
|
| | 56 | | public event Action<Channel> OnChannelUpdated |
| | 57 | | { |
| 0 | 58 | | add => controller.OnChannelUpdated += value; |
| 0 | 59 | | remove => controller.OnChannelUpdated -= value; |
| | 60 | | } |
| | 61 | |
|
| | 62 | | public event Action<Channel> OnChannelJoined |
| | 63 | | { |
| 0 | 64 | | add => controller.OnChannelJoined += value; |
| 0 | 65 | | remove => controller.OnChannelJoined -= value; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | public event Action<string, ChannelErrorCode> OnJoinChannelError |
| | 69 | | { |
| 0 | 70 | | add => controller.OnJoinChannelError += value; |
| 0 | 71 | | remove => controller.OnJoinChannelError -= value; |
| | 72 | | } |
| | 73 | |
|
| | 74 | | public event Action<string> OnChannelLeft |
| | 75 | | { |
| 0 | 76 | | add => controller.OnChannelLeft += value; |
| 0 | 77 | | remove => controller.OnChannelLeft -= value; |
| | 78 | | } |
| | 79 | |
|
| | 80 | | public event Action<string, ChannelErrorCode> OnChannelLeaveError |
| | 81 | | { |
| 0 | 82 | | add => controller.OnChannelLeaveError += value; |
| 0 | 83 | | remove => controller.OnChannelLeaveError -= value; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | public event Action<string, ChannelErrorCode> OnMuteChannelError |
| | 87 | | { |
| 0 | 88 | | add => controller.OnMuteChannelError += value; |
| 0 | 89 | | remove => controller.OnMuteChannelError -= value; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | public event Action<string, ChannelMember[]> OnUpdateChannelMembers |
| | 93 | | { |
| 0 | 94 | | add => controller.OnUpdateChannelMembers += value; |
| 0 | 95 | | remove => controller.OnUpdateChannelMembers -= value; |
| | 96 | | } |
| | 97 | |
|
| | 98 | | public event Action<string, Channel[]> OnChannelSearchResult |
| | 99 | | { |
| 0 | 100 | | add => controller.OnChannelSearchResult += value; |
| 0 | 101 | | remove => controller.OnChannelSearchResult -= value; |
| | 102 | | } |
| | 103 | |
|
| 0 | 104 | | public int TotalUnseenMessages => controller.TotalUnseenMessages; |
| | 105 | |
|
| 0 | 106 | | public ChatChannelsControllerMock( |
| | 107 | | ChatController controller, |
| | 108 | | UserProfileController userProfileController) |
| | 109 | | { |
| 0 | 110 | | this.controller = controller; |
| 0 | 111 | | controller.OnAddMessage += UpdateNearbyUnseenMessages; |
| 0 | 112 | | this.userProfileController = userProfileController; |
| | 113 | |
|
| 0 | 114 | | SimulateDelayedResponseFor_ChatInitialization().ContinueWith(SendNearbyDescriptionMessage).Forget(); |
| 0 | 115 | | AddFakeMessagesFromInput(CancellationToken.None).Forget(); |
| 0 | 116 | | } |
| | 117 | |
|
| 0 | 118 | | public List<ChatMessage> GetAllocatedEntries() => controller.GetAllocatedEntries(); |
| | 119 | |
|
| | 120 | | public List<ChatMessage> GetPrivateAllocatedEntriesByUser(string userId) => |
| 0 | 121 | | controller.GetPrivateAllocatedEntriesByUser(userId); |
| | 122 | |
|
| 0 | 123 | | public void GetUnseenMessagesByChannel() => controller.GetUnseenMessagesByChannel(); |
| | 124 | |
|
| | 125 | | public void Send(ChatMessage message) |
| | 126 | | { |
| 0 | 127 | | message.timestamp = (ulong) DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); |
| | 128 | |
|
| 0 | 129 | | if (IsChannelMessage(message)) |
| | 130 | | { |
| | 131 | | // simulate a response since the real controller does not support channel messages yet |
| 0 | 132 | | controller.AddMessageToChatWindow(JsonUtility.ToJson(message)); |
| 0 | 133 | | } |
| | 134 | | else |
| 0 | 135 | | controller.Send(message); |
| | 136 | |
|
| 0 | 137 | | SimulateDelayedResponseFor_JoinChatMessage(message.body).Forget(); |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | private bool IsChannelMessage(ChatMessage message) |
| | 141 | | { |
| 0 | 142 | | return !string.IsNullOrEmpty(message.recipient) |
| | 143 | | && GetAllocatedChannel(message.recipient) != null; |
| | 144 | | } |
| | 145 | |
|
| | 146 | | private async UniTask SimulateDelayedResponseFor_JoinChatMessage(string chatMessage) |
| | 147 | | { |
| 0 | 148 | | var chatMessageToLower = chatMessage.ToLower(); |
| | 149 | |
|
| 0 | 150 | | if (chatMessageToLower.StartsWith("/join ")) |
| | 151 | | { |
| 0 | 152 | | var channelId = chatMessageToLower.Split(' ')[1].Replace("#", ""); |
| | 153 | |
|
| 0 | 154 | | if (!chatMessageToLower.Contains("error")) |
| 0 | 155 | | JoinOrCreateChannel(channelId); |
| | 156 | | else |
| | 157 | | { |
| 0 | 158 | | await UniTask.Delay(Random.Range(40, 1000)); |
| 0 | 159 | | controller.JoinChannelError(CreateMockedDataFor_JoinChannelErrorPayload(channelId)); |
| | 160 | | } |
| 0 | 161 | | } |
| 0 | 162 | | } |
| | 163 | |
|
| 0 | 164 | | public void MarkMessagesAsSeen(string userId) => controller.MarkMessagesAsSeen(userId); |
| | 165 | |
|
| | 166 | | public void MarkChannelMessagesAsSeen(string channelId) |
| | 167 | | { |
| 0 | 168 | | currentChannelId = channelId; |
| 0 | 169 | | controller.MarkChannelMessagesAsSeen(channelId); |
| | 170 | |
|
| 0 | 171 | | SimulateDelayedResponseFor_MarkChannelAsSeen(channelId).Forget(); |
| 0 | 172 | | } |
| | 173 | |
|
| | 174 | | public void GetPrivateMessages(string userId, int limit, string fromMessageId) => |
| 0 | 175 | | controller.GetPrivateMessages(userId, limit, fromMessageId); |
| | 176 | |
|
| | 177 | | public void JoinOrCreateChannel(string channelId) |
| | 178 | | { |
| 0 | 179 | | currentChannelId = channelId; |
| 0 | 180 | | SimulateDelayedResponseFor_JoinOrCreateChannel(channelId) |
| | 181 | | .ContinueWith(success => |
| | 182 | | { |
| 0 | 183 | | if (success) |
| 0 | 184 | | SendWelcomeMessage(channelId).Forget(); |
| 0 | 185 | | }) |
| | 186 | | .Forget(); |
| 0 | 187 | | } |
| | 188 | |
|
| | 189 | | public void LeaveChannel(string channelId) |
| | 190 | | { |
| 0 | 191 | | if (channelId == currentChannelId) |
| 0 | 192 | | currentChannelId = null; |
| 0 | 193 | | LeaveFakeChannel(channelId).Forget(); |
| 0 | 194 | | } |
| | 195 | |
|
| | 196 | | public void GetChannelMessages(string channelId, int limit, string fromMessageId) |
| | 197 | | { |
| 0 | 198 | | currentChannelId = channelId; |
| 0 | 199 | | } |
| | 200 | |
|
| | 201 | | public void GetJoinedChannels(int limit, int skip) |
| | 202 | | { |
| 0 | 203 | | ChannelInfoPayloads fakeJoinedChannels = new ChannelInfoPayloads { channelInfoPayload = new ChannelInfoPaylo |
| | 204 | |
|
| 0 | 205 | | for (var i = skip; i < skip + limit && i < joinedChannels.Count; i++) |
| | 206 | | { |
| 0 | 207 | | var channelId = joinedChannels[i]; |
| | 208 | |
|
| 0 | 209 | | var msg = new ChannelInfoPayload |
| | 210 | | { |
| | 211 | | joined = true, |
| | 212 | | channelId = channelId, |
| | 213 | | muted = false, |
| | 214 | | memberCount = Random.Range(0, 16), |
| | 215 | | unseenMessages = 0 |
| | 216 | | }; |
| | 217 | |
|
| 0 | 218 | | fakeJoinedChannels.channelInfoPayload[i] = msg; |
| | 219 | | } |
| | 220 | |
|
| 0 | 221 | | controller.UpdateChannelInfo(JsonUtility.ToJson(fakeJoinedChannels)); |
| 0 | 222 | | } |
| | 223 | |
|
| | 224 | | public void GetChannelsByName(int limit, string name, string paginationToken = null) => |
| 0 | 225 | | SearchFakeChannels(limit, name).Forget(); |
| | 226 | |
|
| | 227 | | public void GetChannels(int limit, string paginationToken) => |
| 0 | 228 | | SearchFakeChannels(limit, "").Forget(); |
| | 229 | |
|
| 0 | 230 | | public void MuteChannel(string channelId) => MuteFakeChannel(channelId, true).Forget(); |
| | 231 | |
|
| 0 | 232 | | public void UnmuteChannel(string channelId) => MuteFakeChannel(channelId, false).Forget(); |
| | 233 | |
|
| 0 | 234 | | public Channel GetAllocatedChannel(string channelId) => controller.GetAllocatedChannel(channelId); |
| | 235 | |
|
| 0 | 236 | | public void GetUnseenMessagesByUser() => controller.GetUnseenMessagesByUser(); |
| | 237 | |
|
| 0 | 238 | | public int GetAllocatedUnseenMessages(string userId) => controller.GetAllocatedUnseenMessages(userId); |
| | 239 | |
|
| | 240 | | public int GetAllocatedUnseenChannelMessages(string channelId) => |
| 0 | 241 | | controller.GetAllocatedUnseenChannelMessages(channelId); |
| | 242 | |
|
| | 243 | | public void CreateChannel(string channelId) |
| | 244 | | { |
| 0 | 245 | | currentChannelId = channelId; |
| 0 | 246 | | SimulateDelayedResponseFor_CreateChannel(channelId).Forget(); |
| 0 | 247 | | } |
| | 248 | |
|
| | 249 | | private async UniTask SimulateDelayedResponseFor_CreateChannel(string channelId) |
| | 250 | | { |
| 0 | 251 | | await UniTask.Delay(Random.Range(40, 1000)); |
| | 252 | |
|
| 0 | 253 | | if (joinedChannels.Count >= CHANNEL_LIMIT) |
| | 254 | | { |
| 0 | 255 | | controller.JoinChannelError(CreateMockedDataFor_JoinChannelErrorPayload(channelId)); |
| 0 | 256 | | return; |
| | 257 | | } |
| | 258 | |
|
| 0 | 259 | | if (!joinedChannels.Contains(channelId)) |
| 0 | 260 | | joinedChannels.Add(channelId); |
| | 261 | |
|
| 0 | 262 | | ChannelInfoPayloads fakeCreatedChannels = new ChannelInfoPayloads { channelInfoPayload = new ChannelInfoPayl |
| 0 | 263 | | fakeCreatedChannels.channelInfoPayload[0] = new ChannelInfoPayload |
| | 264 | | { |
| | 265 | | description = "", |
| | 266 | | channelId = channelId, |
| | 267 | | joined = true, |
| | 268 | | memberCount = 1, |
| | 269 | | muted = false, |
| | 270 | | unseenMessages = 0 |
| | 271 | | }; |
| | 272 | |
|
| 0 | 273 | | controller.JoinChannelConfirmation(JsonUtility.ToJson(fakeCreatedChannels)); |
| 0 | 274 | | } |
| | 275 | |
|
| | 276 | | private async UniTask MuteFakeChannel(string channelId, bool muted) |
| | 277 | | { |
| 0 | 278 | | await UniTask.Delay(Random.Range(40, 1000)); |
| | 279 | |
|
| 0 | 280 | | ChannelInfoPayloads fakeMutedChannels = new ChannelInfoPayloads { channelInfoPayload = new ChannelInfoPayloa |
| 0 | 281 | | fakeMutedChannels.channelInfoPayload[0] = new ChannelInfoPayload |
| | 282 | | { |
| | 283 | | joined = true, |
| | 284 | | channelId = channelId, |
| | 285 | | muted = muted, |
| | 286 | | memberCount = Random.Range(0, 16), |
| | 287 | | unseenMessages = Random.Range(0, 16) |
| | 288 | | }; |
| | 289 | |
|
| 0 | 290 | | controller.UpdateChannelInfo(JsonUtility.ToJson(fakeMutedChannels)); |
| 0 | 291 | | } |
| | 292 | |
|
| | 293 | | private async UniTask SimulateDelayedResponseFor_ChatInitialization() |
| | 294 | | { |
| 0 | 295 | | await UniTask.Delay(Random.Range(50, 1000)); |
| | 296 | |
|
| 0 | 297 | | var payload = new InitializeChatPayload |
| | 298 | | { |
| | 299 | | totalUnseenMessages = 0 |
| | 300 | | }; |
| 0 | 301 | | controller.InitializeChat(JsonUtility.ToJson(payload)); |
| 0 | 302 | | } |
| | 303 | |
|
| | 304 | | private async UniTask SimulateDelayedResponseFor_MarkChannelAsSeen(string channelId) |
| | 305 | | { |
| 0 | 306 | | await UniTask.Delay(Random.Range(50, 1000)); |
| | 307 | |
|
| 0 | 308 | | var currentChannelUnseenMessages = GetAllocatedUnseenChannelMessages(channelId); |
| | 309 | |
|
| 0 | 310 | | var userPayload = new UpdateTotalUnseenMessagesByChannelPayload |
| | 311 | | { |
| | 312 | | unseenChannelMessages = new[] |
| | 313 | | { |
| | 314 | | new UpdateTotalUnseenMessagesByChannelPayload.UnseenChannelMessage |
| | 315 | | { |
| | 316 | | channelId = channelId, |
| | 317 | | count = 0 |
| | 318 | | } |
| | 319 | | } |
| | 320 | | }; |
| 0 | 321 | | controller.UpdateTotalUnseenMessagesByChannel(JsonUtility.ToJson(userPayload)); |
| | 322 | |
|
| 0 | 323 | | var totalPayload = new UpdateTotalUnseenMessagesPayload |
| | 324 | | { |
| | 325 | | total = Mathf.Max(0, TotalUnseenMessages - currentChannelUnseenMessages) |
| | 326 | | }; |
| 0 | 327 | | controller.UpdateTotalUnseenMessages(JsonUtility.ToJson(totalPayload)); |
| 0 | 328 | | } |
| | 329 | |
|
| | 330 | | private async UniTask AddFakeMessagesFromInput(CancellationToken cancellationToken) |
| | 331 | | { |
| 0 | 332 | | while (!cancellationToken.IsCancellationRequested) |
| | 333 | | { |
| 0 | 334 | | await UniTask.NextFrame(cancellationToken); |
| | 335 | |
|
| 0 | 336 | | if (Input.GetKeyDown(KeyCode.Z)) |
| | 337 | | { |
| 0 | 338 | | FakeChannelMessage(); |
| | 339 | | } |
| | 340 | |
|
| 0 | 341 | | if (Input.GetKeyDown(KeyCode.G)) |
| | 342 | | { |
| 0 | 343 | | FakeCurrentChannelMessage(); |
| | 344 | | } |
| | 345 | |
|
| 0 | 346 | | if (Input.GetKeyDown(KeyCode.K)) |
| | 347 | | { |
| 0 | 348 | | FakePrivateMessage(); |
| | 349 | | } |
| | 350 | | } |
| 0 | 351 | | } |
| | 352 | |
|
| | 353 | | private void FakeChannelMessage() |
| | 354 | | { |
| 0 | 355 | | var joinedChannels = this.joinedChannels.Concat(new[] {"nearby"}).Distinct().ToList(); |
| 0 | 356 | | var randomChannelId = joinedChannels[Random.Range(0, joinedChannels.Count)]; |
| | 357 | |
|
| 0 | 358 | | var characters = new[] |
| | 359 | | { |
| | 360 | | 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', |
| | 361 | | '9' |
| | 362 | | }; |
| | 363 | |
|
| 0 | 364 | | var userId = "FakeUser-"; |
| 0 | 365 | | for (var x = 0; x < 4; x++) |
| 0 | 366 | | userId += characters[Random.Range(0, characters.Length)]; |
| | 367 | |
|
| 0 | 368 | | var profile = new UserProfileModel |
| | 369 | | { |
| | 370 | | userId = userId, |
| | 371 | | name = userId, |
| | 372 | | snapshots = new UserProfileModel.Snapshots {face256 = $"https://picsum.photos/seed/{userId}/256"} |
| | 373 | | }; |
| | 374 | |
|
| 0 | 375 | | userProfileController.AddUserProfileToCatalog(profile); |
| | 376 | |
|
| 0 | 377 | | var messagePayload = |
| | 378 | | new ChatMessage(ChatMessage.Type.PUBLIC, userId, $"fake message {Random.Range(0, 16000)}") |
| | 379 | | { |
| | 380 | | recipient = randomChannelId == "nearby" ? null : randomChannelId, |
| | 381 | | timestamp = (ulong) DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), |
| | 382 | | isChannelMessage = true |
| | 383 | | }; |
| | 384 | |
|
| 0 | 385 | | controller.AddMessageToChatWindow(JsonUtility.ToJson(messagePayload)); |
| | 386 | |
|
| | 387 | | // skip nearby, the unseen messages are getting updated by triggering the add message event |
| 0 | 388 | | if (randomChannelId == "nearby") return; |
| | 389 | |
|
| 0 | 390 | | var totalUnseenMessagesByChannelPayload = new UpdateTotalUnseenMessagesByChannelPayload |
| | 391 | | { |
| | 392 | | unseenChannelMessages = new[] |
| | 393 | | { |
| | 394 | | new UpdateTotalUnseenMessagesByChannelPayload.UnseenChannelMessage |
| | 395 | | { |
| | 396 | | channelId = randomChannelId, |
| | 397 | | count = GetAllocatedUnseenChannelMessages(randomChannelId) + 1 |
| | 398 | | } |
| | 399 | | } |
| | 400 | | }; |
| 0 | 401 | | controller.UpdateTotalUnseenMessagesByChannel(JsonUtility.ToJson(totalUnseenMessagesByChannelPayload)); |
| | 402 | |
|
| 0 | 403 | | var totalUnseenMessagesPayload = new UpdateTotalUnseenMessagesPayload |
| | 404 | | { |
| | 405 | | total = TotalUnseenMessages + 1 |
| | 406 | | }; |
| 0 | 407 | | controller.UpdateTotalUnseenMessages(JsonUtility.ToJson(totalUnseenMessagesPayload)); |
| 0 | 408 | | } |
| | 409 | |
|
| | 410 | | private void FakeCurrentChannelMessage() |
| | 411 | | { |
| 0 | 412 | | var channelId = string.IsNullOrEmpty(currentChannelId) |
| | 413 | | ? "nearby" |
| | 414 | | : currentChannelId; |
| | 415 | |
|
| 0 | 416 | | var characters = new[] |
| | 417 | | { |
| | 418 | | 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', |
| | 419 | | '9' |
| | 420 | | }; |
| | 421 | |
|
| 0 | 422 | | var userId = "FakeUser-"; |
| 0 | 423 | | for (var x = 0; x < 4; x++) |
| 0 | 424 | | userId += characters[Random.Range(0, characters.Length)]; |
| | 425 | |
|
| 0 | 426 | | var profile = new UserProfileModel |
| | 427 | | { |
| | 428 | | userId = userId, |
| | 429 | | name = userId, |
| | 430 | | snapshots = new UserProfileModel.Snapshots {face256 = $"https://picsum.photos/seed/{userId}/256"} |
| | 431 | | }; |
| | 432 | |
|
| 0 | 433 | | userProfileController.AddUserProfileToCatalog(profile); |
| | 434 | |
|
| 0 | 435 | | var messagePayload = |
| | 436 | | new ChatMessage(ChatMessage.Type.PUBLIC, userId, $"fake message {Random.Range(0, 16000)}") |
| | 437 | | { |
| | 438 | | recipient = channelId == "nearby" ? null : channelId, |
| | 439 | | timestamp = (ulong) DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), |
| | 440 | | isChannelMessage = true |
| | 441 | | }; |
| | 442 | |
|
| 0 | 443 | | controller.AddMessageToChatWindow(JsonUtility.ToJson(messagePayload)); |
| | 444 | |
|
| | 445 | | // skip nearby, the unseen messages are getting updated by triggering the add message event |
| 0 | 446 | | if (channelId == "nearby") return; |
| | 447 | |
|
| 0 | 448 | | var totalUnseenMessagesByChannelPayload = new UpdateTotalUnseenMessagesByChannelPayload |
| | 449 | | { |
| | 450 | | unseenChannelMessages = new[] |
| | 451 | | { |
| | 452 | | new UpdateTotalUnseenMessagesByChannelPayload.UnseenChannelMessage |
| | 453 | | { |
| | 454 | | channelId = channelId, |
| | 455 | | count = GetAllocatedUnseenChannelMessages(channelId) + 1 |
| | 456 | | } |
| | 457 | | } |
| | 458 | | }; |
| 0 | 459 | | controller.UpdateTotalUnseenMessagesByChannel(JsonUtility.ToJson(totalUnseenMessagesByChannelPayload)); |
| | 460 | |
|
| 0 | 461 | | var totalUnseenMessagesPayload = new UpdateTotalUnseenMessagesPayload |
| | 462 | | { |
| | 463 | | total = TotalUnseenMessages + 1 |
| | 464 | | }; |
| 0 | 465 | | controller.UpdateTotalUnseenMessages(JsonUtility.ToJson(totalUnseenMessagesPayload)); |
| 0 | 466 | | } |
| | 467 | |
|
| | 468 | | private void FakePrivateMessage() |
| | 469 | | { |
| 0 | 470 | | var characters = new[] |
| | 471 | | { |
| | 472 | | 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', |
| | 473 | | '9' |
| | 474 | | }; |
| | 475 | |
|
| 0 | 476 | | var userId = "FakeUser-"; |
| 0 | 477 | | for (var x = 0; x < 4; x++) |
| 0 | 478 | | userId += characters[Random.Range(0, characters.Length)]; |
| | 479 | |
|
| 0 | 480 | | var profile = new UserProfileModel |
| | 481 | | { |
| | 482 | | userId = userId, |
| | 483 | | name = userId, |
| | 484 | | snapshots = new UserProfileModel.Snapshots {face256 = $"https://picsum.photos/seed/{userId}/256"} |
| | 485 | | }; |
| | 486 | |
|
| 0 | 487 | | userProfileController.AddUserProfileToCatalog(profile); |
| | 488 | |
|
| 0 | 489 | | var msg = new ChatMessage |
| | 490 | | { |
| | 491 | | body = $"fake message {Random.Range(0, 16000)}", |
| | 492 | | sender = userId, |
| | 493 | | recipient = userProfileController.ownUserProfile.userId, |
| | 494 | | messageType = ChatMessage.Type.PRIVATE, |
| | 495 | | timestamp = (ulong) DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() |
| | 496 | | }; |
| | 497 | |
|
| 0 | 498 | | controller.AddMessageToChatWindow(JsonUtility.ToJson(msg)); |
| | 499 | |
|
| 0 | 500 | | var totalUnseenMessagesByChannelPayload = new UpdateTotalUnseenMessagesByUserPayload |
| | 501 | | { |
| | 502 | | unseenPrivateMessages = new[] |
| | 503 | | { |
| | 504 | | new UpdateTotalUnseenMessagesByUserPayload.UnseenPrivateMessage |
| | 505 | | { |
| | 506 | | userId = userId, |
| | 507 | | count = GetAllocatedUnseenMessages(userId) + 1 |
| | 508 | | } |
| | 509 | | } |
| | 510 | | }; |
| 0 | 511 | | controller.UpdateTotalUnseenMessagesByUser(JsonUtility.ToJson(totalUnseenMessagesByChannelPayload)); |
| | 512 | |
|
| 0 | 513 | | var totalUnseenMessagesPayload = new UpdateTotalUnseenMessagesPayload |
| | 514 | | { |
| | 515 | | total = TotalUnseenMessages + 1 |
| | 516 | | }; |
| 0 | 517 | | controller.UpdateTotalUnseenMessages(JsonUtility.ToJson(totalUnseenMessagesPayload)); |
| 0 | 518 | | } |
| | 519 | |
|
| | 520 | | private async UniTask SendWelcomeMessage(string channelId) |
| | 521 | | { |
| 0 | 522 | | await UniTask.Delay(500); |
| | 523 | |
|
| 0 | 524 | | var messagePayload = |
| | 525 | | new ChatMessage(ChatMessage.Type.SYSTEM, "", @$"This is the start of the channel #{channelId}.\n |
| | 526 | | Invite others to join by quoting the channel name in other chats or include it as a part of your bio.") |
| | 527 | | { |
| | 528 | | recipient = channelId, |
| | 529 | | timestamp = (ulong) DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), |
| | 530 | | isChannelMessage = true |
| | 531 | | }; |
| | 532 | |
|
| 0 | 533 | | controller.AddMessageToChatWindow(JsonUtility.ToJson(messagePayload)); |
| 0 | 534 | | } |
| | 535 | |
|
| | 536 | | private string CreateMockedDataFor_ChannelInfoPayload(string channelId) |
| | 537 | | { |
| 0 | 538 | | ChannelInfoPayloads fakeChannels = new ChannelInfoPayloads { channelInfoPayload = new ChannelInfoPayload[1] |
| 0 | 539 | | fakeChannels.channelInfoPayload[0] = new ChannelInfoPayload |
| | 540 | | { |
| | 541 | | joined = true, |
| | 542 | | channelId = channelId, |
| | 543 | | muted = false, |
| | 544 | | memberCount = Random.Range(0, 16), |
| | 545 | | unseenMessages = Random.Range(0, 16), |
| | 546 | | description = Random.Range(0, 2) == 0 |
| | 547 | | ? "" |
| | 548 | | : "This is a test description for the channel. This will be used to describe the main purpose of the |
| | 549 | | }; |
| | 550 | |
|
| 0 | 551 | | return JsonUtility.ToJson(fakeChannels); |
| | 552 | | } |
| | 553 | |
|
| | 554 | | private string CreateMockedDataFor_JoinChannelErrorPayload(string channelId) |
| | 555 | | { |
| 0 | 556 | | var mockedPayload = new JoinChannelErrorPayload |
| | 557 | | { |
| | 558 | | channelId = channelId, |
| | 559 | | errorCode = (int) ChannelErrorCode.LimitExceeded |
| | 560 | | }; |
| | 561 | |
|
| 0 | 562 | | return JsonUtility.ToJson(mockedPayload); |
| | 563 | | } |
| | 564 | |
|
| | 565 | | private async UniTask<bool> SimulateDelayedResponseFor_JoinOrCreateChannel(string channelId) |
| | 566 | | { |
| 0 | 567 | | await UniTask.Delay(Random.Range(40, 1000)); |
| | 568 | |
|
| 0 | 569 | | if (joinedChannels.Count >= CHANNEL_LIMIT) |
| | 570 | | { |
| 0 | 571 | | controller.JoinChannelError(CreateMockedDataFor_JoinChannelErrorPayload(channelId)); |
| 0 | 572 | | return false; |
| | 573 | | } |
| | 574 | |
|
| 0 | 575 | | if (!joinedChannels.Contains(channelId)) |
| 0 | 576 | | joinedChannels.Add(channelId); |
| | 577 | |
|
| 0 | 578 | | controller.JoinChannelConfirmation(CreateMockedDataFor_ChannelInfoPayload(channelId)); |
| 0 | 579 | | return true; |
| 0 | 580 | | } |
| | 581 | |
|
| | 582 | | private async UniTask LeaveFakeChannel(string channelId) |
| | 583 | | { |
| 0 | 584 | | await UniTask.Delay(Random.Range(40, 1000)); |
| | 585 | |
|
| 0 | 586 | | ChannelInfoPayloads fakeLeftChannels = new ChannelInfoPayloads { channelInfoPayload = new ChannelInfoPayload |
| 0 | 587 | | fakeLeftChannels.channelInfoPayload[0] = new ChannelInfoPayload |
| | 588 | | { |
| | 589 | | joined = false, |
| | 590 | | channelId = channelId, |
| | 591 | | muted = false, |
| | 592 | | memberCount = Random.Range(0, 16), |
| | 593 | | unseenMessages = 0 |
| | 594 | | }; |
| | 595 | |
|
| 0 | 596 | | joinedChannels.Remove(channelId); |
| 0 | 597 | | controller.UpdateChannelInfo(JsonUtility.ToJson(fakeLeftChannels)); |
| 0 | 598 | | } |
| | 599 | |
|
| | 600 | | private async UniTask SearchFakeChannels(int limit, string name) |
| | 601 | | { |
| 0 | 602 | | await UniTask.Delay(Random.Range(40, 1000)); |
| | 603 | |
|
| 0 | 604 | | var ids = new[] |
| | 605 | | { |
| | 606 | | "help", |
| | 607 | | "global", |
| | 608 | | "argentina", |
| | 609 | | "spain", |
| | 610 | | "trade", |
| | 611 | | "ice-poker", |
| | 612 | | "dcl-sdk", |
| | 613 | | "btc", |
| | 614 | | "eth", |
| | 615 | | "nfts", |
| | 616 | | "lands", |
| | 617 | | "art-week", |
| | 618 | | "music-festival" |
| | 619 | | }; |
| | 620 | |
|
| 0 | 621 | | var channelPayloads = new ChannelInfoPayloads(); |
| 0 | 622 | | List<ChannelInfoPayload> channelsInfo = new List<ChannelInfoPayload>(); |
| | 623 | |
|
| 0 | 624 | | for (var i = 0; i < limit && i < ids.Length; i++) |
| | 625 | | { |
| 0 | 626 | | var channelId = ids[i]; |
| 0 | 627 | | if (!channelId.StartsWith(name) && !string.IsNullOrEmpty(name)) continue; |
| | 628 | |
|
| 0 | 629 | | channelsInfo.Add(new ChannelInfoPayload |
| | 630 | | { |
| | 631 | | joined = joinedChannels.Contains(channelId), |
| | 632 | | channelId = channelId, |
| | 633 | | muted = false, |
| | 634 | | memberCount = Random.Range(0, 16), |
| | 635 | | unseenMessages = 0 |
| | 636 | | }); |
| | 637 | | } |
| | 638 | |
|
| 0 | 639 | | channelPayloads.channelInfoPayload = channelsInfo.ToArray(); |
| | 640 | |
|
| 0 | 641 | | var payload = new ChannelSearchResultsPayload |
| | 642 | | { |
| | 643 | | channels = channelPayloads.channelInfoPayload, |
| | 644 | | since = Guid.NewGuid().ToString() |
| | 645 | | }; |
| | 646 | |
|
| 0 | 647 | | controller.UpdateChannelSearchResults(JsonUtility.ToJson(payload)); |
| 0 | 648 | | } |
| | 649 | |
|
| | 650 | | private void SendNearbyDescriptionMessage() |
| | 651 | | { |
| 0 | 652 | | var messagePayload = |
| | 653 | | new ChatMessage(ChatMessage.Type.SYSTEM, "", |
| | 654 | | "Talk to the people around you. If you move far away from someone you will lose contact. All whisper |
| | 655 | | { |
| | 656 | | timestamp = (ulong) DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), |
| | 657 | | isChannelMessage = true |
| | 658 | | }; |
| | 659 | |
|
| 0 | 660 | | controller.AddMessageToChatWindow(JsonUtility.ToJson(messagePayload)); |
| 0 | 661 | | } |
| | 662 | |
|
| | 663 | | private void UpdateNearbyUnseenMessages(ChatMessage message) |
| | 664 | | { |
| | 665 | | const string nearbyChannelId = "nearby"; |
| | 666 | |
|
| 0 | 667 | | if (message.messageType == ChatMessage.Type.PUBLIC |
| | 668 | | && string.IsNullOrEmpty(message.recipient) |
| | 669 | | || message.recipient == nearbyChannelId) |
| | 670 | | { |
| 0 | 671 | | var totalUnseenMessagesByChannelPayload = new UpdateTotalUnseenMessagesByChannelPayload |
| | 672 | | { |
| | 673 | | unseenChannelMessages = new[] |
| | 674 | | { |
| | 675 | | new UpdateTotalUnseenMessagesByChannelPayload.UnseenChannelMessage |
| | 676 | | { |
| | 677 | | channelId = nearbyChannelId, |
| | 678 | | count = GetAllocatedUnseenChannelMessages(nearbyChannelId) + 1 |
| | 679 | | } |
| | 680 | | } |
| | 681 | | }; |
| 0 | 682 | | controller.UpdateTotalUnseenMessagesByChannel(JsonUtility.ToJson(totalUnseenMessagesByChannelPayload)); |
| | 683 | |
|
| 0 | 684 | | var totalUnseenMessagesPayload = new UpdateTotalUnseenMessagesPayload |
| | 685 | | { |
| | 686 | | total = TotalUnseenMessages + 1 |
| | 687 | | }; |
| 0 | 688 | | controller.UpdateTotalUnseenMessages(JsonUtility.ToJson(totalUnseenMessagesPayload)); |
| | 689 | | } |
| 0 | 690 | | } |
| | 691 | |
|
| | 692 | | public void GetChannelInfo(string[] channelIds) => |
| 0 | 693 | | GetFakeChannelInfo(channelIds).Forget(); |
| | 694 | |
|
| | 695 | | private async UniTask GetFakeChannelInfo(string[] channelIds) |
| | 696 | | { |
| 0 | 697 | | await UniTask.Delay(Random.Range(40, 1000)); |
| | 698 | |
|
| 0 | 699 | | ChannelInfoPayloads fakeChannels = new ChannelInfoPayloads { channelInfoPayload = new ChannelInfoPayload[cha |
| | 700 | |
|
| 0 | 701 | | for (int i = 0; i < channelIds.Length; i++) |
| | 702 | | { |
| 0 | 703 | | var msg = new ChannelInfoPayload |
| | 704 | | { |
| | 705 | | joined = true, |
| | 706 | | channelId = channelIds[i], |
| | 707 | | muted = false, |
| | 708 | | memberCount = Random.Range(0, 100), |
| | 709 | | unseenMessages = 0 |
| | 710 | | }; |
| | 711 | |
|
| 0 | 712 | | fakeChannels.channelInfoPayload[i] = msg; |
| | 713 | | } |
| | 714 | |
|
| 0 | 715 | | controller.UpdateChannelInfo(JsonUtility.ToJson(fakeChannels)); |
| 0 | 716 | | } |
| | 717 | |
|
| | 718 | | public void GetChannelMembers(string channelId, int limit, int skip, string name) => |
| 0 | 719 | | GetFakeChannelMembers(channelId, limit, skip, name).Forget(); |
| | 720 | |
|
| | 721 | | public void GetChannelMembers(string channelId, int limit, int skip) => |
| 0 | 722 | | GetFakeChannelMembers(channelId, limit, skip, "").Forget(); |
| | 723 | |
|
| | 724 | | private async UniTask GetFakeChannelMembers(string channelId, int limit, int skip, string name) |
| | 725 | | { |
| 0 | 726 | | List<string> members = new List<string>(); |
| | 727 | |
|
| 0 | 728 | | for (int i = 0; i < 100; i++) |
| | 729 | | { |
| 0 | 730 | | var profile = new UserProfileModel |
| | 731 | | { |
| | 732 | | userId = $"{channelId.Substring(0, 3)}_member{i + 1}", |
| | 733 | | name = $"{channelId.Substring(0, 3)}_member{i + 1}", |
| | 734 | | snapshots = new UserProfileModel.Snapshots { face256 = $"https://picsum.photos/seed/{i}/256" } |
| | 735 | | }; |
| | 736 | |
|
| 0 | 737 | | userProfileController.AddUserProfileToCatalog(profile); |
| 0 | 738 | | members.Add(profile.userId); |
| | 739 | | } |
| | 740 | |
|
| 0 | 741 | | await UniTask.Delay(Random.Range(40, 1000)); |
| | 742 | |
|
| 0 | 743 | | List<ChannelMember> membersToUpdate = new List<ChannelMember>(); |
| 0 | 744 | | for (var i = skip; i < skip + limit && i < members.Count; i++) |
| | 745 | | { |
| 0 | 746 | | var userId = members[i]; |
| 0 | 747 | | if (!userId.Contains(name) && !string.IsNullOrEmpty(name)) continue; |
| | 748 | |
|
| 0 | 749 | | membersToUpdate.Add(new ChannelMember |
| | 750 | | { |
| | 751 | | userId = userId, |
| | 752 | | isOnline = Random.Range(0, 2) == 0 |
| | 753 | | }); |
| | 754 | | } |
| | 755 | |
|
| 0 | 756 | | var msg = new UpdateChannelMembersPayload |
| | 757 | | { |
| | 758 | | channelId = channelId, |
| | 759 | | members = membersToUpdate.ToArray() |
| | 760 | | }; |
| 0 | 761 | | controller.UpdateChannelMembers(JsonUtility.ToJson(msg)); |
| 0 | 762 | | } |
| | 763 | |
|
| 0 | 764 | | public Channel GetAllocatedChannelByName(string channelName) => null; |
| | 765 | | } |
| | 766 | | } |