| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL.Chat.Channels; |
| | 5 | | using DCL.Chat.WebApi; |
| | 6 | | using DCL.Interface; |
| | 7 | | using JetBrains.Annotations; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace DCL.Chat |
| | 11 | | { |
| | 12 | |
|
| | 13 | | public partial class ChatController : MonoBehaviour, IChatController |
| | 14 | | { |
| | 15 | | private const string NEARBY_CHANNEL_DESCRIPTION = |
| | 16 | | "Talk to the people around you. If you move far away from someone you will lose contact. All whispers will b |
| | 17 | |
|
| | 18 | | private const string NEARBY_CHANNEL_ID = "nearby"; |
| | 19 | |
|
| 0 | 20 | | public static ChatController i { get; private set; } |
| | 21 | |
|
| 2 | 22 | | private readonly Dictionary<string, int> unseenMessagesByUser = new Dictionary<string, int>(); |
| 2 | 23 | | private readonly Dictionary<string, int> unseenMessagesByChannel = new Dictionary<string, int>(); |
| 2 | 24 | | private readonly Dictionary<string, Channel> channels = new Dictionary<string, Channel>(); |
| 2 | 25 | | private readonly List<ChatMessage> messages = new List<ChatMessage>(); |
| 0 | 26 | | private BaseVariable<HashSet<string>> autoJoinChannelList => DataStore.i.HUDs.autoJoinChannelList; |
| | 27 | | private bool chatAlreadyInitialized; |
| | 28 | | private int totalUnseenMessages; |
| | 29 | |
|
| | 30 | | public event Action<Channel> OnChannelUpdated; |
| | 31 | | public event Action<Channel> OnChannelJoined; |
| | 32 | | public event Action<string, ChannelErrorCode> OnJoinChannelError; |
| | 33 | | public event Action<string> OnChannelLeft; |
| | 34 | | public event Action<string, ChannelErrorCode> OnChannelLeaveError; |
| | 35 | | public event Action<string, ChannelErrorCode> OnMuteChannelError; |
| | 36 | | public event Action OnInitialized; |
| | 37 | | public event Action<ChatMessage> OnAddMessage; |
| | 38 | | public event Action<int> OnTotalUnseenMessagesUpdated; |
| | 39 | | public event Action<string, int> OnUserUnseenMessagesUpdated; |
| | 40 | | public event Action<string, ChannelMember[]> OnUpdateChannelMembers; |
| | 41 | | public event Action<string, Channel[]> OnChannelSearchResult; |
| | 42 | | public event Action<string, int> OnChannelUnseenMessagesUpdated; |
| | 43 | |
|
| | 44 | | // since kernel does not calculate the #nearby channel unseen messages, it is handled on renderer side |
| 2 | 45 | | public int TotalUnseenMessages => totalUnseenMessages |
| | 46 | | + (unseenMessagesByChannel.ContainsKey(NEARBY_CHANNEL_ID) |
| | 47 | | ? unseenMessagesByChannel[NEARBY_CHANNEL_ID] |
| | 48 | | : 0); |
| | 49 | |
|
| | 50 | | public void Awake() |
| | 51 | | { |
| 2 | 52 | | i = this; |
| | 53 | |
|
| 2 | 54 | | channels[NEARBY_CHANNEL_ID] = new Channel(NEARBY_CHANNEL_ID, NEARBY_CHANNEL_ID, 0, 0, true, false, |
| | 55 | | NEARBY_CHANNEL_DESCRIPTION); |
| 2 | 56 | | } |
| | 57 | |
|
| | 58 | | // called by kernel |
| | 59 | | [PublicAPI] |
| | 60 | | public void InitializeChat(string json) |
| | 61 | | { |
| 0 | 62 | | if (chatAlreadyInitialized) |
| 0 | 63 | | return; |
| | 64 | |
|
| 0 | 65 | | var msg = JsonUtility.FromJson<InitializeChatPayload>(json); |
| | 66 | |
|
| 0 | 67 | | totalUnseenMessages = msg.totalUnseenMessages; |
| 0 | 68 | | OnInitialized?.Invoke(); |
| 0 | 69 | | OnTotalUnseenMessagesUpdated?.Invoke(TotalUnseenMessages); |
| 0 | 70 | | chatAlreadyInitialized = true; |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | // called by kernel |
| | 74 | | [PublicAPI] |
| | 75 | | public void AddMessageToChatWindow(string jsonMessage) => |
| 0 | 76 | | AddMessage(JsonUtility.FromJson<ChatMessage>(jsonMessage)); |
| | 77 | |
|
| | 78 | | // called by kernel |
| | 79 | | [PublicAPI] |
| | 80 | | public void AddChatMessages(string jsonMessage) |
| | 81 | | { |
| 0 | 82 | | var messages = JsonUtility.FromJson<ChatMessageListPayload>(jsonMessage); |
| | 83 | |
|
| 0 | 84 | | if (messages == null) return; |
| | 85 | |
|
| 0 | 86 | | foreach (var message in messages.messages) |
| 0 | 87 | | AddMessage(message); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | // called by kernel |
| | 91 | | [PublicAPI] |
| | 92 | | public void UpdateTotalUnseenMessages(string json) |
| | 93 | | { |
| 0 | 94 | | var msg = JsonUtility.FromJson<UpdateTotalUnseenMessagesPayload>(json); |
| 0 | 95 | | totalUnseenMessages = msg.total; |
| 0 | 96 | | OnTotalUnseenMessagesUpdated?.Invoke(TotalUnseenMessages); |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | // called by kernel |
| | 100 | | [PublicAPI] |
| | 101 | | public void UpdateUserUnseenMessages(string json) |
| | 102 | | { |
| 0 | 103 | | var msg = JsonUtility.FromJson<UpdateUserUnseenMessagesPayload>(json); |
| 0 | 104 | | unseenMessagesByUser[msg.userId] = msg.total; |
| 0 | 105 | | OnUserUnseenMessagesUpdated?.Invoke(msg.userId, msg.total); |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | // called by kernel |
| | 109 | | [PublicAPI] |
| | 110 | | public void UpdateTotalUnseenMessagesByUser(string json) |
| | 111 | | { |
| 0 | 112 | | var msg = JsonUtility.FromJson<UpdateTotalUnseenMessagesByUserPayload>(json); |
| | 113 | |
|
| 0 | 114 | | foreach (var unseenMessages in msg.unseenPrivateMessages) |
| | 115 | | { |
| 0 | 116 | | var userId = unseenMessages.userId; |
| 0 | 117 | | var count = unseenMessages.count; |
| 0 | 118 | | unseenMessagesByUser[userId] = count; |
| 0 | 119 | | OnUserUnseenMessagesUpdated?.Invoke(userId, count); |
| | 120 | | } |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | // called by kernel |
| | 124 | | [PublicAPI] |
| | 125 | | public void UpdateTotalUnseenMessagesByChannel(string json) |
| | 126 | | { |
| 0 | 127 | | var msg = JsonUtility.FromJson<UpdateTotalUnseenMessagesByChannelPayload>(json); |
| 0 | 128 | | foreach (var unseenMessages in msg.unseenChannelMessages) |
| 0 | 129 | | UpdateTotalUnseenMessagesByChannel(unseenMessages.channelId, unseenMessages.count); |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | // called by kernel |
| | 133 | | [PublicAPI] |
| | 134 | | public void UpdateChannelMembers(string payload) |
| | 135 | | { |
| 0 | 136 | | var msg = JsonUtility.FromJson<UpdateChannelMembersPayload>(payload); |
| 0 | 137 | | OnUpdateChannelMembers?.Invoke(msg.channelId, msg.members); |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | // called by kernel |
| | 141 | | [PublicAPI] |
| | 142 | | public void JoinChannelConfirmation(string payload) |
| | 143 | | { |
| 0 | 144 | | var msg = JsonUtility.FromJson<ChannelInfoPayloads>(payload); |
| | 145 | |
|
| 0 | 146 | | if (msg.channelInfoPayload.Length == 0) |
| 0 | 147 | | return; |
| | 148 | |
|
| 0 | 149 | | var channelInfo = msg.channelInfoPayload[0]; |
| 0 | 150 | | var channel = new Channel(channelInfo.channelId, channelInfo.name, channelInfo.unseenMessages, channelInfo.m |
| 0 | 151 | | var channelId = channel.ChannelId; |
| | 152 | |
|
| 0 | 153 | | if (channels.ContainsKey(channelId)) |
| 0 | 154 | | channels[channelId].CopyFrom(channel); |
| | 155 | | else |
| 0 | 156 | | channels[channelId] = channel; |
| | 157 | |
|
| | 158 | | //TODO: rework as explained in PR #3351 |
| 0 | 159 | | if(!autoJoinChannelList.Get().Contains(channelId)) |
| | 160 | | { |
| 0 | 161 | | OnChannelJoined?.Invoke(channel); |
| | 162 | | } |
| | 163 | |
|
| 0 | 164 | | OnChannelUpdated?.Invoke(channel); |
| 0 | 165 | | autoJoinChannelList.Get().Remove(channelId); |
| | 166 | |
|
| | 167 | | // TODO (responsibility issues): extract to another class |
| 0 | 168 | | AudioScriptableObjects.joinChannel.Play(true); |
| | 169 | |
|
| 0 | 170 | | SendChannelWelcomeMessage(channel); |
| 0 | 171 | | } |
| | 172 | |
|
| | 173 | | // called by kernel |
| | 174 | | [PublicAPI] |
| | 175 | | public void JoinChannelError(string payload) |
| | 176 | | { |
| 0 | 177 | | var msg = JsonUtility.FromJson<JoinChannelErrorPayload>(payload); |
| 0 | 178 | | OnJoinChannelError?.Invoke(msg.channelId, (ChannelErrorCode) msg.errorCode); |
| | 179 | |
|
| 0 | 180 | | autoJoinChannelList.Get().Remove(msg.channelId); |
| 0 | 181 | | } |
| | 182 | |
|
| | 183 | | // called by kernel |
| | 184 | | [PublicAPI] |
| | 185 | | public void LeaveChannelError(string payload) |
| | 186 | | { |
| 0 | 187 | | var msg = JsonUtility.FromJson<JoinChannelErrorPayload>(payload); |
| 0 | 188 | | OnChannelLeaveError?.Invoke(msg.channelId, (ChannelErrorCode) msg.errorCode); |
| | 189 | |
|
| 0 | 190 | | autoJoinChannelList.Get().Remove(msg.channelId); |
| 0 | 191 | | } |
| | 192 | |
|
| | 193 | | [PublicAPI] |
| | 194 | | public void UpdateChannelInfo(string payload) |
| | 195 | | { |
| 0 | 196 | | var msg = JsonUtility.FromJson<ChannelInfoPayloads>(payload); |
| 0 | 197 | | var anyChannelLeft = false; |
| | 198 | |
|
| 0 | 199 | | foreach (var channelInfo in msg.channelInfoPayload) |
| | 200 | | { |
| 0 | 201 | | var channelId = channelInfo.channelId; |
| 0 | 202 | | var channel = new Channel(channelId, channelInfo.name, channelInfo.unseenMessages, channelInfo.memberCou |
| | 203 | | channelInfo.joined, channelInfo.muted, channelInfo.description); |
| 0 | 204 | | var justLeft = !channel.Joined; |
| | 205 | |
|
| 0 | 206 | | if (channels.ContainsKey(channelId)) |
| | 207 | | { |
| 0 | 208 | | justLeft = channels[channelId].Joined && !channel.Joined; |
| 0 | 209 | | channels[channelId].CopyFrom(channel); |
| 0 | 210 | | } |
| | 211 | | else |
| 0 | 212 | | channels[channelId] = channel; |
| | 213 | |
|
| 0 | 214 | | if (justLeft) |
| | 215 | | { |
| 0 | 216 | | OnChannelLeft?.Invoke(channelId); |
| 0 | 217 | | anyChannelLeft = true; |
| | 218 | | } |
| | 219 | |
|
| 0 | 220 | | if (anyChannelLeft) |
| | 221 | | { |
| | 222 | | // TODO (responsibility issues): extract to another class |
| 0 | 223 | | AudioScriptableObjects.leaveChannel.Play(true); |
| | 224 | | } |
| | 225 | |
|
| 0 | 226 | | OnChannelUpdated?.Invoke(channel); |
| | 227 | | } |
| 0 | 228 | | } |
| | 229 | |
|
| | 230 | | // called by kernel |
| | 231 | | [PublicAPI] |
| | 232 | | public void MuteChannelError(string payload) |
| | 233 | | { |
| 0 | 234 | | var msg = JsonUtility.FromJson<MuteChannelErrorPayload>(payload); |
| 0 | 235 | | OnMuteChannelError?.Invoke(msg.channelId, (ChannelErrorCode) msg.errorCode); |
| 0 | 236 | | } |
| | 237 | |
|
| | 238 | | public void LeaveChannel(string channelId) |
| | 239 | | { |
| 0 | 240 | | WebInterface.LeaveChannel(channelId); |
| | 241 | |
|
| 0 | 242 | | autoJoinChannelList.Get().Remove(channelId); |
| 0 | 243 | | } |
| | 244 | |
|
| | 245 | | // called by kernel |
| | 246 | | [PublicAPI] |
| | 247 | | public void UpdateChannelSearchResults(string payload) |
| | 248 | | { |
| 0 | 249 | | var msg = JsonUtility.FromJson<ChannelSearchResultsPayload>(payload); |
| 0 | 250 | | var channelsResult = new Channel[msg.channels.Length]; |
| | 251 | |
|
| 0 | 252 | | for (var i = 0; i < msg.channels.Length; i++) |
| | 253 | | { |
| 0 | 254 | | var channelPayload = msg.channels[i]; |
| 0 | 255 | | var channelId = channelPayload.channelId; |
| 0 | 256 | | var channel = new Channel(channelId, channelPayload.name, channelPayload.unseenMessages, |
| | 257 | | channelPayload.memberCount, |
| | 258 | | channelPayload.joined, channelPayload.muted, channelPayload.description); |
| | 259 | |
|
| 0 | 260 | | if (channels.ContainsKey(channelId)) |
| 0 | 261 | | channels[channelId].CopyFrom(channel); |
| | 262 | | else |
| 0 | 263 | | channels[channelId] = channel; |
| | 264 | |
|
| 0 | 265 | | channelsResult[i] = channel; |
| | 266 | | } |
| | 267 | |
|
| 0 | 268 | | OnChannelSearchResult?.Invoke(msg.since, channelsResult); |
| 0 | 269 | | } |
| | 270 | |
|
| 0 | 271 | | public void JoinOrCreateChannel(string channelId) => WebInterface.JoinOrCreateChannel(channelId); |
| | 272 | |
|
| | 273 | |
|
| | 274 | | public void GetChannelMessages(string channelId, int limit, string fromMessageId) => |
| 0 | 275 | | WebInterface.GetChannelMessages(channelId, limit, fromMessageId); |
| | 276 | |
|
| 0 | 277 | | public void GetJoinedChannels(int limit, int skip) => WebInterface.GetJoinedChannels(limit, skip); |
| | 278 | |
|
| | 279 | | public void GetChannelsByName(int limit, string name, string paginationToken = null) => |
| 0 | 280 | | WebInterface.GetChannels(limit, paginationToken, name); |
| | 281 | |
|
| | 282 | | public void GetChannels(int limit, string paginationToken) => |
| 1 | 283 | | WebInterface.GetChannels(limit, paginationToken, string.Empty); |
| | 284 | |
|
| | 285 | | public void MuteChannel(string channelId) |
| | 286 | | { |
| 0 | 287 | | if (channelId == NEARBY_CHANNEL_ID) |
| | 288 | | { |
| 0 | 289 | | var channel = GetAllocatedChannel(NEARBY_CHANNEL_ID); |
| 0 | 290 | | var payload = new ChannelInfoPayloads |
| | 291 | | { |
| | 292 | | channelInfoPayload = new[] |
| | 293 | | { |
| | 294 | | new ChannelInfoPayload |
| | 295 | | { |
| | 296 | | description = channel.Description, |
| | 297 | | joined = channel.Joined, |
| | 298 | | channelId = channel.ChannelId, |
| | 299 | | muted = true, |
| | 300 | | name = channel.Name, |
| | 301 | | memberCount = channel.MemberCount, |
| | 302 | | unseenMessages = channel.UnseenMessages |
| | 303 | | } |
| | 304 | | } |
| | 305 | | }; |
| | 306 | |
|
| 0 | 307 | | UpdateChannelInfo(JsonUtility.ToJson(payload)); |
| 0 | 308 | | } |
| | 309 | | else |
| 0 | 310 | | WebInterface.MuteChannel(channelId, true); |
| 0 | 311 | | } |
| | 312 | |
|
| | 313 | | public void UnmuteChannel(string channelId) |
| | 314 | | { |
| 0 | 315 | | if (channelId == NEARBY_CHANNEL_ID) |
| | 316 | | { |
| 0 | 317 | | var channel = GetAllocatedChannel(NEARBY_CHANNEL_ID); |
| 0 | 318 | | var payload = new ChannelInfoPayloads |
| | 319 | | { |
| | 320 | | channelInfoPayload = new[] |
| | 321 | | { |
| | 322 | | new ChannelInfoPayload |
| | 323 | | { |
| | 324 | | description = channel.Description, |
| | 325 | | joined = channel.Joined, |
| | 326 | | channelId = channel.ChannelId, |
| | 327 | | muted = false, |
| | 328 | | name = channel.Name, |
| | 329 | | memberCount = channel.MemberCount, |
| | 330 | | unseenMessages = channel.UnseenMessages |
| | 331 | | } |
| | 332 | | } |
| | 333 | | }; |
| | 334 | |
|
| 0 | 335 | | UpdateChannelInfo(JsonUtility.ToJson(payload)); |
| 0 | 336 | | } |
| | 337 | | else |
| 0 | 338 | | WebInterface.MuteChannel(channelId, false); |
| 0 | 339 | | } |
| | 340 | |
|
| | 341 | | public Channel GetAllocatedChannel(string channelId) => |
| 2 | 342 | | channels.ContainsKey(channelId) ? channels[channelId] : null; |
| | 343 | |
|
| | 344 | | public Channel GetAllocatedChannelByName(string channelName) => |
| 0 | 345 | | channels.Values.FirstOrDefault(x => x.Name == channelName); |
| | 346 | |
|
| | 347 | | public void GetPrivateMessages(string userId, int limit, string fromMessageId) => |
| 0 | 348 | | WebInterface.GetPrivateMessages(userId, limit, fromMessageId); |
| | 349 | |
|
| | 350 | | public void MarkChannelMessagesAsSeen(string channelId) |
| | 351 | | { |
| 2 | 352 | | if (channelId == NEARBY_CHANNEL_ID) |
| | 353 | | { |
| 1 | 354 | | UpdateTotalUnseenMessagesByChannel(NEARBY_CHANNEL_ID, 0); |
| 1 | 355 | | OnTotalUnseenMessagesUpdated?.Invoke(TotalUnseenMessages); |
| | 356 | | } |
| | 357 | |
|
| 2 | 358 | | WebInterface.MarkChannelMessagesAsSeen(channelId); |
| 2 | 359 | | } |
| | 360 | |
|
| 0 | 361 | | public List<ChatMessage> GetAllocatedEntries() => new List<ChatMessage>(messages); |
| | 362 | |
|
| 0 | 363 | | public void GetUnseenMessagesByUser() => WebInterface.GetUnseenMessagesByUser(); |
| | 364 | |
|
| 0 | 365 | | public void GetUnseenMessagesByChannel() => WebInterface.GetUnseenMessagesByChannel(); |
| | 366 | |
|
| | 367 | | public int GetAllocatedUnseenMessages(string userId) => |
| 1 | 368 | | unseenMessagesByUser.ContainsKey(userId) ? unseenMessagesByUser[userId] : 0; |
| | 369 | |
|
| | 370 | | public int GetAllocatedUnseenChannelMessages(string channelId) => |
| 1 | 371 | | !string.IsNullOrEmpty(channelId) |
| | 372 | | ? unseenMessagesByChannel.ContainsKey(channelId) ? unseenMessagesByChannel[channelId] : 0 |
| | 373 | | : 0; |
| | 374 | |
|
| 0 | 375 | | public void CreateChannel(string channelId) => WebInterface.CreateChannel(channelId); |
| | 376 | |
|
| | 377 | | public List<ChatMessage> GetPrivateAllocatedEntriesByUser(string userId) |
| | 378 | | { |
| 0 | 379 | | return messages |
| 0 | 380 | | .Where(x => (x.sender == userId || x.recipient == userId) && x.messageType == ChatMessage.Type.PRIVATE) |
| | 381 | | .ToList(); |
| | 382 | | } |
| | 383 | |
|
| 0 | 384 | | public void GetChannelInfo(string[] channelIds) => WebInterface.GetChannelInfo(channelIds); |
| | 385 | |
|
| | 386 | | public void GetChannelMembers(string channelId, int limit, int skip, string name) => |
| 0 | 387 | | WebInterface.GetChannelMembers(channelId, limit, skip, name); |
| | 388 | |
|
| | 389 | | public void GetChannelMembers(string channelId, int limit, int skip) => |
| 0 | 390 | | WebInterface.GetChannelMembers(channelId, limit, skip, string.Empty); |
| | 391 | |
|
| 0 | 392 | | public void Send(ChatMessage message) => WebInterface.SendChatMessage(message); |
| | 393 | |
|
| 1 | 394 | | public void MarkMessagesAsSeen(string userId) => WebInterface.MarkMessagesAsSeen(userId); |
| | 395 | |
|
| | 396 | | private void SendChannelWelcomeMessage(Channel channel) |
| | 397 | | { |
| 0 | 398 | | var message = |
| | 399 | | new ChatMessage(ChatMessage.Type.SYSTEM, "", @$"This is the start of the channel #{channel.Name}.\n |
| | 400 | | Invite others to join by quoting the channel name in other chats or include it as a part of your bio.") |
| | 401 | | { |
| | 402 | | recipient = channel.ChannelId, |
| | 403 | | timestamp = 0, |
| | 404 | | isChannelMessage = true, |
| | 405 | | messageId = Guid.NewGuid().ToString() |
| | 406 | | }; |
| | 407 | |
|
| 0 | 408 | | AddMessage(message); |
| 0 | 409 | | } |
| | 410 | |
|
| | 411 | | private void AddMessage(ChatMessage message) |
| | 412 | | { |
| 0 | 413 | | if (message == null) return; |
| | 414 | |
|
| 0 | 415 | | messages.Add(message); |
| | 416 | |
|
| 0 | 417 | | if (message.messageType == ChatMessage.Type.PUBLIC && string.IsNullOrEmpty(message.recipient)) |
| | 418 | | { |
| 0 | 419 | | if (!unseenMessagesByChannel.ContainsKey(NEARBY_CHANNEL_ID)) |
| 0 | 420 | | unseenMessagesByChannel[NEARBY_CHANNEL_ID] = 0; |
| | 421 | |
|
| 0 | 422 | | UpdateTotalUnseenMessagesByChannel(NEARBY_CHANNEL_ID, unseenMessagesByChannel[NEARBY_CHANNEL_ID] + 1); |
| 0 | 423 | | OnTotalUnseenMessagesUpdated?.Invoke(TotalUnseenMessages); |
| | 424 | | } |
| | 425 | |
|
| 0 | 426 | | OnAddMessage?.Invoke(message); |
| 0 | 427 | | } |
| | 428 | |
|
| | 429 | | private void UpdateTotalUnseenMessagesByChannel(string channelId, int count) |
| | 430 | | { |
| 1 | 431 | | unseenMessagesByChannel[channelId] = count; |
| | 432 | |
|
| 1 | 433 | | if (channels.ContainsKey(channelId)) |
| 1 | 434 | | channels[channelId].UnseenMessages = count; |
| | 435 | |
|
| 1 | 436 | | OnChannelUnseenMessagesUpdated?.Invoke(channelId, count); |
| 0 | 437 | | } |
| | 438 | | } |
| | 439 | | } |