Implement client send message endpoint

Fix state res and authorization to handle missing state_keys
This commit is contained in:
Pim Kunis 2021-08-31 16:32:04 +02:00
parent 5b3ec55f8b
commit c42fc7c350
8 changed files with 168 additions and 46 deletions

View file

@ -3,7 +3,7 @@ defmodule MatrixServer.Device do
import Ecto.{Changeset, Query}
alias MatrixServer.{Account, Device, Repo}
alias MatrixServer.{Account, Device, Repo, DeviceTransaction}
alias MatrixServerWeb.Client.Request.Login
@type t :: %__MODULE__{
@ -19,6 +19,7 @@ defmodule MatrixServer.Device do
field :display_name, :string
belongs_to :account, Account
has_many :device_transactions, DeviceTransaction
end
def changeset(device, params \\ %{}) do

View file

@ -0,0 +1,18 @@
defmodule MatrixServer.DeviceTransaction do
use Ecto.Schema
alias MatrixServer.Device
@type t :: %__MODULE__{
txn_id: String.t(),
event_id: String.t(),
device_id: integer()
}
@primary_key {:txn_id, :string, []}
schema "device_transactions" do
field :event_id, :string
belongs_to :device, Device
end
end

View file

@ -10,7 +10,7 @@ defmodule MatrixServer.Event do
@type t :: %__MODULE__{
type: String.t(),
origin_server_ts: integer(),
state_key: String.t(),
state_key: String.t() | nil,
sender: UserId.t(),
content: map(),
prev_events: [String.t()] | nil,
@ -73,6 +73,15 @@ defmodule MatrixServer.Event do
}
end
@spec custom_message(Room.t(), Account.t(), String.t(), map()) :: t()
def custom_message(room, sender, type, content) do
%Event{
Event.new(room, sender)
| type: type,
content: content
}
end
@spec is_control_event(t()) :: boolean()
def is_control_event(%Event{type: "m.room.power_levels", state_key: ""}), do: true
def is_control_event(%Event{type: "m.room.join_rules", state_key: ""}), do: true
@ -131,7 +140,7 @@ defmodule MatrixServer.Event do
length(prev_events) == length(prev_event_ids) and
not MatrixServer.has_duplicates?(state_pairs) and
valid_auth_events?(event, auth_events) and
Enum.find_value(state_pairs, &(&1 == {"m.room.create", ""})) and
Enum.find_value(state_pairs, false, &(&1 == {"m.room.create", ""})) and
do_prevalidate(event, auth_events, prev_events)
end