< Summary

Class:DCL.Chat.Channels.ChatChannelsControllerMock
Assembly:ChatController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ChatController/Channels/ChatChannelsControllerMock.cs
Covered lines:0
Uncovered lines:241
Coverable lines:241
Total lines:766
Line coverage:0% (0 of 241)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ChatChannelsControllerMock(...)0%2100%
add_OnTotalUnseenMessagesUpdated(...)0%2100%
remove_OnTotalUnseenMessagesUpdated(...)0%2100%
add_OnUserUnseenMessagesUpdated(...)0%2100%
remove_OnUserUnseenMessagesUpdated(...)0%2100%
add_OnChannelUnseenMessagesUpdated(...)0%2100%
remove_OnChannelUnseenMessagesUpdated(...)0%2100%
add_OnInitialized(...)0%2100%
remove_OnInitialized(...)0%2100%
add_OnAddMessage(...)0%2100%
remove_OnAddMessage(...)0%2100%
add_OnChannelUpdated(...)0%2100%
remove_OnChannelUpdated(...)0%2100%
add_OnChannelJoined(...)0%2100%
remove_OnChannelJoined(...)0%2100%
add_OnJoinChannelError(...)0%2100%
remove_OnJoinChannelError(...)0%2100%
add_OnChannelLeft(...)0%2100%
remove_OnChannelLeft(...)0%2100%
add_OnChannelLeaveError(...)0%2100%
remove_OnChannelLeaveError(...)0%2100%
add_OnMuteChannelError(...)0%2100%
remove_OnMuteChannelError(...)0%2100%
add_OnUpdateChannelMembers(...)0%2100%
remove_OnUpdateChannelMembers(...)0%2100%
add_OnChannelSearchResult(...)0%2100%
remove_OnChannelSearchResult(...)0%2100%
GetAllocatedEntries()0%2100%
GetPrivateAllocatedEntriesByUser(...)0%2100%
GetUnseenMessagesByChannel()0%2100%
Send(...)0%6200%
IsChannelMessage(...)0%6200%
SimulateDelayedResponseFor_JoinChatMessage()0%30500%
MarkMessagesAsSeen(...)0%2100%
MarkChannelMessagesAsSeen(...)0%2100%
GetPrivateMessages(...)0%2100%
JoinOrCreateChannel(...)0%2100%
LeaveChannel(...)0%6200%
GetChannelMessages(...)0%2100%
GetJoinedChannels(...)0%12300%
GetChannelsByName(...)0%2100%
GetChannels(...)0%2100%
MuteChannel(...)0%2100%
UnmuteChannel(...)0%2100%
GetAllocatedChannel(...)0%2100%
GetUnseenMessagesByUser()0%2100%
GetAllocatedUnseenMessages(...)0%2100%
GetAllocatedUnseenChannelMessages(...)0%2100%
CreateChannel(...)0%2100%
SimulateDelayedResponseFor_CreateChannel()0%30500%
MuteFakeChannel()0%12300%
SimulateDelayedResponseFor_ChatInitialization()0%12300%
SimulateDelayedResponseFor_MarkChannelAsSeen()0%12300%
AddFakeMessagesFromInput()0%56700%
FakeChannelMessage()0%30500%
FakeCurrentChannelMessage()0%56700%
FakePrivateMessage()0%6200%
SendWelcomeMessage()0%12300%
CreateMockedDataFor_ChannelInfoPayload(...)0%12300%
CreateMockedDataFor_JoinChannelErrorPayload(...)0%2100%
SimulateDelayedResponseFor_JoinOrCreateChannel()0%30500%
LeaveFakeChannel()0%12300%
SearchFakeChannels()0%56700%
SendNearbyDescriptionMessage()0%2100%
UpdateNearbyUnseenMessages(...)0%20400%
GetChannelInfo(...)0%2100%
GetFakeChannelInfo()0%20400%
GetChannelMembers(...)0%2100%
GetChannelMembers(...)0%2100%
GetFakeChannelMembers()0%72800%
GetAllocatedChannelByName(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ChatController/Channels/ChatChannelsControllerMock.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Threading;
 5using Cysharp.Threading.Tasks;
 6using DCL.Chat.WebApi;
 7using DCL.Interface;
 8using UnityEngine;
 9using Random = UnityEngine.Random;
 10
 11namespace 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;
 018        private readonly List<string> joinedChannels = new List<string>();
 19        private string currentChannelId;
 20
 21        private static ChatChannelsControllerMock sharedInstance;
 22
 23        public static ChatChannelsControllerMock i =>
 024            sharedInstance ??= new ChatChannelsControllerMock(ChatController.i, UserProfileController.i);
 25
 26        public event Action<int> OnTotalUnseenMessagesUpdated
 27        {
 028            add => controller.OnTotalUnseenMessagesUpdated += value;
 029            remove => controller.OnTotalUnseenMessagesUpdated -= value;
 30        }
 31
 32        public event Action<string, int> OnUserUnseenMessagesUpdated
 33        {
 034            add => controller.OnUserUnseenMessagesUpdated += value;
 035            remove => controller.OnUserUnseenMessagesUpdated -= value;
 36        }
 37
 38        public event Action<string, int> OnChannelUnseenMessagesUpdated
 39        {
 040            add => controller.OnChannelUnseenMessagesUpdated += value;
 041            remove => controller.OnChannelUnseenMessagesUpdated -= value;
 42        }
 43
 44        public event Action OnInitialized
 45        {
 046            add => controller.OnInitialized += value;
 047            remove => controller.OnInitialized -= value;
 48        }
 49
 50        public event Action<ChatMessage> OnAddMessage
 51        {
 052            add => controller.OnAddMessage += value;
 053            remove => controller.OnAddMessage -= value;
 54        }
 55
 56        public event Action<Channel> OnChannelUpdated
 57        {
 058            add => controller.OnChannelUpdated += value;
 059            remove => controller.OnChannelUpdated -= value;
 60        }
 61
 62        public event Action<Channel> OnChannelJoined
 63        {
 064            add => controller.OnChannelJoined += value;
 065            remove => controller.OnChannelJoined -= value;
 66        }
 67
 68        public event Action<string, ChannelErrorCode> OnJoinChannelError
 69        {
 070            add => controller.OnJoinChannelError += value;
 071            remove => controller.OnJoinChannelError -= value;
 72        }
 73
 74        public event Action<string> OnChannelLeft
 75        {
 076            add => controller.OnChannelLeft += value;
 077            remove => controller.OnChannelLeft -= value;
 78        }
 79
 80        public event Action<string, ChannelErrorCode> OnChannelLeaveError
 81        {
 082            add => controller.OnChannelLeaveError += value;
 083            remove => controller.OnChannelLeaveError -= value;
 84        }
 85
 86        public event Action<string, ChannelErrorCode> OnMuteChannelError
 87        {
 088            add => controller.OnMuteChannelError += value;
 089            remove => controller.OnMuteChannelError -= value;
 90        }
 91
 92        public event Action<string, ChannelMember[]> OnUpdateChannelMembers
 93        {
 094            add => controller.OnUpdateChannelMembers += value;
 095            remove => controller.OnUpdateChannelMembers -= value;
 96        }
 97
 98        public event Action<string, Channel[]> OnChannelSearchResult
 99        {
 0100            add => controller.OnChannelSearchResult += value;
 0101            remove => controller.OnChannelSearchResult -= value;
 102        }
 103
 0104        public int TotalUnseenMessages => controller.TotalUnseenMessages;
 105
 0106        public ChatChannelsControllerMock(
 107            ChatController controller,
 108            UserProfileController userProfileController)
 109        {
 0110            this.controller = controller;
 0111            controller.OnAddMessage += UpdateNearbyUnseenMessages;
 0112            this.userProfileController = userProfileController;
 113
 0114            SimulateDelayedResponseFor_ChatInitialization().ContinueWith(SendNearbyDescriptionMessage).Forget();
 0115            AddFakeMessagesFromInput(CancellationToken.None).Forget();
 0116        }
 117
 0118        public List<ChatMessage> GetAllocatedEntries() => controller.GetAllocatedEntries();
 119
 120        public List<ChatMessage> GetPrivateAllocatedEntriesByUser(string userId) =>
 0121            controller.GetPrivateAllocatedEntriesByUser(userId);
 122
 0123        public void GetUnseenMessagesByChannel() => controller.GetUnseenMessagesByChannel();
 124
 125        public void Send(ChatMessage message)
 126        {
 0127            message.timestamp = (ulong) DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
 128
 0129            if (IsChannelMessage(message))
 130            {
 131                // simulate a response since the real controller does not support channel messages yet
 0132                controller.AddMessageToChatWindow(JsonUtility.ToJson(message));
 0133            }
 134            else
 0135                controller.Send(message);
 136
 0137            SimulateDelayedResponseFor_JoinChatMessage(message.body).Forget();
 0138        }
 139
 140        private bool IsChannelMessage(ChatMessage message)
 141        {
 0142            return !string.IsNullOrEmpty(message.recipient)
 143                   && GetAllocatedChannel(message.recipient) != null;
 144        }
 145
 146        private async UniTask SimulateDelayedResponseFor_JoinChatMessage(string chatMessage)
 147        {
 0148            var chatMessageToLower = chatMessage.ToLower();
 149
 0150            if (chatMessageToLower.StartsWith("/join "))
 151            {
 0152                var channelId = chatMessageToLower.Split(' ')[1].Replace("#", "");
 153
 0154                if (!chatMessageToLower.Contains("error"))
 0155                    JoinOrCreateChannel(channelId);
 156                else
 157                {
 0158                    await UniTask.Delay(Random.Range(40, 1000));
 0159                    controller.JoinChannelError(CreateMockedDataFor_JoinChannelErrorPayload(channelId));
 160                }
 0161            }
 0162        }
 163
 0164        public void MarkMessagesAsSeen(string userId) => controller.MarkMessagesAsSeen(userId);
 165
 166        public void MarkChannelMessagesAsSeen(string channelId)
 167        {
 0168            currentChannelId = channelId;
 0169            controller.MarkChannelMessagesAsSeen(channelId);
 170
 0171            SimulateDelayedResponseFor_MarkChannelAsSeen(channelId).Forget();
 0172        }
 173
 174        public void GetPrivateMessages(string userId, int limit, string fromMessageId) =>
 0175            controller.GetPrivateMessages(userId, limit, fromMessageId);
 176
 177        public void JoinOrCreateChannel(string channelId)
 178        {
 0179            currentChannelId = channelId;
 0180            SimulateDelayedResponseFor_JoinOrCreateChannel(channelId)
 181                .ContinueWith(success =>
 182                {
 0183                    if (success)
 0184                        SendWelcomeMessage(channelId).Forget();
 0185                })
 186                .Forget();
 0187        }
 188
 189        public void LeaveChannel(string channelId)
 190        {
 0191            if (channelId == currentChannelId)
 0192                currentChannelId = null;
 0193            LeaveFakeChannel(channelId).Forget();
 0194        }
 195
 196        public void GetChannelMessages(string channelId, int limit, string fromMessageId)
 197        {
 0198            currentChannelId = channelId;
 0199        }
 200
 201        public void GetJoinedChannels(int limit, int skip)
 202        {
 0203            ChannelInfoPayloads fakeJoinedChannels = new ChannelInfoPayloads { channelInfoPayload = new ChannelInfoPaylo
 204
 0205            for (var i = skip; i < skip + limit && i < joinedChannels.Count; i++)
 206            {
 0207                var channelId = joinedChannels[i];
 208
 0209                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
 0218                fakeJoinedChannels.channelInfoPayload[i] = msg;
 219            }
 220
 0221            controller.UpdateChannelInfo(JsonUtility.ToJson(fakeJoinedChannels));
 0222        }
 223
 224        public void GetChannelsByName(int limit, string name, string paginationToken = null) =>
 0225            SearchFakeChannels(limit, name).Forget();
 226
 227        public void GetChannels(int limit, string paginationToken) =>
 0228            SearchFakeChannels(limit, "").Forget();
 229
 0230        public void MuteChannel(string channelId) => MuteFakeChannel(channelId, true).Forget();
 231
 0232        public void UnmuteChannel(string channelId) => MuteFakeChannel(channelId, false).Forget();
 233
 0234        public Channel GetAllocatedChannel(string channelId) => controller.GetAllocatedChannel(channelId);
 235
 0236        public void GetUnseenMessagesByUser() => controller.GetUnseenMessagesByUser();
 237
 0238        public int GetAllocatedUnseenMessages(string userId) => controller.GetAllocatedUnseenMessages(userId);
 239
 240        public int GetAllocatedUnseenChannelMessages(string channelId) =>
 0241            controller.GetAllocatedUnseenChannelMessages(channelId);
 242
 243        public void CreateChannel(string channelId)
 244        {
 0245            currentChannelId = channelId;
 0246            SimulateDelayedResponseFor_CreateChannel(channelId).Forget();
 0247        }
 248
 249        private async UniTask SimulateDelayedResponseFor_CreateChannel(string channelId)
 250        {
 0251            await UniTask.Delay(Random.Range(40, 1000));
 252
 0253            if (joinedChannels.Count >= CHANNEL_LIMIT)
 254            {
 0255                controller.JoinChannelError(CreateMockedDataFor_JoinChannelErrorPayload(channelId));
 0256                return;
 257            }
 258
 0259            if (!joinedChannels.Contains(channelId))
 0260                joinedChannels.Add(channelId);
 261
 0262            ChannelInfoPayloads fakeCreatedChannels = new ChannelInfoPayloads { channelInfoPayload = new ChannelInfoPayl
 0263            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
 0273            controller.JoinChannelConfirmation(JsonUtility.ToJson(fakeCreatedChannels));
 0274        }
 275
 276        private async UniTask MuteFakeChannel(string channelId, bool muted)
 277        {
 0278            await UniTask.Delay(Random.Range(40, 1000));
 279
 0280            ChannelInfoPayloads fakeMutedChannels = new ChannelInfoPayloads { channelInfoPayload = new ChannelInfoPayloa
 0281            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
 0290            controller.UpdateChannelInfo(JsonUtility.ToJson(fakeMutedChannels));
 0291        }
 292
 293        private async UniTask SimulateDelayedResponseFor_ChatInitialization()
 294        {
 0295            await UniTask.Delay(Random.Range(50, 1000));
 296
 0297            var payload = new InitializeChatPayload
 298            {
 299                totalUnseenMessages = 0
 300            };
 0301            controller.InitializeChat(JsonUtility.ToJson(payload));
 0302        }
 303
 304        private async UniTask SimulateDelayedResponseFor_MarkChannelAsSeen(string channelId)
 305        {
 0306            await UniTask.Delay(Random.Range(50, 1000));
 307
 0308            var currentChannelUnseenMessages = GetAllocatedUnseenChannelMessages(channelId);
 309
 0310            var userPayload = new UpdateTotalUnseenMessagesByChannelPayload
 311            {
 312                unseenChannelMessages = new[]
 313                {
 314                    new UpdateTotalUnseenMessagesByChannelPayload.UnseenChannelMessage
 315                    {
 316                        channelId = channelId,
 317                        count = 0
 318                    }
 319                }
 320            };
 0321            controller.UpdateTotalUnseenMessagesByChannel(JsonUtility.ToJson(userPayload));
 322
 0323            var totalPayload = new UpdateTotalUnseenMessagesPayload
 324            {
 325                total = Mathf.Max(0, TotalUnseenMessages - currentChannelUnseenMessages)
 326            };
 0327            controller.UpdateTotalUnseenMessages(JsonUtility.ToJson(totalPayload));
 0328        }
 329
 330        private async UniTask AddFakeMessagesFromInput(CancellationToken cancellationToken)
 331        {
 0332            while (!cancellationToken.IsCancellationRequested)
 333            {
 0334                await UniTask.NextFrame(cancellationToken);
 335
 0336                if (Input.GetKeyDown(KeyCode.Z))
 337                {
 0338                    FakeChannelMessage();
 339                }
 340
 0341                if (Input.GetKeyDown(KeyCode.G))
 342                {
 0343                    FakeCurrentChannelMessage();
 344                }
 345
 0346                if (Input.GetKeyDown(KeyCode.K))
 347                {
 0348                    FakePrivateMessage();
 349                }
 350            }
 0351        }
 352
 353        private void FakeChannelMessage()
 354        {
 0355            var joinedChannels = this.joinedChannels.Concat(new[] {"nearby"}).Distinct().ToList();
 0356            var randomChannelId = joinedChannels[Random.Range(0, joinedChannels.Count)];
 357
 0358            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
 0364            var userId = "FakeUser-";
 0365            for (var x = 0; x < 4; x++)
 0366                userId += characters[Random.Range(0, characters.Length)];
 367
 0368            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
 0375            userProfileController.AddUserProfileToCatalog(profile);
 376
 0377            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
 0385            controller.AddMessageToChatWindow(JsonUtility.ToJson(messagePayload));
 386
 387            // skip nearby, the unseen messages are getting updated by triggering the add message event
 0388            if (randomChannelId == "nearby") return;
 389
 0390            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            };
 0401            controller.UpdateTotalUnseenMessagesByChannel(JsonUtility.ToJson(totalUnseenMessagesByChannelPayload));
 402
 0403            var totalUnseenMessagesPayload = new UpdateTotalUnseenMessagesPayload
 404            {
 405                total = TotalUnseenMessages + 1
 406            };
 0407            controller.UpdateTotalUnseenMessages(JsonUtility.ToJson(totalUnseenMessagesPayload));
 0408        }
 409
 410        private void FakeCurrentChannelMessage()
 411        {
 0412            var channelId = string.IsNullOrEmpty(currentChannelId)
 413                ? "nearby"
 414                : currentChannelId;
 415
 0416            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
 0422            var userId = "FakeUser-";
 0423            for (var x = 0; x < 4; x++)
 0424                userId += characters[Random.Range(0, characters.Length)];
 425
 0426            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
 0433            userProfileController.AddUserProfileToCatalog(profile);
 434
 0435            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
 0443            controller.AddMessageToChatWindow(JsonUtility.ToJson(messagePayload));
 444
 445            // skip nearby, the unseen messages are getting updated by triggering the add message event
 0446            if (channelId == "nearby") return;
 447
 0448            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            };
 0459            controller.UpdateTotalUnseenMessagesByChannel(JsonUtility.ToJson(totalUnseenMessagesByChannelPayload));
 460
 0461            var totalUnseenMessagesPayload = new UpdateTotalUnseenMessagesPayload
 462            {
 463                total = TotalUnseenMessages + 1
 464            };
 0465            controller.UpdateTotalUnseenMessages(JsonUtility.ToJson(totalUnseenMessagesPayload));
 0466        }
 467
 468        private void FakePrivateMessage()
 469        {
 0470            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
 0476            var userId = "FakeUser-";
 0477            for (var x = 0; x < 4; x++)
 0478                userId += characters[Random.Range(0, characters.Length)];
 479
 0480            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
 0487            userProfileController.AddUserProfileToCatalog(profile);
 488
 0489            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
 0498            controller.AddMessageToChatWindow(JsonUtility.ToJson(msg));
 499
 0500            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            };
 0511            controller.UpdateTotalUnseenMessagesByUser(JsonUtility.ToJson(totalUnseenMessagesByChannelPayload));
 512
 0513            var totalUnseenMessagesPayload = new UpdateTotalUnseenMessagesPayload
 514            {
 515                total = TotalUnseenMessages + 1
 516            };
 0517            controller.UpdateTotalUnseenMessages(JsonUtility.ToJson(totalUnseenMessagesPayload));
 0518        }
 519
 520        private async UniTask SendWelcomeMessage(string channelId)
 521        {
 0522            await UniTask.Delay(500);
 523
 0524            var messagePayload =
 525                new ChatMessage(ChatMessage.Type.SYSTEM, "", @$"This is the start of the channel #{channelId}.\n
 526Invite 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
 0533            controller.AddMessageToChatWindow(JsonUtility.ToJson(messagePayload));
 0534        }
 535
 536        private string CreateMockedDataFor_ChannelInfoPayload(string channelId)
 537        {
 0538            ChannelInfoPayloads fakeChannels = new ChannelInfoPayloads { channelInfoPayload = new ChannelInfoPayload[1] 
 0539            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
 0551            return JsonUtility.ToJson(fakeChannels);
 552        }
 553
 554        private string CreateMockedDataFor_JoinChannelErrorPayload(string channelId)
 555        {
 0556            var mockedPayload = new JoinChannelErrorPayload
 557            {
 558                channelId = channelId,
 559                errorCode = (int) ChannelErrorCode.LimitExceeded
 560            };
 561
 0562            return JsonUtility.ToJson(mockedPayload);
 563        }
 564
 565        private async UniTask<bool> SimulateDelayedResponseFor_JoinOrCreateChannel(string channelId)
 566        {
 0567            await UniTask.Delay(Random.Range(40, 1000));
 568
 0569            if (joinedChannels.Count >= CHANNEL_LIMIT)
 570            {
 0571                controller.JoinChannelError(CreateMockedDataFor_JoinChannelErrorPayload(channelId));
 0572                return false;
 573            }
 574
 0575            if (!joinedChannels.Contains(channelId))
 0576                joinedChannels.Add(channelId);
 577
 0578            controller.JoinChannelConfirmation(CreateMockedDataFor_ChannelInfoPayload(channelId));
 0579            return true;
 0580        }
 581
 582        private async UniTask LeaveFakeChannel(string channelId)
 583        {
 0584            await UniTask.Delay(Random.Range(40, 1000));
 585
 0586            ChannelInfoPayloads fakeLeftChannels = new ChannelInfoPayloads { channelInfoPayload = new ChannelInfoPayload
 0587            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
 0596            joinedChannels.Remove(channelId);
 0597            controller.UpdateChannelInfo(JsonUtility.ToJson(fakeLeftChannels));
 0598        }
 599
 600        private async UniTask SearchFakeChannels(int limit, string name)
 601        {
 0602            await UniTask.Delay(Random.Range(40, 1000));
 603
 0604            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
 0621            var channelPayloads = new ChannelInfoPayloads();
 0622            List<ChannelInfoPayload> channelsInfo = new List<ChannelInfoPayload>();
 623
 0624            for (var i = 0; i < limit && i < ids.Length; i++)
 625            {
 0626                var channelId = ids[i];
 0627                if (!channelId.StartsWith(name) && !string.IsNullOrEmpty(name)) continue;
 628
 0629                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
 0639            channelPayloads.channelInfoPayload = channelsInfo.ToArray();
 640
 0641            var payload = new ChannelSearchResultsPayload
 642            {
 643                channels = channelPayloads.channelInfoPayload,
 644                since = Guid.NewGuid().ToString()
 645            };
 646
 0647            controller.UpdateChannelSearchResults(JsonUtility.ToJson(payload));
 0648        }
 649
 650        private void SendNearbyDescriptionMessage()
 651        {
 0652            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
 0660            controller.AddMessageToChatWindow(JsonUtility.ToJson(messagePayload));
 0661        }
 662
 663        private void UpdateNearbyUnseenMessages(ChatMessage message)
 664        {
 665            const string nearbyChannelId = "nearby";
 666
 0667            if (message.messageType == ChatMessage.Type.PUBLIC
 668                && string.IsNullOrEmpty(message.recipient)
 669                || message.recipient == nearbyChannelId)
 670            {
 0671                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                };
 0682                controller.UpdateTotalUnseenMessagesByChannel(JsonUtility.ToJson(totalUnseenMessagesByChannelPayload));
 683
 0684                var totalUnseenMessagesPayload = new UpdateTotalUnseenMessagesPayload
 685                {
 686                    total = TotalUnseenMessages + 1
 687                };
 0688                controller.UpdateTotalUnseenMessages(JsonUtility.ToJson(totalUnseenMessagesPayload));
 689            }
 0690        }
 691
 692        public void GetChannelInfo(string[] channelIds) =>
 0693            GetFakeChannelInfo(channelIds).Forget();
 694
 695        private async UniTask GetFakeChannelInfo(string[] channelIds)
 696        {
 0697            await UniTask.Delay(Random.Range(40, 1000));
 698
 0699            ChannelInfoPayloads fakeChannels = new ChannelInfoPayloads { channelInfoPayload = new ChannelInfoPayload[cha
 700
 0701            for (int i = 0; i < channelIds.Length; i++)
 702            {
 0703                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
 0712                fakeChannels.channelInfoPayload[i] = msg;
 713            }
 714
 0715            controller.UpdateChannelInfo(JsonUtility.ToJson(fakeChannels));
 0716        }
 717
 718        public void GetChannelMembers(string channelId, int limit, int skip, string name) =>
 0719            GetFakeChannelMembers(channelId, limit, skip, name).Forget();
 720
 721        public void GetChannelMembers(string channelId, int limit, int skip) =>
 0722            GetFakeChannelMembers(channelId, limit, skip, "").Forget();
 723
 724        private async UniTask GetFakeChannelMembers(string channelId, int limit, int skip, string name)
 725        {
 0726            List<string> members = new List<string>();
 727
 0728            for (int i = 0; i < 100; i++)
 729            {
 0730                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
 0737                userProfileController.AddUserProfileToCatalog(profile);
 0738                members.Add(profile.userId);
 739            }
 740
 0741            await UniTask.Delay(Random.Range(40, 1000));
 742
 0743            List<ChannelMember> membersToUpdate = new List<ChannelMember>();
 0744            for (var i = skip; i < skip + limit && i < members.Count; i++)
 745            {
 0746                var userId = members[i];
 0747                if (!userId.Contains(name) && !string.IsNullOrEmpty(name)) continue;
 748
 0749                membersToUpdate.Add(new ChannelMember
 750                {
 751                    userId = userId,
 752                    isOnline = Random.Range(0, 2) == 0
 753                });
 754            }
 755
 0756            var msg = new UpdateChannelMembersPayload
 757            {
 758                channelId = channelId,
 759                members = membersToUpdate.ToArray()
 760            };
 0761            controller.UpdateChannelMembers(JsonUtility.ToJson(msg));
 0762        }
 763
 0764        public Channel GetAllocatedChannelByName(string channelName) => null;
 765    }
 766}

Methods/Properties

ChatChannelsControllerMock(DCL.Chat.ChatController, UserProfileController)
i()
add_OnTotalUnseenMessagesUpdated(System.Action[Int32])
remove_OnTotalUnseenMessagesUpdated(System.Action[Int32])
add_OnUserUnseenMessagesUpdated(System.Action[String,Int32])
remove_OnUserUnseenMessagesUpdated(System.Action[String,Int32])
add_OnChannelUnseenMessagesUpdated(System.Action[String,Int32])
remove_OnChannelUnseenMessagesUpdated(System.Action[String,Int32])
add_OnInitialized(System.Action)
remove_OnInitialized(System.Action)
add_OnAddMessage(System.Action[ChatMessage])
remove_OnAddMessage(System.Action[ChatMessage])
add_OnChannelUpdated(System.Action[Channel])
remove_OnChannelUpdated(System.Action[Channel])
add_OnChannelJoined(System.Action[Channel])
remove_OnChannelJoined(System.Action[Channel])
add_OnJoinChannelError(System.Action[String,ChannelErrorCode])
remove_OnJoinChannelError(System.Action[String,ChannelErrorCode])
add_OnChannelLeft(System.Action[String])
remove_OnChannelLeft(System.Action[String])
add_OnChannelLeaveError(System.Action[String,ChannelErrorCode])
remove_OnChannelLeaveError(System.Action[String,ChannelErrorCode])
add_OnMuteChannelError(System.Action[String,ChannelErrorCode])
remove_OnMuteChannelError(System.Action[String,ChannelErrorCode])
add_OnUpdateChannelMembers(System.Action[String,ChannelMember[]])
remove_OnUpdateChannelMembers(System.Action[String,ChannelMember[]])
add_OnChannelSearchResult(System.Action[String,Channel[]])
remove_OnChannelSearchResult(System.Action[String,Channel[]])
TotalUnseenMessages()
GetAllocatedEntries()
GetPrivateAllocatedEntriesByUser(System.String)
GetUnseenMessagesByChannel()
Send(DCL.Interface.ChatMessage)
IsChannelMessage(DCL.Interface.ChatMessage)
SimulateDelayedResponseFor_JoinChatMessage()
MarkMessagesAsSeen(System.String)
MarkChannelMessagesAsSeen(System.String)
GetPrivateMessages(System.String, System.Int32, System.String)
JoinOrCreateChannel(System.String)
LeaveChannel(System.String)
GetChannelMessages(System.String, System.Int32, System.String)
GetJoinedChannels(System.Int32, System.Int32)
GetChannelsByName(System.Int32, System.String, System.String)
GetChannels(System.Int32, System.String)
MuteChannel(System.String)
UnmuteChannel(System.String)
GetAllocatedChannel(System.String)
GetUnseenMessagesByUser()
GetAllocatedUnseenMessages(System.String)
GetAllocatedUnseenChannelMessages(System.String)
CreateChannel(System.String)
SimulateDelayedResponseFor_CreateChannel()
MuteFakeChannel()
SimulateDelayedResponseFor_ChatInitialization()
SimulateDelayedResponseFor_MarkChannelAsSeen()
AddFakeMessagesFromInput()
FakeChannelMessage()
FakeCurrentChannelMessage()
FakePrivateMessage()
SendWelcomeMessage()
CreateMockedDataFor_ChannelInfoPayload(System.String)
CreateMockedDataFor_JoinChannelErrorPayload(System.String)
SimulateDelayedResponseFor_JoinOrCreateChannel()
LeaveFakeChannel()
SearchFakeChannels()
SendNearbyDescriptionMessage()
UpdateNearbyUnseenMessages(DCL.Interface.ChatMessage)
GetChannelInfo(System.String[])
GetFakeChannelInfo()
GetChannelMembers(System.String, System.Int32, System.Int32, System.String)
GetChannelMembers(System.String, System.Int32, System.Int32)
GetFakeChannelMembers()
GetAllocatedChannelByName(System.String)