···
1
+
defmodule Atex.TID do
3
+
Struct and helper functions for dealing with AT Protocol TIDs (Timestamp
4
+
Identifiers), a 13-character string representation of a 64-bit number
5
+
comprised of a Unix timestamp (in microsecond precision) and a random "clock
6
+
identifier" to help avoid collisions.
8
+
ATProto spec: https://atproto.com/specs/tid
10
+
TID strings are always 13 characters long. All bits in the 64-bit number are
11
+
encoded, essentially meaning that the string is padded with "2" if necessary,
12
+
(the 0th character in the base32-sortable alphabet).
15
+
alias Atex.Base32Sortable
18
+
@re ~r/^[234567abcdefghij][234567abcdefghijklmnopqrstuvwxyz]{12}$/
21
+
A Unix timestamp representing when the TID was created.
23
+
@type timestamp() :: integer()
26
+
An integer to be used for the lower 10 bits of the TID.
28
+
@type clock_id() :: 0..1023
30
+
typedstruct enforce: true do
31
+
field :timestamp, timestamp()
32
+
field :clock_id, clock_id()
36
+
Returns a TID for the current moment in time, along with a random clock ID.
41
+
timestamp: DateTime.utc_now(:microsecond) |> DateTime.to_unix(:microsecond),
42
+
clock_id: gen_clock_id()
46
+
Create a new TID from a `DateTime` or an integer representing a Unix timestamp in microseconds.
48
+
If `clock_id` isn't provided, a random one will be generated.
50
+
@spec new(DateTime.t() | integer(), integer() | nil) :: t()
51
+
def new(source, clock_id \\ nil)
53
+
def new(%DateTime{} = datetime, clock_id),
55
+
timestamp: DateTime.to_unix(datetime, :microsecond),
56
+
clock_id: clock_id || gen_clock_id()
59
+
def new(unix, clock_id) when is_integer(unix),
60
+
do: %__MODULE__{timestamp: unix, clock_id: clock_id || gen_clock_id()}
63
+
Convert a TID struct to an instance of `DateTime`.
65
+
def to_datetime(%__MODULE__{} = tid), do: DateTime.from_unix(tid.timestamp, :microsecond)
68
+
Generate a random integer to be used as a `clock_id`.
70
+
@spec gen_clock_id() :: clock_id()
71
+
def gen_clock_id, do: :rand.uniform(1024) - 1
74
+
Decode a TID string into an `Atex.TID` struct, returning an error if it's invalid.
78
+
Syntactically valid TIDs:
80
+
iex> Atex.TID.decode("3jzfcijpj2z2a")
81
+
{:ok, %Atex.TID{clock_id: 6, timestamp: 1688137381887007}}
83
+
iex> Atex.TID.decode("7777777777777")
84
+
{:ok, %Atex.TID{clock_id: 165, timestamp: 5811096293381285}}
86
+
iex> Atex.TID.decode("3zzzzzzzzzzzz")
87
+
{:ok, %Atex.TID{clock_id: 1023, timestamp: 2251799813685247}}
89
+
iex> Atex.TID.decode("2222222222222")
90
+
{:ok, %Atex.TID{clock_id: 0, timestamp: 0}}
95
+
iex> Atex.TID.decode("3jzfcijpj2z21")
97
+
iex> Atex.TID.decode("0000000000000")
101
+
iex> Atex.TID.decode("3JZFCIJPJ2Z2A")
105
+
iex> Atex.TID.decode("3jzfcijpj2z2aa")
107
+
iex> Atex.TID.decode("3jzfcijpj2z2")
109
+
iex> Atex.TID.decode("222")
112
+
# legacy dash syntax *not* supported (TTTT-TTT-TTTT-CC)
113
+
iex> Atex.TID.decode("3jzf-cij-pj2z-2a")
116
+
# high bit can't be set
117
+
iex> Atex.TID.decode("zzzzzzzzzzzzz")
119
+
iex> Atex.TID.decode("kjzfcijpj2z2a")
123
+
@spec decode(String.t()) :: {:ok, t()} | :error
124
+
def decode(<<timestamp::binary-size(11), clock_id::binary-size(2)>> = tid) do
125
+
if Regex.match?(@re, tid) do
126
+
timestamp = Base32Sortable.decode(timestamp)
127
+
clock_id = Base32Sortable.decode(clock_id)
131
+
timestamp: timestamp,
139
+
def decode(_tid), do: :error
142
+
Encode a TID struct into a string.
146
+
iex> Atex.TID.encode(%Atex.TID{clock_id: 6, timestamp: 1688137381887007})
149
+
iex> Atex.TID.encode(%Atex.TID{clock_id: 165, timestamp: 5811096293381285})
152
+
iex> Atex.TID.encode(%Atex.TID{clock_id: 1023, timestamp: 2251799813685247})
155
+
iex> Atex.TID.encode(%Atex.TID{clock_id: 0, timestamp: 0})
159
+
@spec encode(t()) :: String.t()
160
+
def encode(%__MODULE__{} = tid) do
161
+
timestamp = tid.timestamp |> Base32Sortable.encode() |> String.pad_leading(11, "2")
162
+
clock_id = (tid.clock_id &&& 1023) |> Base32Sortable.encode() |> String.pad_leading(2, "2")
163
+
timestamp <> clock_id
167
+
defimpl String.Chars, for: Atex.TID do
168
+
def to_string(tid), do: Atex.TID.encode(tid)