52 lines
1.7 KiB
C#
52 lines
1.7 KiB
C#
using System.Buffers.Binary;
|
|
using System.Collections;
|
|
|
|
namespace Uwaa.Pleroma;
|
|
|
|
/// <summary>
|
|
/// Base class for ActivityStreams objects.
|
|
/// </summary>
|
|
public class ASObject
|
|
{
|
|
public static UInt128 FromBase62(string id)
|
|
{
|
|
//TODO: Optimize
|
|
const string table = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
|
|
BitArray bits = new BitArray(id.Length * 6);
|
|
for (int i = 0; i < id.Length; i++)
|
|
{
|
|
int index = table.IndexOf(id[^(i + 1)]);
|
|
bits[i * 6] = (index & 1) != 0;
|
|
bits[i * 6 + 1] = (index & 2) != 0;
|
|
bits[i * 6 + 2] = (index & 4) != 0;
|
|
bits[i * 6 + 3] = (index & 8) != 0;
|
|
bits[i * 6 + 4] = (index & 16) != 0;
|
|
bits[i * 6 + 5] = (index & 32) != 0;
|
|
}
|
|
|
|
byte[] bytes = new byte[16];
|
|
bits.CopyTo(bytes, 0);
|
|
|
|
return BinaryPrimitives.ReadUInt128LittleEndian(bytes);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pleroma uses 128-bit ids as opposed to Mastodon's 64 bits. However just like Mastodon's ids they are lexically sortable strings
|
|
/// </summary>
|
|
[JsonPropertyName("id")]
|
|
public string ID { get; set; } = null!;
|
|
|
|
[JsonConstructor()]
|
|
internal ASObject()
|
|
{
|
|
}
|
|
|
|
public override string ToString() => ID;
|
|
|
|
public override bool Equals(object? obj) => Equals(obj as ASObject);
|
|
public bool Equals(ASObject? other) => other is not null && ID == other.ID;
|
|
public static bool operator ==(ASObject? left, ASObject? right) => EqualityComparer<ASObject>.Default.Equals(left, right);
|
|
public static bool operator !=(ASObject? left, ASObject? right) => !(left == right);
|
|
public override int GetHashCode() => ID.GetHashCode();
|
|
}
|