2019-03-18 08:23:38 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2022-02-25 23:11:42 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-03-18 08:23:38 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Registration do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
import Ecto.Changeset
|
|
|
|
|
|
|
|
alias Pleroma.Registration
|
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.User
|
|
|
|
|
2019-09-18 08:54:31 -06:00
|
|
|
@primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true}
|
2019-03-20 01:35:31 -06:00
|
|
|
|
2019-03-18 08:23:38 -06:00
|
|
|
schema "registrations" do
|
2019-09-18 08:54:31 -06:00
|
|
|
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
|
2019-03-18 08:23:38 -06:00
|
|
|
field(:provider, :string)
|
|
|
|
field(:uid, :string)
|
|
|
|
field(:info, :map, default: %{})
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2019-03-20 01:35:31 -06:00
|
|
|
def nickname(registration, default \\ nil),
|
|
|
|
do: Map.get(registration.info, "nickname", default)
|
|
|
|
|
|
|
|
def email(registration, default \\ nil),
|
|
|
|
do: Map.get(registration.info, "email", default)
|
|
|
|
|
|
|
|
def name(registration, default \\ nil),
|
|
|
|
do: Map.get(registration.info, "name", default)
|
|
|
|
|
|
|
|
def description(registration, default \\ nil),
|
|
|
|
do: Map.get(registration.info, "description", default)
|
|
|
|
|
2019-03-18 08:23:38 -06:00
|
|
|
def changeset(registration, params \\ %{}) do
|
|
|
|
registration
|
|
|
|
|> cast(params, [:user_id, :provider, :uid, :info])
|
2019-03-18 11:31:24 -06:00
|
|
|
|> validate_required([:provider, :uid])
|
2019-03-18 08:23:38 -06:00
|
|
|
|> foreign_key_constraint(:user_id)
|
|
|
|
|> unique_constraint(:uid, name: :registrations_provider_uid_index)
|
|
|
|
end
|
|
|
|
|
2019-03-20 01:35:31 -06:00
|
|
|
def bind_to_user(registration, user) do
|
|
|
|
registration
|
|
|
|
|> changeset(%{user_id: (user && user.id) || nil})
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
2019-03-18 08:23:38 -06:00
|
|
|
def get_by_provider_uid(provider, uid) do
|
|
|
|
Repo.get_by(Registration,
|
|
|
|
provider: to_string(provider),
|
|
|
|
uid: to_string(uid)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|