summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre de Lacroix <pierre@pdelacroix.com>2021-01-24 14:09:53 +0100
committerPierre de Lacroix <pierre@pdelacroix.com>2021-01-24 14:09:53 +0100
commit1402a1ea47f23ef9275fb8fe174457b57b2ffde6 (patch)
tree79e95553f9dfd48b3786216d0f7d4cf140c86bea
parentfix some typespecs (diff)
refacto TransactionController
-rw-r--r--lib/matrix_app_service/event.ex73
-rw-r--r--lib/matrix_app_service_web/controllers/v1/transaction_controller.ex87
2 files changed, 61 insertions, 99 deletions
diff --git a/lib/matrix_app_service/event.ex b/lib/matrix_app_service/event.ex
index 23abc38..bf37aba 100644
--- a/lib/matrix_app_service/event.ex
+++ b/lib/matrix_app_service/event.ex
@@ -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
diff --git a/lib/matrix_app_service_web/controllers/v1/transaction_controller.ex b/lib/matrix_app_service_web/controllers/v1/transaction_controller.ex
index 4d45819..d2062bf 100644
--- a/lib/matrix_app_service_web/controllers/v1/transaction_controller.ex
+++ b/lib/matrix_app_service_web/controllers/v1/transaction_controller.ex
@@ -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