2024-12-09 21:11:46 +01:00
|
|
|
// Copyright (C) 2023-present sam/u1f320 (vulpine.solutions)
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2024-08-22 15:13:46 +02:00
|
|
|
using System.ComponentModel;
|
2024-05-28 15:29:18 +02:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2024-08-22 15:13:46 +02:00
|
|
|
using System.Globalization;
|
2024-12-10 15:28:44 +01:00
|
|
|
using System.Text.Json;
|
2024-05-27 15:53:54 +02:00
|
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
2024-06-04 17:38:59 +02:00
|
|
|
using Newtonsoft.Json;
|
2024-05-27 15:53:54 +02:00
|
|
|
using NodaTime;
|
2024-09-18 21:44:47 +02:00
|
|
|
using JsonSerializer = Newtonsoft.Json.JsonSerializer;
|
2024-05-27 15:53:54 +02:00
|
|
|
|
|
|
|
namespace Foxnouns.Backend.Database;
|
|
|
|
|
2024-06-04 17:38:59 +02:00
|
|
|
[JsonConverter(typeof(JsonConverter))]
|
2024-12-10 15:28:44 +01:00
|
|
|
[System.Text.Json.Serialization.JsonConverter(typeof(SystemJsonConverter))]
|
2024-08-22 15:13:46 +02:00
|
|
|
[TypeConverter(typeof(TypeConverter))]
|
|
|
|
public readonly struct Snowflake(ulong value) : IEquatable<Snowflake>
|
2024-05-27 15:53:54 +02:00
|
|
|
{
|
|
|
|
public const long Epoch = 1_640_995_200_000; // 2022-01-01 at 00:00:00 UTC
|
|
|
|
public readonly ulong Value = value;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The time this snowflake was created.
|
|
|
|
/// </summary>
|
|
|
|
public Instant Time => Instant.FromUnixTimeMilliseconds(Timestamp);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The Unix timestamp embedded in this snowflake, in milliseconds.
|
|
|
|
/// </summary>
|
|
|
|
public long Timestamp => (long)((Value >> 22) + Epoch);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The process ID embedded in this snowflake.
|
|
|
|
/// </summary>
|
|
|
|
public byte ProcessId => (byte)((Value & 0x3E0000) >> 17);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The thread ID embedded in this snowflake.
|
|
|
|
/// </summary>
|
|
|
|
public byte ThreadId => (byte)((Value & 0x1F000) >> 12);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The increment embedded in this snowflake.
|
|
|
|
/// </summary>
|
|
|
|
public short Increment => (short)(Value & 0xFFF);
|
|
|
|
|
|
|
|
public static bool operator <(Snowflake arg1, Snowflake arg2) => arg1.Value < arg2.Value;
|
2024-10-02 00:28:07 +02:00
|
|
|
|
2024-05-27 15:53:54 +02:00
|
|
|
public static bool operator >(Snowflake arg1, Snowflake arg2) => arg1.Value > arg2.Value;
|
2024-10-02 00:28:07 +02:00
|
|
|
|
2024-05-27 15:53:54 +02:00
|
|
|
public static bool operator ==(Snowflake arg1, Snowflake arg2) => arg1.Value == arg2.Value;
|
2024-10-02 00:28:07 +02:00
|
|
|
|
2024-05-27 15:53:54 +02:00
|
|
|
public static bool operator !=(Snowflake arg1, Snowflake arg2) => arg1.Value != arg2.Value;
|
|
|
|
|
|
|
|
public static implicit operator ulong(Snowflake s) => s.Value;
|
2024-10-02 00:28:07 +02:00
|
|
|
|
2024-05-27 15:53:54 +02:00
|
|
|
public static implicit operator long(Snowflake s) => (long)s.Value;
|
2024-10-02 00:28:07 +02:00
|
|
|
|
2024-05-27 15:53:54 +02:00
|
|
|
public static implicit operator Snowflake(ulong n) => new(n);
|
2024-10-02 00:28:07 +02:00
|
|
|
|
2024-05-27 15:53:54 +02:00
|
|
|
public static implicit operator Snowflake(long n) => new((ulong)n);
|
|
|
|
|
2024-05-28 15:29:18 +02:00
|
|
|
public static bool TryParse(string input, [NotNullWhen(true)] out Snowflake? snowflake)
|
|
|
|
{
|
|
|
|
snowflake = null;
|
2024-12-08 15:07:25 +01:00
|
|
|
if (!ulong.TryParse(input, out ulong res))
|
2024-10-02 00:28:07 +02:00
|
|
|
return false;
|
2024-05-28 15:29:18 +02:00
|
|
|
snowflake = new Snowflake(res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-12-02 18:06:19 +01:00
|
|
|
public static Snowflake FromInstant(Instant instant) =>
|
|
|
|
new((ulong)(instant.ToUnixTimeMilliseconds() - Epoch) << 22);
|
|
|
|
|
2024-05-27 15:53:54 +02:00
|
|
|
public override bool Equals(object? obj) => obj is Snowflake other && Value == other.Value;
|
2024-08-22 15:13:46 +02:00
|
|
|
|
2024-12-08 15:07:25 +01:00
|
|
|
public bool Equals(Snowflake other) => Value == other.Value;
|
2024-08-22 15:13:46 +02:00
|
|
|
|
2024-05-27 15:53:54 +02:00
|
|
|
public override int GetHashCode() => Value.GetHashCode();
|
2024-10-02 00:28:07 +02:00
|
|
|
|
2024-07-08 19:03:04 +02:00
|
|
|
public override string ToString() => Value.ToString();
|
2024-05-27 15:53:54 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// An Entity Framework ValueConverter for Snowflakes to longs.
|
|
|
|
/// </summary>
|
|
|
|
// ReSharper disable once ClassNeverInstantiated.Global
|
2024-12-08 15:07:25 +01:00
|
|
|
public class ValueConverter() : ValueConverter<Snowflake, long>(x => x, x => x);
|
2024-06-04 17:38:59 +02:00
|
|
|
|
2024-12-10 15:28:44 +01:00
|
|
|
private class SystemJsonConverter : System.Text.Json.Serialization.JsonConverter<Snowflake>
|
|
|
|
{
|
|
|
|
public override Snowflake Read(
|
|
|
|
ref Utf8JsonReader reader,
|
|
|
|
Type typeToConvert,
|
|
|
|
JsonSerializerOptions options
|
|
|
|
) => ulong.Parse(reader.GetString()!);
|
|
|
|
|
|
|
|
public override void Write(
|
|
|
|
Utf8JsonWriter writer,
|
|
|
|
Snowflake value,
|
|
|
|
JsonSerializerOptions options
|
|
|
|
) => writer.WriteStringValue(value.Value.ToString());
|
|
|
|
}
|
|
|
|
|
2024-06-04 17:38:59 +02:00
|
|
|
private class JsonConverter : JsonConverter<Snowflake>
|
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
public override void WriteJson(
|
|
|
|
JsonWriter writer,
|
|
|
|
Snowflake value,
|
|
|
|
JsonSerializer serializer
|
|
|
|
)
|
2024-06-04 17:38:59 +02:00
|
|
|
{
|
|
|
|
writer.WriteValue(value.Value.ToString());
|
|
|
|
}
|
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public override Snowflake ReadJson(
|
|
|
|
JsonReader reader,
|
|
|
|
Type objectType,
|
|
|
|
Snowflake existingValue,
|
2024-06-04 17:38:59 +02:00
|
|
|
bool hasExistingValue,
|
2024-10-02 00:28:07 +02:00
|
|
|
JsonSerializer serializer
|
2024-12-08 15:07:25 +01:00
|
|
|
) => ulong.Parse((string)reader.Value!);
|
2024-06-04 17:38:59 +02:00
|
|
|
}
|
2024-08-22 15:13:46 +02:00
|
|
|
|
|
|
|
private class TypeConverter : System.ComponentModel.TypeConverter
|
|
|
|
{
|
|
|
|
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) =>
|
|
|
|
sourceType == typeof(string);
|
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public override bool CanConvertTo(
|
|
|
|
ITypeDescriptorContext? context,
|
|
|
|
[NotNullWhen(true)] Type? destinationType
|
|
|
|
) => destinationType == typeof(Snowflake);
|
2024-08-22 15:13:46 +02:00
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public override object? ConvertFrom(
|
|
|
|
ITypeDescriptorContext? context,
|
|
|
|
CultureInfo? culture,
|
|
|
|
object value
|
2024-12-08 15:07:25 +01:00
|
|
|
) => TryParse((string)value, out Snowflake? snowflake) ? snowflake : null;
|
2024-08-22 15:13:46 +02:00
|
|
|
}
|
2024-10-02 00:28:07 +02:00
|
|
|
}
|