X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fflake_id.ex;h=69ab8ccf929f10184968a009f76fce5aa3384392;hb=8180de951e2ac1efc6cfb9f64d078d6a1b7d0c0e;hp=6b83ee8904690442f8895597d11c1d6d303d42e2;hpb=e36a434b990cfdd650aed1710c55b1f62bd93ccb;p=akkoma diff --git a/lib/pleroma/flake_id.ex b/lib/pleroma/flake_id.ex index 6b83ee890..69ab8ccf9 100644 --- a/lib/pleroma/flake_id.ex +++ b/lib/pleroma/flake_id.ex @@ -33,6 +33,10 @@ defmodule Pleroma.FlakeId do def to_string(s), do: s + def from_string(int) when is_integer(int) do + from_string(Kernel.to_string(int)) + end + for i <- [-1, 0] do def from_string(unquote(i)), do: <<0::integer-size(128)>> def from_string(unquote(Kernel.to_string(i))), do: <<0::integer-size(128)>> @@ -90,7 +94,7 @@ defmodule Pleroma.FlakeId do @impl GenServer def init([]) do - {:ok, %FlakeId{node: mac(), time: time()}} + {:ok, %FlakeId{node: worker_id(), time: time()}} end @impl GenServer @@ -161,23 +165,8 @@ defmodule Pleroma.FlakeId do 1_000_000_000 * mega_seconds + seconds * 1000 + :erlang.trunc(micro_seconds / 1000) end - defp mac do - {:ok, addresses} = :inet.getifaddrs() - - ifaces_with_mac = - Enum.reduce(addresses, [], fn {iface, attrs}, acc -> - if attrs[:hwaddr], do: [iface | acc], else: acc - end) - - iface = Enum.at(ifaces_with_mac, :rand.uniform(length(ifaces_with_mac)) - 1) - mac(iface) - end - - defp mac(name) do - {:ok, addresses} = :inet.getifaddrs() - proplist = :proplists.get_value(name, addresses) - hwaddr = Enum.take(:proplists.get_value(:hwaddr, proplist), 6) - <> = :binary.list_to_bin(hwaddr) + defp worker_id() do + <> = :crypto.strong_rand_bytes(6) worker end end