| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Interface; |
| | 6 | | using Newtonsoft.Json; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | public class LastReadMessagesService : ILastReadMessagesService |
| | 10 | | { |
| | 11 | | private const string PLAYER_PREFS_LAST_READ_CHAT_MESSAGES = "LastReadChatMessages"; |
| | 12 | |
|
| | 13 | | private readonly ReadMessagesDictionary memoryRepository; |
| | 14 | | private readonly IChatController chatController; |
| | 15 | | private readonly IPlayerPrefs persistentRepository; |
| | 16 | | private readonly IUserProfileBridge userProfileBridge; |
| 88 | 17 | | private readonly Dictionary<string, int> channelUnreadCount = new Dictionary<string, int>(); |
| | 18 | |
|
| | 19 | | public event Action<string> OnUpdated; |
| | 20 | |
|
| 88 | 21 | | public LastReadMessagesService(ReadMessagesDictionary memoryRepository, |
| | 22 | | IChatController chatController, |
| | 23 | | IPlayerPrefs persistentRepository, |
| | 24 | | IUserProfileBridge userProfileBridge) |
| | 25 | | { |
| 88 | 26 | | this.memoryRepository = memoryRepository; |
| 88 | 27 | | this.chatController = chatController; |
| 88 | 28 | | this.persistentRepository = persistentRepository; |
| 88 | 29 | | this.userProfileBridge = userProfileBridge; |
| 88 | 30 | | } |
| | 31 | |
|
| | 32 | | public void Initialize() |
| | 33 | | { |
| 88 | 34 | | if (chatController != null) |
| 0 | 35 | | chatController.OnAddMessage += HandleMessageReceived; |
| 88 | 36 | | LoadLastReadTimestamps(); |
| 88 | 37 | | LoadChannelsUnreadCount(); |
| 88 | 38 | | } |
| | 39 | |
|
| | 40 | | public void MarkAllRead(string chatId) |
| | 41 | | { |
| 0 | 42 | | if (string.IsNullOrEmpty(chatId)) |
| | 43 | | { |
| 0 | 44 | | Debug.LogWarning("Trying to clear last read messages for an empty chatId"); |
| 0 | 45 | | return; |
| | 46 | | } |
| | 47 | |
|
| 0 | 48 | | var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); |
| 0 | 49 | | memoryRepository.Remove(chatId); |
| 0 | 50 | | memoryRepository.Add(chatId, timestamp); |
| 0 | 51 | | channelUnreadCount.Remove(chatId); |
| 0 | 52 | | Persist(); |
| 0 | 53 | | OnUpdated?.Invoke(chatId); |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | public int GetUnreadCount(string chatId) |
| | 57 | | { |
| 0 | 58 | | if (string.IsNullOrEmpty(chatId)) |
| | 59 | | { |
| 0 | 60 | | Debug.LogWarning("Trying to get unread messages count for an empty chatId"); |
| 0 | 61 | | return 0; |
| | 62 | | } |
| | 63 | |
|
| 0 | 64 | | return channelUnreadCount.ContainsKey(chatId) ? channelUnreadCount[chatId] : 0; |
| | 65 | | } |
| | 66 | |
|
| 0 | 67 | | public int GetAllUnreadCount() => channelUnreadCount.Sum(pair => pair.Value); |
| | 68 | |
|
| | 69 | | public void Dispose() |
| | 70 | | { |
| 88 | 71 | | if (chatController != null) |
| 0 | 72 | | chatController.OnAddMessage -= HandleMessageReceived; |
| 88 | 73 | | } |
| | 74 | |
|
| | 75 | | private void LoadLastReadTimestamps() |
| | 76 | | { |
| 88 | 77 | | var lastReadChatMessagesList = |
| | 78 | | JsonConvert.DeserializeObject<List<KeyValuePair<string, long>>>( |
| | 79 | | persistentRepository.GetString(PLAYER_PREFS_LAST_READ_CHAT_MESSAGES)); |
| 176 | 80 | | if (lastReadChatMessagesList == null) return; |
| 0 | 81 | | foreach (var item in lastReadChatMessagesList) |
| 0 | 82 | | memoryRepository.Add(item.Key, item.Value); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | private void LoadChannelsUnreadCount() |
| | 86 | | { |
| 176 | 87 | | if (chatController == null) return; |
| 0 | 88 | | var ownUserId = userProfileBridge.GetOwn().userId; |
| 0 | 89 | | using var iterator = memoryRepository.GetEnumerator(); |
| 0 | 90 | | while (iterator.MoveNext()) |
| | 91 | | { |
| 0 | 92 | | var channelId = iterator.Current.Key; |
| 0 | 93 | | var timestamp = GetLastReadTimestamp(channelId); |
| 0 | 94 | | channelUnreadCount[channelId] = chatController.GetEntries() |
| | 95 | | .Count(message => |
| | 96 | | { |
| 0 | 97 | | var messageChannelId = GetChannelId(message); |
| 0 | 98 | | if (string.IsNullOrEmpty(messageChannelId)) return false; |
| 0 | 99 | | if (messageChannelId == ownUserId) return false; |
| 0 | 100 | | return messageChannelId == channelId |
| | 101 | | && message.timestamp >= timestamp; |
| | 102 | | });; |
| | 103 | | } |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | private void HandleMessageReceived(ChatMessage message) |
| | 107 | | { |
| 0 | 108 | | var channelId = GetChannelId(message); |
| 0 | 109 | | if (string.IsNullOrEmpty(channelId)) return; |
| 0 | 110 | | if (channelId == userProfileBridge.GetOwn().userId) return; |
| | 111 | |
|
| 0 | 112 | | var timestamp = GetLastReadTimestamp(channelId); |
| 0 | 113 | | if (message.timestamp >= timestamp) |
| | 114 | | { |
| 0 | 115 | | if (!channelUnreadCount.ContainsKey(channelId)) |
| 0 | 116 | | channelUnreadCount[channelId] = 0; |
| 0 | 117 | | channelUnreadCount[channelId]++; |
| | 118 | | } |
| | 119 | |
|
| 0 | 120 | | OnUpdated?.Invoke(channelId); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | private string GetChannelId(ChatMessage message) |
| | 124 | | { |
| 0 | 125 | | return message.messageType switch |
| | 126 | | { |
| | 127 | | ChatMessage.Type.PRIVATE => message.sender, |
| | 128 | | // TODO: solve public channelId from chatMessage information when is done from catalyst side |
| | 129 | | ChatMessage.Type.PUBLIC => "general", |
| | 130 | | _ => null |
| | 131 | | }; |
| | 132 | | } |
| | 133 | |
|
| | 134 | | private ulong GetLastReadTimestamp(string chatId) |
| | 135 | | { |
| 0 | 136 | | var oldNotificationsFilteringTimestamp = DateTime.UtcNow |
| | 137 | | .Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)) |
| | 138 | | .Subtract(TimeSpan.FromDays(1)) |
| | 139 | | .TotalMilliseconds; |
| | 140 | |
|
| 0 | 141 | | return (ulong) (memoryRepository.ContainsKey(chatId) |
| | 142 | | ? memoryRepository.Get(chatId) |
| | 143 | | : oldNotificationsFilteringTimestamp); |
| | 144 | | } |
| | 145 | |
|
| | 146 | | private void Persist() |
| | 147 | | { |
| 0 | 148 | | var lastReadChatMessagesList = new List<KeyValuePair<string, long>>(); |
| 0 | 149 | | using (var iterator = memoryRepository.GetEnumerator()) |
| | 150 | | { |
| 0 | 151 | | while (iterator.MoveNext()) |
| | 152 | | { |
| 0 | 153 | | lastReadChatMessagesList.Add(new KeyValuePair<string, long>(iterator.Current.Key, |
| | 154 | | iterator.Current.Value)); |
| | 155 | | } |
| 0 | 156 | | } |
| | 157 | |
|
| 0 | 158 | | persistentRepository.Set(PLAYER_PREFS_LAST_READ_CHAT_MESSAGES, |
| | 159 | | JsonConvert.SerializeObject(lastReadChatMessagesList)); |
| 0 | 160 | | persistentRepository.Save(); |
| 0 | 161 | | } |
| | 162 | | } |