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