Verified Commit 1402a1ea authored by Pierre de Lacroix's avatar Pierre de Lacroix
Browse files

refacto TransactionController

parent f7961b5e
Pipeline #246060766 failed with stages
in 18 minutes and 5 seconds
......@@ -2,28 +2,57 @@ defmodule MatrixAppService.Event do
@moduledoc """
Struct for a Matrix event.
"""
use Ecto.Schema
import Ecto.Changeset
@type t :: %__MODULE__{
age: integer(),
content: map(),
event_id: String.t(),
origin_server_ts: integer(),
room_id: String.t(),
sender: String.t(),
state_key: String.t(),
type: String.t(),
unsigned: map(),
user_id: String.t()
}
# @type t :: %__MODULE__{
# age: integer(),
# content: map(),
# event_id: String.t(),
# origin_server_ts: integer(),
# room_id: String.t(),
# sender: String.t(),
# state_key: String.t(),
# type: String.t(),
# unsigned: map(),
# user_id: String.t()
# }
defstruct age: nil,
content: %{},
event_id: nil,
origin_server_ts: nil,
room_id: nil,
sender: nil,
state_key: "",
type: nil,
unsigned: %{},
user_id: nil
# defstruct age: nil,
# content: %{},
# event_id: nil,
# origin_server_ts: nil,
# room_id: nil,
# sender: nil,
# state_key: "",
# type: nil,
# unsigned: %{},
# user_id: nil
embedded_schema do
field :age, :integer
field :content, :map
field :event_id, :string
field :origin_server_ts, :integer
field :room_id, :string
field :sender, :string
field :state_key, :string
field :type, :string
field :unsigned, :map
field :user_id, :string
end
def new(params) do
command = changeset(params)
case command.valid? do
true -> {:ok, apply_changes(command)}
false -> {:error, command.errors}
end
end
defp changeset(params) do
%__MODULE__{}
|> cast(params, [:age, :content, :event_id, :origin_server_ts, :room_id, :sender, :state_key, :type, :unsigned, :user_id])
|> validate_required([:content, :event_id, :origin_server_ts, :room_id, :sender, :type, :unsigned])
end
end
......@@ -3,82 +3,8 @@ defmodule MatrixAppServiceWeb.V1.TransactionController do
Controller for transactions.
"""
use MatrixAppServiceWeb, :controller
# %{"access_token" => access_token,
# "events" => [
# %{"age" => 199,
# "content" => %{
# "body" => "test",
# "msgtype" => "m.room.message"},
# "event_id" => "$pMDN7TcsCapjLRYoITwryk0anpQGs60y2Eng3lxropc",
# "origin_server_ts" => 1588950428966,
# "room_id" => "!SrVtqmyqzuaMnfVImP:matrix.imago.local",
# "sender" => "@alice:matrix.imago.local",
# "type" => "m.room.message",
# "unsigned" => %{"age" => 199},
# "user_id" => "@alice:matrix.imago.local"}],
# "txn_id" => "269"}
defp create_event(
%{
"age" => age,
"content" => content,
"event_id" => event_id,
"origin_server_ts" => origin_server_ts,
"room_id" => room_id,
"sender" => sender,
"state_key" => state_key,
"type" => type,
"unsigned" => unsigned,
"user_id" => user_id
},
adapter
) do
event = %MatrixAppService.Event{
age: age,
content: content,
event_id: event_id,
origin_server_ts: origin_server_ts,
room_id: room_id,
sender: sender,
state_key: state_key,
type: type,
unsigned: unsigned,
user_id: user_id
}
adapter.new_event(event)
end
defp create_event(
%{
"age" => age,
"content" => content,
"event_id" => event_id,
"origin_server_ts" => origin_server_ts,
"room_id" => room_id,
"sender" => sender,
"type" => type,
"unsigned" => unsigned,
"user_id" => user_id
},
adapter
) do
event = %MatrixAppService.Event{
age: age,
content: content,
event_id: event_id,
origin_server_ts: origin_server_ts,
room_id: room_id,
sender: sender,
state_key: nil,
type: type,
unsigned: unsigned,
user_id: user_id
}
adapter.new_event(event)
end
alias MatrixAppService.Event
require Logger
@doc """
https://matrix.org/docs/spec/application_service/r0.1.2#put-matrix-app-v1-transactions-txnid
......@@ -89,7 +15,14 @@ defmodule MatrixAppServiceWeb.V1.TransactionController do
MatrixAppServiceWeb.Endpoint.config(:transaction_adapter) ||
raise "MatrixAppService: config key room_adapter missing"
Enum.each(events, &create_event(&1, adapter))
# Enum.each(events, &create_event(&1, adapter))
Enum.each(events, fn event_params ->
with {:ok, event} <- Event.new(event_params) do
adapter.new_event(event)
else
error -> Logger.error(inspect(error))
end
end)
send_resp(conn, 200, "{}")
end
......
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