@type t :: %__MODULE__{}
@type actor :: String.t()
- @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
+ @primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true}
# https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
@mastodon_notification_types %{
alias Pleroma.Activity
alias Pleroma.ActivityExpiration
- alias Pleroma.FlakeId
alias Pleroma.Repo
import Ecto.Changeset
@min_activity_lifetime :timer.hours(1)
schema "activity_expirations" do
- belongs_to(:activity, Activity, type: FlakeId)
+ belongs_to(:activity, Activity, type: FlakeId.Ecto.CompatType)
field(:scheduled_at, :naive_datetime)
end
Pleroma.Config.TransferTask,
Pleroma.Emoji,
Pleroma.Captcha,
- Pleroma.FlakeId,
Pleroma.Daemons.ScheduledActivityDaemon,
Pleroma.Daemons.ActivityExpirationDaemon
] ++
alias Pleroma.Activity
alias Pleroma.Bookmark
- alias Pleroma.FlakeId
alias Pleroma.Repo
alias Pleroma.User
@type t :: %__MODULE__{}
schema "bookmarks" do
- belongs_to(:user, User, type: FlakeId)
- belongs_to(:activity, Activity, type: FlakeId)
+ belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
+ belongs_to(:activity, Activity, type: FlakeId.Ecto.CompatType)
timestamps()
end
- @spec create(FlakeId.t(), FlakeId.t()) :: {:ok, Bookmark.t()} | {:error, Changeset.t()}
+ @spec create(FlakeId.Ecto.CompatType.t(), FlakeId.Ecto.CompatType.t()) ::
+ {:ok, Bookmark.t()} | {:error, Changeset.t()}
def create(user_id, activity_id) do
attrs = %{
user_id: user_id,
|> Repo.insert()
end
- @spec for_user_query(FlakeId.t()) :: Ecto.Query.t()
+ @spec for_user_query(FlakeId.Ecto.CompatType.t()) :: Ecto.Query.t()
def for_user_query(user_id) do
Bookmark
|> where(user_id: ^user_id)
|> Repo.one()
end
- @spec destroy(FlakeId.t(), FlakeId.t()) :: {:ok, Bookmark.t()} | {:error, Changeset.t()}
+ @spec destroy(FlakeId.Ecto.CompatType.t(), FlakeId.Ecto.CompatType.t()) ::
+ {:ok, Bookmark.t()} | {:error, Changeset.t()}
def destroy(user_id, activity_id) do
from(b in Bookmark,
where: b.user_id == ^user_id,
import Ecto.Query
schema "conversation_participations" do
- belongs_to(:user, User, type: Pleroma.FlakeId)
+ belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
belongs_to(:conversation, Conversation)
field(:read, :boolean, default: false)
- field(:last_activity_id, Pleroma.FlakeId, virtual: true)
+ field(:last_activity_id, FlakeId.Ecto.CompatType, virtual: true)
has_many(:recipient_ships, RecipientShip)
has_many(:recipients, through: [:recipient_ships, :user])
import Ecto.Changeset
schema "conversation_participation_recipient_ships" do
- belongs_to(:user, User, type: Pleroma.FlakeId)
+ belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
belongs_to(:participation, Participation)
end
use Ecto.Schema
alias Pleroma.Delivery
- alias Pleroma.FlakeId
alias Pleroma.Object
alias Pleroma.Repo
alias Pleroma.User
import Ecto.Query
schema "deliveries" do
- belongs_to(:user, User, type: FlakeId)
+ belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
belongs_to(:object, Object)
end
alias Pleroma.User
schema "filters" do
- belongs_to(:user, User, type: Pleroma.FlakeId)
+ belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
field(:filter_id, :integer)
field(:hide, :boolean, default: false)
field(:whole_word, :boolean, default: true)
+++ /dev/null
-# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
-# SPDX-License-Identifier: AGPL-3.0-only
-
-defmodule Pleroma.FlakeId do
- @moduledoc """
- Flake is a decentralized, k-ordered id generation service.
-
- Adapted from:
-
- * [flaky](https://github.com/nirvana/flaky), released under the terms of the Truly Free License,
- * [Flake](https://github.com/boundary/flake), Copyright 2012, Boundary, Apache License, Version 2.0
- """
-
- @type t :: binary
-
- use Ecto.Type
- use GenServer
- require Logger
- alias __MODULE__
- import Kernel, except: [to_string: 1]
-
- defstruct node: nil, time: 0, sq: 0
-
- @doc "Converts a binary Flake to a String"
- def to_string(<<0::integer-size(64), id::integer-size(64)>>) do
- Kernel.to_string(id)
- end
-
- 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(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)>>
- 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)>>
- _ -> nil
- end
- end
-
- def from_string(string) do
- string |> decode_base62 |> from_integer
- end
-
- def to_integer(<<integer::integer-size(128)>>), do: integer
-
- def from_integer(integer) do
- <<_time::integer-size(64), _node::integer-size(48), _seq::integer-size(16)>> =
- <<integer::integer-size(128)>>
- end
-
- @doc "Generates a Flake"
- @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
-
- @impl Ecto.Type
- def cast(value) do
- {:ok, FlakeId.to_string(value)}
- end
-
- @impl Ecto.Type
- def load(value) do
- {:ok, FlakeId.to_string(value)}
- end
-
- @impl Ecto.Type
- def dump(value) do
- {:ok, FlakeId.from_string(value)}
- end
-
- def autogenerate, do: get()
-
- # -- GenServer API
- def start_link(_) do
- :gen_server.start_link({:local, :flake}, __MODULE__, [], [])
- end
-
- @impl GenServer
- def init([]) do
- {:ok, %FlakeId{node: worker_id(), time: time()}}
- end
-
- @impl GenServer
- def handle_call(:get, _from, state) do
- {flake, new_state} = get(time(), state)
- {:reply, flake, new_state}
- end
-
- # Matches when the calling time is the same as the state time. Incr. sq
- defp get(time, %FlakeId{time: time, node: node, sq: seq}) do
- new_state = %FlakeId{time: time, node: node, sq: seq + 1}
- {gen_flake(new_state), new_state}
- end
-
- # Matches when the times are different, reset sq
- defp get(newtime, %FlakeId{time: time, node: node}) when newtime > time do
- new_state = %FlakeId{time: newtime, node: node, sq: 0}
- {gen_flake(new_state), new_state}
- end
-
- # Error when clock is running backwards
- defp get(newtime, %FlakeId{time: time}) when newtime < time do
- {:error, :clock_running_backwards}
- end
-
- defp gen_flake(%FlakeId{time: time, node: node, sq: seq}) do
- <<time::integer-size(64), node::integer-size(48), seq::integer-size(16)>>
- end
-
- defp nthchar_base62(n) when n <= 9, do: ?0 + n
- defp nthchar_base62(n) when n <= 35, do: ?A + n - 10
- defp nthchar_base62(n), do: ?a + n - 36
-
- defp encode_base62(<<integer::integer-size(128)>>) do
- integer
- |> encode_base62([])
- |> List.to_string()
- end
-
- defp encode_base62(int, acc) when int < 0, do: encode_base62(-int, acc)
- defp encode_base62(int, []) when int == 0, do: '0'
- defp encode_base62(int, acc) when int == 0, do: acc
-
- defp encode_base62(int, acc) do
- r = rem(int, 62)
- id = div(int, 62)
- acc = [nthchar_base62(r) | acc]
- encode_base62(id, acc)
- end
-
- defp decode_base62(s) do
- decode_base62(String.to_charlist(s), 0)
- end
-
- defp decode_base62([c | cs], acc) when c >= ?0 and c <= ?9,
- do: decode_base62(cs, 62 * acc + (c - ?0))
-
- defp decode_base62([c | cs], acc) when c >= ?A and c <= ?Z,
- do: decode_base62(cs, 62 * acc + (c - ?A + 10))
-
- defp decode_base62([c | cs], acc) when c >= ?a and c <= ?z,
- do: decode_base62(cs, 62 * acc + (c - ?a + 36))
-
- defp decode_base62([], acc), do: acc
-
- defp time do
- {mega_seconds, seconds, micro_seconds} = :erlang.timestamp()
- 1_000_000_000 * mega_seconds + seconds * 1000 + :erlang.trunc(micro_seconds / 1000)
- end
-
- defp worker_id do
- <<worker::integer-size(48)>> = :crypto.strong_rand_bytes(6)
- worker
- end
-end
alias Pleroma.User
schema "lists" do
- belongs_to(:user, User, type: Pleroma.FlakeId)
+ belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
field(:title, :string)
field(:following, {:array, :string}, default: [])
field(:ap_id, :string)
schema "notifications" do
field(:seen, :boolean, default: false)
- belongs_to(:user, User, type: Pleroma.FlakeId)
- belongs_to(:activity, Activity, type: Pleroma.FlakeId)
+ belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
+ belongs_to(:activity, Activity, type: FlakeId.Ecto.CompatType)
timestamps()
end
alias Pleroma.User
schema "password_reset_tokens" do
- belongs_to(:user, User, type: Pleroma.FlakeId)
+ belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
field(:token, :string)
field(:used, :boolean, default: false)
alias Pleroma.Repo
alias Pleroma.User
- @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
+ @primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true}
schema "registrations" do
- belongs_to(:user, User, type: Pleroma.FlakeId)
+ belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
field(:provider, :string)
field(:uid, :string)
field(:info, :map, default: %{})
@min_offset :timer.minutes(5)
schema "scheduled_activities" do
- belongs_to(:user, User, type: Pleroma.FlakeId)
+ belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
field(:scheduled_at, :naive_datetime)
field(:params, :map)
require Ecto.Query
schema "thread_mutes" do
- belongs_to(:user, User, type: Pleroma.FlakeId)
+ belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
field(:context, :string)
end
end
def query(user_id, context) do
- user_id = Pleroma.FlakeId.from_string(user_id)
+ {:ok, user_id} = FlakeId.Ecto.CompatType.dump(user_id)
ThreadMute
|> Ecto.Query.where(user_id: ^user_id)
@type t :: %__MODULE__{}
- @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
+ @primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true}
# credo:disable-for-next-line Credo.Check.Readability.MaxLineLength
@email_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
restrict_to_local = Pleroma.Config.get([:instance, :limit_to_local_content])
cond do
- is_integer(nickname_or_id) or Pleroma.FlakeId.is_flake_id?(nickname_or_id) ->
+ is_integer(nickname_or_id) or FlakeId.flake_id?(nickname_or_id) ->
get_cached_by_id(nickname_or_id) || get_cached_by_nickname(nickname_or_id)
restrict_to_local == false ->
end
@spec fetch_latest_activity_id_for_context(String.t(), keyword() | map()) ::
- Pleroma.FlakeId.t() | nil
+ FlakeId.Ecto.CompatType.t() | nil
def fetch_latest_activity_id_for_context(context, opts \\ %{}) do
context
|> fetch_activities_for_context_query(Map.merge(%{"skip_preload" => true}, opts))
# This is a hack for twidere.
def get_by_id_or_ap_id(id) do
activity =
- with true <- Pleroma.FlakeId.is_flake_id?(id),
+ with true <- FlakeId.flake_id?(id),
%Activity{} = activity <- Activity.get_by_id_with_object(id) do
activity
else
field(:scopes, {:array, :string}, default: [])
field(:valid_until, :naive_datetime_usec)
field(:used, :boolean, default: false)
- belongs_to(:user, User, type: Pleroma.FlakeId)
+ belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
belongs_to(:app, App)
timestamps()
field(:refresh_token, :string)
field(:scopes, {:array, :string}, default: [])
field(:valid_until, :naive_datetime_usec)
- belongs_to(:user, User, type: Pleroma.FlakeId)
+ belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
belongs_to(:app, App)
timestamps()
@type t :: %__MODULE__{}
schema "push_subscriptions" do
- belongs_to(:user, User, type: Pleroma.FlakeId)
+ belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
belongs_to(:token, Token)
field(:endpoint, :string)
field(:key_p256dh, :string)
field(:state, :string)
field(:subscribers, {:array, :string}, default: [])
field(:hub, :string)
- belongs_to(:user, User, type: Pleroma.FlakeId)
+ belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
timestamps()
end
{:ex_const, "~> 0.2"},
{:plug_static_index_html, "~> 1.0.0"},
{:excoveralls, "~> 0.11.1", only: :test},
+ {:flake_id, "~> 0.1.0"},
{:mox, "~> 0.5", only: :test}
] ++ oauth_deps()
end
%{
"accept": {:hex, :accept, "0.3.5", "b33b127abca7cc948bbe6caa4c263369abf1347cfa9d8e699c6d214660f10cd1", [:rebar3], [], "hexpm"},
"auto_linker": {:git, "https://git.pleroma.social/pleroma/auto_linker.git", "95e8188490e97505c56636c1379ffdf036c1fdde", [ref: "95e8188490e97505c56636c1379ffdf036c1fdde"]},
+ "base62": {:hex, :base62, "1.2.1", "4866763e08555a7b3917064e9eef9194c41667276c51b59de2bc42c6ea65f806", [:mix], [{:custom_base, "~> 0.2.1", [hex: :custom_base, repo: "hexpm", optional: false]}], "hexpm"},
"base64url": {:hex, :base64url, "0.0.1", "36a90125f5948e3afd7be97662a1504b934dd5dac78451ca6e9abf85a10286be", [:rebar], [], "hexpm"},
"bbcode": {:hex, :bbcode, "0.1.1", "0023e2c7814119b2e620b7add67182e3f6019f92bfec9a22da7e99821aceba70", [:mix], [{:nimble_parsec, "~> 0.5", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
"benchee": {:hex, :benchee, "1.0.1", "66b211f9bfd84bd97e6d1beaddf8fc2312aaabe192f776e8931cb0c16f53a521", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}], "hexpm"},
"credo": {:hex, :credo, "0.9.3", "76fa3e9e497ab282e0cf64b98a624aa11da702854c52c82db1bf24e54ab7c97a", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:poison, ">= 0.0.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
"crontab": {:hex, :crontab, "1.1.7", "b9219f0bdc8678b94143655a8f229716c5810c0636a4489f98c0956137e53985", [:mix], [{:ecto, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm"},
"crypt": {:git, "https://github.com/msantos/crypt", "1f2b58927ab57e72910191a7ebaeff984382a1d3", [ref: "1f2b58927ab57e72910191a7ebaeff984382a1d3"]},
+ "custom_base": {:hex, :custom_base, "0.2.1", "4a832a42ea0552299d81652aa0b1f775d462175293e99dfbe4d7dbaab785a706", [:mix], [], "hexpm"},
"db_connection": {:hex, :db_connection, "2.1.1", "a51e8a2ee54ef2ae6ec41a668c85787ed40cb8944928c191280fe34c15b76ae5", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm"},
"decimal": {:hex, :decimal, "1.8.0", "ca462e0d885f09a1c5a342dbd7c1dcf27ea63548c65a65e67334f4b61803822e", [:mix], [], "hexpm"},
"deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm"},
"ex_rated": {:hex, :ex_rated, "1.3.3", "30ecbdabe91f7eaa9d37fa4e81c85ba420f371babeb9d1910adbcd79ec798d27", [:mix], [{:ex2ms, "~> 1.5", [hex: :ex2ms, repo: "hexpm", optional: false]}], "hexpm"},
"ex_syslogger": {:git, "https://github.com/slashmili/ex_syslogger.git", "f3963399047af17e038897c69e20d552e6899e1d", [tag: "1.4.0"]},
"excoveralls": {:hex, :excoveralls, "0.11.1", "dd677fbdd49114fdbdbf445540ec735808250d56b011077798316505064edb2c", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
+ "flake_id": {:hex, :flake_id, "0.1.0", "7716b086d2e405d09b647121a166498a0d93d1a623bead243e1f74216079ccb3", [:mix], [{:base62, "~> 1.2", [hex: :base62, repo: "hexpm", optional: false]}, {:ecto, ">= 2.0.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm"},
"floki": {:hex, :floki, "0.23.0", "956ab6dba828c96e732454809fb0bd8d43ce0979b75f34de6322e73d4c917829", [:mix], [{:html_entities, "~> 0.4.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm"},
"gen_smtp": {:hex, :gen_smtp, "0.14.0", "39846a03522456077c6429b4badfd1d55e5e7d0fdfb65e935b7c5e38549d9202", [:rebar3], [], "hexpm"},
"gen_stage": {:hex, :gen_stage, "0.14.2", "6a2a578a510c5bfca8a45e6b27552f613b41cf584b58210f017088d3d17d0b14", [:mix], [], "hexpm"},
+++ /dev/null
-# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
-# SPDX-License-Identifier: AGPL-3.0-only
-
-defmodule Pleroma.FlakeIdTest do
- use Pleroma.DataCase
- import Kernel, except: [to_string: 1]
- import Pleroma.FlakeId
-
- describe "fake flakes (compatibility with older serial integers)" do
- test "from_string/1" do
- fake_flake = <<0::integer-size(64), 42::integer-size(64)>>
- assert from_string("42") == fake_flake
- assert from_string(42) == fake_flake
- end
-
- test "zero or -1 is a null flake" do
- fake_flake = <<0::integer-size(128)>>
- assert from_string("0") == fake_flake
- assert from_string("-1") == fake_flake
- end
-
- test "to_string/1" do
- fake_flake = <<0::integer-size(64), 42::integer-size(64)>>
- assert to_string(fake_flake) == "42"
- end
- end
-
- test "ecto type behaviour" do
- flake = <<0, 0, 1, 104, 80, 229, 2, 235, 140, 22, 69, 201, 53, 210, 0, 0>>
- flake_s = "9eoozpwTul5mjSEDRI"
-
- assert cast(flake) == {:ok, flake_s}
- assert cast(flake_s) == {:ok, flake_s}
-
- assert load(flake) == {:ok, flake_s}
- assert load(flake_s) == {:ok, flake_s}
-
- assert dump(flake_s) == {:ok, flake}
- assert dump(flake) == {:ok, flake}
- end
-
- test "is_flake_id?/1" do
- assert is_flake_id?("9eoozpwTul5mjSEDRI")
- refute is_flake_id?("http://example.com/activities/3ebbadd1-eb14-4e20-8118-b6f79c0c7b0b")
- end
-end