Browse Source

Added custom contract resolver for message import/export

tags/docs-0.9
RogueException 9 years ago
parent
commit
65ebcaffb2
3 changed files with 29 additions and 3 deletions
  1. +3
    -1
      src/Discord.Net/DiscordClient.Messages.cs
  2. +2
    -1
      src/Discord.Net/DiscordClient.cs
  3. +24
    -1
      src/Discord.Net/Models/Message.cs

+ 3
- 1
src/Discord.Net/DiscordClient.Messages.cs View File

@@ -1,6 +1,7 @@
using Discord.API; using Discord.API;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@@ -33,7 +34,7 @@ namespace Discord
} }
public void Import(Dictionary<long, Message> messages) public void Import(Dictionary<long, Message> messages)
=> base.Import(messages); => base.Import(messages);
}
}


public class MessageEventArgs : EventArgs public class MessageEventArgs : EventArgs
{ {
@@ -274,6 +275,7 @@ namespace Discord


var reader = x.CreateReader(); var reader = x.CreateReader();
_messageImporter.Populate(reader, msg); _messageImporter.Populate(reader, msg);
msg.Text = Mention.Resolve(msg, msg.RawText);
return msg; return msg;
}) })
.ToDictionary(x => x.Id); .ToDictionary(x => x.Id);


+ 2
- 1
src/Discord.Net/DiscordClient.cs View File

@@ -171,7 +171,8 @@ namespace Discord
#endif #endif


_messageImporter = new JsonSerializer(); _messageImporter = new JsonSerializer();
}
_messageImporter.ContractResolver = new MessageImporterResolver();
}
internal override VoiceWebSocket CreateVoiceSocket() internal override VoiceWebSocket CreateVoiceSocket()
{ {
var socket = base.CreateVoiceSocket(); var socket = base.CreateVoiceSocket();


+ 24
- 1
src/Discord.Net/Models/Message.cs View File

@@ -1,8 +1,10 @@
using Discord.API; using Discord.API;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;


namespace Discord namespace Discord
{ {
@@ -12,6 +14,26 @@ namespace Discord
Queued, Queued,
Failed Failed
} }
internal class MessageImporterResolver : DefaultContractResolver
{
protected override List<System.Reflection.MemberInfo> GetSerializableMembers(Type objectType)
{
return base.GetSerializableMembers(objectType);
}
protected override JsonProperty CreateProperty(System.Reflection.MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
if (member is PropertyInfo)
{
if (!(member as PropertyInfo).CanWrite)
return null;

property.Writable = true; //Handles private setters
}
return property;
}
}

public sealed class Message : CachedObject<long> public sealed class Message : CachedObject<long>
{ {
public sealed class Attachment : File public sealed class Attachment : File
@@ -108,8 +130,9 @@ namespace Discord
public MessageState State { get; internal set; } public MessageState State { get; internal set; }
/// <summary> Returns the raw content of this message as it was received from the server. </summary> /// <summary> Returns the raw content of this message as it was received from the server. </summary>
public string RawText { get; private set; } public string RawText { get; private set; }
[JsonIgnore]
/// <summary> Returns the content of this message with any special references such as mentions converted. </summary> /// <summary> Returns the content of this message with any special references such as mentions converted. </summary>
public string Text { get; private set; }
public string Text { get; internal set; }
/// <summary> Returns the timestamp for when this message was sent. </summary> /// <summary> Returns the timestamp for when this message was sent. </summary>
public DateTime Timestamp { get; private set; } public DateTime Timestamp { get; private set; }
/// <summary> Returns the timestamp for when this message was last edited. </summary> /// <summary> Returns the timestamp for when this message was last edited. </summary>


Loading…
Cancel
Save