< Summary

Class:WorldChatWindowController
Assembly:WorldChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/WorldChatWindowController.cs
Covered lines:287
Uncovered lines:59
Coverable lines:346
Total lines:699
Line coverage:82.9% (287 of 346)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WorldChatWindowController(...)0%110100%
Initialize(...)0%880100%
Dispose()0%440100%
SetVisibility(...)0%13130100%
OpenChannelCreationWindow()0%6200%
SetVisiblePanelList(...)0%220100%
LeaveChannel(...)0%220100%
RequestJoinedChannels()0%3.023087.5%
WaitThenHideChannelsLoading()0%9.834028.57%
HandleChatInitialization()0%110100%
ConnectToAutoJoinChannels()0%6.016093.33%
HandleFriendsControllerInitialization()0%330100%
OpenPrivateChat(...)0%220100%
OpenPublicChat(...)0%440100%
HandleViewCloseRequest()0%220100%
HandleFriendshipUpdated(...)0%220100%
HandleUserStatusChanged(...)0%2.52050%
HandleMessageAdded(...)0%6.356078.57%
HandleFriendsWithDirectMessagesAdded(...)0%6.325062.5%
CreatePrivateChatModel(...)0%110100%
ExtractRecipientId(...)0%220100%
OnUserProfileUpdate(...)0%220100%
SearchChats(...)0%440100%
ShowMorePrivateChats()0%4.254075%
UpdateMoreChannelsToLoadHint()0%330100%
RequestFriendsWithDirectMessages()0%220100%
HidePrivateChatsLoadingWhenTimeout()0%6.984042.86%
RequestFriendsWithDirectMessagesFromSearch(...)0%220100%
HandleChannelUpdated(...)0%3.113076.92%
ClearOfflineUnseenMessages(...)0%220100%
HandleAutoChannelJoined(...)0%110100%
HandleChannelJoined(...)0%110100%
ReportChannelJoinedToAnalytics(...)0%220100%
HandleJoinChannelError(...)0%20400%
HandleLeaveChannelError(...)0%2100%
HandleChannelLeft(...)0%440100%
HandleChannelOpened(...)0%6200%
HandleAskForJoinChannel(...)0%12300%
HandleAskForJoinChannelAfterRendererState(...)0%6200%
RequestUnreadMessages()0%110100%
RequestUnreadChannelsMessages()0%110100%
OpenChannelSearch()0%12300%
HideSearchLoadingWhenTimeout()0%9.834028.57%
OnAllowedToCreateChannelsChanged(...)0%2100%
SetAutomaticChannelsInfoUpdatingActive(...)0%220100%
ReloadChannelsInfoPeriodically()0%10.754025%
GetCurrentChannelsInfo()0%330100%
OpenWalletReadme()0%110100%
SignUp()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/WorldChatWindowController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL;
 3using DCL.Chat;
 4using DCL.Chat.Channels;
 5using DCL.Interface;
 6using SocialFeaturesAnalytics;
 7using System;
 8using System.Collections.Generic;
 9using System.Linq;
 10using System.Threading;
 11using DCL.Browser;
 12using DCl.Social.Friends;
 13using DCL.Social.Friends;
 14using UnityEngine;
 15using Channel = DCL.Chat.Channels.Channel;
 16using DCL.Chat.HUD;
 17
 18public class WorldChatWindowController : IHUD
 19{
 20    private const int DMS_PAGE_SIZE = 30;
 21    private const int USER_DM_ENTRIES_TO_REQUEST_FOR_SEARCH = 20;
 22    private const int CHANNELS_PAGE_SIZE = 10;
 23    private const int MINUTES_FOR_AUTOMATIC_CHANNELS_INFO_RELOADING = 1;
 24
 25    private readonly IUserProfileBridge userProfileBridge;
 26    private readonly IFriendsController friendsController;
 27    private readonly IChatController chatController;
 28    private readonly DataStore dataStore;
 29    private readonly IMouseCatcher mouseCatcher;
 30    private readonly ISocialAnalytics socialAnalytics;
 31    private readonly IChannelsFeatureFlagService channelsFeatureFlagService;
 32    private readonly IBrowserBridge browserBridge;
 33    private readonly RendererState rendererState;
 5134    private readonly Dictionary<string, PublicChatModel> publicChannels = new Dictionary<string, PublicChatModel>();
 5135    private readonly Dictionary<string, ChatMessage> lastPrivateMessages = new Dictionary<string, ChatMessage>();
 5136    private readonly HashSet<string> channelsClearedUnseenNotifications = new HashSet<string>();
 137    private HashSet<string> autoJoinChannelList => dataStore.HUDs.autoJoinChannelList.Get();
 6838    private BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels;
 39    private int hiddenDMs;
 5140    private string currentSearch = "";
 41    private DateTime channelsRequestTimestamp;
 42    private bool areDMsRequestedByFirstTime;
 43    private int lastSkipForDMs;
 5144    private CancellationTokenSource hideLoadingCancellationToken = new CancellationTokenSource();
 45    private IWorldChatWindowView view;
 46    private UserProfile ownUserProfile;
 47    private bool isRequestingDMs;
 48    private bool areUnseenMessajesRequestedByFirstTime;
 49    private string channelToJoinAtTheBeginning;
 5150    private CancellationTokenSource hideChannelsLoadingCancellationToken = new CancellationTokenSource();
 5151    private CancellationTokenSource hidePrivateChatsLoadingCancellationToken = new CancellationTokenSource();
 5152    private CancellationTokenSource reloadingChannelsInfoCancellationToken = new CancellationTokenSource();
 5553    private bool showOnlyOnlineMembersOnPublicChannels => !dataStore.featureFlags.flags.Get().IsFeatureEnabled("matrix_p
 54
 5155    private bool isVisible = true;
 1756    public IWorldChatWindowView View => view;
 57
 58    public event Action OnCloseView;
 59    public event Action<string> OnOpenPrivateChat;
 60    public event Action<string> OnOpenPublicChat;
 61    public event Action<string> OnOpenChannel;
 62    public event Action OnOpen;
 63    public event Action OnOpenChannelSearch;
 64    public event Action OnOpenChannelCreation;
 65    public event Action<string> OnOpenChannelLeave;
 66
 5167    public WorldChatWindowController(
 68        IUserProfileBridge userProfileBridge,
 69        IFriendsController friendsController,
 70        IChatController chatController,
 71        DataStore dataStore,
 72        IMouseCatcher mouseCatcher,
 73        ISocialAnalytics socialAnalytics,
 74        IChannelsFeatureFlagService channelsFeatureFlagService,
 75        IBrowserBridge browserBridge,
 76        RendererState rendererState)
 77    {
 5178        this.userProfileBridge = userProfileBridge;
 5179        this.friendsController = friendsController;
 5180        this.chatController = chatController;
 5181        this.dataStore = dataStore;
 5182        this.mouseCatcher = mouseCatcher;
 5183        this.socialAnalytics = socialAnalytics;
 5184        this.channelsFeatureFlagService = channelsFeatureFlagService;
 5185        this.browserBridge = browserBridge;
 5186        this.rendererState = rendererState;
 5187    }
 88
 89    public void Initialize(IWorldChatWindowView view, bool isVisible = true)
 90    {
 5191        this.view = view;
 5192        view.Initialize(chatController);
 93
 5194        if (mouseCatcher != null)
 5095            mouseCatcher.OnMouseLock += HandleViewCloseRequest;
 96
 5197        view.OnClose += HandleViewCloseRequest;
 5198        view.OnOpenPrivateChat += OpenPrivateChat;
 5199        view.OnOpenPublicChat += OpenPublicChat;
 51100        view.OnSearchChatRequested += SearchChats;
 51101        view.OnRequireMorePrivateChats += ShowMorePrivateChats;
 51102        view.OnSignUp += SignUp;
 51103        view.OnRequireWalletReadme += OpenWalletReadme;
 104
 51105        ownUserProfile = userProfileBridge.GetOwn();
 51106        if (ownUserProfile != null)
 50107            ownUserProfile.OnUpdate += OnUserProfileUpdate;
 108
 51109        var channel = chatController.GetAllocatedChannel(ChatUtils.NEARBY_CHANNEL_ID);
 51110        publicChannels[ChatUtils.NEARBY_CHANNEL_ID] = new PublicChatModel(ChatUtils.NEARBY_CHANNEL_ID, channel.Name,
 111            channel.Description,
 112            channel.Joined,
 113            channel.MemberCount,
 114            false,
 115            showOnlyOnlineMembersOnPublicChannels);
 51116        view.SetPublicChat(publicChannels[ChatUtils.NEARBY_CHANNEL_ID]);
 117
 51118        if (!friendsController.IsInitialized)
 7119            if (ownUserProfile?.hasConnectedWeb3 ?? false)
 5120                view.ShowPrivateChatsLoading();
 121
 51122        chatController.OnInitialized += HandleChatInitialization;
 51123        chatController.OnAddMessage += HandleMessageAdded;
 51124        friendsController.OnAddFriendsWithDirectMessages += HandleFriendsWithDirectMessagesAdded;
 51125        friendsController.OnUpdateUserStatus += HandleUserStatusChanged;
 51126        friendsController.OnUpdateFriendship += HandleFriendshipUpdated;
 51127        friendsController.OnInitialized += HandleFriendsControllerInitialization;
 128
 51129        if (channelsFeatureFlagService.IsChannelsFeatureEnabled())
 130        {
 48131            channelsFeatureFlagService.OnAllowedToCreateChannelsChanged += OnAllowedToCreateChannelsChanged;
 132
 48133            view.OnOpenChannelSearch += OpenChannelSearch;
 48134            view.OnLeaveChannel += LeaveChannel;
 48135            view.OnCreateChannel += OpenChannelCreationWindow;
 136
 48137            chatController.OnChannelUpdated += HandleChannelUpdated;
 48138            chatController.OnChannelJoined += HandleChannelJoined;
 48139            chatController.OnAutoChannelJoined += HandleAutoChannelJoined;
 48140            chatController.OnJoinChannelError += HandleJoinChannelError;
 48141            chatController.OnChannelLeaveError += HandleLeaveChannelError;
 48142            chatController.OnChannelLeft += HandleChannelLeft;
 48143            chatController.OnAskForJoinChannel += HandleAskForJoinChannel;
 48144            dataStore.channels.channelToBeOpened.OnChange += HandleChannelOpened;
 145
 48146            view.ShowChannelsLoading();
 48147            view.SetSearchAndCreateContainerActive(true);
 148        }
 149        else
 150        {
 3151            view.HideChannelsLoading();
 3152            view.SetSearchAndCreateContainerActive(false);
 153        }
 154
 51155        view.SetChannelsPromoteLabelVisible(channelsFeatureFlagService.IsPromoteChannelsToastEnabled());
 156
 51157        SetVisibility(isVisible);
 51158        this.isVisible = isVisible;
 51159    }
 160
 161    public void Dispose()
 162    {
 3163        view.OnClose -= HandleViewCloseRequest;
 3164        view.OnOpenPrivateChat -= OpenPrivateChat;
 3165        view.OnOpenPublicChat -= OpenPublicChat;
 3166        view.OnSearchChatRequested -= SearchChats;
 3167        view.OnRequireMorePrivateChats -= ShowMorePrivateChats;
 3168        view.OnOpenChannelSearch -= OpenChannelSearch;
 3169        view.OnCreateChannel -= OpenChannelCreationWindow;
 3170        view.OnSignUp -= SignUp;
 3171        view.OnRequireWalletReadme -= OpenWalletReadme;
 3172        view.Dispose();
 3173        chatController.OnInitialized -= HandleChatInitialization;
 3174        chatController.OnAddMessage -= HandleMessageAdded;
 3175        chatController.OnChannelUpdated -= HandleChannelUpdated;
 3176        chatController.OnChannelJoined -= HandleChannelJoined;
 3177        chatController.OnAutoChannelJoined -= HandleAutoChannelJoined;
 3178        chatController.OnJoinChannelError -= HandleJoinChannelError;
 3179        chatController.OnChannelLeaveError -= HandleLeaveChannelError;
 3180        chatController.OnChannelLeft -= HandleChannelLeft;
 3181        chatController.OnAskForJoinChannel -= HandleAskForJoinChannel;
 3182        friendsController.OnAddFriendsWithDirectMessages -= HandleFriendsWithDirectMessagesAdded;
 3183        friendsController.OnUpdateUserStatus -= HandleUserStatusChanged;
 3184        friendsController.OnUpdateFriendship -= HandleFriendshipUpdated;
 3185        friendsController.OnInitialized -= HandleFriendsControllerInitialization;
 3186        dataStore.channels.channelToBeOpened.OnChange -= HandleChannelOpened;
 3187        rendererState.OnChange -= HandleAskForJoinChannelAfterRendererState;
 188
 3189        if (ownUserProfile != null)
 2190            ownUserProfile.OnUpdate -= OnUserProfileUpdate;
 191
 3192        hideChannelsLoadingCancellationToken?.Cancel();
 3193        hideChannelsLoadingCancellationToken?.Dispose();
 3194        reloadingChannelsInfoCancellationToken.Cancel();
 3195        reloadingChannelsInfoCancellationToken.Dispose();
 196
 3197        channelsFeatureFlagService.OnAllowedToCreateChannelsChanged -= OnAllowedToCreateChannelsChanged;
 3198    }
 199
 200    public void SetVisibility(bool visible)
 201    {
 72202        if (isVisible == visible)
 38203            return;
 204
 34205        isVisible = visible;
 34206        SetVisiblePanelList(visible);
 207
 34208        if (visible)
 209        {
 16210            view.Show();
 16211            OnOpen?.Invoke();
 212
 16213            if (friendsController.IsInitialized && !areDMsRequestedByFirstTime)
 214            {
 12215                RequestFriendsWithDirectMessages();
 12216                RequestUnreadMessages();
 217            }
 218
 16219            if (channelsFeatureFlagService.IsChannelsFeatureEnabled())
 220            {
 14221                if (chatController.IsInitialized)
 222                {
 13223                    RequestJoinedChannels();
 13224                    SetAutomaticChannelsInfoUpdatingActive(true);
 225                }
 1226                else if (ownUserProfile.isGuest)
 227                {
 228                    // TODO: channels are not allowed for guests. When we support it in the future, remove this call
 1229                    view.HideChannelsLoading();
 230                }
 231
 14232                if (!areUnseenMessajesRequestedByFirstTime)
 14233                    RequestUnreadChannelsMessages();
 234            }
 235
 16236            if (ownUserProfile?.isGuest ?? false)
 2237                view.ShowConnectWallet();
 238            else
 14239                view.HideConnectWallet();
 240        }
 241        else
 242        {
 18243            view.DisableSearchMode();
 18244            view.Hide();
 18245            SetAutomaticChannelsInfoUpdatingActive(false);
 246        }
 18247    }
 248
 249    private void OpenChannelCreationWindow()
 250    {
 0251        dataStore.channels.channelJoinedSource.Set(ChannelJoinedSource.ConversationList);
 0252        OnOpenChannelCreation?.Invoke();
 0253    }
 254
 255    private void SetVisiblePanelList(bool visible)
 256    {
 34257        HashSet<string> newSet = visibleTaskbarPanels.Get();
 34258        if (visible)
 16259            newSet.Add("WorldChatPanel");
 260        else
 18261            newSet.Remove("WorldChatPanel");
 262
 34263        visibleTaskbarPanels.Set(newSet, true);
 34264    }
 265
 266    private void LeaveChannel(string channelId)
 267    {
 1268        dataStore.channels.channelLeaveSource.Set(ChannelLeaveSource.ConversationList);
 1269        OnOpenChannelLeave?.Invoke(channelId);
 1270    }
 271
 272    private void RequestJoinedChannels()
 273    {
 15274        if ((DateTime.UtcNow - channelsRequestTimestamp).TotalSeconds < 3) return;
 275
 276        // skip=0: we do not support pagination for channels, it is supposed that a user can have a limited amount of jo
 15277        chatController.GetJoinedChannels(CHANNELS_PAGE_SIZE, 0);
 15278        channelsRequestTimestamp = DateTime.UtcNow;
 279
 15280        hideChannelsLoadingCancellationToken?.Cancel();
 15281        hideChannelsLoadingCancellationToken = new CancellationTokenSource();
 15282        WaitThenHideChannelsLoading(hideChannelsLoadingCancellationToken.Token).Forget();
 15283    }
 284
 285    private async UniTask WaitThenHideChannelsLoading(CancellationToken cancellationToken)
 286    {
 30287        await UniTask.Delay(3000, cancellationToken: cancellationToken);
 0288        if (cancellationToken.IsCancellationRequested) return;
 0289        view.HideChannelsLoading();
 0290    }
 291
 292    private void HandleChatInitialization()
 293    {
 294        // we do request joined channels as soon as possible to be able to display messages correctly in the notificatio
 2295        RequestJoinedChannels();
 2296        ConnectToAutoJoinChannels();
 2297    }
 298
 299    private void ConnectToAutoJoinChannels()
 300    {
 2301        AutomaticJoinChannelList joinChannelList = channelsFeatureFlagService.GetAutoJoinChannelsList();
 3302        if (joinChannelList == null) return;
 1303        if (joinChannelList.automaticJoinChannelList == null) return;
 304
 4305        foreach (var channel in joinChannelList.automaticJoinChannelList)
 306        {
 1307            var channelId = channel.channelId;
 1308            if (string.IsNullOrEmpty(channelId)) continue;
 1309            autoJoinChannelList.Add(channelId);
 1310            chatController.JoinOrCreateChannel(channelId);
 1311            if (!channel.enableNotifications)
 1312                chatController.MuteChannel(channelId);
 313        }
 1314    }
 315
 316    private void HandleFriendsControllerInitialization()
 317    {
 3318        if (view.IsActive && !areDMsRequestedByFirstTime)
 319        {
 2320            RequestFriendsWithDirectMessages();
 2321            RequestUnreadMessages();
 322        }
 323        else
 1324            view.HidePrivateChatsLoading();
 1325    }
 326
 327    private void OpenPrivateChat(string userId)
 328    {
 1329        OnOpenPrivateChat?.Invoke(userId);
 1330    }
 331
 332    private void OpenPublicChat(string channelId)
 333    {
 4334        if (channelId == ChatUtils.NEARBY_CHANNEL_ID)
 1335            OnOpenPublicChat?.Invoke(channelId);
 336        else
 3337            OnOpenChannel?.Invoke(channelId);
 1338    }
 339
 340    private void HandleViewCloseRequest()
 341    {
 1342        OnCloseView?.Invoke();
 1343        SetVisibility(false);
 1344    }
 345
 346    private void HandleFriendshipUpdated(string userId, FriendshipAction friendship)
 347    {
 6348        if (friendship != FriendshipAction.APPROVED)
 349        {
 350            // show only private chats from friends. Change it whenever the catalyst supports to send pms to any user
 6351            view.RemovePrivateChat(userId);
 352        }
 6353    }
 354
 355    private void HandleUserStatusChanged(string userId, UserStatus status)
 356    {
 4357        if (status.friendshipStatus != FriendshipStatus.FRIEND)
 358        {
 359            // show only private chats from friends. Change it whenever the catalyst supports to send pms to any user
 4360            view.RemovePrivateChat(userId);
 361        }
 362        else
 363        {
 0364            view.RefreshPrivateChatPresence(userId, status.presence == PresenceStatus.ONLINE);
 365        }
 0366    }
 367
 368    private void HandleMessageAdded(ChatMessage[] messages)
 369    {
 8370        foreach (var message in messages)
 371        {
 2372            if (message.messageType != ChatMessage.Type.PRIVATE) continue;
 373
 2374            var userId = ExtractRecipientId(message);
 375
 2376            if (lastPrivateMessages.ContainsKey(userId))
 377            {
 0378                if (message.timestamp > lastPrivateMessages[userId].timestamp)
 0379                    lastPrivateMessages[userId] = message;
 380            }
 381            else
 2382                lastPrivateMessages[userId] = message;
 383
 2384            var profile = userProfileBridge.Get(userId);
 2385            if (profile == null) return;
 386
 2387            view.SetPrivateChat(CreatePrivateChatModel(message, profile));
 388        }
 2389    }
 390
 391    private void HandleFriendsWithDirectMessagesAdded(List<FriendWithDirectMessages> usersWithDM)
 392    {
 12393        for (var i = 0; i < usersWithDM.Count; i++)
 394        {
 3395            var profile = userProfileBridge.Get(usersWithDM[i].userId);
 3396            if (profile == null) continue;
 397
 0398            var lastMessage = new ChatMessage
 399            {
 400                messageType = ChatMessage.Type.PRIVATE,
 401                body = usersWithDM[i].lastMessageBody,
 402                timestamp = (ulong) usersWithDM[i].lastMessageTimestamp
 403            };
 404
 0405            if (lastPrivateMessages.ContainsKey(profile.userId))
 406            {
 0407                if (lastMessage.timestamp > lastPrivateMessages[profile.userId].timestamp)
 0408                    lastPrivateMessages[profile.userId] = lastMessage;
 409            }
 410            else
 0411                lastPrivateMessages[profile.userId] = lastMessage;
 412
 0413            view.SetPrivateChat(CreatePrivateChatModel(lastMessage, profile));
 414        }
 415
 3416        UpdateMoreChannelsToLoadHint();
 3417        view.HidePrivateChatsLoading();
 3418        view.HideSearchLoading();
 419
 3420        isRequestingDMs = false;
 3421    }
 422
 423    private PrivateChatModel CreatePrivateChatModel(ChatMessage recentMessage, UserProfile profile)
 424    {
 2425        return new PrivateChatModel
 426        {
 427            user = profile,
 428            recentMessage = recentMessage,
 429            isBlocked = ownUserProfile.IsBlocked(profile.userId),
 430            isOnline = friendsController.GetUserStatus(profile.userId).presence == PresenceStatus.ONLINE
 431        };
 432    }
 433
 434    private string ExtractRecipientId(ChatMessage message)
 435    {
 2436        return message.sender != ownUserProfile.userId ? message.sender : message.recipient;
 437    }
 438
 439    private void OnUserProfileUpdate(UserProfile profile)
 440    {
 2441        view.RefreshBlockedDirectMessages(profile.blocked);
 442
 2443        if (profile.isGuest)
 1444            view.ShowConnectWallet();
 445        else
 1446            view.HideConnectWallet();
 1447    }
 448
 449    private void SearchChats(string search)
 450    {
 2451        currentSearch = search;
 452
 2453        if (string.IsNullOrEmpty(search))
 454        {
 1455            View.DisableSearchMode();
 1456            UpdateMoreChannelsToLoadHint();
 1457            return;
 458        }
 459
 1460        UpdateMoreChannelsToLoadHint();
 1461        View.EnableSearchMode();
 462
 1463        var matchedChannels = publicChannels.Values
 2464            .Where(model => model.name.ToLower().Contains(search.ToLower()));
 4465        foreach (var channelMatch in matchedChannels)
 1466            View.SetPublicChat(channelMatch);
 467
 1468        RequestFriendsWithDirectMessagesFromSearch(search, USER_DM_ENTRIES_TO_REQUEST_FOR_SEARCH);
 1469    }
 470
 471    private void ShowMorePrivateChats()
 472    {
 1473        if (isRequestingDMs ||
 474            hiddenDMs == 0 ||
 475            !string.IsNullOrEmpty(currentSearch))
 0476            return;
 477
 1478        RequestFriendsWithDirectMessages();
 1479    }
 480
 481    private void UpdateMoreChannelsToLoadHint()
 482    {
 5483        hiddenDMs = Mathf.Clamp(friendsController.TotalFriendsWithDirectMessagesCount - lastSkipForDMs,
 484            0,
 485            friendsController.TotalFriendsWithDirectMessagesCount);
 486
 5487        if (hiddenDMs <= 0 || !string.IsNullOrEmpty(currentSearch))
 3488            View.HideMoreChatsToLoadHint();
 489        else
 2490            View.ShowMoreChatsToLoadHint(hiddenDMs);
 2491    }
 492
 493    private void RequestFriendsWithDirectMessages()
 494    {
 15495        isRequestingDMs = true;
 496
 15497        if (!areDMsRequestedByFirstTime)
 498        {
 14499            view.ShowPrivateChatsLoading();
 14500            view.HideMoreChatsToLoadHint();
 501        }
 502
 15503        friendsController.GetFriendsWithDirectMessages(DMS_PAGE_SIZE, lastSkipForDMs);
 15504        lastSkipForDMs += DMS_PAGE_SIZE;
 15505        areDMsRequestedByFirstTime = true;
 506
 15507        hidePrivateChatsLoadingCancellationToken.Cancel();
 15508        hidePrivateChatsLoadingCancellationToken = new CancellationTokenSource();
 15509        HidePrivateChatsLoadingWhenTimeout(hidePrivateChatsLoadingCancellationToken.Token).Forget();
 15510    }
 511
 512    private async UniTaskVoid HidePrivateChatsLoadingWhenTimeout(CancellationToken cancellationToken)
 513    {
 31514        await UniTask.Delay(3000, cancellationToken: cancellationToken);
 0515        if (cancellationToken.IsCancellationRequested) return;
 0516        view.HidePrivateChatsLoading();
 0517    }
 518
 519    internal void RequestFriendsWithDirectMessagesFromSearch(string userNameOrId, int limit)
 520    {
 2521        view.ShowSearchLoading();
 2522        friendsController.GetFriendsWithDirectMessages(userNameOrId, limit);
 2523        hideLoadingCancellationToken?.Cancel();
 2524        hideLoadingCancellationToken = new CancellationTokenSource();
 2525        HideSearchLoadingWhenTimeout(hideLoadingCancellationToken.Token).Forget();
 2526    }
 527
 528    private void HandleChannelUpdated(Channel channel)
 529    {
 4530        if (!channel.Joined)
 531        {
 0532            view.RemovePublicChat(channel.ChannelId);
 0533            publicChannels.Remove(channel.ChannelId);
 0534            return;
 535        }
 536
 4537        var channelId = channel.ChannelId;
 4538        var model = new PublicChatModel(channelId, channel.Name, channel.Description, channel.Joined,
 539            channel.MemberCount, channel.Muted, showOnlyOnlineMembersOnPublicChannels);
 540
 4541        if (publicChannels.ContainsKey(channelId))
 1542            publicChannels[channelId].CopyFrom(model);
 543        else
 3544            publicChannels[channelId] = model;
 545
 4546        view.SetPublicChat(model);
 4547        view.HideChannelsLoading();
 548
 549        // we clear the unseen messages to avoid showing many of them while the user was offline
 550        // TODO: we should consider avoid clearing when the channel is private in the future
 4551        ClearOfflineUnseenMessages(channelId);
 4552    }
 553
 554    private void ClearOfflineUnseenMessages(string channelId)
 555    {
 5556        if (channelsClearedUnseenNotifications.Contains(channelId)) return;
 3557        chatController.MarkChannelMessagesAsSeen(channelId);
 3558        channelsClearedUnseenNotifications.Add(channelId);
 3559    }
 560
 1561    private void HandleAutoChannelJoined(Channel channel) => ReportChannelJoinedToAnalytics(channel, "auto");
 562
 563    private void HandleChannelJoined(Channel channel)
 564    {
 2565        ReportChannelJoinedToAnalytics(channel, "manual");
 2566        OpenPublicChat(channel.ChannelId);
 2567    }
 568
 569    private void ReportChannelJoinedToAnalytics(Channel channel, string method)
 570    {
 3571        if (channel.MemberCount <= 1)
 1572            socialAnalytics.SendEmptyChannelCreated(channel.Name, dataStore.channels.channelJoinedSource.Get());
 573        else
 2574            socialAnalytics.SendPopulatedChannelJoined(channel.Name, dataStore.channels.channelJoinedSource.Get(), metho
 2575    }
 576
 577    private void HandleJoinChannelError(string channelId, ChannelErrorCode errorCode)
 578    {
 0579        if (dataStore.channels.isCreationModalVisible.Get()) return;
 580
 581        switch (errorCode)
 582        {
 583            case ChannelErrorCode.LimitExceeded:
 0584                dataStore.channels.currentChannelLimitReached.Set(channelId, true);
 0585                break;
 586            case ChannelErrorCode.Unknown:
 0587                dataStore.channels.joinChannelError.Set(channelId, true);
 588                break;
 589        }
 0590    }
 591
 592    private void HandleLeaveChannelError(string channelId, ChannelErrorCode errorCode) =>
 0593        dataStore.channels.leaveChannelError.Set(channelId, true);
 594
 595    private void HandleChannelLeft(string channelId)
 596    {
 1597        publicChannels.Remove(channelId);
 1598        view.RemovePublicChat(channelId);
 1599        var channel = chatController.GetAllocatedChannel(channelId);
 1600        socialAnalytics.SendLeaveChannel(channel?.Name ?? channelId, dataStore.channels.channelLeaveSource.Get());
 1601    }
 602
 603    private void HandleChannelOpened(string channelId, string previousChannelId)
 604    {
 0605        if (string.IsNullOrEmpty(channelId))
 0606            return;
 607
 0608        OpenPublicChat(channelId);
 0609    }
 610
 611    private void HandleAskForJoinChannel(string channelName)
 612    {
 0613        chatController.OnAskForJoinChannel -= HandleAskForJoinChannel;
 614
 0615        if (!channelsFeatureFlagService.IsAllowedToCreateChannels())
 0616            return;
 617
 0618        if (rendererState.Get())
 0619            dataStore.channels.currentJoinChannelModal.Set(channelName, true);
 620        else
 621        {
 0622            channelToJoinAtTheBeginning = channelName;
 0623            rendererState.OnChange += HandleAskForJoinChannelAfterRendererState;
 624        }
 0625    }
 626
 627    private void HandleAskForJoinChannelAfterRendererState(bool current, bool _)
 628    {
 0629        if (!current)
 0630            return;
 631
 0632        rendererState.OnChange -= HandleAskForJoinChannelAfterRendererState;
 0633        dataStore.channels.currentJoinChannelModal.Set(channelToJoinAtTheBeginning, true);
 0634    }
 635
 14636    private void RequestUnreadMessages() => chatController.GetUnseenMessagesByUser();
 637
 638    private void RequestUnreadChannelsMessages()
 639    {
 14640        chatController.GetUnseenMessagesByChannel();
 14641        areUnseenMessajesRequestedByFirstTime = true;
 14642    }
 643
 644    private void OpenChannelSearch()
 645    {
 0646        if (!ownUserProfile.isGuest)
 0647            OnOpenChannelSearch?.Invoke();
 648        else
 0649            dataStore.HUDs.connectWalletModalVisible.Set(true);
 0650    }
 651
 652    private async UniTask HideSearchLoadingWhenTimeout(CancellationToken cancellationToken)
 653    {
 4654        await UniTask.Delay(3000, cancellationToken: cancellationToken);
 0655        if (cancellationToken.IsCancellationRequested) return;
 0656        view.HideSearchLoading();
 0657    }
 658
 0659    private void OnAllowedToCreateChannelsChanged(bool isAllowed) => view.SetCreateChannelButtonActive(isAllowed);
 660
 661    private void SetAutomaticChannelsInfoUpdatingActive(bool isActive)
 662    {
 31663        reloadingChannelsInfoCancellationToken.Cancel();
 664
 31665        if (isActive)
 666        {
 13667            GetCurrentChannelsInfo();
 13668            reloadingChannelsInfoCancellationToken = new CancellationTokenSource();
 13669            ReloadChannelsInfoPeriodically(reloadingChannelsInfoCancellationToken.Token).Forget();
 670        }
 31671    }
 672
 673    private async UniTask ReloadChannelsInfoPeriodically(CancellationToken cancellationToken)
 674    {
 0675        while (true)
 676        {
 26677            await UniTask.Delay(MINUTES_FOR_AUTOMATIC_CHANNELS_INFO_RELOADING * 60 * 1000,
 678                cancellationToken: cancellationToken);
 679
 0680            if (cancellationToken.IsCancellationRequested)
 0681                return;
 682
 0683            GetCurrentChannelsInfo();
 684        }
 0685    }
 686
 687    private void GetCurrentChannelsInfo()
 688    {
 13689        chatController.GetChannelInfo(publicChannels
 13690            .Select(x => x.Key)
 13691            .Where(x => x != ChatUtils.NEARBY_CHANNEL_ID)
 692            .ToArray());
 13693    }
 694
 695    private void OpenWalletReadme() =>
 1696        browserBridge.OpenUrl("https://docs.decentraland.org/player/blockchain-integration/get-a-wallet/");
 697
 1698    private void SignUp() => userProfileBridge.SignUp();
 699}

Methods/Properties

WorldChatWindowController(IUserProfileBridge, DCL.Social.Friends.IFriendsController, IChatController, DCL.DataStore, DCL.IMouseCatcher, SocialFeaturesAnalytics.ISocialAnalytics, DCL.Chat.IChannelsFeatureFlagService, DCL.Browser.IBrowserBridge, RendererState)
autoJoinChannelList()
visibleTaskbarPanels()
showOnlyOnlineMembersOnPublicChannels()
View()
Initialize(IWorldChatWindowView, System.Boolean)
Dispose()
SetVisibility(System.Boolean)
OpenChannelCreationWindow()
SetVisiblePanelList(System.Boolean)
LeaveChannel(System.String)
RequestJoinedChannels()
WaitThenHideChannelsLoading()
HandleChatInitialization()
ConnectToAutoJoinChannels()
HandleFriendsControllerInitialization()
OpenPrivateChat(System.String)
OpenPublicChat(System.String)
HandleViewCloseRequest()
HandleFriendshipUpdated(System.String, DCL.Social.Friends.FriendshipAction)
HandleUserStatusChanged(System.String, UserStatus)
HandleMessageAdded(DCL.Interface.ChatMessage[])
HandleFriendsWithDirectMessagesAdded(System.Collections.Generic.List[FriendWithDirectMessages])
CreatePrivateChatModel(DCL.Interface.ChatMessage, UserProfile)
ExtractRecipientId(DCL.Interface.ChatMessage)
OnUserProfileUpdate(UserProfile)
SearchChats(System.String)
ShowMorePrivateChats()
UpdateMoreChannelsToLoadHint()
RequestFriendsWithDirectMessages()
HidePrivateChatsLoadingWhenTimeout()
RequestFriendsWithDirectMessagesFromSearch(System.String, System.Int32)
HandleChannelUpdated(DCL.Chat.Channels.Channel)
ClearOfflineUnseenMessages(System.String)
HandleAutoChannelJoined(DCL.Chat.Channels.Channel)
HandleChannelJoined(DCL.Chat.Channels.Channel)
ReportChannelJoinedToAnalytics(DCL.Chat.Channels.Channel, System.String)
HandleJoinChannelError(System.String, DCL.Chat.Channels.ChannelErrorCode)
HandleLeaveChannelError(System.String, DCL.Chat.Channels.ChannelErrorCode)
HandleChannelLeft(System.String)
HandleChannelOpened(System.String, System.String)
HandleAskForJoinChannel(System.String)
HandleAskForJoinChannelAfterRendererState(System.Boolean, System.Boolean)
RequestUnreadMessages()
RequestUnreadChannelsMessages()
OpenChannelSearch()
HideSearchLoadingWhenTimeout()
OnAllowedToCreateChannelsChanged(System.Boolean)
SetAutomaticChannelsInfoUpdatingActive(System.Boolean)
ReloadChannelsInfoPeriodically()
GetCurrentChannelsInfo()
OpenWalletReadme()
SignUp()