From 896004c0f3d252a2f3c53f51815600182498e517 Mon Sep 17 00:00:00 2001 From: RogueException Date: Fri, 27 Nov 2015 02:52:50 -0400 Subject: [PATCH] Added message cache import/export --- src/Discord.Net/DiscordClient.Messages.cs | 24 +++++++++++++++++++++--- src/Discord.Net/Helpers/AsyncCollection.cs | 26 ++++++++++++++++++-------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/Discord.Net/DiscordClient.Messages.cs b/src/Discord.Net/DiscordClient.Messages.cs index 254eb20b2..72c4a0b3b 100644 --- a/src/Discord.Net/DiscordClient.Messages.cs +++ b/src/Discord.Net/DiscordClient.Messages.cs @@ -1,4 +1,5 @@ using Discord.API; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; @@ -27,8 +28,10 @@ namespace Discord msg.Cache(); //Builds references return msg; } - } - } + } + public void Import(Message[] messages) + => Import(messages.ToDictionary(x => x.Id)); + } public class MessageEventArgs : EventArgs { @@ -214,7 +217,6 @@ namespace Discord } } - /// Downloads last count messages from the server, returning all messages before or after relativeMessageId, if it's provided. public async Task DownloadMessages(Channel channel, int count, long? relativeMessageId = null, RelativeDirection relativeDir = RelativeDirection.Before, bool useCache = true) { @@ -254,6 +256,22 @@ namespace Discord } return new Message[0]; } + + /// Deserializes messages from JSON format and imports them into the message cache. + public void ImportMessages(string json) + { + if (json == null) throw new ArgumentNullException(nameof(json)); + + var msgs = JsonConvert.DeserializeObject(json); + _messages.Import(msgs); + } + /// Serializes the message cache for a given channel to JSON. + public string ExportMessages(Channel channel) + { + if (channel == null) throw new ArgumentNullException(nameof(channel)); + + return JsonConvert.SerializeObject(channel.Messages); + } private Task MessageQueueLoop() { diff --git a/src/Discord.Net/Helpers/AsyncCollection.cs b/src/Discord.Net/Helpers/AsyncCollection.cs index 0e3252d13..c192d02db 100644 --- a/src/Discord.Net/Helpers/AsyncCollection.cs +++ b/src/Discord.Net/Helpers/AsyncCollection.cs @@ -94,6 +94,15 @@ namespace Discord } return result; } + protected void Import(IEnumerable> items) + { + lock (_writerLock) + { + foreach (var pair in items) + _dictionary.TryAdd(pair.Key, pair.Value); + } + } + public TValue TryRemove(TKey key) { if (_dictionary.ContainsKey(key)) @@ -110,6 +119,15 @@ namespace Discord } return null; } + public void Clear() + { + lock (_writerLock) + { + _dictionary.Clear(); + RaiseCleared(); + } + } + public TValue Remap(TKey oldKey, TKey newKey) { if (_dictionary.ContainsKey(oldKey)) @@ -124,14 +142,6 @@ namespace Discord } return null; } - public void Clear() - { - lock (_writerLock) - { - _dictionary.Clear(); - RaiseCleared(); - } - } public IEnumerator GetEnumerator() => _dictionary.Select(x => x.Value).GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();