X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fflake_id.ex;h=58ab3650dc952de4465c30a77cbcf78f06b11e66;hb=6d19bb4eae43270099a68f749519ba0f323da01a;hp=f23c6d4b065e20b4c217c5be198c01c94c4db9d7;hpb=9d63b27dcd61dff61b77b6df0ef8a4cf8d7c87e3;p=akkoma
diff --git a/lib/pleroma/flake_id.ex b/lib/pleroma/flake_id.ex
index f23c6d4b0..58ab3650d 100644
--- a/lib/pleroma/flake_id.ex
+++ b/lib/pleroma/flake_id.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2018 Pleroma Authors
+# Copyright © 2017-2019 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.FlakeId do
@@ -27,14 +27,14 @@ defmodule Pleroma.FlakeId do
Kernel.to_string(id)
end
- def to_string(flake = <<_::integer-size(64), _::integer-size(48), _::integer-size(16)>>) do
+ def to_string(<<_::integer-size(64), _::integer-size(48), _::integer-size(16)>> = flake) do
encode_base62(flake)
end
def to_string(s), do: s
- def from_string(<>) do
- <<0::integer-size(64), id::integer-size(64)>>
+ def from_string(int) when is_integer(int) do
+ from_string(Kernel.to_string(int))
end
for i <- [-1, 0] do
@@ -42,11 +42,11 @@ defmodule Pleroma.FlakeId do
def from_string(unquote(Kernel.to_string(i))), do: <<0::integer-size(128)>>
end
- def from_string(flake = <<_::integer-size(128)>>), do: flake
+ def from_string(<<_::integer-size(128)>> = flake), do: flake
def from_string(string) when is_binary(string) and byte_size(string) < 18 do
case Integer.parse(string) do
- {id, _} -> <<0::integer-size(64), id::integer-size(64)>>
+ {id, ""} -> <<0::integer-size(64), id::integer-size(64)>>
_ -> nil
end
end
@@ -85,7 +85,7 @@ defmodule Pleroma.FlakeId do
{:ok, FlakeId.from_string(value)}
end
- def autogenerate(), do: get()
+ def autogenerate, do: get()
# -- GenServer API
def start_link do
@@ -94,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
@@ -165,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