Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / lib / pleroma / flake_id.ex
index 69482f69a7ce9554131ee19912a1d646fc09addc..ca0610abc5c3128134f55a5d0478fcb8eb982806 100644 (file)
@@ -27,7 +27,7 @@ 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
 
@@ -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
@@ -66,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
@@ -85,7 +95,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 +104,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 +175,8 @@ defmodule Pleroma.FlakeId do
     1_000_000_000 * mega_seconds + seconds * 1000 + :erlang.trunc(micro_seconds / 1000)
   end
 
-  def mac do
-    {:ok, addresses} = :inet.getifaddrs()
-
-    macids =
-      Enum.reduce(addresses, [], fn {_iface, attrs}, acc ->
-        case attrs[:hwaddr] do
-          [0, 0, 0 | _] -> acc
-          mac when is_list(mac) -> [mac_to_worker_id(mac) | acc]
-          _ -> acc
-        end
-      end)
-
-    List.first(macids)
-  end
-
-  def mac_to_worker_id(mac) do
-    <<worker::integer-size(48)>> = :binary.list_to_bin(mac)
+  defp worker_id do
+    <<worker::integer-size(48)>> = :crypto.strong_rand_bytes(6)
     worker
   end
 end