Update
This commit is contained in:
parent
58049c93d4
commit
894866ca03
22 changed files with 344 additions and 213 deletions
30
lib/nulla/models/relation.ex
Normal file
30
lib/nulla/models/relation.ex
Normal file
|
@ -0,0 +1,30 @@
|
|||
defmodule Nulla.Models.Relation do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
alias Nulla.Models.Actor
|
||||
alias Nulla.Models.Activity
|
||||
|
||||
@primary_key {:id, :integer, autogenerate: false}
|
||||
schema "relations" do
|
||||
field :type, :string
|
||||
field :status, :string
|
||||
|
||||
belongs_to :source, Actor, foreign_key: :source_id, type: :integer
|
||||
belongs_to :target, Actor, foreign_key: :target_id, type: :integer
|
||||
belongs_to :activity, Activity, foreign_key: :activity_id, type: :integer
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
def changeset(relation, attrs) do
|
||||
relation
|
||||
|> cast(attrs, [:id, :source_id, :target_id, :type, :status, :activity_id])
|
||||
|> validate_required([:id, :source_id, :target_id, :type])
|
||||
|> validate_inclusion(:type, ~w(follow block mute friend_request))
|
||||
|> validate_inclusion(:status, ~w(pending accepted rejected active))
|
||||
|> foreign_key_constraint(:source_id)
|
||||
|> foreign_key_constraint(:target_id)
|
||||
|> foreign_key_constraint(:activity_id)
|
||||
|> unique_constraint([:source_id, :target_id, :type])
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue