summaryrefslogtreecommitdiff
path: root/lib/pleroma/conversation/participation/recipient_ship.ex
blob: de40bacace7cf1f4687d8f69037cd0701e224d23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Conversation.Participation.RecipientShip do
  use Ecto.Schema

  alias Pleroma.Conversation.Participation
  alias Pleroma.Repo
  alias Pleroma.User

  import Ecto.Changeset

  schema "conversation_participation_recipient_ships" do
    belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
    belongs_to(:participation, Participation)
  end

  def creation_cng(struct, params) do
    struct
    |> cast(params, [:user_id, :participation_id])
    |> validate_required([:user_id, :participation_id])
  end

  def create(%User{} = user, participation), do: create([user], participation)

  def create(users, participation) do
    Enum.each(users, fn user ->
      %__MODULE__{}
      |> creation_cng(%{user_id: user.id, participation_id: participation.id})
      |> Repo.insert!()
    end)
  end
end