logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

registration.ex (1563B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Registration do
  5. use Ecto.Schema
  6. import Ecto.Changeset
  7. alias Pleroma.Registration
  8. alias Pleroma.Repo
  9. alias Pleroma.User
  10. @primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true}
  11. schema "registrations" do
  12. belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
  13. field(:provider, :string)
  14. field(:uid, :string)
  15. field(:info, :map, default: %{})
  16. timestamps()
  17. end
  18. def nickname(registration, default \\ nil),
  19. do: Map.get(registration.info, "nickname", default)
  20. def email(registration, default \\ nil),
  21. do: Map.get(registration.info, "email", default)
  22. def name(registration, default \\ nil),
  23. do: Map.get(registration.info, "name", default)
  24. def description(registration, default \\ nil),
  25. do: Map.get(registration.info, "description", default)
  26. def changeset(registration, params \\ %{}) do
  27. registration
  28. |> cast(params, [:user_id, :provider, :uid, :info])
  29. |> validate_required([:provider, :uid])
  30. |> foreign_key_constraint(:user_id)
  31. |> unique_constraint(:uid, name: :registrations_provider_uid_index)
  32. end
  33. def bind_to_user(registration, user) do
  34. registration
  35. |> changeset(%{user_id: (user && user.id) || nil})
  36. |> Repo.update()
  37. end
  38. def get_by_provider_uid(provider, uid) do
  39. Repo.get_by(Registration,
  40. provider: to_string(provider),
  41. uid: to_string(uid)
  42. )
  43. end
  44. end