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