| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Threading; |
| | 4 | | using Cysharp.Threading.Tasks; |
| | 5 | | using DCL.Chat; |
| | 6 | | using DCL.Friends.WebApi; |
| | 7 | | using DCL.Interface; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | public class WorldChatWindowController : IHUD |
| | 11 | | { |
| | 12 | | private const int DMS_PAGE_SIZE = 30; |
| | 13 | | private const int USER_DM_ENTRIES_TO_REQUEST_FOR_SEARCH = 30; |
| | 14 | |
|
| | 15 | | private readonly IUserProfileBridge userProfileBridge; |
| | 16 | | private readonly IFriendsController friendsController; |
| | 17 | | private readonly IChatController chatController; |
| | 18 | |
|
| 35 | 19 | | private readonly Dictionary<string, PublicChatChannelModel> publicChannels = |
| | 20 | | new Dictionary<string, PublicChatChannelModel>(); |
| | 21 | |
|
| 35 | 22 | | private readonly Dictionary<string, ChatMessage> lastPrivateMessages = new Dictionary<string, ChatMessage>(); |
| | 23 | | private int hiddenDMs; |
| 35 | 24 | | private string currentSearch = ""; |
| | 25 | | private bool areDMsRequestedByFirstTime; |
| | 26 | | private bool isRequestingFriendsWithDMs; |
| | 27 | | private int lastSkipForDMs; |
| 35 | 28 | | private CancellationTokenSource hideLoadingCancellationToken = new CancellationTokenSource(); |
| | 29 | | private IWorldChatWindowView view; |
| | 30 | | private UserProfile ownUserProfile; |
| | 31 | |
|
| 0 | 32 | | public IWorldChatWindowView View => view; |
| | 33 | |
|
| | 34 | | public event Action<string> OnOpenPrivateChat; |
| | 35 | | public event Action<string> OnOpenPublicChannel; |
| | 36 | | public event Action OnOpen; |
| | 37 | |
|
| 35 | 38 | | public WorldChatWindowController( |
| | 39 | | IUserProfileBridge userProfileBridge, |
| | 40 | | IFriendsController friendsController, |
| | 41 | | IChatController chatController) |
| | 42 | | { |
| 35 | 43 | | this.userProfileBridge = userProfileBridge; |
| 35 | 44 | | this.friendsController = friendsController; |
| 35 | 45 | | this.chatController = chatController; |
| 35 | 46 | | } |
| | 47 | |
|
| | 48 | | public void Initialize(IWorldChatWindowView view) |
| | 49 | | { |
| 35 | 50 | | this.view = view; |
| 35 | 51 | | view.Initialize(chatController); |
| 35 | 52 | | view.OnClose += HandleViewCloseRequest; |
| 35 | 53 | | view.OnOpenPrivateChat += OpenPrivateChat; |
| 35 | 54 | | view.OnOpenPublicChannel += OpenPublicChannel; |
| 35 | 55 | | view.OnSearchChannelRequested += SearchChannels; |
| 35 | 56 | | view.OnRequireMorePrivateChats += ShowMorePrivateChats; |
| | 57 | |
|
| 35 | 58 | | ownUserProfile = userProfileBridge.GetOwn(); |
| 35 | 59 | | if (ownUserProfile != null) |
| 34 | 60 | | ownUserProfile.OnUpdate += OnUserProfileUpdate; |
| | 61 | |
|
| | 62 | | // TODO: this data should come from the chat service when channels are implemented |
| 35 | 63 | | publicChannels[ChatUtils.NEARBY_CHANNEL_ID] = new PublicChatChannelModel(ChatUtils.NEARBY_CHANNEL_ID, ChatUtils. |
| | 64 | | "Talk to the people around you. If you move far away from someone you will lose contact. All whispers will b |
| 35 | 65 | | view.SetPublicChannel(publicChannels[ChatUtils.NEARBY_CHANNEL_ID]); |
| | 66 | |
|
| 84 | 67 | | foreach (var value in chatController.GetAllocatedEntries()) |
| 7 | 68 | | HandleMessageAdded(value); |
| | 69 | |
|
| 35 | 70 | | if (!friendsController.IsInitialized) |
| 7 | 71 | | if (ownUserProfile?.hasConnectedWeb3 ?? false) |
| 5 | 72 | | view.ShowPrivateChatsLoading(); |
| | 73 | |
|
| 35 | 74 | | chatController.OnAddMessage += HandleMessageAdded; |
| 35 | 75 | | friendsController.OnAddFriendsWithDirectMessages += HandleFriendsWithDirectMessagesAdded; |
| 35 | 76 | | friendsController.OnUpdateUserStatus += HandleUserStatusChanged; |
| 35 | 77 | | friendsController.OnUpdateFriendship += HandleFriendshipUpdated; |
| 35 | 78 | | friendsController.OnInitialized += HandleFriendsControllerInitialization; |
| 35 | 79 | | } |
| | 80 | |
|
| | 81 | | public void Dispose() |
| | 82 | | { |
| 3 | 83 | | view.OnClose -= HandleViewCloseRequest; |
| 3 | 84 | | view.OnOpenPrivateChat -= OpenPrivateChat; |
| 3 | 85 | | view.OnOpenPublicChannel -= OpenPublicChannel; |
| 3 | 86 | | view.OnSearchChannelRequested -= SearchChannels; |
| 3 | 87 | | view.OnRequireMorePrivateChats -= ShowMorePrivateChats; |
| 3 | 88 | | view.Dispose(); |
| 3 | 89 | | chatController.OnAddMessage -= HandleMessageAdded; |
| 3 | 90 | | friendsController.OnAddFriendsWithDirectMessages -= HandleFriendsWithDirectMessagesAdded; |
| 3 | 91 | | friendsController.OnUpdateUserStatus -= HandleUserStatusChanged; |
| 3 | 92 | | friendsController.OnUpdateFriendship -= HandleFriendshipUpdated; |
| 3 | 93 | | friendsController.OnInitialized -= HandleFriendsControllerInitialization; |
| | 94 | |
|
| 3 | 95 | | if (ownUserProfile != null) |
| 2 | 96 | | ownUserProfile.OnUpdate -= OnUserProfileUpdate; |
| 3 | 97 | | } |
| | 98 | |
|
| | 99 | | public void SetVisibility(bool visible) |
| | 100 | | { |
| 14 | 101 | | if (visible) |
| | 102 | | { |
| 8 | 103 | | view.Show(); |
| 8 | 104 | | OnOpen?.Invoke(); |
| | 105 | |
|
| 8 | 106 | | if (friendsController.IsInitialized && !areDMsRequestedByFirstTime) |
| | 107 | | { |
| 6 | 108 | | RequestFriendsWithDirectMessages(); |
| 6 | 109 | | RequestUnreadMessages(); |
| | 110 | | } |
| 6 | 111 | | } |
| | 112 | | else |
| 6 | 113 | | view.Hide(); |
| 8 | 114 | | } |
| | 115 | |
|
| | 116 | | private void HandleFriendsControllerInitialization() |
| | 117 | | { |
| 2 | 118 | | if (view.IsActive && !areDMsRequestedByFirstTime) |
| | 119 | | { |
| 1 | 120 | | RequestFriendsWithDirectMessages(); |
| 1 | 121 | | lastSkipForDMs += DMS_PAGE_SIZE; |
| 1 | 122 | | RequestUnreadMessages(); |
| 1 | 123 | | } |
| | 124 | | else |
| 1 | 125 | | view.HidePrivateChatsLoading(); |
| 1 | 126 | | } |
| | 127 | |
|
| | 128 | | private void OpenPrivateChat(string userId) |
| | 129 | | { |
| 1 | 130 | | OnOpenPrivateChat?.Invoke(userId); |
| 1 | 131 | | } |
| | 132 | |
|
| 1 | 133 | | private void OpenPublicChannel(string channelId) => OnOpenPublicChannel?.Invoke(channelId); |
| | 134 | |
|
| 1 | 135 | | private void HandleViewCloseRequest() => SetVisibility(false); |
| | 136 | |
|
| | 137 | | private void HandleFriendshipUpdated(string userId, FriendshipAction friendship) |
| | 138 | | { |
| 6 | 139 | | if (friendship != FriendshipAction.APPROVED) |
| | 140 | | { |
| | 141 | | // show only private chats from friends. Change it whenever the catalyst supports to send pms to any user |
| 6 | 142 | | view.RemovePrivateChat(userId); |
| | 143 | | } |
| 6 | 144 | | } |
| | 145 | |
|
| | 146 | | private void HandleUserStatusChanged(string userId, UserStatus status) |
| | 147 | | { |
| 4 | 148 | | if (!lastPrivateMessages.ContainsKey(userId)) return; |
| | 149 | |
|
| 4 | 150 | | if (status.friendshipStatus != FriendshipStatus.FRIEND) |
| | 151 | | { |
| | 152 | | // show only private chats from friends. Change it whenever the catalyst supports to send pms to any user |
| 4 | 153 | | view.RemovePrivateChat(userId); |
| | 154 | | } |
| 4 | 155 | | } |
| | 156 | |
|
| | 157 | | private void HandleMessageAdded(ChatMessage message) |
| | 158 | | { |
| 11 | 159 | | if (message.messageType != ChatMessage.Type.PRIVATE) return; |
| | 160 | |
|
| 7 | 161 | | var userId = ExtractRecipientId(message); |
| | 162 | |
|
| 7 | 163 | | if (lastPrivateMessages.ContainsKey(userId)) |
| | 164 | | { |
| 0 | 165 | | if (message.timestamp > lastPrivateMessages[userId].timestamp) |
| 0 | 166 | | lastPrivateMessages[userId] = message; |
| 0 | 167 | | } |
| | 168 | | else |
| 7 | 169 | | lastPrivateMessages[userId] = message; |
| | 170 | |
|
| 7 | 171 | | var profile = userProfileBridge.Get(userId); |
| 7 | 172 | | if (profile == null) return; |
| | 173 | |
|
| 7 | 174 | | view.SetPrivateChat(CreatePrivateChatModel(message, profile)); |
| 7 | 175 | | } |
| | 176 | |
|
| | 177 | | private void HandleFriendsWithDirectMessagesAdded(List<FriendWithDirectMessages> usersWithDM) |
| | 178 | | { |
| 12 | 179 | | for (var i = 0; i < usersWithDM.Count; i++) |
| | 180 | | { |
| 3 | 181 | | var profile = userProfileBridge.Get(usersWithDM[i].userId); |
| 3 | 182 | | if (profile == null) continue; |
| | 183 | |
|
| 0 | 184 | | var lastMessage = new ChatMessage |
| | 185 | | { |
| | 186 | | messageType = ChatMessage.Type.PRIVATE, |
| | 187 | | body = usersWithDM[i].lastMessageBody, |
| | 188 | | timestamp = (ulong) usersWithDM[i].lastMessageTimestamp |
| | 189 | | }; |
| | 190 | |
|
| 0 | 191 | | if (lastPrivateMessages.ContainsKey(profile.userId)) |
| | 192 | | { |
| 0 | 193 | | if (lastMessage.timestamp > lastPrivateMessages[profile.userId].timestamp) |
| 0 | 194 | | lastPrivateMessages[profile.userId] = lastMessage; |
| 0 | 195 | | } |
| | 196 | | else |
| 0 | 197 | | lastPrivateMessages[profile.userId] = lastMessage; |
| | 198 | |
|
| 0 | 199 | | view.SetPrivateChat(CreatePrivateChatModel(lastMessage, profile)); |
| | 200 | | } |
| | 201 | |
|
| 3 | 202 | | UpdateMoreChannelsToLoadHint(); |
| 3 | 203 | | view.HidePrivateChatsLoading(); |
| 3 | 204 | | view.HideSearchLoading(); |
| | 205 | |
|
| 3 | 206 | | isRequestingFriendsWithDMs = false; |
| 3 | 207 | | } |
| | 208 | |
|
| | 209 | | private PrivateChatModel CreatePrivateChatModel(ChatMessage recentMessage, UserProfile profile) |
| | 210 | | { |
| 7 | 211 | | return new PrivateChatModel |
| | 212 | | { |
| | 213 | | user = profile, |
| | 214 | | recentMessage = recentMessage, |
| | 215 | | isBlocked = ownUserProfile.IsBlocked(profile.userId), |
| | 216 | | isOnline = friendsController.GetUserStatus(profile.userId).presence == PresenceStatus.ONLINE |
| | 217 | | }; |
| | 218 | | } |
| | 219 | |
|
| | 220 | | private string ExtractRecipientId(ChatMessage message) |
| | 221 | | { |
| 7 | 222 | | return message.sender != ownUserProfile.userId ? message.sender : message.recipient; |
| | 223 | | } |
| | 224 | |
|
| | 225 | | private void OnUserProfileUpdate(UserProfile profile) |
| | 226 | | { |
| 1 | 227 | | view.RefreshBlockedDirectMessages(profile.blocked); |
| | 228 | |
|
| 1 | 229 | | if (!profile.hasConnectedWeb3) |
| 1 | 230 | | view.HidePrivateChatsLoading(); |
| 1 | 231 | | } |
| | 232 | |
|
| | 233 | | private void SearchChannels(string search) |
| | 234 | | { |
| 2 | 235 | | currentSearch = search; |
| | 236 | |
|
| 2 | 237 | | if (string.IsNullOrEmpty(search)) |
| | 238 | | { |
| 1 | 239 | | View.DisableSearchMode(); |
| 1 | 240 | | UpdateMoreChannelsToLoadHint(); |
| 1 | 241 | | return; |
| | 242 | | } |
| | 243 | |
|
| 1 | 244 | | UpdateMoreChannelsToLoadHint(); |
| 1 | 245 | | View.EnableSearchMode(); |
| 1 | 246 | | RequestFriendsWithDirectMessagesFromSearch(search, USER_DM_ENTRIES_TO_REQUEST_FOR_SEARCH); |
| 1 | 247 | | } |
| | 248 | |
|
| | 249 | | private void ShowMorePrivateChats() |
| | 250 | | { |
| 1 | 251 | | if (isRequestingFriendsWithDMs || |
| | 252 | | hiddenDMs == 0 || |
| | 253 | | !string.IsNullOrEmpty(currentSearch)) |
| 0 | 254 | | return; |
| | 255 | |
|
| 1 | 256 | | RequestFriendsWithDirectMessages(); |
| 1 | 257 | | } |
| | 258 | |
|
| | 259 | | private void UpdateMoreChannelsToLoadHint() |
| | 260 | | { |
| 5 | 261 | | hiddenDMs = Mathf.Clamp(friendsController.TotalFriendsWithDirectMessagesCount - lastSkipForDMs, |
| | 262 | | 0, |
| | 263 | | friendsController.TotalFriendsWithDirectMessagesCount); |
| | 264 | |
|
| 5 | 265 | | if (hiddenDMs <= 0 || !string.IsNullOrEmpty(currentSearch)) |
| 3 | 266 | | View.HideMoreChatsToLoadHint(); |
| | 267 | | else |
| 2 | 268 | | View.ShowMoreChatsToLoadHint(hiddenDMs); |
| 2 | 269 | | } |
| | 270 | |
|
| | 271 | | private void RequestFriendsWithDirectMessages() |
| | 272 | | { |
| 8 | 273 | | isRequestingFriendsWithDMs = true; |
| | 274 | |
|
| 8 | 275 | | if (!areDMsRequestedByFirstTime) |
| | 276 | | { |
| 7 | 277 | | view.ShowPrivateChatsLoading(); |
| 7 | 278 | | view.HideMoreChatsToLoadHint(); |
| | 279 | | } |
| | 280 | |
|
| 8 | 281 | | friendsController.GetFriendsWithDirectMessages(DMS_PAGE_SIZE, lastSkipForDMs); |
| 8 | 282 | | lastSkipForDMs += DMS_PAGE_SIZE; |
| 8 | 283 | | areDMsRequestedByFirstTime = true; |
| 8 | 284 | | } |
| | 285 | |
|
| | 286 | | internal void RequestFriendsWithDirectMessagesFromSearch(string userNameOrId, int limit) |
| | 287 | | { |
| 2 | 288 | | view.ShowSearchLoading(); |
| 2 | 289 | | friendsController.GetFriendsWithDirectMessages(userNameOrId, limit); |
| 2 | 290 | | hideLoadingCancellationToken?.Cancel(); |
| 2 | 291 | | hideLoadingCancellationToken = new CancellationTokenSource(); |
| 2 | 292 | | HideSearchLoadingWhenTimeout(hideLoadingCancellationToken.Token).Forget(); |
| 2 | 293 | | } |
| | 294 | |
|
| 7 | 295 | | private void RequestUnreadMessages() => chatController.GetUnseenMessagesByUser(); |
| | 296 | |
|
| | 297 | | private async UniTask HideSearchLoadingWhenTimeout(CancellationToken cancellationToken) |
| | 298 | | { |
| 4 | 299 | | await UniTask.Delay(3000, cancellationToken: cancellationToken); |
| 0 | 300 | | if (cancellationToken.IsCancellationRequested) return; |
| 0 | 301 | | view.HideSearchLoading(); |
| 0 | 302 | | } |
| | 303 | | } |