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