| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Chat; |
| | 3 | | using DCL.Chat.Channels; |
| | 4 | | using DCL.Interface; |
| | 5 | | using SocialFeaturesAnalytics; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Linq; |
| | 9 | | using System.Threading; |
| | 10 | | using DCL.Browser; |
| | 11 | | using DCl.Social.Friends; |
| | 12 | | using DCL.Social.Friends; |
| | 13 | | using UnityEngine; |
| | 14 | | using Channel = DCL.Chat.Channels.Channel; |
| | 15 | | using DCL.Tasks; |
| | 16 | | using DCLServices.CopyPaste.Analytics; |
| | 17 | |
|
| | 18 | | namespace DCL.Social.Chat |
| | 19 | | { |
| | 20 | | public class WorldChatWindowController : IHUD |
| | 21 | | { |
| | 22 | | private const int DMS_PAGE_SIZE = 30; |
| | 23 | | private const int USER_DM_ENTRIES_TO_REQUEST_FOR_SEARCH = 20; |
| | 24 | | private const int CHANNELS_PAGE_SIZE = 10; |
| | 25 | | private const int MINUTES_FOR_AUTOMATIC_CHANNELS_INFO_RELOADING = 1; |
| | 26 | |
|
| | 27 | | private readonly IUserProfileBridge userProfileBridge; |
| | 28 | | private readonly IFriendsController friendsController; |
| | 29 | | private readonly IChatController chatController; |
| | 30 | | private readonly DataStore dataStore; |
| | 31 | | private readonly IMouseCatcher mouseCatcher; |
| | 32 | | private readonly ISocialAnalytics socialAnalytics; |
| | 33 | | private readonly IChannelsFeatureFlagService channelsFeatureFlagService; |
| | 34 | | private readonly IBrowserBridge browserBridge; |
| | 35 | | private readonly RendererState rendererState; |
| | 36 | | private readonly DataStore_Mentions mentionsDataStore; |
| | 37 | | private readonly IClipboard clipboard; |
| | 38 | | private readonly ICopyPasteAnalyticsService copyPasteAnalyticsService; |
| 53 | 39 | | private readonly Dictionary<string, PublicChatModel> publicChannels = new Dictionary<string, PublicChatModel>(); |
| 53 | 40 | | private readonly Dictionary<string, ChatMessage> lastPrivateMessages = new Dictionary<string, ChatMessage>(); |
| 53 | 41 | | private readonly HashSet<string> channelsClearedUnseenNotifications = new HashSet<string>(); |
| 1 | 42 | | private HashSet<string> autoJoinChannelList => dataStore.HUDs.autoJoinChannelList.Get(); |
| 68 | 43 | | private BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels; |
| | 44 | | private int hiddenDMs; |
| 53 | 45 | | private string currentSearch = ""; |
| | 46 | | private DateTime channelsRequestTimestamp; |
| | 47 | | private bool areDMsRequestedByFirstTime; |
| | 48 | | private int lastSkipForDMs; |
| 53 | 49 | | private CancellationTokenSource hideLoadingCancellationToken = new CancellationTokenSource(); |
| | 50 | | private IWorldChatWindowView view; |
| | 51 | | private UserProfile ownUserProfile; |
| | 52 | | private bool isRequestingDMs; |
| | 53 | | private bool areUnseenMessagesRequestedByFirstTime; |
| | 54 | | private string channelToJoinAtTheBeginning; |
| 53 | 55 | | private CancellationTokenSource hideChannelsLoadingCancellationToken = new CancellationTokenSource(); |
| 53 | 56 | | private CancellationTokenSource hidePrivateChatsLoadingCancellationToken = new CancellationTokenSource(); |
| 53 | 57 | | private CancellationTokenSource reloadingChannelsInfoCancellationToken = new CancellationTokenSource(); |
| 53 | 58 | | private CancellationTokenSource showDMsCancellationToken = new (); |
| 57 | 59 | | private bool showOnlyOnlineMembersOnPublicChannels => !dataStore.featureFlags.flags.Get().IsFeatureEnabled("matr |
| | 60 | |
|
| 53 | 61 | | private bool isVisible = true; |
| 16 | 62 | | public IWorldChatWindowView View => view; |
| | 63 | |
|
| | 64 | | public event Action OnCloseView; |
| | 65 | | public event Action<string> OnOpenPrivateChat; |
| | 66 | | public event Action<string> OnOpenPublicChat; |
| | 67 | | public event Action<string> OnOpenChannel; |
| | 68 | | public event Action OnOpen; |
| | 69 | | public event Action OnOpenChannelSearch; |
| | 70 | | public event Action OnOpenChannelCreation; |
| | 71 | | public event Action<string> OnOpenChannelLeave; |
| | 72 | |
|
| 53 | 73 | | public WorldChatWindowController( |
| | 74 | | IUserProfileBridge userProfileBridge, |
| | 75 | | IFriendsController friendsController, |
| | 76 | | IChatController chatController, |
| | 77 | | DataStore dataStore, |
| | 78 | | IMouseCatcher mouseCatcher, |
| | 79 | | ISocialAnalytics socialAnalytics, |
| | 80 | | IChannelsFeatureFlagService channelsFeatureFlagService, |
| | 81 | | IBrowserBridge browserBridge, |
| | 82 | | RendererState rendererState, |
| | 83 | | DataStore_Mentions mentionsDataStore, |
| | 84 | | IClipboard clipboard, |
| | 85 | | ICopyPasteAnalyticsService copyPasteAnalyticsService) |
| | 86 | | { |
| 53 | 87 | | this.userProfileBridge = userProfileBridge; |
| 53 | 88 | | this.friendsController = friendsController; |
| 53 | 89 | | this.chatController = chatController; |
| 53 | 90 | | this.dataStore = dataStore; |
| 53 | 91 | | this.mouseCatcher = mouseCatcher; |
| 53 | 92 | | this.socialAnalytics = socialAnalytics; |
| 53 | 93 | | this.channelsFeatureFlagService = channelsFeatureFlagService; |
| 53 | 94 | | this.browserBridge = browserBridge; |
| 53 | 95 | | this.rendererState = rendererState; |
| 53 | 96 | | this.mentionsDataStore = mentionsDataStore; |
| 53 | 97 | | this.clipboard = clipboard; |
| 53 | 98 | | this.copyPasteAnalyticsService = copyPasteAnalyticsService; |
| 53 | 99 | | } |
| | 100 | |
|
| | 101 | | public void Initialize(IWorldChatWindowView view, bool isVisible = true) |
| | 102 | | { |
| 53 | 103 | | this.view = view; |
| 53 | 104 | | view.Initialize(chatController, mentionsDataStore); |
| | 105 | |
|
| 53 | 106 | | if (mouseCatcher != null) |
| 52 | 107 | | mouseCatcher.OnMouseLock += HandleViewCloseRequest; |
| | 108 | |
|
| 53 | 109 | | view.OnClose += HandleViewCloseRequest; |
| 53 | 110 | | view.OnOpenPrivateChat += OpenPrivateChat; |
| 53 | 111 | | view.OnOpenPublicChat += OpenPublicChat; |
| 53 | 112 | | view.OnSearchChatRequested += SearchChats; |
| 53 | 113 | | view.OnRequireMorePrivateChats += ShowMorePrivateChats; |
| 53 | 114 | | view.OnSignUp += SignUp; |
| 53 | 115 | | view.OnRequireWalletReadme += OpenWalletReadme; |
| | 116 | |
|
| 53 | 117 | | ownUserProfile = userProfileBridge.GetOwn(); |
| 53 | 118 | | if (ownUserProfile != null) |
| 53 | 119 | | ownUserProfile.OnUpdate += OnUserProfileUpdate; |
| | 120 | |
|
| 53 | 121 | | var channel = chatController.GetAllocatedChannel(ChatUtils.NEARBY_CHANNEL_ID); |
| 53 | 122 | | publicChannels[ChatUtils.NEARBY_CHANNEL_ID] = new PublicChatModel(ChatUtils.NEARBY_CHANNEL_ID, channel.Name, |
| | 123 | | channel.Description, |
| | 124 | | channel.Joined, |
| | 125 | | channel.MemberCount, |
| | 126 | | false, |
| | 127 | | showOnlyOnlineMembersOnPublicChannels); |
| 53 | 128 | | view.SetPublicChat(publicChannels[ChatUtils.NEARBY_CHANNEL_ID]); |
| | 129 | |
|
| 53 | 130 | | if (!friendsController.IsInitialized) |
| 7 | 131 | | if (ownUserProfile?.hasConnectedWeb3 ?? false) |
| 6 | 132 | | view.ShowPrivateChatsLoading(); |
| | 133 | |
|
| 53 | 134 | | chatController.OnInitialized += HandleChatInitialization; |
| 53 | 135 | | chatController.OnAddMessage += HandleMessageAdded; |
| 53 | 136 | | friendsController.OnAddFriendsWithDirectMessages += HandleFriendsWithDirectMessagesAdded; |
| 53 | 137 | | friendsController.OnUpdateUserStatus += HandleUserStatusChanged; |
| 53 | 138 | | friendsController.OnUpdateFriendship += HandleFriendshipUpdated; |
| 53 | 139 | | friendsController.OnInitialized += HandleFriendsControllerInitialization; |
| | 140 | |
|
| 53 | 141 | | if (channelsFeatureFlagService.IsChannelsFeatureEnabled()) |
| | 142 | | { |
| 50 | 143 | | channelsFeatureFlagService.OnAllowedToCreateChannelsChanged += OnAllowedToCreateChannelsChanged; |
| | 144 | |
|
| 50 | 145 | | view.OnOpenChannelSearch += OpenChannelSearch; |
| 50 | 146 | | view.OnLeaveChannel += LeaveChannel; |
| 50 | 147 | | view.OnCreateChannel += OpenChannelCreationWindow; |
| 50 | 148 | | view.OnCopyChannelNameRequested += CopyChannelNameToClipboard; |
| | 149 | |
|
| 50 | 150 | | chatController.OnChannelUpdated += HandleChannelUpdated; |
| 50 | 151 | | chatController.OnChannelJoined += HandleChannelJoined; |
| 50 | 152 | | chatController.OnAutoChannelJoined += HandleAutoChannelJoined; |
| 50 | 153 | | chatController.OnJoinChannelError += HandleJoinChannelError; |
| 50 | 154 | | chatController.OnChannelLeaveError += HandleLeaveChannelError; |
| 50 | 155 | | chatController.OnChannelLeft += HandleChannelLeft; |
| 50 | 156 | | chatController.OnAskForJoinChannel += HandleAskForJoinChannel; |
| 50 | 157 | | dataStore.channels.channelToBeOpened.OnChange += HandleChannelOpened; |
| | 158 | |
|
| 50 | 159 | | view.ShowChannelsLoading(); |
| 50 | 160 | | view.SetSearchAndCreateContainerActive(true); |
| | 161 | | } |
| | 162 | | else |
| | 163 | | { |
| 3 | 164 | | view.HideChannelsLoading(); |
| 3 | 165 | | view.SetSearchAndCreateContainerActive(false); |
| | 166 | | } |
| | 167 | |
|
| 53 | 168 | | view.SetChannelsPromoteLabelVisible(channelsFeatureFlagService.IsPromoteChannelsToastEnabled()); |
| | 169 | |
|
| 53 | 170 | | SetVisibility(isVisible); |
| 53 | 171 | | this.isVisible = isVisible; |
| 53 | 172 | | } |
| | 173 | |
|
| | 174 | | public void Dispose() |
| | 175 | | { |
| 3 | 176 | | view.OnClose -= HandleViewCloseRequest; |
| 3 | 177 | | view.OnOpenPrivateChat -= OpenPrivateChat; |
| 3 | 178 | | view.OnOpenPublicChat -= OpenPublicChat; |
| 3 | 179 | | view.OnSearchChatRequested -= SearchChats; |
| 3 | 180 | | view.OnRequireMorePrivateChats -= ShowMorePrivateChats; |
| 3 | 181 | | view.OnOpenChannelSearch -= OpenChannelSearch; |
| 3 | 182 | | view.OnCreateChannel -= OpenChannelCreationWindow; |
| 3 | 183 | | view.OnSignUp -= SignUp; |
| 3 | 184 | | view.OnRequireWalletReadme -= OpenWalletReadme; |
| 3 | 185 | | view.Dispose(); |
| 3 | 186 | | chatController.OnInitialized -= HandleChatInitialization; |
| 3 | 187 | | chatController.OnAddMessage -= HandleMessageAdded; |
| 3 | 188 | | chatController.OnChannelUpdated -= HandleChannelUpdated; |
| 3 | 189 | | chatController.OnChannelJoined -= HandleChannelJoined; |
| 3 | 190 | | chatController.OnAutoChannelJoined -= HandleAutoChannelJoined; |
| 3 | 191 | | chatController.OnJoinChannelError -= HandleJoinChannelError; |
| 3 | 192 | | chatController.OnChannelLeaveError -= HandleLeaveChannelError; |
| 3 | 193 | | chatController.OnChannelLeft -= HandleChannelLeft; |
| 3 | 194 | | chatController.OnAskForJoinChannel -= HandleAskForJoinChannel; |
| 3 | 195 | | friendsController.OnAddFriendsWithDirectMessages -= HandleFriendsWithDirectMessagesAdded; |
| 3 | 196 | | friendsController.OnUpdateUserStatus -= HandleUserStatusChanged; |
| 3 | 197 | | friendsController.OnUpdateFriendship -= HandleFriendshipUpdated; |
| 3 | 198 | | friendsController.OnInitialized -= HandleFriendsControllerInitialization; |
| 3 | 199 | | dataStore.channels.channelToBeOpened.OnChange -= HandleChannelOpened; |
| 3 | 200 | | rendererState.OnChange -= HandleAskForJoinChannelAfterRendererState; |
| | 201 | |
|
| 3 | 202 | | if (ownUserProfile != null) |
| 3 | 203 | | ownUserProfile.OnUpdate -= OnUserProfileUpdate; |
| | 204 | |
|
| 3 | 205 | | hideChannelsLoadingCancellationToken?.SafeCancelAndDispose(); |
| 3 | 206 | | reloadingChannelsInfoCancellationToken.SafeCancelAndDispose(); |
| 3 | 207 | | showDMsCancellationToken.SafeCancelAndDispose(); |
| | 208 | |
|
| 3 | 209 | | channelsFeatureFlagService.OnAllowedToCreateChannelsChanged -= OnAllowedToCreateChannelsChanged; |
| 3 | 210 | | } |
| | 211 | |
|
| | 212 | | public void SetVisibility(bool visible) |
| | 213 | | { |
| 74 | 214 | | if (isVisible == visible) |
| 40 | 215 | | return; |
| | 216 | |
|
| 34 | 217 | | isVisible = visible; |
| 34 | 218 | | SetVisiblePanelList(visible); |
| | 219 | |
|
| 34 | 220 | | if (visible) |
| | 221 | | { |
| 16 | 222 | | view.Show(); |
| 16 | 223 | | OnOpen?.Invoke(); |
| | 224 | |
|
| 16 | 225 | | if (friendsController.IsInitialized && !areDMsRequestedByFirstTime) |
| | 226 | | { |
| 12 | 227 | | RequestFriendsWithDirectMessages(); |
| 12 | 228 | | RequestUnreadMessages(); |
| | 229 | | } |
| | 230 | |
|
| 16 | 231 | | if (channelsFeatureFlagService.IsChannelsFeatureEnabled()) |
| | 232 | | { |
| 14 | 233 | | if (chatController.IsInitialized) |
| | 234 | | { |
| 13 | 235 | | RequestJoinedChannels(); |
| 13 | 236 | | SetAutomaticChannelsInfoUpdatingActive(true); |
| | 237 | | } |
| 1 | 238 | | else if (ownUserProfile.isGuest) |
| | 239 | | { |
| | 240 | | // TODO: channels are not allowed for guests. When we support it in the future, remove this call |
| 1 | 241 | | view.HideChannelsLoading(); |
| | 242 | | } |
| | 243 | |
|
| 14 | 244 | | if (!areUnseenMessagesRequestedByFirstTime) |
| 14 | 245 | | RequestUnreadChannelsMessages(); |
| | 246 | | } |
| | 247 | |
|
| 16 | 248 | | if (ownUserProfile?.isGuest ?? false) |
| 2 | 249 | | view.ShowConnectWallet(); |
| | 250 | | else |
| 14 | 251 | | view.HideConnectWallet(); |
| | 252 | | } |
| | 253 | | else |
| | 254 | | { |
| 18 | 255 | | view.DisableSearchMode(); |
| 18 | 256 | | view.Hide(); |
| 18 | 257 | | SetAutomaticChannelsInfoUpdatingActive(false); |
| | 258 | | } |
| 18 | 259 | | } |
| | 260 | |
|
| | 261 | | private void OpenChannelCreationWindow() |
| | 262 | | { |
| 0 | 263 | | dataStore.channels.channelJoinedSource.Set(ChannelJoinedSource.ConversationList); |
| 0 | 264 | | OnOpenChannelCreation?.Invoke(); |
| 0 | 265 | | } |
| | 266 | |
|
| | 267 | | private void SetVisiblePanelList(bool visible) |
| | 268 | | { |
| 34 | 269 | | HashSet<string> newSet = visibleTaskbarPanels.Get(); |
| 34 | 270 | | if (visible) |
| 16 | 271 | | newSet.Add("WorldChatPanel"); |
| | 272 | | else |
| 18 | 273 | | newSet.Remove("WorldChatPanel"); |
| | 274 | |
|
| 34 | 275 | | visibleTaskbarPanels.Set(newSet, true); |
| 34 | 276 | | } |
| | 277 | |
|
| | 278 | | private void LeaveChannel(string channelId) |
| | 279 | | { |
| 1 | 280 | | dataStore.channels.channelLeaveSource.Set(ChannelLeaveSource.ConversationList); |
| 1 | 281 | | OnOpenChannelLeave?.Invoke(channelId); |
| 1 | 282 | | } |
| | 283 | |
|
| | 284 | | private void RequestJoinedChannels() |
| | 285 | | { |
| 15 | 286 | | if ((DateTime.UtcNow - channelsRequestTimestamp).TotalSeconds < 3) return; |
| | 287 | |
|
| | 288 | | // skip=0: we do not support pagination for channels, it is supposed that a user can have a limited amount o |
| 15 | 289 | | chatController.GetJoinedChannels(CHANNELS_PAGE_SIZE, 0); |
| 15 | 290 | | channelsRequestTimestamp = DateTime.UtcNow; |
| | 291 | |
|
| 15 | 292 | | hideChannelsLoadingCancellationToken?.Cancel(); |
| 15 | 293 | | hideChannelsLoadingCancellationToken = new CancellationTokenSource(); |
| 15 | 294 | | WaitThenHideChannelsLoading(hideChannelsLoadingCancellationToken.Token).Forget(); |
| 15 | 295 | | } |
| | 296 | |
|
| | 297 | | private async UniTask WaitThenHideChannelsLoading(CancellationToken cancellationToken) |
| | 298 | | { |
| 30 | 299 | | await UniTask.Delay(3000, cancellationToken: cancellationToken); |
| 0 | 300 | | if (cancellationToken.IsCancellationRequested) return; |
| 0 | 301 | | view.HideChannelsLoading(); |
| 0 | 302 | | } |
| | 303 | |
|
| | 304 | | private void HandleChatInitialization() |
| | 305 | | { |
| | 306 | | // we do request joined channels as soon as possible to be able to display messages correctly in the notific |
| 2 | 307 | | RequestJoinedChannels(); |
| 2 | 308 | | ConnectToAutoJoinChannels(); |
| 2 | 309 | | } |
| | 310 | |
|
| | 311 | | private void ConnectToAutoJoinChannels() |
| | 312 | | { |
| 2 | 313 | | AutomaticJoinChannelList joinChannelList = channelsFeatureFlagService.GetAutoJoinChannelsList(); |
| 3 | 314 | | if (joinChannelList == null) return; |
| 1 | 315 | | if (joinChannelList.automaticJoinChannelList == null) return; |
| | 316 | |
|
| 4 | 317 | | foreach (var channel in joinChannelList.automaticJoinChannelList) |
| | 318 | | { |
| 1 | 319 | | var channelId = channel.channelId; |
| 1 | 320 | | if (string.IsNullOrEmpty(channelId)) continue; |
| 1 | 321 | | autoJoinChannelList.Add(channelId); |
| 1 | 322 | | chatController.JoinOrCreateChannel(channelId); |
| 1 | 323 | | if (!channel.enableNotifications) |
| 1 | 324 | | chatController.MuteChannel(channelId); |
| | 325 | | } |
| 1 | 326 | | } |
| | 327 | |
|
| | 328 | | private void HandleFriendsControllerInitialization() |
| | 329 | | { |
| 3 | 330 | | if (view.IsActive && !areDMsRequestedByFirstTime) |
| | 331 | | { |
| 2 | 332 | | RequestFriendsWithDirectMessages(); |
| 2 | 333 | | RequestUnreadMessages(); |
| | 334 | | } |
| | 335 | | else |
| 1 | 336 | | view.HidePrivateChatsLoading(); |
| 1 | 337 | | } |
| | 338 | |
|
| | 339 | | private void OpenPrivateChat(string userId) |
| | 340 | | { |
| 1 | 341 | | OnOpenPrivateChat?.Invoke(userId); |
| 1 | 342 | | } |
| | 343 | |
|
| | 344 | | private void OpenPublicChat(string channelId) |
| | 345 | | { |
| 4 | 346 | | if (channelId == ChatUtils.NEARBY_CHANNEL_ID) |
| 1 | 347 | | OnOpenPublicChat?.Invoke(channelId); |
| | 348 | | else |
| 3 | 349 | | OnOpenChannel?.Invoke(channelId); |
| 1 | 350 | | } |
| | 351 | |
|
| | 352 | | private void HandleViewCloseRequest() |
| | 353 | | { |
| 1 | 354 | | OnCloseView?.Invoke(); |
| 1 | 355 | | SetVisibility(false); |
| 1 | 356 | | } |
| | 357 | |
|
| | 358 | | private void HandleFriendshipUpdated(string userId, FriendshipAction friendship) |
| | 359 | | { |
| 6 | 360 | | if (friendship != FriendshipAction.APPROVED) |
| | 361 | | { |
| | 362 | | // show only private chats from friends. Change it whenever the catalyst supports to send pms to any use |
| 6 | 363 | | view.RemovePrivateChat(userId); |
| | 364 | | } |
| 6 | 365 | | } |
| | 366 | |
|
| | 367 | | private void HandleUserStatusChanged(string userId, UserStatus status) |
| | 368 | | { |
| 4 | 369 | | if (status.friendshipStatus != FriendshipStatus.FRIEND) |
| | 370 | | { |
| | 371 | | // show only private chats from friends. Change it whenever the catalyst supports to send pms to any use |
| 4 | 372 | | view.RemovePrivateChat(userId); |
| | 373 | | } |
| | 374 | | else |
| | 375 | | { |
| 0 | 376 | | view.RefreshPrivateChatPresence(userId, status.presence == PresenceStatus.ONLINE); |
| | 377 | | } |
| 0 | 378 | | } |
| | 379 | |
|
| | 380 | | private void HandleMessageAdded(ChatMessage[] messages) |
| | 381 | | { |
| | 382 | | async UniTaskVoid AddMessages(ChatMessage[] messages, CancellationToken cancellationToken) |
| | 383 | | { |
| 11 | 384 | | foreach (var message in messages) |
| | 385 | | { |
| 3 | 386 | | if (message.messageType != ChatMessage.Type.PRIVATE) continue; |
| | 387 | |
|
| 3 | 388 | | var userId = ExtractRecipientId(message); |
| | 389 | |
|
| 3 | 390 | | if (lastPrivateMessages.ContainsKey(userId)) |
| | 391 | | { |
| 0 | 392 | | if (message.timestamp > lastPrivateMessages[userId].timestamp) |
| 0 | 393 | | lastPrivateMessages[userId] = message; |
| | 394 | | } |
| | 395 | | else |
| 3 | 396 | | lastPrivateMessages[userId] = message; |
| | 397 | |
|
| | 398 | | try |
| | 399 | | { |
| | 400 | | // incoming friend request's message is added as a DM. This check filters it |
| 4 | 401 | | if (await friendsController.GetFriendshipStatus(userId, cancellationToken) != FriendshipStatus.F |
| | 402 | |
|
| 2 | 403 | | var profile = userProfileBridge.Get(userId) |
| | 404 | | ?? await userProfileBridge.RequestFullUserProfileAsync(userId, cancellationToken); |
| | 405 | |
|
| 2 | 406 | | view.SetPrivateChat(CreatePrivateChatModel(message, profile)); |
| 2 | 407 | | } |
| 0 | 408 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 409 | | { |
| 0 | 410 | | Debug.LogException(e); |
| 0 | 411 | | } |
| 2 | 412 | | } |
| 3 | 413 | | } |
| | 414 | |
|
| 3 | 415 | | AddMessages(messages, showDMsCancellationToken.Token).Forget(); |
| 3 | 416 | | } |
| | 417 | |
|
| | 418 | | private void HandleFriendsWithDirectMessagesAdded(List<FriendWithDirectMessages> usersWithDM) |
| | 419 | | { |
| | 420 | | async UniTaskVoid HandleFriendsWithDirectMessagesAddedAsync(List<FriendWithDirectMessages> usersWithDM, Canc |
| | 421 | | { |
| 12 | 422 | | for (var i = 0; i < usersWithDM.Count; i++) |
| | 423 | | { |
| 3 | 424 | | FriendWithDirectMessages dm = usersWithDM[i]; |
| | 425 | |
|
| 3 | 426 | | var lastMessage = new ChatMessage |
| | 427 | | { |
| | 428 | | messageType = ChatMessage.Type.PRIVATE, |
| | 429 | | body = dm.lastMessageBody, |
| | 430 | | timestamp = (ulong) dm.lastMessageTimestamp |
| | 431 | | }; |
| | 432 | |
|
| 3 | 433 | | string userId = dm.userId; |
| | 434 | |
|
| 3 | 435 | | if (lastPrivateMessages.ContainsKey(userId)) |
| | 436 | | { |
| 0 | 437 | | if (lastMessage.timestamp > lastPrivateMessages[userId].timestamp) |
| 0 | 438 | | lastPrivateMessages[userId] = lastMessage; |
| | 439 | | } |
| | 440 | | else |
| 3 | 441 | | lastPrivateMessages[userId] = lastMessage; |
| | 442 | |
|
| 3 | 443 | | var profile = userProfileBridge.Get(userId); |
| | 444 | |
|
| 6 | 445 | | try { profile ??= await userProfileBridge.RequestFullUserProfileAsync(userId, cancellationToken); } |
| 0 | 446 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 447 | | { |
| 0 | 448 | | var fallbackDM = new PrivateChatModel |
| | 449 | | { |
| | 450 | | recentMessage = lastMessage, |
| | 451 | | faceSnapshotUrl = "", |
| | 452 | | isBlocked = ownUserProfile.IsBlocked(userId), |
| | 453 | | isOnline = false, |
| | 454 | | userId = userId, |
| | 455 | | userName = userId, |
| | 456 | | }; |
| | 457 | |
|
| 0 | 458 | | view.SetPrivateChat(fallbackDM); |
| | 459 | |
|
| 0 | 460 | | throw; |
| | 461 | | } |
| | 462 | |
|
| 3 | 463 | | view.SetPrivateChat(CreatePrivateChatModel(lastMessage, profile)); |
| 3 | 464 | | } |
| | 465 | |
|
| 3 | 466 | | UpdateMoreChannelsToLoadHint(); |
| 3 | 467 | | view.HidePrivateChatsLoading(); |
| 3 | 468 | | view.HideSearchLoading(); |
| | 469 | |
|
| 3 | 470 | | isRequestingDMs = false; |
| 3 | 471 | | } |
| | 472 | |
|
| 3 | 473 | | HandleFriendsWithDirectMessagesAddedAsync(usersWithDM, showDMsCancellationToken.Token).Forget(); |
| 3 | 474 | | } |
| | 475 | |
|
| | 476 | | private PrivateChatModel CreatePrivateChatModel(ChatMessage recentMessage, UserProfile profile) |
| | 477 | | { |
| 5 | 478 | | return new PrivateChatModel |
| | 479 | | { |
| | 480 | | userId = profile.userId, |
| | 481 | | userName = profile.userName, |
| | 482 | | faceSnapshotUrl = profile.face256SnapshotURL, |
| | 483 | | recentMessage = recentMessage, |
| | 484 | | isBlocked = ownUserProfile.IsBlocked(profile.userId), |
| | 485 | | isOnline = friendsController.GetUserStatus(profile.userId).presence == PresenceStatus.ONLINE |
| | 486 | | }; |
| | 487 | | } |
| | 488 | |
|
| | 489 | | private string ExtractRecipientId(ChatMessage message) |
| | 490 | | { |
| 3 | 491 | | return message.sender != ownUserProfile.userId ? message.sender : message.recipient; |
| | 492 | | } |
| | 493 | |
|
| | 494 | | private void OnUserProfileUpdate(UserProfile profile) |
| | 495 | | { |
| 2 | 496 | | view.RefreshBlockedDirectMessages(profile.blocked); |
| | 497 | |
|
| 2 | 498 | | if (profile.isGuest) |
| 1 | 499 | | view.ShowConnectWallet(); |
| | 500 | | else |
| 1 | 501 | | view.HideConnectWallet(); |
| 1 | 502 | | } |
| | 503 | |
|
| | 504 | | private void SearchChats(string search) |
| | 505 | | { |
| 2 | 506 | | currentSearch = search; |
| | 507 | |
|
| 2 | 508 | | if (string.IsNullOrEmpty(search)) |
| | 509 | | { |
| 1 | 510 | | View.DisableSearchMode(); |
| 1 | 511 | | UpdateMoreChannelsToLoadHint(); |
| 1 | 512 | | return; |
| | 513 | | } |
| | 514 | |
|
| 1 | 515 | | UpdateMoreChannelsToLoadHint(); |
| 1 | 516 | | View.EnableSearchMode(); |
| | 517 | |
|
| 1 | 518 | | var matchedChannels = publicChannels.Values |
| 2 | 519 | | .Where(model => model.name.ToLower().Contains(search.ToLower())); |
| 4 | 520 | | foreach (var channelMatch in matchedChannels) |
| 1 | 521 | | View.SetPublicChat(channelMatch); |
| | 522 | |
|
| 1 | 523 | | RequestFriendsWithDirectMessagesFromSearch(search, USER_DM_ENTRIES_TO_REQUEST_FOR_SEARCH); |
| 1 | 524 | | } |
| | 525 | |
|
| | 526 | | private void ShowMorePrivateChats() |
| | 527 | | { |
| 1 | 528 | | if (isRequestingDMs || |
| | 529 | | hiddenDMs == 0 || |
| | 530 | | !string.IsNullOrEmpty(currentSearch)) |
| 0 | 531 | | return; |
| | 532 | |
|
| 1 | 533 | | RequestFriendsWithDirectMessages(); |
| 1 | 534 | | } |
| | 535 | |
|
| | 536 | | private void UpdateMoreChannelsToLoadHint() |
| | 537 | | { |
| 5 | 538 | | hiddenDMs = Mathf.Clamp(friendsController.TotalFriendsWithDirectMessagesCount - lastSkipForDMs, |
| | 539 | | 0, |
| | 540 | | friendsController.TotalFriendsWithDirectMessagesCount); |
| | 541 | |
|
| 5 | 542 | | if (hiddenDMs <= 0 || !string.IsNullOrEmpty(currentSearch)) |
| 3 | 543 | | View.HideMoreChatsToLoadHint(); |
| | 544 | | else |
| 2 | 545 | | View.ShowMoreChatsToLoadHint(hiddenDMs); |
| 2 | 546 | | } |
| | 547 | |
|
| | 548 | | private void RequestFriendsWithDirectMessages() |
| | 549 | | { |
| 15 | 550 | | isRequestingDMs = true; |
| | 551 | |
|
| 15 | 552 | | if (!areDMsRequestedByFirstTime) |
| | 553 | | { |
| 14 | 554 | | view.ShowPrivateChatsLoading(); |
| 14 | 555 | | view.HideMoreChatsToLoadHint(); |
| | 556 | | } |
| | 557 | |
|
| 15 | 558 | | friendsController.GetFriendsWithDirectMessages(DMS_PAGE_SIZE, lastSkipForDMs); |
| 15 | 559 | | lastSkipForDMs += DMS_PAGE_SIZE; |
| 15 | 560 | | areDMsRequestedByFirstTime = true; |
| | 561 | |
|
| 15 | 562 | | hidePrivateChatsLoadingCancellationToken.Cancel(); |
| 15 | 563 | | hidePrivateChatsLoadingCancellationToken = new CancellationTokenSource(); |
| 15 | 564 | | HidePrivateChatsLoadingWhenTimeout(hidePrivateChatsLoadingCancellationToken.Token).Forget(); |
| 15 | 565 | | } |
| | 566 | |
|
| | 567 | | private async UniTaskVoid HidePrivateChatsLoadingWhenTimeout(CancellationToken cancellationToken) |
| | 568 | | { |
| 31 | 569 | | await UniTask.Delay(3000, cancellationToken: cancellationToken); |
| 0 | 570 | | if (cancellationToken.IsCancellationRequested) return; |
| 0 | 571 | | view.HidePrivateChatsLoading(); |
| 0 | 572 | | } |
| | 573 | |
|
| | 574 | | internal void RequestFriendsWithDirectMessagesFromSearch(string userNameOrId, int limit) |
| | 575 | | { |
| 2 | 576 | | view.ShowSearchLoading(); |
| 2 | 577 | | friendsController.GetFriendsWithDirectMessages(userNameOrId, limit); |
| 2 | 578 | | hideLoadingCancellationToken?.Cancel(); |
| 2 | 579 | | hideLoadingCancellationToken = new CancellationTokenSource(); |
| 2 | 580 | | HideSearchLoadingWhenTimeout(hideLoadingCancellationToken.Token).Forget(); |
| 2 | 581 | | } |
| | 582 | |
|
| | 583 | | private void HandleChannelUpdated(Channel channel) |
| | 584 | | { |
| 4 | 585 | | if (!channel.Joined) |
| | 586 | | { |
| 0 | 587 | | view.RemovePublicChat(channel.ChannelId); |
| 0 | 588 | | publicChannels.Remove(channel.ChannelId); |
| 0 | 589 | | return; |
| | 590 | | } |
| | 591 | |
|
| 4 | 592 | | var channelId = channel.ChannelId; |
| 4 | 593 | | var model = new PublicChatModel(channelId, channel.Name, channel.Description, channel.Joined, |
| | 594 | | channel.MemberCount, channel.Muted, showOnlyOnlineMembersOnPublicChannels); |
| | 595 | |
|
| 4 | 596 | | if (publicChannels.ContainsKey(channelId)) |
| 1 | 597 | | publicChannels[channelId].CopyFrom(model); |
| | 598 | | else |
| 3 | 599 | | publicChannels[channelId] = model; |
| | 600 | |
|
| 4 | 601 | | view.SetPublicChat(model); |
| 4 | 602 | | view.HideChannelsLoading(); |
| | 603 | |
|
| | 604 | | // we clear the unseen messages to avoid showing many of them while the user was offline |
| | 605 | | // TODO: we should consider avoid clearing when the channel is private in the future |
| 4 | 606 | | ClearOfflineUnseenMessages(channelId); |
| 4 | 607 | | } |
| | 608 | |
|
| | 609 | | private void ClearOfflineUnseenMessages(string channelId) |
| | 610 | | { |
| 5 | 611 | | if (channelsClearedUnseenNotifications.Contains(channelId)) return; |
| 3 | 612 | | chatController.MarkChannelMessagesAsSeen(channelId); |
| 3 | 613 | | channelsClearedUnseenNotifications.Add(channelId); |
| 3 | 614 | | } |
| | 615 | |
|
| 1 | 616 | | private void HandleAutoChannelJoined(Channel channel) => ReportChannelJoinedToAnalytics(channel, "auto"); |
| | 617 | |
|
| | 618 | | private void HandleChannelJoined(Channel channel) |
| | 619 | | { |
| 2 | 620 | | ReportChannelJoinedToAnalytics(channel, "manual"); |
| 2 | 621 | | OpenPublicChat(channel.ChannelId); |
| 2 | 622 | | } |
| | 623 | |
|
| | 624 | | private void ReportChannelJoinedToAnalytics(Channel channel, string method) |
| | 625 | | { |
| 3 | 626 | | if (channel.MemberCount <= 1) |
| 1 | 627 | | socialAnalytics.SendEmptyChannelCreated(channel.Name, dataStore.channels.channelJoinedSource.Get()); |
| | 628 | | else |
| 2 | 629 | | socialAnalytics.SendPopulatedChannelJoined(channel.Name, dataStore.channels.channelJoinedSource.Get(), m |
| 2 | 630 | | } |
| | 631 | |
|
| | 632 | | private void HandleJoinChannelError(string channelId, ChannelErrorCode errorCode) |
| | 633 | | { |
| 0 | 634 | | if (dataStore.channels.isCreationModalVisible.Get()) return; |
| | 635 | |
|
| | 636 | | switch (errorCode) |
| | 637 | | { |
| | 638 | | case ChannelErrorCode.LimitExceeded: |
| 0 | 639 | | dataStore.channels.currentChannelLimitReached.Set(channelId, true); |
| 0 | 640 | | break; |
| | 641 | | case ChannelErrorCode.Unknown: |
| 0 | 642 | | dataStore.channels.joinChannelError.Set(channelId, true); |
| | 643 | | break; |
| | 644 | | } |
| 0 | 645 | | } |
| | 646 | |
|
| | 647 | | private void HandleLeaveChannelError(string channelId, ChannelErrorCode errorCode) => |
| 0 | 648 | | dataStore.channels.leaveChannelError.Set(channelId, true); |
| | 649 | |
|
| | 650 | | private void HandleChannelLeft(string channelId) |
| | 651 | | { |
| 1 | 652 | | publicChannels.Remove(channelId); |
| 1 | 653 | | view.RemovePublicChat(channelId); |
| 1 | 654 | | var channel = chatController.GetAllocatedChannel(channelId); |
| 1 | 655 | | socialAnalytics.SendLeaveChannel(channel?.Name ?? channelId, dataStore.channels.channelLeaveSource.Get()); |
| 1 | 656 | | } |
| | 657 | |
|
| | 658 | | private void HandleChannelOpened(string channelId, string previousChannelId) |
| | 659 | | { |
| 0 | 660 | | if (string.IsNullOrEmpty(channelId)) |
| 0 | 661 | | return; |
| | 662 | |
|
| 0 | 663 | | OpenPublicChat(channelId); |
| 0 | 664 | | } |
| | 665 | |
|
| | 666 | | private void HandleAskForJoinChannel(string channelName) |
| | 667 | | { |
| 0 | 668 | | chatController.OnAskForJoinChannel -= HandleAskForJoinChannel; |
| | 669 | |
|
| 0 | 670 | | if (!channelsFeatureFlagService.IsAllowedToCreateChannels()) |
| 0 | 671 | | return; |
| | 672 | |
|
| 0 | 673 | | if (rendererState.Get()) |
| 0 | 674 | | dataStore.channels.currentJoinChannelModal.Set(channelName, true); |
| | 675 | | else |
| | 676 | | { |
| 0 | 677 | | channelToJoinAtTheBeginning = channelName; |
| 0 | 678 | | rendererState.OnChange += HandleAskForJoinChannelAfterRendererState; |
| | 679 | | } |
| 0 | 680 | | } |
| | 681 | |
|
| | 682 | | private void HandleAskForJoinChannelAfterRendererState(bool current, bool _) |
| | 683 | | { |
| 0 | 684 | | if (!current) |
| 0 | 685 | | return; |
| | 686 | |
|
| 0 | 687 | | rendererState.OnChange -= HandleAskForJoinChannelAfterRendererState; |
| 0 | 688 | | dataStore.channels.currentJoinChannelModal.Set(channelToJoinAtTheBeginning, true); |
| 0 | 689 | | } |
| | 690 | |
|
| 14 | 691 | | private void RequestUnreadMessages() => chatController.GetUnseenMessagesByUser(); |
| | 692 | |
|
| | 693 | | private void RequestUnreadChannelsMessages() |
| | 694 | | { |
| 14 | 695 | | chatController.GetUnseenMessagesByChannel(); |
| 14 | 696 | | areUnseenMessagesRequestedByFirstTime = true; |
| 14 | 697 | | } |
| | 698 | |
|
| | 699 | | private void OpenChannelSearch() |
| | 700 | | { |
| 0 | 701 | | if (!ownUserProfile.isGuest) |
| 0 | 702 | | OnOpenChannelSearch?.Invoke(); |
| | 703 | | else |
| 0 | 704 | | dataStore.HUDs.connectWalletModalVisible.Set(true); |
| 0 | 705 | | } |
| | 706 | |
|
| | 707 | | private async UniTask HideSearchLoadingWhenTimeout(CancellationToken cancellationToken) |
| | 708 | | { |
| 4 | 709 | | await UniTask.Delay(3000, cancellationToken: cancellationToken); |
| 0 | 710 | | if (cancellationToken.IsCancellationRequested) return; |
| 0 | 711 | | view.HideSearchLoading(); |
| 0 | 712 | | } |
| | 713 | |
|
| 0 | 714 | | private void OnAllowedToCreateChannelsChanged(bool isAllowed) => view.SetCreateChannelButtonActive(isAllowed); |
| | 715 | |
|
| | 716 | | private void SetAutomaticChannelsInfoUpdatingActive(bool isActive) |
| | 717 | | { |
| 31 | 718 | | reloadingChannelsInfoCancellationToken.Cancel(); |
| | 719 | |
|
| 31 | 720 | | if (isActive) |
| | 721 | | { |
| 13 | 722 | | GetCurrentChannelsInfo(); |
| 13 | 723 | | reloadingChannelsInfoCancellationToken = new CancellationTokenSource(); |
| 13 | 724 | | ReloadChannelsInfoPeriodically(reloadingChannelsInfoCancellationToken.Token).Forget(); |
| | 725 | | } |
| 31 | 726 | | } |
| | 727 | |
|
| | 728 | | private async UniTask ReloadChannelsInfoPeriodically(CancellationToken cancellationToken) |
| | 729 | | { |
| 0 | 730 | | while (true) |
| | 731 | | { |
| 26 | 732 | | await UniTask.Delay(MINUTES_FOR_AUTOMATIC_CHANNELS_INFO_RELOADING * 60 * 1000, |
| | 733 | | cancellationToken: cancellationToken); |
| | 734 | |
|
| 0 | 735 | | if (cancellationToken.IsCancellationRequested) |
| 0 | 736 | | return; |
| | 737 | |
|
| 0 | 738 | | GetCurrentChannelsInfo(); |
| | 739 | | } |
| 0 | 740 | | } |
| | 741 | |
|
| | 742 | | private void GetCurrentChannelsInfo() |
| | 743 | | { |
| 13 | 744 | | chatController.GetChannelInfo(publicChannels |
| 13 | 745 | | .Select(x => x.Key) |
| 13 | 746 | | .Where(x => x != ChatUtils.NEARBY_CHANNEL_ID) |
| | 747 | | .ToArray()); |
| 13 | 748 | | } |
| | 749 | |
|
| | 750 | | private void OpenWalletReadme() => |
| 1 | 751 | | browserBridge.OpenUrl("https://docs.decentraland.org/player/blockchain-integration/get-a-wallet/"); |
| | 752 | |
|
| 1 | 753 | | private void SignUp() => userProfileBridge.SignUp(); |
| | 754 | |
|
| | 755 | | private void CopyChannelNameToClipboard(string channelName) |
| | 756 | | { |
| 1 | 757 | | clipboard.WriteText(channelName); |
| 1 | 758 | | copyPasteAnalyticsService.Copy("channel_name"); |
| 1 | 759 | | } |
| | 760 | | } |
| | 761 | | } |