X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fflake_id.ex;h=47d61ca5f16e85990ebaa0197800fd08498ea702;hb=94076a23e40f74256fec507e6236aaff10218277;hp=3c72807caef18ff64905e8fb754c0759be24974e;hpb=28d77e373cbaf0908f86973a873c9bfd6c3221cb;p=akkoma
diff --git a/lib/pleroma/flake_id.ex b/lib/pleroma/flake_id.ex
index 3c72807ca..47d61ca5f 100644
--- a/lib/pleroma/flake_id.ex
+++ b/lib/pleroma/flake_id.ex
@@ -1,3 +1,7 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors
+# SPDX-License-Identifier: AGPL-3.0-only
+
defmodule Pleroma.FlakeId do
@moduledoc """
Flake is a decentralized, k-ordered id generation service.
@@ -23,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
@@ -38,9 +42,11 @@ defmodule Pleroma.FlakeId do
def from_string(unquote(Kernel.to_string(i))), do: <<0::integer-size(128)>>
end
+ 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
@@ -60,6 +66,16 @@ defmodule Pleroma.FlakeId do
@spec get :: binary
def get, do: to_string(:gen_server.call(:flake, :get))
+ # checks that ID is is valid FlakeID
+ #
+ @spec is_flake_id?(String.t()) :: boolean
+ def is_flake_id?(id), do: is_flake_id?(String.to_charlist(id), true)
+ defp is_flake_id?([c | cs], true) when c >= ?0 and c <= ?9, do: is_flake_id?(cs, true)
+ defp is_flake_id?([c | cs], true) when c >= ?A and c <= ?Z, do: is_flake_id?(cs, true)
+ defp is_flake_id?([c | cs], true) when c >= ?a and c <= ?z, do: is_flake_id?(cs, true)
+ defp is_flake_id?([], true), do: true
+ defp is_flake_id?(_, _), do: false
+
# -- Ecto.Type API
@impl Ecto.Type
def type, do: :uuid
@@ -79,16 +95,16 @@ defmodule Pleroma.FlakeId do
{:ok, FlakeId.from_string(value)}
end
- def autogenerate(), do: get()
+ def autogenerate, do: get()
# -- GenServer API
- def start_link do
+ def start_link(_) do
:gen_server.start_link({:local, :flake}, __MODULE__, [], [])
end
@impl GenServer
def init([]) do
- {:ok, %FlakeId{node: mac(), time: time()}}
+ {:ok, %FlakeId{node: worker_id(), time: time()}}
end
@impl GenServer
@@ -159,23 +175,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