| | 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>(); |
| 40 | 34 | | private BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels; |
| | 35 | | private int hiddenDMs; |
| 49 | 36 | | private string currentSearch = ""; |
| | 37 | | private DateTime channelsRequestTimestamp; |
| | 38 | | private bool areDMsRequestedByFirstTime; |
| | 39 | | private int lastSkipForDMs; |
| 49 | 40 | | private CancellationTokenSource hideLoadingCancellationToken = new CancellationTokenSource(); |
| | 41 | | private IWorldChatWindowView view; |
| | 42 | | private UserProfile ownUserProfile; |
| | 43 | | private bool isRequestingDMs; |
| | 44 | | private bool areJoinedChannelsRequestedByFirstTime; |
| | 45 | | private bool areUnseenMessajesRequestedByFirstTime; |
| 49 | 46 | | private CancellationTokenSource hideChannelsLoadingCancellationToken = new CancellationTokenSource(); |
| 49 | 47 | | private CancellationTokenSource hidePrivateChatsLoadingCancellationToken = new CancellationTokenSource(); |
| 49 | 48 | | private CancellationTokenSource reloadingChannelsInfoCancellationToken = new CancellationTokenSource(); |
| | 49 | |
|
| 0 | 50 | | public IWorldChatWindowView View => view; |
| | 51 | |
|
| | 52 | | public event Action OnCloseView; |
| | 53 | | public event Action<string> OnOpenPrivateChat; |
| | 54 | | public event Action<string> OnOpenPublicChat; |
| | 55 | | public event Action<string> OnOpenChannel; |
| | 56 | | public event Action OnOpen; |
| | 57 | | public event Action OnOpenChannelSearch; |
| | 58 | | public event Action OnOpenChannelCreation; |
| | 59 | | public event Action<string> OnOpenChannelLeave; |
| | 60 | |
|
| 49 | 61 | | public WorldChatWindowController( |
| | 62 | | IUserProfileBridge userProfileBridge, |
| | 63 | | IFriendsController friendsController, |
| | 64 | | IChatController chatController, |
| | 65 | | DataStore dataStore, |
| | 66 | | IMouseCatcher mouseCatcher, |
| | 67 | | ISocialAnalytics socialAnalytics, |
| | 68 | | IChannelsFeatureFlagService channelsFeatureFlagService, |
| | 69 | | IBrowserBridge browserBridge) |
| | 70 | | { |
| 49 | 71 | | this.userProfileBridge = userProfileBridge; |
| 49 | 72 | | this.friendsController = friendsController; |
| 49 | 73 | | this.chatController = chatController; |
| 49 | 74 | | this.dataStore = dataStore; |
| 49 | 75 | | this.mouseCatcher = mouseCatcher; |
| 49 | 76 | | this.socialAnalytics = socialAnalytics; |
| 49 | 77 | | this.channelsFeatureFlagService = channelsFeatureFlagService; |
| 49 | 78 | | this.browserBridge = browserBridge; |
| 49 | 79 | | } |
| | 80 | |
|
| | 81 | | public void Initialize(IWorldChatWindowView view) |
| | 82 | | { |
| 49 | 83 | | this.view = view; |
| 49 | 84 | | view.Initialize(chatController); |
| | 85 | |
|
| 49 | 86 | | if (mouseCatcher != null) |
| 49 | 87 | | mouseCatcher.OnMouseLock += HandleViewCloseRequest; |
| | 88 | |
|
| 49 | 89 | | view.OnClose += HandleViewCloseRequest; |
| 49 | 90 | | view.OnOpenPrivateChat += OpenPrivateChat; |
| 49 | 91 | | view.OnOpenPublicChat += OpenPublicChat; |
| 49 | 92 | | view.OnSearchChatRequested += SearchChats; |
| 49 | 93 | | view.OnRequireMorePrivateChats += ShowMorePrivateChats; |
| 49 | 94 | | view.OnSignUp += SignUp; |
| 49 | 95 | | view.OnRequireWalletReadme += OpenWalletReadme; |
| | 96 | |
|
| 49 | 97 | | ownUserProfile = userProfileBridge.GetOwn(); |
| 49 | 98 | | if (ownUserProfile != null) |
| 48 | 99 | | ownUserProfile.OnUpdate += OnUserProfileUpdate; |
| | 100 | |
|
| 49 | 101 | | var channel = chatController.GetAllocatedChannel(ChatUtils.NEARBY_CHANNEL_ID); |
| 49 | 102 | | publicChannels[ChatUtils.NEARBY_CHANNEL_ID] = new PublicChatModel(ChatUtils.NEARBY_CHANNEL_ID, channel.Name, |
| | 103 | | channel.Description, |
| | 104 | | channel.Joined, |
| | 105 | | channel.MemberCount, |
| | 106 | | false); |
| 49 | 107 | | view.SetPublicChat(publicChannels[ChatUtils.NEARBY_CHANNEL_ID]); |
| | 108 | |
|
| 112 | 109 | | foreach (var value in chatController.GetAllocatedEntries()) |
| 7 | 110 | | HandleMessageAdded(value); |
| | 111 | |
|
| 49 | 112 | | if (!friendsController.IsInitialized) |
| 7 | 113 | | if (ownUserProfile?.hasConnectedWeb3 ?? false) |
| 5 | 114 | | view.ShowPrivateChatsLoading(); |
| | 115 | |
|
| 49 | 116 | | chatController.OnInitialized += HandleChatInitialization; |
| 49 | 117 | | chatController.OnAddMessage += HandleMessageAdded; |
| 49 | 118 | | friendsController.OnAddFriendsWithDirectMessages += HandleFriendsWithDirectMessagesAdded; |
| 49 | 119 | | friendsController.OnUpdateUserStatus += HandleUserStatusChanged; |
| 49 | 120 | | friendsController.OnUpdateFriendship += HandleFriendshipUpdated; |
| 49 | 121 | | friendsController.OnInitialized += HandleFriendsControllerInitialization; |
| | 122 | |
|
| 49 | 123 | | if (channelsFeatureFlagService.IsChannelsFeatureEnabled()) |
| | 124 | | { |
| 46 | 125 | | channelsFeatureFlagService.OnAllowedToCreateChannelsChanged += OnAllowedToCreateChannelsChanged; |
| | 126 | |
|
| 46 | 127 | | view.OnOpenChannelSearch += OpenChannelSearch; |
| 46 | 128 | | view.OnLeaveChannel += LeaveChannel; |
| 46 | 129 | | view.OnCreateChannel += OpenChannelCreationWindow; |
| | 130 | |
|
| 46 | 131 | | chatController.OnChannelUpdated += HandleChannelUpdated; |
| 46 | 132 | | chatController.OnChannelJoined += HandleChannelJoined; |
| 46 | 133 | | chatController.OnJoinChannelError += HandleJoinChannelError; |
| 46 | 134 | | chatController.OnChannelLeaveError += HandleLeaveChannelError; |
| 46 | 135 | | chatController.OnChannelLeft += HandleChannelLeft; |
| 46 | 136 | | dataStore.channels.channelToBeOpenedFromLink.OnChange += HandleChannelOpenedFromLink; |
| | 137 | |
|
| 46 | 138 | | view.ShowChannelsLoading(); |
| 46 | 139 | | view.SetSearchAndCreateContainerActive(true); |
| 46 | 140 | | } |
| | 141 | | else |
| | 142 | | { |
| 3 | 143 | | view.HideChannelsLoading(); |
| 3 | 144 | | view.SetSearchAndCreateContainerActive(false); |
| | 145 | | } |
| 3 | 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 | | { |
| 14 | 190 | | view.Show(); |
| 14 | 191 | | OnOpen?.Invoke(); |
| | 192 | |
|
| 14 | 193 | | if (friendsController.IsInitialized && !areDMsRequestedByFirstTime) |
| | 194 | | { |
| 11 | 195 | | RequestFriendsWithDirectMessages(); |
| 11 | 196 | | RequestUnreadMessages(); |
| | 197 | | } |
| | 198 | |
|
| 14 | 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 | |
|
| 14 | 210 | | if (ownUserProfile?.isGuest ?? false) |
| 1 | 211 | | view.ShowConnectWallet(); |
| | 212 | | else |
| 13 | 213 | | view.HideConnectWallet(); |
| 13 | 214 | | } |
| | 215 | | else |
| | 216 | | { |
| 6 | 217 | | view.DisableSearchMode(); |
| 6 | 218 | | view.Hide(); |
| 6 | 219 | | SetAutomaticChannelsInfoUpdatingActive(false); |
| | 220 | | } |
| 6 | 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) |
| 14 | 233 | | newSet.Add("WorldChatPanel"); |
| | 234 | | else |
| 6 | 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 | | { |
| 14 | 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 |
| 14 | 251 | | chatController.GetJoinedChannels(CHANNELS_PAGE_SIZE, 0); |
| 14 | 252 | | channelsRequestTimestamp = DateTime.UtcNow; |
| | 253 | |
|
| 14 | 254 | | areJoinedChannelsRequestedByFirstTime = true; |
| | 255 | |
|
| 14 | 256 | | hideChannelsLoadingCancellationToken?.Cancel(); |
| 14 | 257 | | hideChannelsLoadingCancellationToken = new CancellationTokenSource(); |
| 14 | 258 | | WaitThenHideChannelsLoading(hideChannelsLoadingCancellationToken.Token).Forget(); |
| 14 | 259 | | } |
| | 260 | |
|
| | 261 | | private async UniTask WaitThenHideChannelsLoading(CancellationToken cancellationToken) |
| | 262 | | { |
| 28 | 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 | | { |
| 1 | 270 | | if (areJoinedChannelsRequestedByFirstTime) return; |
| | 271 | | // we do request joined channels as soon as possible to be able to display messages correctly in the notificatio |
| 1 | 272 | | RequestJoinedChannels(); |
| 1 | 273 | | } |
| | 274 | |
|
| | 275 | | private void HandleFriendsControllerInitialization() |
| | 276 | | { |
| 3 | 277 | | if (view.IsActive && !areDMsRequestedByFirstTime) |
| | 278 | | { |
| 2 | 279 | | RequestFriendsWithDirectMessages(); |
| 2 | 280 | | RequestUnreadMessages(); |
| 2 | 281 | | } |
| | 282 | | else |
| 1 | 283 | | view.HidePrivateChatsLoading(); |
| 1 | 284 | | } |
| | 285 | |
|
| | 286 | | private void OpenPrivateChat(string userId) |
| | 287 | | { |
| 1 | 288 | | OnOpenPrivateChat?.Invoke(userId); |
| 1 | 289 | | } |
| | 290 | |
|
| | 291 | | private void OpenPublicChat(string channelId) |
| | 292 | | { |
| 4 | 293 | | if (channelId == ChatUtils.NEARBY_CHANNEL_ID) |
| 1 | 294 | | OnOpenPublicChat?.Invoke(channelId); |
| | 295 | | else |
| 3 | 296 | | OnOpenChannel?.Invoke(channelId); |
| 1 | 297 | | } |
| | 298 | |
|
| | 299 | | private void HandleViewCloseRequest() |
| | 300 | | { |
| 1 | 301 | | OnCloseView?.Invoke(); |
| 1 | 302 | | SetVisibility(false); |
| 1 | 303 | | } |
| | 304 | |
|
| | 305 | | private void HandleFriendshipUpdated(string userId, FriendshipAction friendship) |
| | 306 | | { |
| 6 | 307 | | if (friendship != FriendshipAction.APPROVED) |
| | 308 | | { |
| | 309 | | // show only private chats from friends. Change it whenever the catalyst supports to send pms to any user |
| 6 | 310 | | view.RemovePrivateChat(userId); |
| | 311 | | } |
| 6 | 312 | | } |
| | 313 | |
|
| | 314 | | private void HandleUserStatusChanged(string userId, UserStatus status) |
| | 315 | | { |
| 4 | 316 | | if (status.friendshipStatus != FriendshipStatus.FRIEND) |
| | 317 | | { |
| | 318 | | // show only private chats from friends. Change it whenever the catalyst supports to send pms to any user |
| 4 | 319 | | view.RemovePrivateChat(userId); |
| | 320 | | } |
| 4 | 321 | | } |
| | 322 | |
|
| | 323 | | private void HandleMessageAdded(ChatMessage message) |
| | 324 | | { |
| 11 | 325 | | if (message.messageType != ChatMessage.Type.PRIVATE) return; |
| | 326 | |
|
| 7 | 327 | | var userId = ExtractRecipientId(message); |
| | 328 | |
|
| 7 | 329 | | if (lastPrivateMessages.ContainsKey(userId)) |
| | 330 | | { |
| 0 | 331 | | if (message.timestamp > lastPrivateMessages[userId].timestamp) |
| 0 | 332 | | lastPrivateMessages[userId] = message; |
| 0 | 333 | | } |
| | 334 | | else |
| 7 | 335 | | lastPrivateMessages[userId] = message; |
| | 336 | |
|
| 7 | 337 | | var profile = userProfileBridge.Get(userId); |
| 7 | 338 | | if (profile == null) return; |
| | 339 | |
|
| 7 | 340 | | view.SetPrivateChat(CreatePrivateChatModel(message, profile)); |
| 7 | 341 | | } |
| | 342 | |
|
| | 343 | | private void HandleFriendsWithDirectMessagesAdded(List<FriendWithDirectMessages> usersWithDM) |
| | 344 | | { |
| 12 | 345 | | for (var i = 0; i < usersWithDM.Count; i++) |
| | 346 | | { |
| 3 | 347 | | var profile = userProfileBridge.Get(usersWithDM[i].userId); |
| 3 | 348 | | if (profile == null) continue; |
| | 349 | |
|
| 0 | 350 | | var lastMessage = new ChatMessage |
| | 351 | | { |
| | 352 | | messageType = ChatMessage.Type.PRIVATE, |
| | 353 | | body = usersWithDM[i].lastMessageBody, |
| | 354 | | timestamp = (ulong) usersWithDM[i].lastMessageTimestamp |
| | 355 | | }; |
| | 356 | |
|
| 0 | 357 | | if (lastPrivateMessages.ContainsKey(profile.userId)) |
| | 358 | | { |
| 0 | 359 | | if (lastMessage.timestamp > lastPrivateMessages[profile.userId].timestamp) |
| 0 | 360 | | lastPrivateMessages[profile.userId] = lastMessage; |
| 0 | 361 | | } |
| | 362 | | else |
| 0 | 363 | | lastPrivateMessages[profile.userId] = lastMessage; |
| | 364 | |
|
| 0 | 365 | | view.SetPrivateChat(CreatePrivateChatModel(lastMessage, profile)); |
| | 366 | | } |
| | 367 | |
|
| 3 | 368 | | UpdateMoreChannelsToLoadHint(); |
| 3 | 369 | | view.HidePrivateChatsLoading(); |
| 3 | 370 | | view.HideSearchLoading(); |
| | 371 | |
|
| 3 | 372 | | isRequestingDMs = false; |
| 3 | 373 | | } |
| | 374 | |
|
| | 375 | | private PrivateChatModel CreatePrivateChatModel(ChatMessage recentMessage, UserProfile profile) |
| | 376 | | { |
| 7 | 377 | | return new PrivateChatModel |
| | 378 | | { |
| | 379 | | user = profile, |
| | 380 | | recentMessage = recentMessage, |
| | 381 | | isBlocked = ownUserProfile.IsBlocked(profile.userId), |
| | 382 | | isOnline = friendsController.GetUserStatus(profile.userId).presence == PresenceStatus.ONLINE |
| | 383 | | }; |
| | 384 | | } |
| | 385 | |
|
| | 386 | | private string ExtractRecipientId(ChatMessage message) |
| | 387 | | { |
| 7 | 388 | | return message.sender != ownUserProfile.userId ? message.sender : message.recipient; |
| | 389 | | } |
| | 390 | |
|
| | 391 | | private void OnUserProfileUpdate(UserProfile profile) |
| | 392 | | { |
| 2 | 393 | | view.RefreshBlockedDirectMessages(profile.blocked); |
| | 394 | |
|
| 2 | 395 | | if (profile.isGuest) |
| 1 | 396 | | view.ShowConnectWallet(); |
| | 397 | | else |
| 1 | 398 | | view.HideConnectWallet(); |
| 1 | 399 | | } |
| | 400 | |
|
| | 401 | | private void SearchChats(string search) |
| | 402 | | { |
| 2 | 403 | | currentSearch = search; |
| | 404 | |
|
| 2 | 405 | | if (string.IsNullOrEmpty(search)) |
| | 406 | | { |
| 1 | 407 | | View.DisableSearchMode(); |
| 1 | 408 | | UpdateMoreChannelsToLoadHint(); |
| 1 | 409 | | return; |
| | 410 | | } |
| | 411 | |
|
| 1 | 412 | | UpdateMoreChannelsToLoadHint(); |
| 1 | 413 | | View.EnableSearchMode(); |
| | 414 | |
|
| 1 | 415 | | var matchedChannels = publicChannels.Values |
| 2 | 416 | | .Where(model => model.name.ToLower().Contains(search.ToLower())); |
| 4 | 417 | | foreach (var channelMatch in matchedChannels) |
| 1 | 418 | | View.SetPublicChat(channelMatch); |
| | 419 | |
|
| 1 | 420 | | RequestFriendsWithDirectMessagesFromSearch(search, USER_DM_ENTRIES_TO_REQUEST_FOR_SEARCH); |
| 1 | 421 | | } |
| | 422 | |
|
| | 423 | | private void ShowMorePrivateChats() |
| | 424 | | { |
| 1 | 425 | | if (isRequestingDMs || |
| | 426 | | hiddenDMs == 0 || |
| | 427 | | !string.IsNullOrEmpty(currentSearch)) |
| 0 | 428 | | return; |
| | 429 | |
|
| 1 | 430 | | RequestFriendsWithDirectMessages(); |
| 1 | 431 | | } |
| | 432 | |
|
| | 433 | | private void UpdateMoreChannelsToLoadHint() |
| | 434 | | { |
| 5 | 435 | | hiddenDMs = Mathf.Clamp(friendsController.TotalFriendsWithDirectMessagesCount - lastSkipForDMs, |
| | 436 | | 0, |
| | 437 | | friendsController.TotalFriendsWithDirectMessagesCount); |
| | 438 | |
|
| 5 | 439 | | if (hiddenDMs <= 0 || !string.IsNullOrEmpty(currentSearch)) |
| 3 | 440 | | View.HideMoreChatsToLoadHint(); |
| | 441 | | else |
| 2 | 442 | | View.ShowMoreChatsToLoadHint(hiddenDMs); |
| 2 | 443 | | } |
| | 444 | |
|
| | 445 | | private void RequestFriendsWithDirectMessages() |
| | 446 | | { |
| 14 | 447 | | isRequestingDMs = true; |
| | 448 | |
|
| 14 | 449 | | if (!areDMsRequestedByFirstTime) |
| | 450 | | { |
| 13 | 451 | | view.ShowPrivateChatsLoading(); |
| 13 | 452 | | view.HideMoreChatsToLoadHint(); |
| | 453 | | } |
| | 454 | |
|
| 14 | 455 | | friendsController.GetFriendsWithDirectMessages(DMS_PAGE_SIZE, lastSkipForDMs); |
| 14 | 456 | | lastSkipForDMs += DMS_PAGE_SIZE; |
| 14 | 457 | | areDMsRequestedByFirstTime = true; |
| | 458 | |
|
| 14 | 459 | | hidePrivateChatsLoadingCancellationToken.Cancel(); |
| 14 | 460 | | hidePrivateChatsLoadingCancellationToken = new CancellationTokenSource(); |
| 14 | 461 | | HidePrivateChatsLoadingWhenTimeout(hidePrivateChatsLoadingCancellationToken.Token).Forget(); |
| 14 | 462 | | } |
| | 463 | |
|
| | 464 | | private async UniTaskVoid HidePrivateChatsLoadingWhenTimeout(CancellationToken cancellationToken) |
| | 465 | | { |
| 29 | 466 | | await UniTask.Delay(3000, cancellationToken: cancellationToken); |
| 0 | 467 | | if (cancellationToken.IsCancellationRequested) return; |
| 0 | 468 | | view.HidePrivateChatsLoading(); |
| 0 | 469 | | } |
| | 470 | |
|
| | 471 | | internal void RequestFriendsWithDirectMessagesFromSearch(string userNameOrId, int limit) |
| | 472 | | { |
| 2 | 473 | | view.ShowSearchLoading(); |
| 2 | 474 | | friendsController.GetFriendsWithDirectMessages(userNameOrId, limit); |
| 2 | 475 | | hideLoadingCancellationToken?.Cancel(); |
| 2 | 476 | | hideLoadingCancellationToken = new CancellationTokenSource(); |
| 2 | 477 | | HideSearchLoadingWhenTimeout(hideLoadingCancellationToken.Token).Forget(); |
| 2 | 478 | | } |
| | 479 | |
|
| | 480 | | private void HandleChannelUpdated(Channel channel) |
| | 481 | | { |
| 4 | 482 | | if (!channel.Joined) |
| | 483 | | { |
| 0 | 484 | | view.RemovePublicChat(channel.ChannelId); |
| 0 | 485 | | publicChannels.Remove(channel.ChannelId); |
| 0 | 486 | | return; |
| | 487 | | } |
| | 488 | |
|
| 4 | 489 | | var channelId = channel.ChannelId; |
| 4 | 490 | | var model = new PublicChatModel(channelId, channel.Name, channel.Description, channel.Joined, channel.MemberCoun |
| | 491 | |
|
| 4 | 492 | | if (publicChannels.ContainsKey(channelId)) |
| 1 | 493 | | publicChannels[channelId].CopyFrom(model); |
| | 494 | | else |
| 3 | 495 | | publicChannels[channelId] = model; |
| | 496 | |
|
| 4 | 497 | | view.SetPublicChat(model); |
| 4 | 498 | | view.HideChannelsLoading(); |
| | 499 | |
|
| | 500 | | // we clear the unseen messages to avoid showing many of them while the user was offline |
| | 501 | | // TODO: we should consider avoid clearing when the channel is private in the future |
| 4 | 502 | | ClearOfflineUnseenMessages(channelId); |
| 4 | 503 | | } |
| | 504 | |
|
| | 505 | | private void ClearOfflineUnseenMessages(string channelId) |
| | 506 | | { |
| 5 | 507 | | if (channelsClearedUnseenNotifications.Contains(channelId)) return; |
| 3 | 508 | | chatController.MarkChannelMessagesAsSeen(channelId); |
| 3 | 509 | | channelsClearedUnseenNotifications.Add(channelId); |
| 3 | 510 | | } |
| | 511 | |
|
| | 512 | | private void HandleChannelJoined(Channel channel) |
| | 513 | | { |
| 2 | 514 | | if (channel.MemberCount <= 1) |
| 1 | 515 | | socialAnalytics.SendEmptyChannelCreated(channel.Name, dataStore.channels.channelJoinedSource.Get()); |
| | 516 | | else |
| 1 | 517 | | socialAnalytics.SendPopulatedChannelJoined(channel.Name, dataStore.channels.channelJoinedSource.Get()); |
| | 518 | |
|
| 2 | 519 | | OpenPublicChat(channel.ChannelId); |
| 2 | 520 | | } |
| | 521 | |
|
| | 522 | | private void HandleJoinChannelError(string channelId, ChannelErrorCode errorCode) |
| | 523 | | { |
| 0 | 524 | | if (dataStore.channels.isCreationModalVisible.Get()) return; |
| | 525 | |
|
| | 526 | | switch (errorCode) |
| | 527 | | { |
| | 528 | | case ChannelErrorCode.LimitExceeded: |
| 0 | 529 | | dataStore.channels.currentChannelLimitReached.Set(channelId, true); |
| 0 | 530 | | break; |
| | 531 | | case ChannelErrorCode.Unknown: |
| 0 | 532 | | dataStore.channels.joinChannelError.Set(channelId, true); |
| | 533 | | break; |
| | 534 | | } |
| 0 | 535 | | } |
| | 536 | |
|
| | 537 | | private void HandleLeaveChannelError(string channelId, ChannelErrorCode errorCode) => |
| 0 | 538 | | dataStore.channels.leaveChannelError.Set(channelId, true); |
| | 539 | |
|
| | 540 | | private void HandleChannelLeft(string channelId) |
| | 541 | | { |
| 1 | 542 | | publicChannels.Remove(channelId); |
| 1 | 543 | | view.RemovePublicChat(channelId); |
| 1 | 544 | | var channel = chatController.GetAllocatedChannel(channelId); |
| 1 | 545 | | socialAnalytics.SendLeaveChannel(channel?.Name ?? channelId, dataStore.channels.channelLeaveSource.Get()); |
| 1 | 546 | | } |
| | 547 | |
|
| | 548 | | private void HandleChannelOpenedFromLink(string channelId, string previousChannelId) |
| | 549 | | { |
| 0 | 550 | | if (string.IsNullOrEmpty(channelId)) |
| 0 | 551 | | return; |
| | 552 | |
|
| 0 | 553 | | OpenPublicChat(channelId); |
| 0 | 554 | | } |
| | 555 | |
|
| 13 | 556 | | private void RequestUnreadMessages() => chatController.GetUnseenMessagesByUser(); |
| | 557 | |
|
| | 558 | | private void RequestUnreadChannelsMessages() |
| | 559 | | { |
| 13 | 560 | | chatController.GetUnseenMessagesByChannel(); |
| 13 | 561 | | areUnseenMessajesRequestedByFirstTime = true; |
| 13 | 562 | | } |
| | 563 | |
|
| | 564 | | private void OpenChannelSearch() |
| | 565 | | { |
| 0 | 566 | | if (!ownUserProfile.isGuest) |
| 0 | 567 | | OnOpenChannelSearch?.Invoke(); |
| | 568 | | else |
| 0 | 569 | | dataStore.HUDs.connectWalletModalVisible.Set(true); |
| 0 | 570 | | } |
| | 571 | |
|
| | 572 | | private async UniTask HideSearchLoadingWhenTimeout(CancellationToken cancellationToken) |
| | 573 | | { |
| 4 | 574 | | await UniTask.Delay(3000, cancellationToken: cancellationToken); |
| 0 | 575 | | if (cancellationToken.IsCancellationRequested) return; |
| 0 | 576 | | view.HideSearchLoading(); |
| 0 | 577 | | } |
| | 578 | |
|
| 0 | 579 | | private void OnAllowedToCreateChannelsChanged(bool isAllowed) => view.SetCreateChannelButtonActive(isAllowed); |
| | 580 | |
|
| | 581 | | private void SetAutomaticChannelsInfoUpdatingActive(bool isActive) |
| | 582 | | { |
| 6 | 583 | | reloadingChannelsInfoCancellationToken.Cancel(); |
| | 584 | |
|
| 6 | 585 | | if (isActive) |
| | 586 | | { |
| 0 | 587 | | GetCurrentChannelsInfo(); |
| 0 | 588 | | reloadingChannelsInfoCancellationToken = new CancellationTokenSource(); |
| 0 | 589 | | ReloadChannelsInfoPeriodically(reloadingChannelsInfoCancellationToken.Token).Forget(); |
| | 590 | | } |
| 6 | 591 | | } |
| | 592 | |
|
| | 593 | | private async UniTask ReloadChannelsInfoPeriodically(CancellationToken cancellationToken) |
| | 594 | | { |
| 0 | 595 | | while (true) |
| | 596 | | { |
| 0 | 597 | | await UniTask.Delay(MINUTES_FOR_AUTOMATIC_CHANNELS_INFO_RELOADING * 60 * 1000, cancellationToken: cancellati |
| | 598 | |
|
| 0 | 599 | | if (cancellationToken.IsCancellationRequested) |
| 0 | 600 | | return; |
| | 601 | |
|
| 0 | 602 | | GetCurrentChannelsInfo(); |
| | 603 | | } |
| 0 | 604 | | } |
| | 605 | |
|
| | 606 | | private void GetCurrentChannelsInfo() |
| | 607 | | { |
| 0 | 608 | | chatController.GetChannelInfo(publicChannels |
| 0 | 609 | | .Select(x => x.Key) |
| 0 | 610 | | .Where(x => x != ChatUtils.NEARBY_CHANNEL_ID) |
| | 611 | | .ToArray()); |
| 0 | 612 | | } |
| | 613 | |
|
| 1 | 614 | | private void OpenWalletReadme() => browserBridge.OpenUrl("https://docs.decentraland.org/player/blockchain-integratio |
| | 615 | |
|
| 1 | 616 | | private void SignUp() => userProfileBridge.SignUp(); |
| | 617 | | } |