Port the DM stuff
This commit is contained in:
@@ -55,6 +55,17 @@ namespace Myriad.Extensions
|
||||
return restUser;
|
||||
}
|
||||
|
||||
public static async ValueTask<Channel?> GetOrFetchChannel(this IDiscordCache cache, DiscordApiClient rest, ulong channelId)
|
||||
{
|
||||
if (cache.TryGetChannel(channelId, out var cacheChannel))
|
||||
return cacheChannel;
|
||||
|
||||
var restChannel = await rest.GetChannel(channelId);
|
||||
if (restChannel != null)
|
||||
await cache.SaveChannel(restChannel);
|
||||
return restChannel;
|
||||
}
|
||||
|
||||
public static async Task<Channel> GetOrCreateDmChannel(this IDiscordCache cache, DiscordApiClient rest, ulong recipientId)
|
||||
{
|
||||
if (cache.TryGetDmChannel(recipientId, out var cacheChannel))
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
namespace Myriad.Gateway
|
||||
using Myriad.Utils;
|
||||
|
||||
namespace Myriad.Gateway
|
||||
{
|
||||
public record MessageUpdateEvent(ulong Id, ulong ChannelId): IGatewayEvent
|
||||
{
|
||||
public Optional<string?> Content { get; init; }
|
||||
// TODO: lots of partials
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ namespace Myriad.Serialization
|
||||
|
||||
opts.Converters.Add(new PermissionSetJsonConverter());
|
||||
opts.Converters.Add(new ShardInfoJsonConverter());
|
||||
opts.Converters.Add(new OptionalConverterFactory());
|
||||
|
||||
return opts;
|
||||
}
|
||||
|
||||
@@ -7,28 +7,33 @@ using Myriad.Utils;
|
||||
|
||||
namespace Myriad.Serialization
|
||||
{
|
||||
public class OptionalConverter: JsonConverter<IOptional>
|
||||
public class OptionalConverterFactory: JsonConverterFactory
|
||||
{
|
||||
public override IOptional? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
public class Inner<T>: JsonConverter<Optional<T>>
|
||||
{
|
||||
public override Optional<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var inner = JsonSerializer.Deserialize<T>(ref reader, options);
|
||||
return new(inner!);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, Optional<T> value, JsonSerializerOptions options)
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value.HasValue ? value.GetValue() : default, typeof(T), options);
|
||||
}
|
||||
}
|
||||
|
||||
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var innerType = typeToConvert.GetGenericArguments()[0];
|
||||
var inner = JsonSerializer.Deserialize(ref reader, innerType, options);
|
||||
|
||||
// TODO: rewrite to JsonConverterFactory to cut down on reflection
|
||||
return (IOptional?) Activator.CreateInstance(
|
||||
typeof(Optional<>).MakeGenericType(innerType),
|
||||
return (JsonConverter?) Activator.CreateInstance(
|
||||
typeof(Inner<>).MakeGenericType(innerType),
|
||||
BindingFlags.Instance | BindingFlags.Public,
|
||||
null,
|
||||
new[] {inner},
|
||||
null,
|
||||
null);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, IOptional value, JsonSerializerOptions options)
|
||||
{
|
||||
var innerType = value.GetType().GetGenericArguments()[0];
|
||||
JsonSerializer.Serialize(writer, value.GetValue(), innerType, options);
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
if (!typeToConvert.IsGenericType)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Mail;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using Myriad.Utils;
|
||||
|
||||
namespace Myriad.Types
|
||||
{
|
||||
@@ -59,8 +62,8 @@ namespace Myriad.Types
|
||||
public Reference? MessageReference { get; set; }
|
||||
public MessageFlags Flags { get; init; }
|
||||
|
||||
// todo: null vs. absence
|
||||
public Message? ReferencedMessage { get; init; }
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
|
||||
public Optional<Message?> ReferencedMessage { get; init; }
|
||||
|
||||
public record Reference(ulong? GuildId, ulong? ChannelId, ulong? MessageId);
|
||||
|
||||
|
||||
@@ -1,16 +1,10 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using Myriad.Serialization;
|
||||
|
||||
namespace Myriad.Utils
|
||||
namespace Myriad.Utils
|
||||
{
|
||||
public interface IOptional
|
||||
{
|
||||
bool HasValue { get; }
|
||||
object? GetValue();
|
||||
}
|
||||
|
||||
[JsonConverter(typeof(OptionalConverter))]
|
||||
public readonly struct Optional<T>: IOptional
|
||||
{
|
||||
public Optional(T value)
|
||||
|
||||
Reference in New Issue
Block a user