| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Chat; |
| | 4 | | using DCL.Chat.Channels; |
| | 5 | | using DCL.Friends.WebApi; |
| | 6 | | using DCL.Interface; |
| | 7 | | using SocialFeaturesAnalytics; |
| | 8 | | using System; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using System.Linq; |
| | 11 | | using System.Threading; |
| | 12 | | using DCL.Browser; |
| | 13 | | using UnityEngine; |
| | 14 | | using Channel = DCL.Chat.Channels.Channel; |
| | 15 | |
|
| | 16 | | public 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; |
| 49 | 31 | | private readonly Dictionary<string, PublicChatModel> publicChannels = new Dictionary<string, PublicChatModel>(); |
| 49 | 32 | | private readonly Dictionary<string, ChatMessage> lastPrivateMessages = new Dictionary<string, ChatMessage>(); |
| 49 | 33 | | private readonly HashSet<string> channelsClearedUnseenNotifications = new HashSet<string>(); |
| 1 | 34 | | private BaseVariable<HashSet<string>> autoJoinChannelList => dataStore.HUDs.autoJoinChannelList; |
| 40 | 35 | | private BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels; |
| | 36 | | private int hiddenDMs; |
| 49 | 37 | | private string currentSearch = ""; |
| | 38 | | private DateTime channelsRequestTimestamp; |
| | 39 | | private bool areDMsRequestedByFirstTime; |
| | 40 | | private int lastSkipForDMs; |
| 49 | 41 | | 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; |
| 49 | 47 | | private CancellationTokenSource hideChannelsLoadingCancellationToken = new CancellationTokenSource(); |
| 49 | 48 | | private CancellationTokenSource hidePrivateChatsLoadingCancellationToken = new CancellationTokenSource(); |
| 49 | 49 | | private CancellationTokenSource reloadingChannelsInfoCancellationToken = new CancellationTokenSource(); |
| | 50 | |
|
| 0 | 51 | | 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 | |
|
| 49 | 62 | | 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 | | { |
| 49 | 72 | | this.userProfileBridge = userProfileBridge; |
| 49 | 73 | | this.friendsController = friendsController; |
| 49 | 74 | | this.chatController = chatController; |
| 49 | 75 | | this.dataStore = dataStore; |
| 49 | 76 | | this.mouseCatcher = mouseCatcher; |
| 49 | 77 | | this.socialAnalytics = socialAnalytics; |
| 49 | 78 | | this.channelsFeatureFlagService = channelsFeatureFlagService; |
| 49 | 79 | | this.browserBridge = browserBridge; |
| 49 | 80 | | } |
| | 81 | |
|
| | 82 | | public void Initialize(IWorldChatWindowView view) |
| | 83 | | { |
| 49 | 84 | | this.view = view; |
| 49 | 85 | | view.Initialize(chatController); |
| | 86 | |
|
| 49 | 87 | | if (mouseCatcher != null) |
| 49 | 88 | | mouseCatcher.OnMouseLock += HandleViewCloseRequest; |
| | 89 | |
|
| 49 | 90 | | view.OnClose += HandleViewCloseRequest; |
| 49 | 91 | | view.OnOpenPrivateChat += OpenPrivateChat; |
| 49 | 92 | | view.OnOpenPublicChat += OpenPublicChat; |
| 49 | 93 | | view.OnSearchChatRequested += SearchChats; |
| 49 | 94 | | view.OnRequireMorePrivateChats += ShowMorePrivateChats; |
| 49 | 95 | | view.OnSignUp += SignUp; |
| 49 | 96 | | view.OnRequireWalletReadme += OpenWalletReadme; |
| | 97 | |
|
| 49 | 98 | | ownUserProfile = userProfileBridge.GetOwn(); |
| 49 | 99 | | if (ownUserProfile != null) |
| 48 | 100 | | ownUserProfile.OnUpdate += OnUserProfileUpdate; |
| | 101 | |
|
| 49 | 102 | | var channel = chatController.GetAllocatedChannel(ChatUtils.NEARBY_CHANNEL_ID); |
| 49 | 103 | | publicChannels[ChatUtils.NEARBY_CHANNEL_ID] = new PublicChatModel(ChatUtils.NEARBY_CHANNEL_ID, channel.Name, |
| | 104 | | channel.Description, |
| | 105 | | channel.Joined, |
| | 106 | | channel.MemberCount, |
| | 107 | | false); |
| 49 | 108 | | view.SetPublicChat(publicChannels[ChatUtils.NEARBY_CHANNEL_ID]); |
| | 109 | |
|
| 49 | 110 | | if (!friendsController.IsInitialized) |
| 7 | 111 | | if (ownUserProfile?.hasConnectedWeb3 ?? false) |
| 5 | 112 | | view.ShowPrivateChatsLoading(); |
| | 113 | |
|
| 49 | 114 | | chatController.OnInitialized += HandleChatInitialization; |
| 49 | 115 | | chatController.OnAddMessage += HandleMessageAdded; |
| 49 | 116 | | friendsController.OnAddFriendsWithDirectMessages += HandleFriendsWithDirectMessagesAdded; |
| 49 | 117 | | friendsController.OnUpdateUserStatus += HandleUserStatusChanged; |
| 49 | 118 | | friendsController.OnUpdateFriendship += HandleFriendshipUpdated; |
| 49 | 119 | | friendsController.OnInitialized += HandleFriendsControllerInitialization; |
| | 120 | |
|
| 49 | 121 | | if (channelsFeatureFlagService.IsChannelsFeatureEnabled()) |
| | 122 | | { |
| 46 | 123 | | channelsFeatureFlagService.OnAllowedToCreateChannelsChanged += OnAllowedToCreateChannelsChanged; |
| | 124 | |
|
| 46 | 125 | | view.OnOpenChannelSearch += OpenChannelSearch; |
| 46 | 126 | | view.OnLeaveChannel += LeaveChannel; |
| 46 | 127 | | view.OnCreateChannel += OpenChannelCreationWindow; |
| | 128 | |
|
| 46 | 129 | | chatController.OnChannelUpdated += HandleChannelUpdated; |
| 46 | 130 | | chatController.OnChannelJoined += HandleChannelJoined; |
| 46 | 131 | | chatController.OnJoinChannelError += HandleJoinChannelError; |
| 46 | 132 | | chatController.OnChannelLeaveError += HandleLeaveChannelError; |
| 46 | 133 | | chatController.OnChannelLeft += HandleChannelLeft; |
| 46 | 134 | | dataStore.channels.channelToBeOpenedFromLink.OnChange += HandleChannelOpenedFromLink; |
| | 135 | |
|
| 46 | 136 | | view.ShowChannelsLoading(); |
| 46 | 137 | | view.SetSearchAndCreateContainerActive(true); |
| 46 | 138 | | } |
| | 139 | | else |
| | 140 | | { |
| 3 | 141 | | view.HideChannelsLoading(); |
| 3 | 142 | | view.SetSearchAndCreateContainerActive(false); |
| | 143 | | } |
| | 144 | |
|
| 49 | 145 | | view.SetChannelsPromoteLabelVisible(channelsFeatureFlagService.IsPromoteChannelsToastEnabled()); |
| 49 | 146 | | } |
| | 147 | |
|
| | 148 | | public void Dispose() |
| | 149 | | { |
| 3 | 150 | | view.OnClose -= HandleViewCloseRequest; |
| 3 | 151 | | view.OnOpenPrivateChat -= OpenPrivateChat; |
| 3 | 152 | | view.OnOpenPublicChat -= OpenPublicChat; |
| 3 | 153 | | view.OnSearchChatRequested -= SearchChats; |
| 3 | 154 | | view.OnRequireMorePrivateChats -= ShowMorePrivateChats; |
| 3 | 155 | | view.OnOpenChannelSearch -= OpenChannelSearch; |
| 3 | 156 | | view.OnCreateChannel -= OpenChannelCreationWindow; |
| 3 | 157 | | view.OnSignUp -= SignUp; |
| 3 | 158 | | view.OnRequireWalletReadme -= OpenWalletReadme; |
| 3 | 159 | | view.Dispose(); |
| 3 | 160 | | chatController.OnInitialized -= HandleChatInitialization; |
| 3 | 161 | | chatController.OnAddMessage -= HandleMessageAdded; |
| 3 | 162 | | chatController.OnChannelUpdated -= HandleChannelUpdated; |
| 3 | 163 | | chatController.OnChannelJoined -= HandleChannelJoined; |
| 3 | 164 | | chatController.OnJoinChannelError -= HandleJoinChannelError; |
| 3 | 165 | | chatController.OnChannelLeaveError += HandleLeaveChannelError; |
| 3 | 166 | | chatController.OnChannelLeft -= HandleChannelLeft; |
| 3 | 167 | | friendsController.OnAddFriendsWithDirectMessages -= HandleFriendsWithDirectMessagesAdded; |
| 3 | 168 | | friendsController.OnUpdateUserStatus -= HandleUserStatusChanged; |
| 3 | 169 | | friendsController.OnUpdateFriendship -= HandleFriendshipUpdated; |
| 3 | 170 | | friendsController.OnInitialized -= HandleFriendsControllerInitialization; |
| 3 | 171 | | dataStore.channels.channelToBeOpenedFromLink.OnChange -= HandleChannelOpenedFromLink; |
| | 172 | |
|
| 3 | 173 | | if (ownUserProfile != null) |
| 2 | 174 | | ownUserProfile.OnUpdate -= OnUserProfileUpdate; |
| | 175 | |
|
| 3 | 176 | | hideChannelsLoadingCancellationToken?.Cancel(); |
| 3 | 177 | | hideChannelsLoadingCancellationToken?.Dispose(); |
| 3 | 178 | | reloadingChannelsInfoCancellationToken.Cancel(); |
| 3 | 179 | | reloadingChannelsInfoCancellationToken.Dispose(); |
| | 180 | |
|
| 3 | 181 | | channelsFeatureFlagService.OnAllowedToCreateChannelsChanged -= OnAllowedToCreateChannelsChanged; |
| 3 | 182 | | } |
| | 183 | |
|
| | 184 | | public void SetVisibility(bool visible) |
| | 185 | | { |
| 20 | 186 | | SetVisiblePanelList(visible); |
| | 187 | |
|
| 20 | 188 | | if (visible) |
| | 189 | | { |
| 15 | 190 | | view.Show(); |
| 15 | 191 | | OnOpen?.Invoke(); |
| | 192 | |
|
| 15 | 193 | | if (friendsController.IsInitialized && !areDMsRequestedByFirstTime) |
| | 194 | | { |
| 11 | 195 | | RequestFriendsWithDirectMessages(); |
| 11 | 196 | | RequestUnreadMessages(); |
| | 197 | | } |
| | 198 | |
|
| 15 | 199 | | if (channelsFeatureFlagService.IsChannelsFeatureEnabled()) |
| | 200 | | { |
| 13 | 201 | | if (!areJoinedChannelsRequestedByFirstTime) |
| 13 | 202 | | RequestJoinedChannels(); |
| | 203 | | else |
| 0 | 204 | | SetAutomaticChannelsInfoUpdatingActive(true); |
| | 205 | |
|
| 13 | 206 | | if (!areUnseenMessajesRequestedByFirstTime) |
| 13 | 207 | | RequestUnreadChannelsMessages(); |
| | 208 | | } |
| | 209 | |
|
| 15 | 210 | | if (ownUserProfile?.isGuest ?? false) |
| 1 | 211 | | view.ShowConnectWallet(); |
| | 212 | | else |
| 14 | 213 | | view.HideConnectWallet(); |
| 14 | 214 | | } |
| | 215 | | else |
| | 216 | | { |
| 5 | 217 | | view.DisableSearchMode(); |
| 5 | 218 | | view.Hide(); |
| 5 | 219 | | SetAutomaticChannelsInfoUpdatingActive(false); |
| | 220 | | } |
| 5 | 221 | | } |
| | 222 | |
|
| | 223 | | private void OpenChannelCreationWindow() |
| | 224 | | { |
| 0 | 225 | | dataStore.channels.channelJoinedSource.Set(ChannelJoinedSource.ConversationList); |
| 0 | 226 | | OnOpenChannelCreation?.Invoke(); |
| 0 | 227 | | } |
| | 228 | |
|
| | 229 | | private void SetVisiblePanelList(bool visible) |
| | 230 | | { |
| 20 | 231 | | HashSet<string> newSet = visibleTaskbarPanels.Get(); |
| 20 | 232 | | if (visible) |
| 15 | 233 | | newSet.Add("WorldChatPanel"); |
| | 234 | | else |
| 5 | 235 | | newSet.Remove("WorldChatPanel"); |
| | 236 | |
|
| 20 | 237 | | visibleTaskbarPanels.Set(newSet, true); |
| 20 | 238 | | } |
| | 239 | |
|
| | 240 | | private void LeaveChannel(string channelId) |
| | 241 | | { |
| 1 | 242 | | dataStore.channels.channelLeaveSource.Set(ChannelLeaveSource.ConversationList); |
| 1 | 243 | | OnOpenChannelLeave?.Invoke(channelId); |
| 1 | 244 | | } |
| | 245 | |
|
| | 246 | | private void RequestJoinedChannels() |
| | 247 | | { |
| 15 | 248 | | 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 |
| 15 | 251 | | chatController.GetJoinedChannels(CHANNELS_PAGE_SIZE, 0); |
| 15 | 252 | | channelsRequestTimestamp = DateTime.UtcNow; |
| | 253 | |
|
| 15 | 254 | | areJoinedChannelsRequestedByFirstTime = true; |
| | 255 | |
|
| 15 | 256 | | hideChannelsLoadingCancellationToken?.Cancel(); |
| 15 | 257 | | hideChannelsLoadingCancellationToken = new CancellationTokenSource(); |
| 15 | 258 | | WaitThenHideChannelsLoading(hideChannelsLoadingCancellationToken.Token).Forget(); |
| 15 | 259 | | } |
| | 260 | |
|
| | 261 | | private async UniTask WaitThenHideChannelsLoading(CancellationToken cancellationToken) |
| | 262 | | { |
| 30 | 263 | | await UniTask.Delay(3000, cancellationToken: cancellationToken); |
| 0 | 264 | | if (cancellationToken.IsCancellationRequested) return; |
| 0 | 265 | | view.HideChannelsLoading(); |
| 0 | 266 | | } |
| | 267 | |
|
| | 268 | | private void HandleChatInitialization() |
| | 269 | | { |
| 2 | 270 | | if (areJoinedChannelsRequestedByFirstTime) return; |
| | 271 | | // we do request joined channels as soon as possible to be able to display messages correctly in the notificatio |
| 2 | 272 | | RequestJoinedChannels(); |
| 2 | 273 | | ConnectToAutoJoinChannels(); |
| 2 | 274 | | } |
| | 275 | |
|
| | 276 | | private void ConnectToAutoJoinChannels() |
| | 277 | | { |
| 2 | 278 | | AutomaticJoinChannelList joinChannelList = channelsFeatureFlagService.GetAutoJoinChannelsList(); |
| 3 | 279 | | if (joinChannelList == null) return; |
| 1 | 280 | | if (joinChannelList.automaticJoinChannelList == null) return; |
| | 281 | |
|
| 4 | 282 | | foreach (var channel in joinChannelList.automaticJoinChannelList) |
| | 283 | | { |
| 1 | 284 | | var channelId = channel.channelId; |
| 1 | 285 | | if (string.IsNullOrEmpty(channelId)) continue; |
| 1 | 286 | | autoJoinChannelList.Get().Add(channelId); |
| 1 | 287 | | chatController.JoinOrCreateChannel(channelId); |
| 1 | 288 | | if (!channel.enableNotifications) |
| 1 | 289 | | chatController.MuteChannel(channelId); |
| | 290 | | } |
| 1 | 291 | | } |
| | 292 | |
|
| | 293 | | private void HandleFriendsControllerInitialization() |
| | 294 | | { |
| 3 | 295 | | if (view.IsActive && !areDMsRequestedByFirstTime) |
| | 296 | | { |
| 2 | 297 | | RequestFriendsWithDirectMessages(); |
| 2 | 298 | | RequestUnreadMessages(); |
| 2 | 299 | | } |
| | 300 | | else |
| 1 | 301 | | view.HidePrivateChatsLoading(); |
| 1 | 302 | | } |
| | 303 | |
|
| | 304 | | private void OpenPrivateChat(string userId) |
| | 305 | | { |
| 1 | 306 | | OnOpenPrivateChat?.Invoke(userId); |
| 1 | 307 | | } |
| | 308 | |
|
| | 309 | | private void OpenPublicChat(string channelId) |
| | 310 | | { |
| 4 | 311 | | if (channelId == ChatUtils.NEARBY_CHANNEL_ID) |
| 1 | 312 | | OnOpenPublicChat?.Invoke(channelId); |
| | 313 | | else |
| 3 | 314 | | OnOpenChannel?.Invoke(channelId); |
| 1 | 315 | | } |
| | 316 | |
|
| | 317 | | private void HandleViewCloseRequest() |
| | 318 | | { |
| 1 | 319 | | OnCloseView?.Invoke(); |
| 1 | 320 | | SetVisibility(false); |
| 1 | 321 | | } |
| | 322 | |
|
| | 323 | | private void HandleFriendshipUpdated(string userId, FriendshipAction friendship) |
| | 324 | | { |
| 6 | 325 | | if (friendship != FriendshipAction.APPROVED) |
| | 326 | | { |
| | 327 | | // show only private chats from friends. Change it whenever the catalyst supports to send pms to any user |
| 6 | 328 | | view.RemovePrivateChat(userId); |
| | 329 | | } |
| 6 | 330 | | } |
| | 331 | |
|
| | 332 | | private void HandleUserStatusChanged(string userId, UserStatus status) |
| | 333 | | { |
| 4 | 334 | | 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 |
| 4 | 337 | | view.RemovePrivateChat(userId); |
| | 338 | | } |
| 4 | 339 | | } |
| | 340 | |
|
| | 341 | | private void HandleMessageAdded(ChatMessage message) |
| | 342 | | { |
| 2 | 343 | | if (message.messageType != ChatMessage.Type.PRIVATE) return; |
| | 344 | |
|
| 2 | 345 | | var userId = ExtractRecipientId(message); |
| | 346 | |
|
| 2 | 347 | | if (lastPrivateMessages.ContainsKey(userId)) |
| | 348 | | { |
| 0 | 349 | | if (message.timestamp > lastPrivateMessages[userId].timestamp) |
| 0 | 350 | | lastPrivateMessages[userId] = message; |
| 0 | 351 | | } |
| | 352 | | else |
| 2 | 353 | | lastPrivateMessages[userId] = message; |
| | 354 | |
|
| 2 | 355 | | var profile = userProfileBridge.Get(userId); |
| 2 | 356 | | if (profile == null) return; |
| | 357 | |
|
| 2 | 358 | | view.SetPrivateChat(CreatePrivateChatModel(message, profile)); |
| 2 | 359 | | } |
| | 360 | |
|
| | 361 | | private void HandleFriendsWithDirectMessagesAdded(List<FriendWithDirectMessages> usersWithDM) |
| | 362 | | { |
| 12 | 363 | | for (var i = 0; i < usersWithDM.Count; i++) |
| | 364 | | { |
| 3 | 365 | | var profile = userProfileBridge.Get(usersWithDM[i].userId); |
| 3 | 366 | | if (profile == null) continue; |
| | 367 | |
|
| 0 | 368 | | var lastMessage = new ChatMessage |
| | 369 | | { |
| | 370 | | messageType = ChatMessage.Type.PRIVATE, |
| | 371 | | body = usersWithDM[i].lastMessageBody, |
| | 372 | | timestamp = (ulong) usersWithDM[i].lastMessageTimestamp |
| | 373 | | }; |
| | 374 | |
|
| 0 | 375 | | if (lastPrivateMessages.ContainsKey(profile.userId)) |
| | 376 | | { |
| 0 | 377 | | if (lastMessage.timestamp > lastPrivateMessages[profile.userId].timestamp) |
| 0 | 378 | | lastPrivateMessages[profile.userId] = lastMessage; |
| 0 | 379 | | } |
| | 380 | | else |
| 0 | 381 | | lastPrivateMessages[profile.userId] = lastMessage; |
| | 382 | |
|
| 0 | 383 | | view.SetPrivateChat(CreatePrivateChatModel(lastMessage, profile)); |
| | 384 | | } |
| | 385 | |
|
| 3 | 386 | | UpdateMoreChannelsToLoadHint(); |
| 3 | 387 | | view.HidePrivateChatsLoading(); |
| 3 | 388 | | view.HideSearchLoading(); |
| | 389 | |
|
| 3 | 390 | | isRequestingDMs = false; |
| 3 | 391 | | } |
| | 392 | |
|
| | 393 | | private PrivateChatModel CreatePrivateChatModel(ChatMessage recentMessage, UserProfile profile) |
| | 394 | | { |
| 2 | 395 | | 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 | | { |
| 2 | 406 | | return message.sender != ownUserProfile.userId ? message.sender : message.recipient; |
| | 407 | | } |
| | 408 | |
|
| | 409 | | private void OnUserProfileUpdate(UserProfile profile) |
| | 410 | | { |
| 2 | 411 | | view.RefreshBlockedDirectMessages(profile.blocked); |
| | 412 | |
|
| 2 | 413 | | if (profile.isGuest) |
| 1 | 414 | | view.ShowConnectWallet(); |
| | 415 | | else |
| 1 | 416 | | view.HideConnectWallet(); |
| 1 | 417 | | } |
| | 418 | |
|
| | 419 | | private void SearchChats(string search) |
| | 420 | | { |
| 2 | 421 | | currentSearch = search; |
| | 422 | |
|
| 2 | 423 | | if (string.IsNullOrEmpty(search)) |
| | 424 | | { |
| 1 | 425 | | View.DisableSearchMode(); |
| 1 | 426 | | UpdateMoreChannelsToLoadHint(); |
| 1 | 427 | | return; |
| | 428 | | } |
| | 429 | |
|
| 1 | 430 | | UpdateMoreChannelsToLoadHint(); |
| 1 | 431 | | View.EnableSearchMode(); |
| | 432 | |
|
| 1 | 433 | | var matchedChannels = publicChannels.Values |
| 2 | 434 | | .Where(model => model.name.ToLower().Contains(search.ToLower())); |
| 4 | 435 | | foreach (var channelMatch in matchedChannels) |
| 1 | 436 | | View.SetPublicChat(channelMatch); |
| | 437 | |
|
| 1 | 438 | | RequestFriendsWithDirectMessagesFromSearch(search, USER_DM_ENTRIES_TO_REQUEST_FOR_SEARCH); |
| 1 | 439 | | } |
| | 440 | |
|
| | 441 | | private void ShowMorePrivateChats() |
| | 442 | | { |
| 1 | 443 | | if (isRequestingDMs || |
| | 444 | | hiddenDMs == 0 || |
| | 445 | | !string.IsNullOrEmpty(currentSearch)) |
| 0 | 446 | | return; |
| | 447 | |
|
| 1 | 448 | | RequestFriendsWithDirectMessages(); |
| 1 | 449 | | } |
| | 450 | |
|
| | 451 | | private void UpdateMoreChannelsToLoadHint() |
| | 452 | | { |
| 5 | 453 | | hiddenDMs = Mathf.Clamp(friendsController.TotalFriendsWithDirectMessagesCount - lastSkipForDMs, |
| | 454 | | 0, |
| | 455 | | friendsController.TotalFriendsWithDirectMessagesCount); |
| | 456 | |
|
| 5 | 457 | | if (hiddenDMs <= 0 || !string.IsNullOrEmpty(currentSearch)) |
| 3 | 458 | | View.HideMoreChatsToLoadHint(); |
| | 459 | | else |
| 2 | 460 | | View.ShowMoreChatsToLoadHint(hiddenDMs); |
| 2 | 461 | | } |
| | 462 | |
|
| | 463 | | private void RequestFriendsWithDirectMessages() |
| | 464 | | { |
| 14 | 465 | | isRequestingDMs = true; |
| | 466 | |
|
| 14 | 467 | | if (!areDMsRequestedByFirstTime) |
| | 468 | | { |
| 13 | 469 | | view.ShowPrivateChatsLoading(); |
| 13 | 470 | | view.HideMoreChatsToLoadHint(); |
| | 471 | | } |
| | 472 | |
|
| 14 | 473 | | friendsController.GetFriendsWithDirectMessages(DMS_PAGE_SIZE, lastSkipForDMs); |
| 14 | 474 | | lastSkipForDMs += DMS_PAGE_SIZE; |
| 14 | 475 | | areDMsRequestedByFirstTime = true; |
| | 476 | |
|
| 14 | 477 | | hidePrivateChatsLoadingCancellationToken.Cancel(); |
| 14 | 478 | | hidePrivateChatsLoadingCancellationToken = new CancellationTokenSource(); |
| 14 | 479 | | HidePrivateChatsLoadingWhenTimeout(hidePrivateChatsLoadingCancellationToken.Token).Forget(); |
| 14 | 480 | | } |
| | 481 | |
|
| | 482 | | private async UniTaskVoid HidePrivateChatsLoadingWhenTimeout(CancellationToken cancellationToken) |
| | 483 | | { |
| 29 | 484 | | await UniTask.Delay(3000, cancellationToken: cancellationToken); |
| 0 | 485 | | if (cancellationToken.IsCancellationRequested) return; |
| 0 | 486 | | view.HidePrivateChatsLoading(); |
| 0 | 487 | | } |
| | 488 | |
|
| | 489 | | internal void RequestFriendsWithDirectMessagesFromSearch(string userNameOrId, int limit) |
| | 490 | | { |
| 2 | 491 | | view.ShowSearchLoading(); |
| 2 | 492 | | friendsController.GetFriendsWithDirectMessages(userNameOrId, limit); |
| 2 | 493 | | hideLoadingCancellationToken?.Cancel(); |
| 2 | 494 | | hideLoadingCancellationToken = new CancellationTokenSource(); |
| 2 | 495 | | HideSearchLoadingWhenTimeout(hideLoadingCancellationToken.Token).Forget(); |
| 2 | 496 | | } |
| | 497 | |
|
| | 498 | | private void HandleChannelUpdated(Channel channel) |
| | 499 | | { |
| 4 | 500 | | if (!channel.Joined) |
| | 501 | | { |
| 0 | 502 | | view.RemovePublicChat(channel.ChannelId); |
| 0 | 503 | | publicChannels.Remove(channel.ChannelId); |
| 0 | 504 | | return; |
| | 505 | | } |
| | 506 | |
|
| 4 | 507 | | var channelId = channel.ChannelId; |
| 4 | 508 | | var model = new PublicChatModel(channelId, channel.Name, channel.Description, channel.Joined, |
| | 509 | | channel.MemberCount, channel.Muted); |
| | 510 | |
|
| 4 | 511 | | if (publicChannels.ContainsKey(channelId)) |
| 1 | 512 | | publicChannels[channelId].CopyFrom(model); |
| | 513 | | else |
| 3 | 514 | | publicChannels[channelId] = model; |
| | 515 | |
|
| 4 | 516 | | view.SetPublicChat(model); |
| 4 | 517 | | 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 |
| 4 | 521 | | ClearOfflineUnseenMessages(channelId); |
| 4 | 522 | | } |
| | 523 | |
|
| | 524 | | private void ClearOfflineUnseenMessages(string channelId) |
| | 525 | | { |
| 5 | 526 | | if (channelsClearedUnseenNotifications.Contains(channelId)) return; |
| 3 | 527 | | chatController.MarkChannelMessagesAsSeen(channelId); |
| 3 | 528 | | channelsClearedUnseenNotifications.Add(channelId); |
| 3 | 529 | | } |
| | 530 | |
|
| | 531 | | private void HandleChannelJoined(Channel channel) |
| | 532 | | { |
| 2 | 533 | | if (channel.MemberCount <= 1) |
| 1 | 534 | | socialAnalytics.SendEmptyChannelCreated(channel.Name, dataStore.channels.channelJoinedSource.Get()); |
| | 535 | | else |
| 1 | 536 | | socialAnalytics.SendPopulatedChannelJoined(channel.Name, dataStore.channels.channelJoinedSource.Get()); |
| | 537 | |
|
| 2 | 538 | | OpenPublicChat(channel.ChannelId); |
| 2 | 539 | | } |
| | 540 | |
|
| | 541 | | private void HandleJoinChannelError(string channelId, ChannelErrorCode errorCode) |
| | 542 | | { |
| 0 | 543 | | if (dataStore.channels.isCreationModalVisible.Get()) return; |
| | 544 | |
|
| | 545 | | switch (errorCode) |
| | 546 | | { |
| | 547 | | case ChannelErrorCode.LimitExceeded: |
| 0 | 548 | | dataStore.channels.currentChannelLimitReached.Set(channelId, true); |
| 0 | 549 | | break; |
| | 550 | | case ChannelErrorCode.Unknown: |
| 0 | 551 | | dataStore.channels.joinChannelError.Set(channelId, true); |
| | 552 | | break; |
| | 553 | | } |
| 0 | 554 | | } |
| | 555 | |
|
| | 556 | | private void HandleLeaveChannelError(string channelId, ChannelErrorCode errorCode) => |
| 0 | 557 | | dataStore.channels.leaveChannelError.Set(channelId, true); |
| | 558 | |
|
| | 559 | | private void HandleChannelLeft(string channelId) |
| | 560 | | { |
| 1 | 561 | | publicChannels.Remove(channelId); |
| 1 | 562 | | view.RemovePublicChat(channelId); |
| 1 | 563 | | var channel = chatController.GetAllocatedChannel(channelId); |
| 1 | 564 | | socialAnalytics.SendLeaveChannel(channel?.Name ?? channelId, dataStore.channels.channelLeaveSource.Get()); |
| 1 | 565 | | } |
| | 566 | |
|
| | 567 | | private void HandleChannelOpenedFromLink(string channelId, string previousChannelId) |
| | 568 | | { |
| 0 | 569 | | if (string.IsNullOrEmpty(channelId)) |
| 0 | 570 | | return; |
| | 571 | |
|
| 0 | 572 | | OpenPublicChat(channelId); |
| 0 | 573 | | } |
| | 574 | |
|
| 13 | 575 | | private void RequestUnreadMessages() => chatController.GetUnseenMessagesByUser(); |
| | 576 | |
|
| | 577 | | private void RequestUnreadChannelsMessages() |
| | 578 | | { |
| 13 | 579 | | chatController.GetUnseenMessagesByChannel(); |
| 13 | 580 | | areUnseenMessajesRequestedByFirstTime = true; |
| 13 | 581 | | } |
| | 582 | |
|
| | 583 | | private void OpenChannelSearch() |
| | 584 | | { |
| 0 | 585 | | if (!ownUserProfile.isGuest) |
| 0 | 586 | | OnOpenChannelSearch?.Invoke(); |
| | 587 | | else |
| 0 | 588 | | dataStore.HUDs.connectWalletModalVisible.Set(true); |
| 0 | 589 | | } |
| | 590 | |
|
| | 591 | | private async UniTask HideSearchLoadingWhenTimeout(CancellationToken cancellationToken) |
| | 592 | | { |
| 4 | 593 | | await UniTask.Delay(3000, cancellationToken: cancellationToken); |
| 0 | 594 | | if (cancellationToken.IsCancellationRequested) return; |
| 0 | 595 | | view.HideSearchLoading(); |
| 0 | 596 | | } |
| | 597 | |
|
| 0 | 598 | | private void OnAllowedToCreateChannelsChanged(bool isAllowed) => view.SetCreateChannelButtonActive(isAllowed); |
| | 599 | |
|
| | 600 | | private void SetAutomaticChannelsInfoUpdatingActive(bool isActive) |
| | 601 | | { |
| 5 | 602 | | reloadingChannelsInfoCancellationToken.Cancel(); |
| | 603 | |
|
| 5 | 604 | | if (isActive) |
| | 605 | | { |
| 0 | 606 | | GetCurrentChannelsInfo(); |
| 0 | 607 | | reloadingChannelsInfoCancellationToken = new CancellationTokenSource(); |
| 0 | 608 | | ReloadChannelsInfoPeriodically(reloadingChannelsInfoCancellationToken.Token).Forget(); |
| | 609 | | } |
| 5 | 610 | | } |
| | 611 | |
|
| | 612 | | private async UniTask ReloadChannelsInfoPeriodically(CancellationToken cancellationToken) |
| | 613 | | { |
| 0 | 614 | | while (true) |
| | 615 | | { |
| 0 | 616 | | await UniTask.Delay(MINUTES_FOR_AUTOMATIC_CHANNELS_INFO_RELOADING * 60 * 1000, |
| | 617 | | cancellationToken: cancellationToken); |
| | 618 | |
|
| 0 | 619 | | if (cancellationToken.IsCancellationRequested) |
| 0 | 620 | | return; |
| | 621 | |
|
| 0 | 622 | | GetCurrentChannelsInfo(); |
| | 623 | | } |
| 0 | 624 | | } |
| | 625 | |
|
| | 626 | | private void GetCurrentChannelsInfo() |
| | 627 | | { |
| 0 | 628 | | chatController.GetChannelInfo(publicChannels |
| 0 | 629 | | .Select(x => x.Key) |
| 0 | 630 | | .Where(x => x != ChatUtils.NEARBY_CHANNEL_ID) |
| | 631 | | .ToArray()); |
| 0 | 632 | | } |
| | 633 | |
|
| | 634 | | private void OpenWalletReadme() => |
| 1 | 635 | | browserBridge.OpenUrl("https://docs.decentraland.org/player/blockchain-integration/get-a-wallet/"); |
| | 636 | |
|
| 1 | 637 | | private void SignUp() => userProfileBridge.SignUp(); |
| | 638 | | } |