773e25fa604b05effcefa33b4f8bcac58b86d1c8
[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 |> validate_required([:provider, :uid])
27 |> foreign_key_constraint(:user_id)
28 |> unique_constraint(:uid, name: :registrations_provider_uid_index)
29 end
30
31 def get_by_provider_uid(provider, uid) do
32 Repo.get_by(Registration,
33 provider: to_string(provider),
34 uid: to_string(uid)
35 )
36 end
37 end