architex/lib/architex_web/federation/transaction.ex

37 lines
892 B
Elixir
Raw Normal View History

2021-09-01 12:43:55 +00:00
defmodule ArchitexWeb.Federation.Transaction do
alias Architex.Event
alias ArchitexWeb.Federation.Transaction
# TODO
@type edu :: any()
@type t :: %__MODULE__{
origin: String.t(),
origin_server_ts: integer(),
pdus: [Event.t()],
edus: [edu()] | nil
}
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{
2021-09-01 12:43:55 +00:00
origin: Architex.server_name(),
origin_server_ts: System.os_time(:millisecond),
2021-09-01 12:43:55 +00:00
pdus: Enum.map(pdu_events, &Architex.to_serializable_map/1),
edus: edus
}
end
end