| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Text.RegularExpressions; |
| | 5 | | using DCL.Interface; |
| | 6 | |
|
| | 7 | | public class WorldChatWindowController : IHUD |
| | 8 | | { |
| | 9 | | private const string GENERAL_CHANNEL_ID = "general"; |
| | 10 | | private const int MAX_SEARCHED_CHANNELS = 100; |
| | 11 | | private const int LOAD_PRIVATE_CHATS_ON_DEMAND_COUNT = 30; |
| | 12 | | private const int INITIAL_DISPLAYED_PRIVATE_CHAT_COUNT = 50; |
| | 13 | |
|
| | 14 | | private readonly IUserProfileBridge userProfileBridge; |
| | 15 | | private readonly IFriendsController friendsController; |
| | 16 | | private readonly IChatController chatController; |
| | 17 | | private readonly ILastReadMessagesService lastReadMessagesService; |
| 21 | 18 | | private readonly Queue<string> pendingPrivateChats = new Queue<string>(); |
| 21 | 19 | | private readonly Dictionary<string, PublicChatChannelModel> publicChannels = new Dictionary<string, PublicChatChanne |
| 21 | 20 | | private readonly Dictionary<string, UserProfile> recipientsFromPrivateChats = new Dictionary<string, UserProfile>(); |
| 21 | 21 | | private readonly Dictionary<string, ChatMessage> lastPrivateMessages = new Dictionary<string, ChatMessage>(); |
| | 22 | |
|
| | 23 | | private IWorldChatWindowView view; |
| | 24 | | private UserProfile ownUserProfile; |
| | 25 | |
|
| 0 | 26 | | public IWorldChatWindowView View => view; |
| | 27 | |
|
| | 28 | | public event Action<string> OnOpenPrivateChat; |
| | 29 | | public event Action<string> OnOpenPublicChannel; |
| | 30 | | public event Action OnOpen; |
| | 31 | |
|
| 21 | 32 | | public WorldChatWindowController( |
| | 33 | | IUserProfileBridge userProfileBridge, |
| | 34 | | IFriendsController friendsController, |
| | 35 | | IChatController chatController, |
| | 36 | | ILastReadMessagesService lastReadMessagesService) |
| | 37 | | { |
| 21 | 38 | | this.userProfileBridge = userProfileBridge; |
| 21 | 39 | | this.friendsController = friendsController; |
| 21 | 40 | | this.chatController = chatController; |
| 21 | 41 | | this.lastReadMessagesService = lastReadMessagesService; |
| 21 | 42 | | } |
| | 43 | |
|
| | 44 | | public void Initialize(IWorldChatWindowView view) |
| | 45 | | { |
| 21 | 46 | | this.view = view; |
| 21 | 47 | | view.Initialize(chatController, lastReadMessagesService); |
| 21 | 48 | | view.OnClose += HandleViewCloseRequest; |
| 21 | 49 | | view.OnOpenPrivateChat += OpenPrivateChat; |
| 21 | 50 | | view.OnOpenPublicChannel += OpenPublicChannel; |
| 21 | 51 | | view.OnSearchChannelRequested += SearchChannels; |
| 21 | 52 | | view.OnRequireMorePrivateChats += ShowMorePrivateChats; |
| | 53 | |
|
| 21 | 54 | | ownUserProfile = userProfileBridge.GetOwn(); |
| 21 | 55 | | if (ownUserProfile != null) |
| 20 | 56 | | ownUserProfile.OnUpdate += OnUserProfileUpdate; |
| | 57 | |
|
| | 58 | | // TODO: this data should come from the chat service when channels are implemented |
| 21 | 59 | | publicChannels[GENERAL_CHANNEL_ID] = new PublicChatChannelModel(GENERAL_CHANNEL_ID, "nearby", |
| | 60 | | "Talk to the people around you. If you move far away from someone you will lose contact. All whispers will b |
| 21 | 61 | | view.SetPublicChannel(publicChannels[GENERAL_CHANNEL_ID]); |
| | 62 | |
|
| 60 | 63 | | foreach (var value in chatController.GetEntries()) |
| 9 | 64 | | HandleMessageAdded(value); |
| | 65 | |
|
| 21 | 66 | | if (!friendsController.isInitialized) |
| 7 | 67 | | if (ownUserProfile?.hasConnectedWeb3 ?? false) |
| 5 | 68 | | view.ShowPrivateChatsLoading(); |
| | 69 | |
|
| 21 | 70 | | chatController.OnAddMessage += HandleMessageAdded; |
| 21 | 71 | | friendsController.OnUpdateUserStatus += HandleUserStatusChanged; |
| 21 | 72 | | friendsController.OnInitialized += HandleFriendsControllerInitialization; |
| | 73 | |
|
| 21 | 74 | | UpdateMoreChannelsToLoadHint(); |
| 21 | 75 | | } |
| | 76 | |
|
| | 77 | | public void Dispose() |
| | 78 | | { |
| 3 | 79 | | view.OnClose -= HandleViewCloseRequest; |
| 3 | 80 | | view.OnOpenPrivateChat -= OpenPrivateChat; |
| 3 | 81 | | view.OnOpenPublicChannel -= OpenPublicChannel; |
| 3 | 82 | | view.OnSearchChannelRequested -= SearchChannels; |
| 3 | 83 | | view.OnRequireMorePrivateChats -= ShowMorePrivateChats; |
| 3 | 84 | | view.Dispose(); |
| 3 | 85 | | chatController.OnAddMessage -= HandleMessageAdded; |
| 3 | 86 | | friendsController.OnUpdateUserStatus -= HandleUserStatusChanged; |
| 3 | 87 | | friendsController.OnInitialized -= HandleFriendsControllerInitialization; |
| | 88 | |
|
| 3 | 89 | | if (ownUserProfile != null) |
| 2 | 90 | | ownUserProfile.OnUpdate -= OnUserProfileUpdate; |
| 3 | 91 | | } |
| | 92 | |
|
| | 93 | | public void SetVisibility(bool visible) |
| | 94 | | { |
| 8 | 95 | | if (visible) |
| | 96 | | { |
| 2 | 97 | | view.Show(); |
| 2 | 98 | | OnOpen?.Invoke(); |
| 1 | 99 | | } |
| | 100 | | else |
| 6 | 101 | | view.Hide(); |
| 6 | 102 | | } |
| | 103 | |
|
| | 104 | | private void HandleFriendsControllerInitialization() |
| | 105 | | { |
| 1 | 106 | | view.HidePrivateChatsLoading(); |
| | 107 | |
|
| | 108 | | // show only private chats from friends. Change it whenever the catalyst supports to send pms to any user |
| 2 | 109 | | foreach(var userId in recipientsFromPrivateChats.Keys) |
| 0 | 110 | | if (!friendsController.IsFriend(userId)) |
| 0 | 111 | | view.RemovePrivateChat(userId); |
| 1 | 112 | | } |
| | 113 | |
|
| 1 | 114 | | private void OpenPrivateChat(string userId) => OnOpenPrivateChat?.Invoke(userId); |
| | 115 | |
|
| 1 | 116 | | private void OpenPublicChannel(string channelId) => OnOpenPublicChannel?.Invoke(channelId); |
| | 117 | |
|
| 1 | 118 | | private void HandleViewCloseRequest() => SetVisibility(false); |
| | 119 | |
|
| | 120 | | private void HandleUserStatusChanged(string userId, FriendsController.UserStatus status) |
| | 121 | | { |
| 2 | 122 | | if (!recipientsFromPrivateChats.ContainsKey(userId)) return; |
| 2 | 123 | | if (!lastPrivateMessages.ContainsKey(userId)) return; |
| 2 | 124 | | if (pendingPrivateChats.Contains(userId)) return; |
| | 125 | |
|
| 2 | 126 | | if (status.friendshipStatus == FriendshipStatus.FRIEND) |
| | 127 | | { |
| 1 | 128 | | var profile = recipientsFromPrivateChats[userId]; |
| | 129 | |
|
| 1 | 130 | | if (ShouldDisplayPrivateChat(profile.userId)) |
| | 131 | | { |
| 1 | 132 | | view.SetPrivateChat(new PrivateChatModel |
| | 133 | | { |
| | 134 | | user = profile, |
| | 135 | | recentMessage = lastPrivateMessages[userId], |
| | 136 | | isBlocked = ownUserProfile.IsBlocked(userId), |
| | 137 | | isOnline = status.presence == PresenceStatus.ONLINE |
| | 138 | | }); |
| 1 | 139 | | } |
| 0 | 140 | | else if (!pendingPrivateChats.Contains(profile.userId)) |
| | 141 | | { |
| 0 | 142 | | pendingPrivateChats.Enqueue(profile.userId); |
| 0 | 143 | | UpdateMoreChannelsToLoadHint(); |
| | 144 | | } |
| 0 | 145 | | } |
| 1 | 146 | | else if (status.friendshipStatus == FriendshipStatus.NOT_FRIEND) |
| | 147 | | { |
| | 148 | | // show only private chats from friends. Change it whenever the catalyst supports to send pms to any user |
| 1 | 149 | | view.RemovePrivateChat(userId); |
| | 150 | | } |
| 1 | 151 | | } |
| | 152 | |
|
| | 153 | | private void HandleMessageAdded(ChatMessage message) |
| | 154 | | { |
| 14 | 155 | | if (message.messageType != ChatMessage.Type.PRIVATE) return; |
| 10 | 156 | | var profile = ExtractRecipient(message); |
| 10 | 157 | | if (profile == null) return; |
| | 158 | |
|
| 10 | 159 | | if (friendsController.isInitialized) |
| 10 | 160 | | if (!friendsController.IsFriend(profile.userId)) |
| 0 | 161 | | return; |
| | 162 | |
|
| 10 | 163 | | if (lastPrivateMessages.ContainsKey(profile.userId)) |
| | 164 | | { |
| 0 | 165 | | if (message.timestamp > lastPrivateMessages[profile.userId].timestamp) |
| 0 | 166 | | lastPrivateMessages[profile.userId] = message; |
| 0 | 167 | | } |
| | 168 | | else |
| 10 | 169 | | lastPrivateMessages[profile.userId] = message; |
| | 170 | |
|
| 10 | 171 | | recipientsFromPrivateChats[profile.userId] = profile; |
| | 172 | |
|
| 10 | 173 | | if (ShouldDisplayPrivateChat(profile.userId)) |
| 9 | 174 | | view.SetPrivateChat(CreatePrivateChatModel(message, profile)); |
| 1 | 175 | | else if (!pendingPrivateChats.Contains(profile.userId)) |
| | 176 | | { |
| 1 | 177 | | pendingPrivateChats.Enqueue(profile.userId); |
| 1 | 178 | | UpdateMoreChannelsToLoadHint(); |
| | 179 | | } |
| 1 | 180 | | } |
| | 181 | |
|
| | 182 | | private bool ShouldDisplayPrivateChat(string userId) |
| | 183 | | { |
| 11 | 184 | | if (!friendsController.isInitialized) return false; |
| 20 | 185 | | if (view.PrivateChannelsCount < INITIAL_DISPLAYED_PRIVATE_CHAT_COUNT) return true; |
| 3 | 186 | | if (view.ContainsPrivateChannel(userId)) return true; |
| 1 | 187 | | return false; |
| | 188 | | } |
| | 189 | |
|
| | 190 | | private PrivateChatModel CreatePrivateChatModel(ChatMessage recentMessage, UserProfile profile) |
| | 191 | | { |
| 10 | 192 | | return new PrivateChatModel |
| | 193 | | { |
| | 194 | | user = profile, |
| | 195 | | recentMessage = recentMessage, |
| | 196 | | isBlocked = ownUserProfile.IsBlocked(profile.userId), |
| | 197 | | isOnline = friendsController.GetUserStatus(profile.userId).presence == PresenceStatus.ONLINE |
| | 198 | | }; |
| | 199 | | } |
| | 200 | |
|
| | 201 | | private UserProfile ExtractRecipient(ChatMessage message) => |
| 10 | 202 | | userProfileBridge.Get(message.sender != ownUserProfile.userId ? message.sender : message.recipient); |
| | 203 | |
|
| | 204 | | private void OnUserProfileUpdate(UserProfile profile) |
| | 205 | | { |
| 1 | 206 | | view.RefreshBlockedDirectMessages(profile.blocked); |
| | 207 | |
|
| 1 | 208 | | if (!profile.hasConnectedWeb3) |
| 1 | 209 | | view.HidePrivateChatsLoading(); |
| 1 | 210 | | } |
| | 211 | |
|
| | 212 | | private void ShowOrHideMoreFriendsToLoadHint() |
| | 213 | | { |
| 1 | 214 | | if (pendingPrivateChats.Count == 0) |
| 1 | 215 | | View.HideMoreChatsToLoadHint(); |
| | 216 | | else |
| 0 | 217 | | View.ShowMoreChatsToLoadHint(pendingPrivateChats.Count); |
| 0 | 218 | | } |
| | 219 | |
|
| | 220 | | private void SearchChannels(string search) |
| | 221 | | { |
| 2 | 222 | | if (string.IsNullOrEmpty(search)) |
| | 223 | | { |
| 1 | 224 | | View.ClearFilter(); |
| 1 | 225 | | ShowOrHideMoreFriendsToLoadHint(); |
| 1 | 226 | | return; |
| | 227 | | } |
| | 228 | |
|
| | 229 | | Dictionary<string, PrivateChatModel> FilterPrivateChannelsByUserName(string search) |
| | 230 | | { |
| 1 | 231 | | var regex = new Regex(search, RegexOptions.IgnoreCase); |
| | 232 | |
|
| 1 | 233 | | return recipientsFromPrivateChats.Values.Where(profile => |
| 4 | 234 | | !string.IsNullOrEmpty(profile.userName) && regex.IsMatch(profile.userName)) |
| | 235 | | .Take(MAX_SEARCHED_CHANNELS) |
| 2 | 236 | | .ToDictionary(model => model.userId, profile => CreatePrivateChatModel(lastPrivateMessages[profile.userI |
| | 237 | | } |
| | 238 | |
|
| | 239 | | Dictionary<string, PublicChatChannelModel> FilterPublicChannelsByName(string search) |
| | 240 | | { |
| 1 | 241 | | var regex = new Regex(search, RegexOptions.IgnoreCase); |
| | 242 | |
|
| 1 | 243 | | return publicChannels.Values |
| 1 | 244 | | .Where(model => !string.IsNullOrEmpty(model.name) && regex.IsMatch(model.name)) |
| | 245 | | .Take(MAX_SEARCHED_CHANNELS) |
| 2 | 246 | | .ToDictionary(model => model.channelId, model => model); |
| | 247 | | } |
| | 248 | |
|
| 1 | 249 | | View.Filter(FilterPrivateChannelsByUserName(search), FilterPublicChannelsByName(search)); |
| 1 | 250 | | } |
| | 251 | |
|
| | 252 | | private void ShowMorePrivateChats() |
| | 253 | | { |
| 0 | 254 | | for (var i = 0; i < LOAD_PRIVATE_CHATS_ON_DEMAND_COUNT && pendingPrivateChats.Count > 0; i++) |
| | 255 | | { |
| 0 | 256 | | var userId = pendingPrivateChats.Dequeue(); |
| 0 | 257 | | if (!lastPrivateMessages.ContainsKey(userId)) continue; |
| 0 | 258 | | if (!recipientsFromPrivateChats.ContainsKey(userId)) continue; |
| 0 | 259 | | var recentMessage = lastPrivateMessages[userId]; |
| 0 | 260 | | var profile = recipientsFromPrivateChats[userId]; |
| 0 | 261 | | View.SetPrivateChat(CreatePrivateChatModel(recentMessage, profile)); |
| | 262 | | } |
| | 263 | |
|
| 0 | 264 | | UpdateMoreChannelsToLoadHint(); |
| 0 | 265 | | } |
| | 266 | |
|
| | 267 | | private void UpdateMoreChannelsToLoadHint() |
| | 268 | | { |
| 22 | 269 | | if (pendingPrivateChats.Count == 0) |
| 21 | 270 | | View.HideMoreChatsToLoadHint(); |
| | 271 | | else |
| 1 | 272 | | View.ShowMoreChatsToLoadHint(pendingPrivateChats.Count); |
| 1 | 273 | | } |
| | 274 | | } |