Verified Commit 2ef26d1a authored by Nicolas's avatar Nicolas

Add event_auth

make mx_event_id optional
parent 098cca71
Pipeline #107558144 passed with stages
in 3 minutes and 52 seconds
defmodule PlasmaRepo.Events.Event do
use PlasmaRepo.Schema
alias PlasmaRepo.Events.{Event, EventContent, EventEdge}
alias PlasmaRepo.Events.{Event, EventContent, EventEdge, EventAuth}
alias PlasmaRepo.Events.EventType
alias PlasmaRepo.Events.Identifier
alias PlasmaRepo.Rooms.Room
......@@ -17,8 +17,9 @@ defmodule PlasmaRepo.Events.Event do
event_type: EventType.t(),
inserted_at: DateTime.t(),
updated_at: DateTime.t(),
child_edges: [EventEdge],
parent_edges: [ParentEdge],
child_edges: [EventEdge.t()],
parent_edges: [EventEdge.t()],
auth_events: [EventAuth.t()]
}
schema "events" do
......@@ -32,11 +33,12 @@ defmodule PlasmaRepo.Events.Event do
belongs_to :event_type, EventType
has_many :child_edges, EventEdge, foreign_key: :child_event_id
has_many :parent_edges, EventEdge, foreign_key: :parent_event_id
has_many :auth_events, EventAuth, foreign_key: :event_id
field :rejected, :boolean, default: false
timestamps()
end
def changeset(event, attrs \\ %{}) do
def changeset(%Event{} = event, attrs \\ %{}) do
event
|> cast(attrs, [
:mx_event_id,
......@@ -48,14 +50,14 @@ defmodule PlasmaRepo.Events.Event do
:event_type_id,
:rejected
])
|> validate_required([:mx_event_id, :sender_id, :room_id, :event_type_id], message: "required")
|> validate_required([:sender_id, :room_id, :event_type_id], message: "required")
|> unique_constraint(:mx_event_id, message: "unique")
|> assoc_constraint(:sender)
|> assoc_constraint(:room)
|> assoc_constraint(:event_type)
end
def create_changeset(event, attrs \\ %{}) do
def create_changeset(%Event{} = event, attrs \\ %{}) do
event
|> changeset(attrs)
end
......
defmodule PlasmaRepo.Events.EventAuth do
use PlasmaRepo.Schema
alias PlasmaRepo.Events.Event
@type t :: %__MODULE__{
event: Event.t(),
auth_event: Event.t()
}
@primary_key false
schema "event_auths" do
belongs_to :event, Event
belongs_to :auth_event, Event
end
def changeset(event_edge, attrs \\ %{}) do
event_edge
|> cast(attrs, [:event_id, :auth_event_id])
|> validate_required([:event_id, :auth_event_id], message: "required")
|> assoc_constraint(:event)
|> assoc_constraint(:auth_event)
end
def create_changeset(event_edge, attrs \\ %{}) do
event_edge |> changeset(attrs)
end
end
\ No newline at end of file
......@@ -23,6 +23,17 @@ defmodule PlasmaRepo.Events.EventsTest do
assert {:ok, _event} = create_event_res
end
test "create_event/2 succeeds without Matrix event_id" do
room = insert(:room)
event_type = insert(:event_type)
sender = insert(:user)
create_event_res = PlasmaRepo.Events.create_event(%{event_type_id: event_type.id,
room_id: room.id,
sender_id: sender.id})
assert {:ok, _event} = create_event_res
end
test "create_event/2 fails without sender" do
room = insert(:room)
event_type = insert(:event_type)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment