1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Registration do
10 alias Pleroma.Registration
14 @primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true}
16 schema "registrations" do
17 belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
18 field(:provider, :string)
20 field(:info, :map, default: %{})
25 def nickname(registration, default \\ nil),
26 do: Map.get(registration.info, "nickname", default)
28 def email(registration, default \\ nil),
29 do: Map.get(registration.info, "email", default)
31 def name(registration, default \\ nil),
32 do: Map.get(registration.info, "name", default)
34 def description(registration, default \\ nil),
35 do: Map.get(registration.info, "description", default)
37 def changeset(registration, params \\ %{}) do
39 |> cast(params, [:user_id, :provider, :uid, :info])
40 |> validate_required([:provider, :uid])
41 |> foreign_key_constraint(:user_id)
42 |> unique_constraint(:uid, name: :registrations_provider_uid_index)
45 def bind_to_user(registration, user) do
47 |> changeset(%{user_id: (user && user.id) || nil})
51 def get_by_provider_uid(provider, uid) do
52 Repo.get_by(Registration,
53 provider: to_string(provider),