< Summary

Class:WorldChatWindowController
Assembly:WorldChatWindowHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/WorldChatWindowHUD/WorldChatWindowController.cs
Covered lines:264
Uncovered lines:56
Coverable lines:320
Total lines:638
Line coverage:82.5% (264 of 320)
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%11.0111095.24%
OpenChannelCreationWindow()0%6200%
SetVisiblePanelList(...)0%220100%
LeaveChannel(...)0%220100%
RequestJoinedChannels()0%3.013088.89%
WaitThenHideChannelsLoading()0%9.834028.57%
HandleChatInitialization()0%2.032080%
ConnectToAutoJoinChannels()0%6.016093.33%
HandleFriendsControllerInitialization()0%330100%
OpenPrivateChat(...)0%220100%
OpenPublicChat(...)0%440100%
HandleViewCloseRequest()0%220100%
HandleFriendshipUpdated(...)0%220100%
HandleUserStatusChanged(...)0%220100%
HandleMessageAdded(...)0%6.425061.54%
HandleFriendsWithDirectMessagesAdded(...)0%6.755058.82%
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%
HandleChannelJoined(...)0%220100%
HandleJoinChannelError(...)0%20400%
HandleLeaveChannelError(...)0%2100%
HandleChannelLeft(...)0%440100%
HandleChannelOpenedFromLink(...)0%6200%
RequestUnreadMessages()0%110100%
RequestUnreadChannelsMessages()0%110100%
OpenChannelSearch()0%12300%
HideSearchLoadingWhenTimeout()0%9.834028.57%
OnAllowedToCreateChannelsChanged(...)0%2100%
SetAutomaticChannelsInfoUpdatingActive(...)0%2.52050%
ReloadChannelsInfoPeriodically()0%20400%
GetCurrentChannelsInfo()0%12300%
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.Friends.WebApi;
 6using DCL.Interface;
 7using SocialFeaturesAnalytics;
 8using System;
 9using System.Collections.Generic;
 10using System.Linq;
 11using System.Threading;
 12using DCL.Browser;
 13using UnityEngine;
 14using Channel = DCL.Chat.Channels.Channel;
 15
 16public class WorldChatWindowController : IHUD
 17{
 18    private const int DMS_PAGE_SIZE = 30;
 19    private const int USER_DM_ENTRIES_TO_REQUEST_FOR_SEARCH = 20;
 20    private const int CHANNELS_PAGE_SIZE = 10;
 21    private const int MINUTES_FOR_AUTOMATIC_CHANNELS_INFO_RELOADING = 1;
 22
 23    private readonly IUserProfileBridge userProfileBridge;
 24    private readonly IFriendsController friendsController;
 25    private readonly IChatController chatController;
 26    private readonly DataStore dataStore;
 27    private readonly IMouseCatcher mouseCatcher;
 28    private readonly ISocialAnalytics socialAnalytics;
 29    private readonly IChannelsFeatureFlagService channelsFeatureFlagService;
 30    private readonly IBrowserBridge browserBridge;
 4931    private readonly Dictionary<string, PublicChatModel> publicChannels = new Dictionary<string, PublicChatModel>();
 4932    private readonly Dictionary<string, ChatMessage> lastPrivateMessages = new Dictionary<string, ChatMessage>();
 4933    private readonly HashSet<string> channelsClearedUnseenNotifications = new HashSet<string>();
 134    private BaseVariable<HashSet<string>> autoJoinChannelList => dataStore.HUDs.autoJoinChannelList;
 4035    private BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels;
 36    private int hiddenDMs;
 4937    private string currentSearch = "";
 38    private DateTime channelsRequestTimestamp;
 39    private bool areDMsRequestedByFirstTime;
 40    private int lastSkipForDMs;
 4941    private CancellationTokenSource hideLoadingCancellationToken = new CancellationTokenSource();
 42    private IWorldChatWindowView view;
 43    private UserProfile ownUserProfile;
 44    private bool isRequestingDMs;
 45    private bool areJoinedChannelsRequestedByFirstTime;
 46    private bool areUnseenMessajesRequestedByFirstTime;
 4947    private CancellationTokenSource hideChannelsLoadingCancellationToken = new CancellationTokenSource();
 4948    private CancellationTokenSource hidePrivateChatsLoadingCancellationToken = new CancellationTokenSource();
 4949    private CancellationTokenSource reloadingChannelsInfoCancellationToken = new CancellationTokenSource();
 50
 051    public IWorldChatWindowView View => view;
 52
 53    public event Action OnCloseView;
 54    public event Action<string> OnOpenPrivateChat;
 55    public event Action<string> OnOpenPublicChat;
 56    public event Action<string> OnOpenChannel;
 57    public event Action OnOpen;
 58    public event Action OnOpenChannelSearch;
 59    public event Action OnOpenChannelCreation;
 60    public event Action<string> OnOpenChannelLeave;
 61
 4962    public WorldChatWindowController(
 63        IUserProfileBridge userProfileBridge,
 64        IFriendsController friendsController,
 65        IChatController chatController,
 66        DataStore dataStore,
 67        IMouseCatcher mouseCatcher,
 68        ISocialAnalytics socialAnalytics,
 69        IChannelsFeatureFlagService channelsFeatureFlagService,
 70        IBrowserBridge browserBridge)
 71    {
 4972        this.userProfileBridge = userProfileBridge;
 4973        this.friendsController = friendsController;
 4974        this.chatController = chatController;
 4975        this.dataStore = dataStore;
 4976        this.mouseCatcher = mouseCatcher;
 4977        this.socialAnalytics = socialAnalytics;
 4978        this.channelsFeatureFlagService = channelsFeatureFlagService;
 4979        this.browserBridge = browserBridge;
 4980    }
 81
 82    public void Initialize(IWorldChatWindowView view)
 83    {
 4984        this.view = view;
 4985        view.Initialize(chatController);
 86
 4987        if (mouseCatcher != null)
 4988            mouseCatcher.OnMouseLock += HandleViewCloseRequest;
 89
 4990        view.OnClose += HandleViewCloseRequest;
 4991        view.OnOpenPrivateChat += OpenPrivateChat;
 4992        view.OnOpenPublicChat += OpenPublicChat;
 4993        view.OnSearchChatRequested += SearchChats;
 4994        view.OnRequireMorePrivateChats += ShowMorePrivateChats;
 4995        view.OnSignUp += SignUp;
 4996        view.OnRequireWalletReadme += OpenWalletReadme;
 97
 4998        ownUserProfile = userProfileBridge.GetOwn();
 4999        if (ownUserProfile != null)
 48100            ownUserProfile.OnUpdate += OnUserProfileUpdate;
 101
 49102        var channel = chatController.GetAllocatedChannel(ChatUtils.NEARBY_CHANNEL_ID);
 49103        publicChannels[ChatUtils.NEARBY_CHANNEL_ID] = new PublicChatModel(ChatUtils.NEARBY_CHANNEL_ID, channel.Name,
 104            channel.Description,
 105            channel.Joined,
 106            channel.MemberCount,
 107            false);
 49108        view.SetPublicChat(publicChannels[ChatUtils.NEARBY_CHANNEL_ID]);
 109
 49110        if (!friendsController.IsInitialized)
 7111            if (ownUserProfile?.hasConnectedWeb3 ?? false)
 5112                view.ShowPrivateChatsLoading();
 113
 49114        chatController.OnInitialized += HandleChatInitialization;
 49115        chatController.OnAddMessage += HandleMessageAdded;
 49116        friendsController.OnAddFriendsWithDirectMessages += HandleFriendsWithDirectMessagesAdded;
 49117        friendsController.OnUpdateUserStatus += HandleUserStatusChanged;
 49118        friendsController.OnUpdateFriendship += HandleFriendshipUpdated;
 49119        friendsController.OnInitialized += HandleFriendsControllerInitialization;
 120
 49121        if (channelsFeatureFlagService.IsChannelsFeatureEnabled())
 122        {
 46123            channelsFeatureFlagService.OnAllowedToCreateChannelsChanged += OnAllowedToCreateChannelsChanged;
 124
 46125            view.OnOpenChannelSearch += OpenChannelSearch;
 46126            view.OnLeaveChannel += LeaveChannel;
 46127            view.OnCreateChannel += OpenChannelCreationWindow;
 128
 46129            chatController.OnChannelUpdated += HandleChannelUpdated;
 46130            chatController.OnChannelJoined += HandleChannelJoined;
 46131            chatController.OnJoinChannelError += HandleJoinChannelError;
 46132            chatController.OnChannelLeaveError += HandleLeaveChannelError;
 46133            chatController.OnChannelLeft += HandleChannelLeft;
 46134            dataStore.channels.channelToBeOpenedFromLink.OnChange += HandleChannelOpenedFromLink;
 135
 46136            view.ShowChannelsLoading();
 46137            view.SetSearchAndCreateContainerActive(true);
 46138        }
 139        else
 140        {
 3141            view.HideChannelsLoading();
 3142            view.SetSearchAndCreateContainerActive(false);
 143        }
 144
 49145        view.SetChannelsPromoteLabelVisible(channelsFeatureFlagService.IsPromoteChannelsToastEnabled());
 49146    }
 147
 148    public void Dispose()
 149    {
 3150        view.OnClose -= HandleViewCloseRequest;
 3151        view.OnOpenPrivateChat -= OpenPrivateChat;
 3152        view.OnOpenPublicChat -= OpenPublicChat;
 3153        view.OnSearchChatRequested -= SearchChats;
 3154        view.OnRequireMorePrivateChats -= ShowMorePrivateChats;
 3155        view.OnOpenChannelSearch -= OpenChannelSearch;
 3156        view.OnCreateChannel -= OpenChannelCreationWindow;
 3157        view.OnSignUp -= SignUp;
 3158        view.OnRequireWalletReadme -= OpenWalletReadme;
 3159        view.Dispose();
 3160        chatController.OnInitialized -= HandleChatInitialization;
 3161        chatController.OnAddMessage -= HandleMessageAdded;
 3162        chatController.OnChannelUpdated -= HandleChannelUpdated;
 3163        chatController.OnChannelJoined -= HandleChannelJoined;
 3164        chatController.OnJoinChannelError -= HandleJoinChannelError;
 3165        chatController.OnChannelLeaveError += HandleLeaveChannelError;
 3166        chatController.OnChannelLeft -= HandleChannelLeft;
 3167        friendsController.OnAddFriendsWithDirectMessages -= HandleFriendsWithDirectMessagesAdded;
 3168        friendsController.OnUpdateUserStatus -= HandleUserStatusChanged;
 3169        friendsController.OnUpdateFriendship -= HandleFriendshipUpdated;
 3170        friendsController.OnInitialized -= HandleFriendsControllerInitialization;
 3171        dataStore.channels.channelToBeOpenedFromLink.OnChange -= HandleChannelOpenedFromLink;
 172
 3173        if (ownUserProfile != null)
 2174            ownUserProfile.OnUpdate -= OnUserProfileUpdate;
 175
 3176        hideChannelsLoadingCancellationToken?.Cancel();
 3177        hideChannelsLoadingCancellationToken?.Dispose();
 3178        reloadingChannelsInfoCancellationToken.Cancel();
 3179        reloadingChannelsInfoCancellationToken.Dispose();
 180
 3181        channelsFeatureFlagService.OnAllowedToCreateChannelsChanged -= OnAllowedToCreateChannelsChanged;
 3182    }
 183
 184    public void SetVisibility(bool visible)
 185    {
 20186        SetVisiblePanelList(visible);
 187
 20188        if (visible)
 189        {
 15190            view.Show();
 15191            OnOpen?.Invoke();
 192
 15193            if (friendsController.IsInitialized && !areDMsRequestedByFirstTime)
 194            {
 11195                RequestFriendsWithDirectMessages();
 11196                RequestUnreadMessages();
 197            }
 198
 15199            if (channelsFeatureFlagService.IsChannelsFeatureEnabled())
 200            {
 13201                if (!areJoinedChannelsRequestedByFirstTime)
 13202                    RequestJoinedChannels();
 203                else
 0204                    SetAutomaticChannelsInfoUpdatingActive(true);
 205
 13206                if (!areUnseenMessajesRequestedByFirstTime)
 13207                    RequestUnreadChannelsMessages();
 208            }
 209
 15210            if (ownUserProfile?.isGuest ?? false)
 1211                view.ShowConnectWallet();
 212            else
 14213                view.HideConnectWallet();
 14214        }
 215        else
 216        {
 5217            view.DisableSearchMode();
 5218            view.Hide();
 5219            SetAutomaticChannelsInfoUpdatingActive(false);
 220        }
 5221    }
 222
 223    private void OpenChannelCreationWindow()
 224    {
 0225        dataStore.channels.channelJoinedSource.Set(ChannelJoinedSource.ConversationList);
 0226        OnOpenChannelCreation?.Invoke();
 0227    }
 228
 229    private void SetVisiblePanelList(bool visible)
 230    {
 20231        HashSet<string> newSet = visibleTaskbarPanels.Get();
 20232        if (visible)
 15233            newSet.Add("WorldChatPanel");
 234        else
 5235            newSet.Remove("WorldChatPanel");
 236
 20237        visibleTaskbarPanels.Set(newSet, true);
 20238    }
 239
 240    private void LeaveChannel(string channelId)
 241    {
 1242        dataStore.channels.channelLeaveSource.Set(ChannelLeaveSource.ConversationList);
 1243        OnOpenChannelLeave?.Invoke(channelId);
 1244    }
 245
 246    private void RequestJoinedChannels()
 247    {
 15248        if ((DateTime.UtcNow - channelsRequestTimestamp).TotalSeconds < 3) return;
 249
 250        // skip=0: we do not support pagination for channels, it is supposed that a user can have a limited amount of jo
 15251        chatController.GetJoinedChannels(CHANNELS_PAGE_SIZE, 0);
 15252        channelsRequestTimestamp = DateTime.UtcNow;
 253
 15254        areJoinedChannelsRequestedByFirstTime = true;
 255
 15256        hideChannelsLoadingCancellationToken?.Cancel();
 15257        hideChannelsLoadingCancellationToken = new CancellationTokenSource();
 15258        WaitThenHideChannelsLoading(hideChannelsLoadingCancellationToken.Token).Forget();
 15259    }
 260
 261    private async UniTask WaitThenHideChannelsLoading(CancellationToken cancellationToken)
 262    {
 30263        await UniTask.Delay(3000, cancellationToken: cancellationToken);
 0264        if (cancellationToken.IsCancellationRequested) return;
 0265        view.HideChannelsLoading();
 0266    }
 267
 268    private void HandleChatInitialization()
 269    {
 2270        if (areJoinedChannelsRequestedByFirstTime) return;
 271        // we do request joined channels as soon as possible to be able to display messages correctly in the notificatio
 2272        RequestJoinedChannels();
 2273        ConnectToAutoJoinChannels();
 2274    }
 275
 276    private void ConnectToAutoJoinChannels()
 277    {
 2278        AutomaticJoinChannelList joinChannelList = channelsFeatureFlagService.GetAutoJoinChannelsList();
 3279        if (joinChannelList == null) return;
 1280        if (joinChannelList.automaticJoinChannelList == null) return;
 281
 4282        foreach (var channel in joinChannelList.automaticJoinChannelList)
 283        {
 1284            var channelId = channel.channelId;
 1285            if (string.IsNullOrEmpty(channelId)) continue;
 1286            autoJoinChannelList.Get().Add(channelId);
 1287            chatController.JoinOrCreateChannel(channelId);
 1288            if (!channel.enableNotifications)
 1289                chatController.MuteChannel(channelId);
 290        }
 1291    }
 292
 293    private void HandleFriendsControllerInitialization()
 294    {
 3295        if (view.IsActive && !areDMsRequestedByFirstTime)
 296        {
 2297            RequestFriendsWithDirectMessages();
 2298            RequestUnreadMessages();
 2299        }
 300        else
 1301            view.HidePrivateChatsLoading();
 1302    }
 303
 304    private void OpenPrivateChat(string userId)
 305    {
 1306        OnOpenPrivateChat?.Invoke(userId);
 1307    }
 308
 309    private void OpenPublicChat(string channelId)
 310    {
 4311        if (channelId == ChatUtils.NEARBY_CHANNEL_ID)
 1312            OnOpenPublicChat?.Invoke(channelId);
 313        else
 3314            OnOpenChannel?.Invoke(channelId);
 1315    }
 316
 317    private void HandleViewCloseRequest()
 318    {
 1319        OnCloseView?.Invoke();
 1320        SetVisibility(false);
 1321    }
 322
 323    private void HandleFriendshipUpdated(string userId, FriendshipAction friendship)
 324    {
 6325        if (friendship != FriendshipAction.APPROVED)
 326        {
 327            // show only private chats from friends. Change it whenever the catalyst supports to send pms to any user
 6328            view.RemovePrivateChat(userId);
 329        }
 6330    }
 331
 332    private void HandleUserStatusChanged(string userId, UserStatus status)
 333    {
 4334        if (status.friendshipStatus != FriendshipStatus.FRIEND)
 335        {
 336            // show only private chats from friends. Change it whenever the catalyst supports to send pms to any user
 4337            view.RemovePrivateChat(userId);
 338        }
 4339    }
 340
 341    private void HandleMessageAdded(ChatMessage message)
 342    {
 2343        if (message.messageType != ChatMessage.Type.PRIVATE) return;
 344
 2345        var userId = ExtractRecipientId(message);
 346
 2347        if (lastPrivateMessages.ContainsKey(userId))
 348        {
 0349            if (message.timestamp > lastPrivateMessages[userId].timestamp)
 0350                lastPrivateMessages[userId] = message;
 0351        }
 352        else
 2353            lastPrivateMessages[userId] = message;
 354
 2355        var profile = userProfileBridge.Get(userId);
 2356        if (profile == null) return;
 357
 2358        view.SetPrivateChat(CreatePrivateChatModel(message, profile));
 2359    }
 360
 361    private void HandleFriendsWithDirectMessagesAdded(List<FriendWithDirectMessages> usersWithDM)
 362    {
 12363        for (var i = 0; i < usersWithDM.Count; i++)
 364        {
 3365            var profile = userProfileBridge.Get(usersWithDM[i].userId);
 3366            if (profile == null) continue;
 367
 0368            var lastMessage = new ChatMessage
 369            {
 370                messageType = ChatMessage.Type.PRIVATE,
 371                body = usersWithDM[i].lastMessageBody,
 372                timestamp = (ulong) usersWithDM[i].lastMessageTimestamp
 373            };
 374
 0375            if (lastPrivateMessages.ContainsKey(profile.userId))
 376            {
 0377                if (lastMessage.timestamp > lastPrivateMessages[profile.userId].timestamp)
 0378                    lastPrivateMessages[profile.userId] = lastMessage;
 0379            }
 380            else
 0381                lastPrivateMessages[profile.userId] = lastMessage;
 382
 0383            view.SetPrivateChat(CreatePrivateChatModel(lastMessage, profile));
 384        }
 385
 3386        UpdateMoreChannelsToLoadHint();
 3387        view.HidePrivateChatsLoading();
 3388        view.HideSearchLoading();
 389
 3390        isRequestingDMs = false;
 3391    }
 392
 393    private PrivateChatModel CreatePrivateChatModel(ChatMessage recentMessage, UserProfile profile)
 394    {
 2395        return new PrivateChatModel
 396        {
 397            user = profile,
 398            recentMessage = recentMessage,
 399            isBlocked = ownUserProfile.IsBlocked(profile.userId),
 400            isOnline = friendsController.GetUserStatus(profile.userId).presence == PresenceStatus.ONLINE
 401        };
 402    }
 403
 404    private string ExtractRecipientId(ChatMessage message)
 405    {
 2406        return message.sender != ownUserProfile.userId ? message.sender : message.recipient;
 407    }
 408
 409    private void OnUserProfileUpdate(UserProfile profile)
 410    {
 2411        view.RefreshBlockedDirectMessages(profile.blocked);
 412
 2413        if (profile.isGuest)
 1414            view.ShowConnectWallet();
 415        else
 1416            view.HideConnectWallet();
 1417    }
 418
 419    private void SearchChats(string search)
 420    {
 2421        currentSearch = search;
 422
 2423        if (string.IsNullOrEmpty(search))
 424        {
 1425            View.DisableSearchMode();
 1426            UpdateMoreChannelsToLoadHint();
 1427            return;
 428        }
 429
 1430        UpdateMoreChannelsToLoadHint();
 1431        View.EnableSearchMode();
 432
 1433        var matchedChannels = publicChannels.Values
 2434            .Where(model => model.name.ToLower().Contains(search.ToLower()));
 4435        foreach (var channelMatch in matchedChannels)
 1436            View.SetPublicChat(channelMatch);
 437
 1438        RequestFriendsWithDirectMessagesFromSearch(search, USER_DM_ENTRIES_TO_REQUEST_FOR_SEARCH);
 1439    }
 440
 441    private void ShowMorePrivateChats()
 442    {
 1443        if (isRequestingDMs ||
 444            hiddenDMs == 0 ||
 445            !string.IsNullOrEmpty(currentSearch))
 0446            return;
 447
 1448        RequestFriendsWithDirectMessages();
 1449    }
 450
 451    private void UpdateMoreChannelsToLoadHint()
 452    {
 5453        hiddenDMs = Mathf.Clamp(friendsController.TotalFriendsWithDirectMessagesCount - lastSkipForDMs,
 454            0,
 455            friendsController.TotalFriendsWithDirectMessagesCount);
 456
 5457        if (hiddenDMs <= 0 || !string.IsNullOrEmpty(currentSearch))
 3458            View.HideMoreChatsToLoadHint();
 459        else
 2460            View.ShowMoreChatsToLoadHint(hiddenDMs);
 2461    }
 462
 463    private void RequestFriendsWithDirectMessages()
 464    {
 14465        isRequestingDMs = true;
 466
 14467        if (!areDMsRequestedByFirstTime)
 468        {
 13469            view.ShowPrivateChatsLoading();
 13470            view.HideMoreChatsToLoadHint();
 471        }
 472
 14473        friendsController.GetFriendsWithDirectMessages(DMS_PAGE_SIZE, lastSkipForDMs);
 14474        lastSkipForDMs += DMS_PAGE_SIZE;
 14475        areDMsRequestedByFirstTime = true;
 476
 14477        hidePrivateChatsLoadingCancellationToken.Cancel();
 14478        hidePrivateChatsLoadingCancellationToken = new CancellationTokenSource();
 14479        HidePrivateChatsLoadingWhenTimeout(hidePrivateChatsLoadingCancellationToken.Token).Forget();
 14480    }
 481
 482    private async UniTaskVoid HidePrivateChatsLoadingWhenTimeout(CancellationToken cancellationToken)
 483    {
 29484        await UniTask.Delay(3000, cancellationToken: cancellationToken);
 0485        if (cancellationToken.IsCancellationRequested) return;
 0486        view.HidePrivateChatsLoading();
 0487    }
 488
 489    internal void RequestFriendsWithDirectMessagesFromSearch(string userNameOrId, int limit)
 490    {
 2491        view.ShowSearchLoading();
 2492        friendsController.GetFriendsWithDirectMessages(userNameOrId, limit);
 2493        hideLoadingCancellationToken?.Cancel();
 2494        hideLoadingCancellationToken = new CancellationTokenSource();
 2495        HideSearchLoadingWhenTimeout(hideLoadingCancellationToken.Token).Forget();
 2496    }
 497
 498    private void HandleChannelUpdated(Channel channel)
 499    {
 4500        if (!channel.Joined)
 501        {
 0502            view.RemovePublicChat(channel.ChannelId);
 0503            publicChannels.Remove(channel.ChannelId);
 0504            return;
 505        }
 506
 4507        var channelId = channel.ChannelId;
 4508        var model = new PublicChatModel(channelId, channel.Name, channel.Description, channel.Joined,
 509            channel.MemberCount, channel.Muted);
 510
 4511        if (publicChannels.ContainsKey(channelId))
 1512            publicChannels[channelId].CopyFrom(model);
 513        else
 3514            publicChannels[channelId] = model;
 515
 4516        view.SetPublicChat(model);
 4517        view.HideChannelsLoading();
 518
 519        // we clear the unseen messages to avoid showing many of them while the user was offline
 520        // TODO: we should consider avoid clearing when the channel is private in the future
 4521        ClearOfflineUnseenMessages(channelId);
 4522    }
 523
 524    private void ClearOfflineUnseenMessages(string channelId)
 525    {
 5526        if (channelsClearedUnseenNotifications.Contains(channelId)) return;
 3527        chatController.MarkChannelMessagesAsSeen(channelId);
 3528        channelsClearedUnseenNotifications.Add(channelId);
 3529    }
 530
 531    private void HandleChannelJoined(Channel channel)
 532    {
 2533        if (channel.MemberCount <= 1)
 1534            socialAnalytics.SendEmptyChannelCreated(channel.Name, dataStore.channels.channelJoinedSource.Get());
 535        else
 1536            socialAnalytics.SendPopulatedChannelJoined(channel.Name, dataStore.channels.channelJoinedSource.Get());
 537
 2538        OpenPublicChat(channel.ChannelId);
 2539    }
 540
 541    private void HandleJoinChannelError(string channelId, ChannelErrorCode errorCode)
 542    {
 0543        if (dataStore.channels.isCreationModalVisible.Get()) return;
 544
 545        switch (errorCode)
 546        {
 547            case ChannelErrorCode.LimitExceeded:
 0548                dataStore.channels.currentChannelLimitReached.Set(channelId, true);
 0549                break;
 550            case ChannelErrorCode.Unknown:
 0551                dataStore.channels.joinChannelError.Set(channelId, true);
 552                break;
 553        }
 0554    }
 555
 556    private void HandleLeaveChannelError(string channelId, ChannelErrorCode errorCode) =>
 0557        dataStore.channels.leaveChannelError.Set(channelId, true);
 558
 559    private void HandleChannelLeft(string channelId)
 560    {
 1561        publicChannels.Remove(channelId);
 1562        view.RemovePublicChat(channelId);
 1563        var channel = chatController.GetAllocatedChannel(channelId);
 1564        socialAnalytics.SendLeaveChannel(channel?.Name ?? channelId, dataStore.channels.channelLeaveSource.Get());
 1565    }
 566
 567    private void HandleChannelOpenedFromLink(string channelId, string previousChannelId)
 568    {
 0569        if (string.IsNullOrEmpty(channelId))
 0570            return;
 571
 0572        OpenPublicChat(channelId);
 0573    }
 574
 13575    private void RequestUnreadMessages() => chatController.GetUnseenMessagesByUser();
 576
 577    private void RequestUnreadChannelsMessages()
 578    {
 13579        chatController.GetUnseenMessagesByChannel();
 13580        areUnseenMessajesRequestedByFirstTime = true;
 13581    }
 582
 583    private void OpenChannelSearch()
 584    {
 0585        if (!ownUserProfile.isGuest)
 0586            OnOpenChannelSearch?.Invoke();
 587        else
 0588            dataStore.HUDs.connectWalletModalVisible.Set(true);
 0589    }
 590
 591    private async UniTask HideSearchLoadingWhenTimeout(CancellationToken cancellationToken)
 592    {
 4593        await UniTask.Delay(3000, cancellationToken: cancellationToken);
 0594        if (cancellationToken.IsCancellationRequested) return;
 0595        view.HideSearchLoading();
 0596    }
 597
 0598    private void OnAllowedToCreateChannelsChanged(bool isAllowed) => view.SetCreateChannelButtonActive(isAllowed);
 599
 600    private void SetAutomaticChannelsInfoUpdatingActive(bool isActive)
 601    {
 5602        reloadingChannelsInfoCancellationToken.Cancel();
 603
 5604        if (isActive)
 605        {
 0606            GetCurrentChannelsInfo();
 0607            reloadingChannelsInfoCancellationToken = new CancellationTokenSource();
 0608            ReloadChannelsInfoPeriodically(reloadingChannelsInfoCancellationToken.Token).Forget();
 609        }
 5610    }
 611
 612    private async UniTask ReloadChannelsInfoPeriodically(CancellationToken cancellationToken)
 613    {
 0614        while (true)
 615        {
 0616            await UniTask.Delay(MINUTES_FOR_AUTOMATIC_CHANNELS_INFO_RELOADING * 60 * 1000,
 617                cancellationToken: cancellationToken);
 618
 0619            if (cancellationToken.IsCancellationRequested)
 0620                return;
 621
 0622            GetCurrentChannelsInfo();
 623        }
 0624    }
 625
 626    private void GetCurrentChannelsInfo()
 627    {
 0628        chatController.GetChannelInfo(publicChannels
 0629            .Select(x => x.Key)
 0630            .Where(x => x != ChatUtils.NEARBY_CHANNEL_ID)
 631            .ToArray());
 0632    }
 633
 634    private void OpenWalletReadme() =>
 1635        browserBridge.OpenUrl("https://docs.decentraland.org/player/blockchain-integration/get-a-wallet/");
 636
 1637    private void SignUp() => userProfileBridge.SignUp();
 638}

Methods/Properties

WorldChatWindowController(IUserProfileBridge, IFriendsController, IChatController, DCL.DataStore, DCL.IMouseCatcher, SocialFeaturesAnalytics.ISocialAnalytics, DCL.Chat.IChannelsFeatureFlagService, DCL.Browser.IBrowserBridge)
autoJoinChannelList()
visibleTaskbarPanels()
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, 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)
HandleChannelJoined(DCL.Chat.Channels.Channel)
HandleJoinChannelError(System.String, DCL.Chat.Channels.ChannelErrorCode)
HandleLeaveChannelError(System.String, DCL.Chat.Channels.ChannelErrorCode)
HandleChannelLeft(System.String)
HandleChannelOpenedFromLink(System.String, System.String)
RequestUnreadMessages()
RequestUnreadChannelsMessages()
OpenChannelSearch()
HideSearchLoadingWhenTimeout()
OnAllowedToCreateChannelsChanged(System.Boolean)
SetAutomaticChannelsInfoUpdatingActive(System.Boolean)
ReloadChannelsInfoPeriodically()
GetCurrentChannelsInfo()
OpenWalletReadme()
SignUp()