< Summary

Class:WorldChatWindowController
Assembly:WorldChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/WorldChatWindowController.cs
Covered lines:281
Uncovered lines:59
Coverable lines:340
Total lines:691
Line coverage:82.6% (281 of 340)
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%12120100%
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();
 4238    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
 1755    public IWorldChatWindowView View => view;
 56
 57    public event Action OnCloseView;
 58    public event Action<string> OnOpenPrivateChat;
 59    public event Action<string> OnOpenPublicChat;
 60    public event Action<string> OnOpenChannel;
 61    public event Action OnOpen;
 62    public event Action OnOpenChannelSearch;
 63    public event Action OnOpenChannelCreation;
 64    public event Action<string> OnOpenChannelLeave;
 65
 5166    public WorldChatWindowController(
 67        IUserProfileBridge userProfileBridge,
 68        IFriendsController friendsController,
 69        IChatController chatController,
 70        DataStore dataStore,
 71        IMouseCatcher mouseCatcher,
 72        ISocialAnalytics socialAnalytics,
 73        IChannelsFeatureFlagService channelsFeatureFlagService,
 74        IBrowserBridge browserBridge,
 75        RendererState rendererState)
 76    {
 5177        this.userProfileBridge = userProfileBridge;
 5178        this.friendsController = friendsController;
 5179        this.chatController = chatController;
 5180        this.dataStore = dataStore;
 5181        this.mouseCatcher = mouseCatcher;
 5182        this.socialAnalytics = socialAnalytics;
 5183        this.channelsFeatureFlagService = channelsFeatureFlagService;
 5184        this.browserBridge = browserBridge;
 5185        this.rendererState = rendererState;
 5186    }
 87
 88    public void Initialize(IWorldChatWindowView view)
 89    {
 5190        this.view = view;
 5191        view.Initialize(chatController);
 92
 5193        if (mouseCatcher != null)
 5094            mouseCatcher.OnMouseLock += HandleViewCloseRequest;
 95
 5196        view.OnClose += HandleViewCloseRequest;
 5197        view.OnOpenPrivateChat += OpenPrivateChat;
 5198        view.OnOpenPublicChat += OpenPublicChat;
 5199        view.OnSearchChatRequested += SearchChats;
 51100        view.OnRequireMorePrivateChats += ShowMorePrivateChats;
 51101        view.OnSignUp += SignUp;
 51102        view.OnRequireWalletReadme += OpenWalletReadme;
 103
 51104        ownUserProfile = userProfileBridge.GetOwn();
 51105        if (ownUserProfile != null)
 50106            ownUserProfile.OnUpdate += OnUserProfileUpdate;
 107
 51108        var channel = chatController.GetAllocatedChannel(ChatUtils.NEARBY_CHANNEL_ID);
 51109        publicChannels[ChatUtils.NEARBY_CHANNEL_ID] = new PublicChatModel(ChatUtils.NEARBY_CHANNEL_ID, channel.Name,
 110            channel.Description,
 111            channel.Joined,
 112            channel.MemberCount,
 113            false,
 114            showOnlyOnlineMembersOnPublicChannels);
 51115        view.SetPublicChat(publicChannels[ChatUtils.NEARBY_CHANNEL_ID]);
 116
 51117        if (!friendsController.IsInitialized)
 7118            if (ownUserProfile?.hasConnectedWeb3 ?? false)
 5119                view.ShowPrivateChatsLoading();
 120
 51121        chatController.OnInitialized += HandleChatInitialization;
 51122        chatController.OnAddMessage += HandleMessageAdded;
 51123        friendsController.OnAddFriendsWithDirectMessages += HandleFriendsWithDirectMessagesAdded;
 51124        friendsController.OnUpdateUserStatus += HandleUserStatusChanged;
 51125        friendsController.OnUpdateFriendship += HandleFriendshipUpdated;
 51126        friendsController.OnInitialized += HandleFriendsControllerInitialization;
 127
 51128        if (channelsFeatureFlagService.IsChannelsFeatureEnabled())
 129        {
 48130            channelsFeatureFlagService.OnAllowedToCreateChannelsChanged += OnAllowedToCreateChannelsChanged;
 131
 48132            view.OnOpenChannelSearch += OpenChannelSearch;
 48133            view.OnLeaveChannel += LeaveChannel;
 48134            view.OnCreateChannel += OpenChannelCreationWindow;
 135
 48136            chatController.OnChannelUpdated += HandleChannelUpdated;
 48137            chatController.OnChannelJoined += HandleChannelJoined;
 48138            chatController.OnAutoChannelJoined += HandleAutoChannelJoined;
 48139            chatController.OnJoinChannelError += HandleJoinChannelError;
 48140            chatController.OnChannelLeaveError += HandleLeaveChannelError;
 48141            chatController.OnChannelLeft += HandleChannelLeft;
 48142            chatController.OnAskForJoinChannel += HandleAskForJoinChannel;
 48143            dataStore.channels.channelToBeOpened.OnChange += HandleChannelOpened;
 144
 48145            view.ShowChannelsLoading();
 48146            view.SetSearchAndCreateContainerActive(true);
 147        }
 148        else
 149        {
 3150            view.HideChannelsLoading();
 3151            view.SetSearchAndCreateContainerActive(false);
 152        }
 153
 51154        view.SetChannelsPromoteLabelVisible(channelsFeatureFlagService.IsPromoteChannelsToastEnabled());
 51155    }
 156
 157    public void Dispose()
 158    {
 3159        view.OnClose -= HandleViewCloseRequest;
 3160        view.OnOpenPrivateChat -= OpenPrivateChat;
 3161        view.OnOpenPublicChat -= OpenPublicChat;
 3162        view.OnSearchChatRequested -= SearchChats;
 3163        view.OnRequireMorePrivateChats -= ShowMorePrivateChats;
 3164        view.OnOpenChannelSearch -= OpenChannelSearch;
 3165        view.OnCreateChannel -= OpenChannelCreationWindow;
 3166        view.OnSignUp -= SignUp;
 3167        view.OnRequireWalletReadme -= OpenWalletReadme;
 3168        view.Dispose();
 3169        chatController.OnInitialized -= HandleChatInitialization;
 3170        chatController.OnAddMessage -= HandleMessageAdded;
 3171        chatController.OnChannelUpdated -= HandleChannelUpdated;
 3172        chatController.OnChannelJoined -= HandleChannelJoined;
 3173        chatController.OnAutoChannelJoined -= HandleAutoChannelJoined;
 3174        chatController.OnJoinChannelError -= HandleJoinChannelError;
 3175        chatController.OnChannelLeaveError -= HandleLeaveChannelError;
 3176        chatController.OnChannelLeft -= HandleChannelLeft;
 3177        chatController.OnAskForJoinChannel -= HandleAskForJoinChannel;
 3178        friendsController.OnAddFriendsWithDirectMessages -= HandleFriendsWithDirectMessagesAdded;
 3179        friendsController.OnUpdateUserStatus -= HandleUserStatusChanged;
 3180        friendsController.OnUpdateFriendship -= HandleFriendshipUpdated;
 3181        friendsController.OnInitialized -= HandleFriendsControllerInitialization;
 3182        dataStore.channels.channelToBeOpened.OnChange -= HandleChannelOpened;
 3183        rendererState.OnChange -= HandleAskForJoinChannelAfterRendererState;
 184
 3185        if (ownUserProfile != null)
 2186            ownUserProfile.OnUpdate -= OnUserProfileUpdate;
 187
 3188        hideChannelsLoadingCancellationToken?.Cancel();
 3189        hideChannelsLoadingCancellationToken?.Dispose();
 3190        reloadingChannelsInfoCancellationToken.Cancel();
 3191        reloadingChannelsInfoCancellationToken.Dispose();
 192
 3193        channelsFeatureFlagService.OnAllowedToCreateChannelsChanged -= OnAllowedToCreateChannelsChanged;
 3194    }
 195
 196    public void SetVisibility(bool visible)
 197    {
 21198        SetVisiblePanelList(visible);
 199
 21200        if (visible)
 201        {
 16202            view.Show();
 16203            OnOpen?.Invoke();
 204
 16205            if (friendsController.IsInitialized && !areDMsRequestedByFirstTime)
 206            {
 12207                RequestFriendsWithDirectMessages();
 12208                RequestUnreadMessages();
 209            }
 210
 16211            if (channelsFeatureFlagService.IsChannelsFeatureEnabled())
 212            {
 14213                if (chatController.IsInitialized)
 214                {
 13215                    RequestJoinedChannels();
 13216                    SetAutomaticChannelsInfoUpdatingActive(true);
 217                }
 1218                else if (ownUserProfile.isGuest)
 219                {
 220                    // TODO: channels are not allowed for guests. When we support it in the future, remove this call
 1221                    view.HideChannelsLoading();
 222                }
 223
 14224                if (!areUnseenMessajesRequestedByFirstTime)
 14225                    RequestUnreadChannelsMessages();
 226            }
 227
 16228            if (ownUserProfile?.isGuest ?? false)
 2229                view.ShowConnectWallet();
 230            else
 14231                view.HideConnectWallet();
 232        }
 233        else
 234        {
 5235            view.DisableSearchMode();
 5236            view.Hide();
 5237            SetAutomaticChannelsInfoUpdatingActive(false);
 238        }
 5239    }
 240
 241    private void OpenChannelCreationWindow()
 242    {
 0243        dataStore.channels.channelJoinedSource.Set(ChannelJoinedSource.ConversationList);
 0244        OnOpenChannelCreation?.Invoke();
 0245    }
 246
 247    private void SetVisiblePanelList(bool visible)
 248    {
 21249        HashSet<string> newSet = visibleTaskbarPanels.Get();
 21250        if (visible)
 16251            newSet.Add("WorldChatPanel");
 252        else
 5253            newSet.Remove("WorldChatPanel");
 254
 21255        visibleTaskbarPanels.Set(newSet, true);
 21256    }
 257
 258    private void LeaveChannel(string channelId)
 259    {
 1260        dataStore.channels.channelLeaveSource.Set(ChannelLeaveSource.ConversationList);
 1261        OnOpenChannelLeave?.Invoke(channelId);
 1262    }
 263
 264    private void RequestJoinedChannels()
 265    {
 15266        if ((DateTime.UtcNow - channelsRequestTimestamp).TotalSeconds < 3) return;
 267
 268        // skip=0: we do not support pagination for channels, it is supposed that a user can have a limited amount of jo
 15269        chatController.GetJoinedChannels(CHANNELS_PAGE_SIZE, 0);
 15270        channelsRequestTimestamp = DateTime.UtcNow;
 271
 15272        hideChannelsLoadingCancellationToken?.Cancel();
 15273        hideChannelsLoadingCancellationToken = new CancellationTokenSource();
 15274        WaitThenHideChannelsLoading(hideChannelsLoadingCancellationToken.Token).Forget();
 15275    }
 276
 277    private async UniTask WaitThenHideChannelsLoading(CancellationToken cancellationToken)
 278    {
 30279        await UniTask.Delay(3000, cancellationToken: cancellationToken);
 0280        if (cancellationToken.IsCancellationRequested) return;
 0281        view.HideChannelsLoading();
 0282    }
 283
 284    private void HandleChatInitialization()
 285    {
 286        // we do request joined channels as soon as possible to be able to display messages correctly in the notificatio
 2287        RequestJoinedChannels();
 2288        ConnectToAutoJoinChannels();
 2289    }
 290
 291    private void ConnectToAutoJoinChannels()
 292    {
 2293        AutomaticJoinChannelList joinChannelList = channelsFeatureFlagService.GetAutoJoinChannelsList();
 3294        if (joinChannelList == null) return;
 1295        if (joinChannelList.automaticJoinChannelList == null) return;
 296
 4297        foreach (var channel in joinChannelList.automaticJoinChannelList)
 298        {
 1299            var channelId = channel.channelId;
 1300            if (string.IsNullOrEmpty(channelId)) continue;
 1301            autoJoinChannelList.Add(channelId);
 1302            chatController.JoinOrCreateChannel(channelId);
 1303            if (!channel.enableNotifications)
 1304                chatController.MuteChannel(channelId);
 305        }
 1306    }
 307
 308    private void HandleFriendsControllerInitialization()
 309    {
 3310        if (view.IsActive && !areDMsRequestedByFirstTime)
 311        {
 2312            RequestFriendsWithDirectMessages();
 2313            RequestUnreadMessages();
 314        }
 315        else
 1316            view.HidePrivateChatsLoading();
 1317    }
 318
 319    private void OpenPrivateChat(string userId)
 320    {
 1321        OnOpenPrivateChat?.Invoke(userId);
 1322    }
 323
 324    private void OpenPublicChat(string channelId)
 325    {
 4326        if (channelId == ChatUtils.NEARBY_CHANNEL_ID)
 1327            OnOpenPublicChat?.Invoke(channelId);
 328        else
 3329            OnOpenChannel?.Invoke(channelId);
 1330    }
 331
 332    private void HandleViewCloseRequest()
 333    {
 1334        OnCloseView?.Invoke();
 1335        SetVisibility(false);
 1336    }
 337
 338    private void HandleFriendshipUpdated(string userId, FriendshipAction friendship)
 339    {
 6340        if (friendship != FriendshipAction.APPROVED)
 341        {
 342            // show only private chats from friends. Change it whenever the catalyst supports to send pms to any user
 6343            view.RemovePrivateChat(userId);
 344        }
 6345    }
 346
 347    private void HandleUserStatusChanged(string userId, UserStatus status)
 348    {
 4349        if (status.friendshipStatus != FriendshipStatus.FRIEND)
 350        {
 351            // show only private chats from friends. Change it whenever the catalyst supports to send pms to any user
 4352            view.RemovePrivateChat(userId);
 353        }
 354        else
 355        {
 0356            view.RefreshPrivateChatPresence(userId, status.presence == PresenceStatus.ONLINE);
 357        }
 0358    }
 359
 360    private void HandleMessageAdded(ChatMessage[] messages)
 361    {
 8362        foreach (var message in messages)
 363        {
 2364            if (message.messageType != ChatMessage.Type.PRIVATE) continue;
 365
 2366            var userId = ExtractRecipientId(message);
 367
 2368            if (lastPrivateMessages.ContainsKey(userId))
 369            {
 0370                if (message.timestamp > lastPrivateMessages[userId].timestamp)
 0371                    lastPrivateMessages[userId] = message;
 372            }
 373            else
 2374                lastPrivateMessages[userId] = message;
 375
 2376            var profile = userProfileBridge.Get(userId);
 2377            if (profile == null) return;
 378
 2379            view.SetPrivateChat(CreatePrivateChatModel(message, profile));
 380        }
 2381    }
 382
 383    private void HandleFriendsWithDirectMessagesAdded(List<FriendWithDirectMessages> usersWithDM)
 384    {
 12385        for (var i = 0; i < usersWithDM.Count; i++)
 386        {
 3387            var profile = userProfileBridge.Get(usersWithDM[i].userId);
 3388            if (profile == null) continue;
 389
 0390            var lastMessage = new ChatMessage
 391            {
 392                messageType = ChatMessage.Type.PRIVATE,
 393                body = usersWithDM[i].lastMessageBody,
 394                timestamp = (ulong) usersWithDM[i].lastMessageTimestamp
 395            };
 396
 0397            if (lastPrivateMessages.ContainsKey(profile.userId))
 398            {
 0399                if (lastMessage.timestamp > lastPrivateMessages[profile.userId].timestamp)
 0400                    lastPrivateMessages[profile.userId] = lastMessage;
 401            }
 402            else
 0403                lastPrivateMessages[profile.userId] = lastMessage;
 404
 0405            view.SetPrivateChat(CreatePrivateChatModel(lastMessage, profile));
 406        }
 407
 3408        UpdateMoreChannelsToLoadHint();
 3409        view.HidePrivateChatsLoading();
 3410        view.HideSearchLoading();
 411
 3412        isRequestingDMs = false;
 3413    }
 414
 415    private PrivateChatModel CreatePrivateChatModel(ChatMessage recentMessage, UserProfile profile)
 416    {
 2417        return new PrivateChatModel
 418        {
 419            user = profile,
 420            recentMessage = recentMessage,
 421            isBlocked = ownUserProfile.IsBlocked(profile.userId),
 422            isOnline = friendsController.GetUserStatus(profile.userId).presence == PresenceStatus.ONLINE
 423        };
 424    }
 425
 426    private string ExtractRecipientId(ChatMessage message)
 427    {
 2428        return message.sender != ownUserProfile.userId ? message.sender : message.recipient;
 429    }
 430
 431    private void OnUserProfileUpdate(UserProfile profile)
 432    {
 2433        view.RefreshBlockedDirectMessages(profile.blocked);
 434
 2435        if (profile.isGuest)
 1436            view.ShowConnectWallet();
 437        else
 1438            view.HideConnectWallet();
 1439    }
 440
 441    private void SearchChats(string search)
 442    {
 2443        currentSearch = search;
 444
 2445        if (string.IsNullOrEmpty(search))
 446        {
 1447            View.DisableSearchMode();
 1448            UpdateMoreChannelsToLoadHint();
 1449            return;
 450        }
 451
 1452        UpdateMoreChannelsToLoadHint();
 1453        View.EnableSearchMode();
 454
 1455        var matchedChannels = publicChannels.Values
 2456            .Where(model => model.name.ToLower().Contains(search.ToLower()));
 4457        foreach (var channelMatch in matchedChannels)
 1458            View.SetPublicChat(channelMatch);
 459
 1460        RequestFriendsWithDirectMessagesFromSearch(search, USER_DM_ENTRIES_TO_REQUEST_FOR_SEARCH);
 1461    }
 462
 463    private void ShowMorePrivateChats()
 464    {
 1465        if (isRequestingDMs ||
 466            hiddenDMs == 0 ||
 467            !string.IsNullOrEmpty(currentSearch))
 0468            return;
 469
 1470        RequestFriendsWithDirectMessages();
 1471    }
 472
 473    private void UpdateMoreChannelsToLoadHint()
 474    {
 5475        hiddenDMs = Mathf.Clamp(friendsController.TotalFriendsWithDirectMessagesCount - lastSkipForDMs,
 476            0,
 477            friendsController.TotalFriendsWithDirectMessagesCount);
 478
 5479        if (hiddenDMs <= 0 || !string.IsNullOrEmpty(currentSearch))
 3480            View.HideMoreChatsToLoadHint();
 481        else
 2482            View.ShowMoreChatsToLoadHint(hiddenDMs);
 2483    }
 484
 485    private void RequestFriendsWithDirectMessages()
 486    {
 15487        isRequestingDMs = true;
 488
 15489        if (!areDMsRequestedByFirstTime)
 490        {
 14491            view.ShowPrivateChatsLoading();
 14492            view.HideMoreChatsToLoadHint();
 493        }
 494
 15495        friendsController.GetFriendsWithDirectMessages(DMS_PAGE_SIZE, lastSkipForDMs);
 15496        lastSkipForDMs += DMS_PAGE_SIZE;
 15497        areDMsRequestedByFirstTime = true;
 498
 15499        hidePrivateChatsLoadingCancellationToken.Cancel();
 15500        hidePrivateChatsLoadingCancellationToken = new CancellationTokenSource();
 15501        HidePrivateChatsLoadingWhenTimeout(hidePrivateChatsLoadingCancellationToken.Token).Forget();
 15502    }
 503
 504    private async UniTaskVoid HidePrivateChatsLoadingWhenTimeout(CancellationToken cancellationToken)
 505    {
 31506        await UniTask.Delay(3000, cancellationToken: cancellationToken);
 0507        if (cancellationToken.IsCancellationRequested) return;
 0508        view.HidePrivateChatsLoading();
 0509    }
 510
 511    internal void RequestFriendsWithDirectMessagesFromSearch(string userNameOrId, int limit)
 512    {
 2513        view.ShowSearchLoading();
 2514        friendsController.GetFriendsWithDirectMessages(userNameOrId, limit);
 2515        hideLoadingCancellationToken?.Cancel();
 2516        hideLoadingCancellationToken = new CancellationTokenSource();
 2517        HideSearchLoadingWhenTimeout(hideLoadingCancellationToken.Token).Forget();
 2518    }
 519
 520    private void HandleChannelUpdated(Channel channel)
 521    {
 4522        if (!channel.Joined)
 523        {
 0524            view.RemovePublicChat(channel.ChannelId);
 0525            publicChannels.Remove(channel.ChannelId);
 0526            return;
 527        }
 528
 4529        var channelId = channel.ChannelId;
 4530        var model = new PublicChatModel(channelId, channel.Name, channel.Description, channel.Joined,
 531            channel.MemberCount, channel.Muted, showOnlyOnlineMembersOnPublicChannels);
 532
 4533        if (publicChannels.ContainsKey(channelId))
 1534            publicChannels[channelId].CopyFrom(model);
 535        else
 3536            publicChannels[channelId] = model;
 537
 4538        view.SetPublicChat(model);
 4539        view.HideChannelsLoading();
 540
 541        // we clear the unseen messages to avoid showing many of them while the user was offline
 542        // TODO: we should consider avoid clearing when the channel is private in the future
 4543        ClearOfflineUnseenMessages(channelId);
 4544    }
 545
 546    private void ClearOfflineUnseenMessages(string channelId)
 547    {
 5548        if (channelsClearedUnseenNotifications.Contains(channelId)) return;
 3549        chatController.MarkChannelMessagesAsSeen(channelId);
 3550        channelsClearedUnseenNotifications.Add(channelId);
 3551    }
 552
 1553    private void HandleAutoChannelJoined(Channel channel) => ReportChannelJoinedToAnalytics(channel, "auto");
 554
 555    private void HandleChannelJoined(Channel channel)
 556    {
 2557        ReportChannelJoinedToAnalytics(channel, "manual");
 2558        OpenPublicChat(channel.ChannelId);
 2559    }
 560
 561    private void ReportChannelJoinedToAnalytics(Channel channel, string method)
 562    {
 3563        if (channel.MemberCount <= 1)
 1564            socialAnalytics.SendEmptyChannelCreated(channel.Name, dataStore.channels.channelJoinedSource.Get());
 565        else
 2566            socialAnalytics.SendPopulatedChannelJoined(channel.Name, dataStore.channels.channelJoinedSource.Get(), metho
 2567    }
 568
 569    private void HandleJoinChannelError(string channelId, ChannelErrorCode errorCode)
 570    {
 0571        if (dataStore.channels.isCreationModalVisible.Get()) return;
 572
 573        switch (errorCode)
 574        {
 575            case ChannelErrorCode.LimitExceeded:
 0576                dataStore.channels.currentChannelLimitReached.Set(channelId, true);
 0577                break;
 578            case ChannelErrorCode.Unknown:
 0579                dataStore.channels.joinChannelError.Set(channelId, true);
 580                break;
 581        }
 0582    }
 583
 584    private void HandleLeaveChannelError(string channelId, ChannelErrorCode errorCode) =>
 0585        dataStore.channels.leaveChannelError.Set(channelId, true);
 586
 587    private void HandleChannelLeft(string channelId)
 588    {
 1589        publicChannels.Remove(channelId);
 1590        view.RemovePublicChat(channelId);
 1591        var channel = chatController.GetAllocatedChannel(channelId);
 1592        socialAnalytics.SendLeaveChannel(channel?.Name ?? channelId, dataStore.channels.channelLeaveSource.Get());
 1593    }
 594
 595    private void HandleChannelOpened(string channelId, string previousChannelId)
 596    {
 0597        if (string.IsNullOrEmpty(channelId))
 0598            return;
 599
 0600        OpenPublicChat(channelId);
 0601    }
 602
 603    private void HandleAskForJoinChannel(string channelName)
 604    {
 0605        chatController.OnAskForJoinChannel -= HandleAskForJoinChannel;
 606
 0607        if (!channelsFeatureFlagService.IsAllowedToCreateChannels())
 0608            return;
 609
 0610        if (rendererState.Get())
 0611            dataStore.channels.currentJoinChannelModal.Set(channelName, true);
 612        else
 613        {
 0614            channelToJoinAtTheBeginning = channelName;
 0615            rendererState.OnChange += HandleAskForJoinChannelAfterRendererState;
 616        }
 0617    }
 618
 619    private void HandleAskForJoinChannelAfterRendererState(bool current, bool _)
 620    {
 0621        if (!current)
 0622            return;
 623
 0624        rendererState.OnChange -= HandleAskForJoinChannelAfterRendererState;
 0625        dataStore.channels.currentJoinChannelModal.Set(channelToJoinAtTheBeginning, true);
 0626    }
 627
 14628    private void RequestUnreadMessages() => chatController.GetUnseenMessagesByUser();
 629
 630    private void RequestUnreadChannelsMessages()
 631    {
 14632        chatController.GetUnseenMessagesByChannel();
 14633        areUnseenMessajesRequestedByFirstTime = true;
 14634    }
 635
 636    private void OpenChannelSearch()
 637    {
 0638        if (!ownUserProfile.isGuest)
 0639            OnOpenChannelSearch?.Invoke();
 640        else
 0641            dataStore.HUDs.connectWalletModalVisible.Set(true);
 0642    }
 643
 644    private async UniTask HideSearchLoadingWhenTimeout(CancellationToken cancellationToken)
 645    {
 4646        await UniTask.Delay(3000, cancellationToken: cancellationToken);
 0647        if (cancellationToken.IsCancellationRequested) return;
 0648        view.HideSearchLoading();
 0649    }
 650
 0651    private void OnAllowedToCreateChannelsChanged(bool isAllowed) => view.SetCreateChannelButtonActive(isAllowed);
 652
 653    private void SetAutomaticChannelsInfoUpdatingActive(bool isActive)
 654    {
 18655        reloadingChannelsInfoCancellationToken.Cancel();
 656
 18657        if (isActive)
 658        {
 13659            GetCurrentChannelsInfo();
 13660            reloadingChannelsInfoCancellationToken = new CancellationTokenSource();
 13661            ReloadChannelsInfoPeriodically(reloadingChannelsInfoCancellationToken.Token).Forget();
 662        }
 18663    }
 664
 665    private async UniTask ReloadChannelsInfoPeriodically(CancellationToken cancellationToken)
 666    {
 0667        while (true)
 668        {
 26669            await UniTask.Delay(MINUTES_FOR_AUTOMATIC_CHANNELS_INFO_RELOADING * 60 * 1000,
 670                cancellationToken: cancellationToken);
 671
 0672            if (cancellationToken.IsCancellationRequested)
 0673                return;
 674
 0675            GetCurrentChannelsInfo();
 676        }
 0677    }
 678
 679    private void GetCurrentChannelsInfo()
 680    {
 13681        chatController.GetChannelInfo(publicChannels
 13682            .Select(x => x.Key)
 13683            .Where(x => x != ChatUtils.NEARBY_CHANNEL_ID)
 684            .ToArray());
 13685    }
 686
 687    private void OpenWalletReadme() =>
 1688        browserBridge.OpenUrl("https://docs.decentraland.org/player/blockchain-integration/get-a-wallet/");
 689
 1690    private void SignUp() => userProfileBridge.SignUp();
 691}

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)
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()