2021-08-21 09:25:36 +00:00
|
|
|
defmodule MatrixServerWeb.Federation.Transaction do
|
|
|
|
alias MatrixServer.Event
|
|
|
|
alias MatrixServerWeb.Federation.Transaction
|
|
|
|
|
|
|
|
# TODO
|
|
|
|
@type edu :: any()
|
|
|
|
|
|
|
|
@type t :: %__MODULE__{
|
2021-08-21 19:39:28 +00:00
|
|
|
origin: String.t(),
|
|
|
|
origin_server_ts: integer(),
|
|
|
|
pdus: [Event.t()],
|
|
|
|
edus: [edu()] | nil
|
|
|
|
}
|
2021-08-21 09:25:36 +00:00
|
|
|
|
|
|
|
defstruct [:origin, :origin_server_ts, :pdus, :edus]
|
|
|
|
|
|
|
|
defimpl Jason.Encoder, for: Transaction do
|
|
|
|
@fields [:origin, :origin_server_ts, :pdus, :edus]
|
|
|
|
|
|
|
|
def encode(transaction, opts) do
|
|
|
|
transaction
|
|
|
|
|> Map.take(@fields)
|
|
|
|
|> Jason.Encode.map(opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec new([Event.t()], [edu()] | nil) :: t()
|
|
|
|
def new(pdu_events, edus \\ nil) do
|
|
|
|
%Transaction{
|
|
|
|
origin: MatrixServer.server_name(),
|
|
|
|
origin_server_ts: System.os_time(:millisecond),
|
|
|
|
pdus: Enum.map(pdu_events, &MatrixServer.to_serializable_map/1),
|
|
|
|
edus: edus
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|