1bd91a31652adaf8522c503ee104d13dd0f8afe3
[akkoma] / lib / pleroma / registration.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Registration do
6 use Ecto.Schema
7
8 import Ecto.Changeset
9
10 alias Pleroma.Registration
11 alias Pleroma.Repo
12 alias Pleroma.User
13
14 schema "registrations" do
15 belongs_to(:user, User, type: Pleroma.FlakeId)
16 field(:provider, :string)
17 field(:uid, :string)
18 field(:info, :map, default: %{})
19
20 timestamps()
21 end
22
23 def changeset(registration, params \\ %{}) do
24 registration
25 |> cast(params, [:user_id, :provider, :uid, :info])
26 |> foreign_key_constraint(:user_id)
27 |> unique_constraint(:uid, name: :registrations_provider_uid_index)
28 end
29
30 def get_by_provider_uid(provider, uid) do
31 Repo.get_by(Registration,
32 provider: to_string(provider),
33 uid: to_string(uid)
34 )
35 end
36 end