2026-10-01 11:25:48 +02:00
|
|
|
using System.Buffers.Binary;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
|
2026-10-01 13:19:16 +02:00
|
|
|
using MongoDB.Bson;
|
|
|
|
|
|
2026-10-01 11:25:48 +02:00
|
|
|
namespace PrivaPub.Infrastructure.Ids
|
|
|
|
|
{
|
|
|
|
|
public static class PrivacyIds
|
|
|
|
|
{
|
|
|
|
|
public static string ForDay(DateTime when) => At(DateTime.SpecifyKind(when, DateTimeKind.Utc).Date);
|
|
|
|
|
|
2026-10-01 17:39:58 +02:00
|
|
|
public static DateTime PublishedDay(DateTime created) =>
|
|
|
|
|
DateTime.SpecifyKind(created, DateTimeKind.Utc).Date.AddDays(-RandomNumberGenerator.GetInt32(0, PublishedSpreadDays));
|
|
|
|
|
|
|
|
|
|
const int PublishedSpreadDays = 14;
|
|
|
|
|
|
2026-10-01 13:19:16 +02:00
|
|
|
public static string Arrived(DateTime published) =>
|
|
|
|
|
DateTime.SpecifyKind(published, DateTimeKind.Utc) > DateTime.UtcNow - LiveWindow ? ObjectId.GenerateNewId().ToString() : At(published);
|
|
|
|
|
|
|
|
|
|
static readonly TimeSpan LiveWindow = TimeSpan.FromHours(1);
|
|
|
|
|
|
2026-10-01 11:25:48 +02:00
|
|
|
public static string At(DateTime when)
|
|
|
|
|
{
|
|
|
|
|
var seconds = Math.Clamp(new DateTimeOffset(DateTime.SpecifyKind(when, DateTimeKind.Utc)).ToUnixTimeSeconds(), 0,
|
|
|
|
|
DateTimeOffset.UtcNow.AddDays(1).ToUnixTimeSeconds());
|
|
|
|
|
Span<byte> bytes = stackalloc byte[12];
|
|
|
|
|
BinaryPrimitives.WriteInt32BigEndian(bytes, (int)seconds);
|
|
|
|
|
RandomNumberGenerator.Fill(bytes[4..]);
|
|
|
|
|
return Convert.ToHexStringLower(bytes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|