Add missing fields to events
Fix Dialyzer issues
This commit is contained in:
parent
0871c3cdd9
commit
27c673e07a
13 changed files with 147 additions and 124 deletions
|
@ -154,16 +154,6 @@ defmodule Architex do
|
||||||
|> Enum.into(%{})
|
|> Enum.into(%{})
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
|
||||||
Serialize and encode the given struct.
|
|
||||||
"""
|
|
||||||
@spec serialize_and_encode(struct()) :: {:ok, String.t()} | {:error, Jason.EncodeError.t()}
|
|
||||||
def serialize_and_encode(struct) do
|
|
||||||
struct
|
|
||||||
|> to_serializable_map()
|
|
||||||
|> encode_canonical_json()
|
|
||||||
end
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Add a signature to the given map under the `:signatures` key.
|
Add a signature to the given map under the `:signatures` key.
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
# https://github.com/michalmuskala/jason/issues/69
|
# https://github.com/michalmuskala/jason/issues/69
|
||||||
defmodule Architex.EncodableMap do
|
defmodule Architex.EncodableMap do
|
||||||
alias Architex.EncodableMap
|
alias Architex.EncodableMap
|
||||||
alias Architex.Types.{UserId, RoomId, EventId, GroupId, AliasId}
|
|
||||||
|
|
||||||
defstruct pairs: []
|
defstruct pairs: []
|
||||||
|
|
||||||
|
@ -15,12 +14,6 @@ defmodule Architex.EncodableMap do
|
||||||
pairs =
|
pairs =
|
||||||
map
|
map
|
||||||
|> Enum.map(fn
|
|> Enum.map(fn
|
||||||
{k, v}
|
|
||||||
when is_struct(v, UserId) or is_struct(v, RoomId) or is_struct(v, EventId) or
|
|
||||||
is_struct(v, GroupId) or is_struct(v, AliasId) ->
|
|
||||||
# Simply convert IDs to a string.
|
|
||||||
{k, to_string(v)}
|
|
||||||
|
|
||||||
{k, v} when is_map(v) ->
|
{k, v} when is_map(v) ->
|
||||||
{k, from_map(v)}
|
{k, from_map(v)}
|
||||||
|
|
||||||
|
|
|
@ -398,7 +398,7 @@ defmodule Architex.RoomServer do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec insert_single_event(Room.t(), t(), Event.t()) ::
|
@spec insert_single_event(Room.t(), t(), %Event{}) ::
|
||||||
(() -> {t(), Room.t(), Event.t()} | {:error, atom()})
|
(() -> {t(), Room.t(), Event.t()} | {:error, atom()})
|
||||||
defp insert_single_event(room, state_set, event) do
|
defp insert_single_event(room, state_set, event) do
|
||||||
fn ->
|
fn ->
|
||||||
|
@ -472,7 +472,7 @@ defmodule Architex.RoomServer do
|
||||||
# Get the events for room creation as dictated by the given preset.
|
# Get the events for room creation as dictated by the given preset.
|
||||||
# TODO: trusted_private_chat:
|
# TODO: trusted_private_chat:
|
||||||
# All invitees are given the same power level as the room creator.
|
# All invitees are given the same power level as the room creator.
|
||||||
@spec room_creation_preset(Account.t(), String.t() | nil, Room.t()) :: [Event.t()]
|
@spec room_creation_preset(Account.t(), String.t() | nil, Room.t()) :: [%Event{}]
|
||||||
defp room_creation_preset(account, nil, %Room{visibility: visibility} = room) do
|
defp room_creation_preset(account, nil, %Room{visibility: visibility} = room) do
|
||||||
preset =
|
preset =
|
||||||
case visibility do
|
case visibility do
|
||||||
|
@ -505,7 +505,7 @@ defmodule Architex.RoomServer do
|
||||||
# - Content hash
|
# - Content hash
|
||||||
# - Event ID
|
# - Event ID
|
||||||
# - Signature
|
# - Signature
|
||||||
@spec finalize_and_insert_event(Event.t(), t(), Room.t()) ::
|
@spec finalize_and_insert_event(%Event{}, t(), Room.t()) ::
|
||||||
{:ok, t(), Room.t(), Event.t()} | {:error, atom()}
|
{:ok, t(), Room.t(), Event.t()} | {:error, atom()}
|
||||||
defp finalize_and_insert_event(
|
defp finalize_and_insert_event(
|
||||||
event,
|
event,
|
||||||
|
@ -516,6 +516,7 @@ defmodule Architex.RoomServer do
|
||||||
event
|
event
|
||||||
|> Map.put(:auth_events, auth_events_for_event(event, state_set))
|
|> Map.put(:auth_events, auth_events_for_event(event, state_set))
|
||||||
|> Map.put(:prev_events, forward_extremities)
|
|> Map.put(:prev_events, forward_extremities)
|
||||||
|
|> Map.put(:depth, get_depth(forward_extremities))
|
||||||
|
|
||||||
case Event.post_process(event) do
|
case Event.post_process(event) do
|
||||||
{:ok, event} -> authenticate_and_insert_event(event, state_set, room)
|
{:ok, event} -> authenticate_and_insert_event(event, state_set, room)
|
||||||
|
@ -523,8 +524,17 @@ defmodule Architex.RoomServer do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@spec get_depth([String.t()]) :: integer()
|
||||||
|
defp get_depth(prev_event_ids) do
|
||||||
|
Event
|
||||||
|
|> where([e], e.id in ^prev_event_ids)
|
||||||
|
|> select([e], e.depth)
|
||||||
|
|> Repo.all()
|
||||||
|
|> Enum.max(fn -> 0 end)
|
||||||
|
end
|
||||||
|
|
||||||
# Get the auth events for an events.
|
# Get the auth events for an events.
|
||||||
@spec auth_events_for_event(Event.t(), t()) :: [{String.t(), String.t()}]
|
@spec auth_events_for_event(%Event{}, t()) :: [Event.t()]
|
||||||
defp auth_events_for_event(%Event{type: "m.room.create"}, _), do: []
|
defp auth_events_for_event(%Event{type: "m.room.create"}, _), do: []
|
||||||
|
|
||||||
defp auth_events_for_event(
|
defp auth_events_for_event(
|
||||||
|
|
|
@ -6,76 +6,58 @@ defmodule Architex.Event do
|
||||||
alias Architex.{Repo, Room, Event, Account, EncodableMap, KeyServer}
|
alias Architex.{Repo, Room, Event, Account, EncodableMap, KeyServer}
|
||||||
alias Architex.Types.UserId
|
alias Architex.Types.UserId
|
||||||
|
|
||||||
# TODO: It seems unsigned is always set, even though it is not specified?
|
# TODO: It seems unsigned is always set in DB, even though it is not specified?
|
||||||
@type t :: %__MODULE__{
|
@type t :: %__MODULE__{
|
||||||
type: String.t(),
|
nid: integer(),
|
||||||
origin_server_ts: integer(),
|
id: String.t(),
|
||||||
state_key: String.t() | nil,
|
|
||||||
sender: UserId.t(),
|
|
||||||
content: map(),
|
|
||||||
prev_events: [String.t()] | nil,
|
|
||||||
auth_events: [String.t()],
|
auth_events: [String.t()],
|
||||||
unsigned: map() | nil,
|
content: map(),
|
||||||
signatures: map() | nil,
|
depth: integer(),
|
||||||
hashes: map() | nil
|
hashes: map(),
|
||||||
|
origin: String.t(),
|
||||||
|
origin_server_ts: integer(),
|
||||||
|
prev_events: [String.t()],
|
||||||
|
redacts: String.t() | nil,
|
||||||
|
room_id: String.t(),
|
||||||
|
sender: UserId.t(),
|
||||||
|
signatures: map(),
|
||||||
|
state_key: String.t() | nil,
|
||||||
|
type: String.t(),
|
||||||
|
unsigned: map() | nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@primary_key {:nid, :id, autogenerate: true}
|
@primary_key false
|
||||||
schema "events" do
|
schema "events" do
|
||||||
field :type, :string
|
field :nid, :id, primary_key: true, autogenerate: true
|
||||||
field :origin_server_ts, :integer
|
|
||||||
field :state_key, :string
|
|
||||||
field :sender, UserId
|
|
||||||
field :content, :map
|
|
||||||
field :prev_events, {:array, :string}
|
|
||||||
field :auth_events, {:array, :string}
|
|
||||||
field :unsigned, :map
|
|
||||||
field :signatures, {:map, {:map, :string}}
|
|
||||||
field :hashes, {:map, :string}
|
|
||||||
field :id, :string
|
field :id, :string
|
||||||
|
|
||||||
|
# PDU fields
|
||||||
|
field :auth_events, {:array, :string}
|
||||||
|
field :content, :map
|
||||||
|
field :depth, :integer
|
||||||
|
field :hashes, {:map, :string}
|
||||||
|
field :origin, :string
|
||||||
|
field :origin_server_ts, :integer
|
||||||
|
field :prev_events, {:array, :string}
|
||||||
|
field :redacts, :string
|
||||||
belongs_to :room, Room, type: :string
|
belongs_to :room, Room, type: :string
|
||||||
|
field :sender, UserId
|
||||||
|
field :signatures, {:map, {:map, :string}}
|
||||||
|
field :state_key, :string
|
||||||
|
field :type, :string
|
||||||
|
field :unsigned, :map
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: Move this to a dedicated function in Event.Formatters.
|
|
||||||
defimpl Jason.Encoder, for: Event do
|
|
||||||
@pdu_keys [
|
|
||||||
:auth_events,
|
|
||||||
:content,
|
|
||||||
:depth,
|
|
||||||
:hashes,
|
|
||||||
:origin,
|
|
||||||
:origin_server_ts,
|
|
||||||
:prev_events,
|
|
||||||
:redacts,
|
|
||||||
:room_id,
|
|
||||||
:sender,
|
|
||||||
:signatures,
|
|
||||||
:state_key,
|
|
||||||
:type,
|
|
||||||
:unsigned
|
|
||||||
]
|
|
||||||
|
|
||||||
def encode(event, opts) do
|
|
||||||
event
|
|
||||||
|> Map.take(@pdu_keys)
|
|
||||||
|> Map.update!(:sender, &Kernel.to_string/1)
|
|
||||||
|> Jason.Encode.map(opts)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec new(Room.t(), Account.t()) :: %Event{}
|
|
||||||
def new(%Room{id: room_id}, %Account{localpart: localpart}) do
|
def new(%Room{id: room_id}, %Account{localpart: localpart}) do
|
||||||
%Event{
|
%Event{
|
||||||
room_id: room_id,
|
room_id: room_id,
|
||||||
sender: %UserId{localpart: localpart, domain: Architex.server_name()},
|
sender: %UserId{localpart: localpart, domain: Architex.server_name()},
|
||||||
origin_server_ts: DateTime.utc_now() |> DateTime.to_unix(:millisecond),
|
origin_server_ts: DateTime.utc_now() |> DateTime.to_unix(:millisecond),
|
||||||
prev_events: [],
|
origin: Architex.server_name()
|
||||||
auth_events: []
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec custom_message(Room.t(), Account.t(), String.t(), map()) :: t()
|
@spec custom_message(Room.t(), Account.t(), String.t(), map()) :: %Event{}
|
||||||
def custom_message(room, sender, type, content) do
|
def custom_message(room, sender, type, content) do
|
||||||
%Event{
|
%Event{
|
||||||
Event.new(room, sender)
|
Event.new(room, sender)
|
||||||
|
@ -224,7 +206,7 @@ defmodule Architex.Event do
|
||||||
defp calculate_content_hash(event) do
|
defp calculate_content_hash(event) do
|
||||||
m =
|
m =
|
||||||
event
|
event
|
||||||
|> Architex.to_serializable_map()
|
|> Architex.Event.Formatters.as_pdu()
|
||||||
|> Map.drop([:unsigned, :signature, :hashes])
|
|> Map.drop([:unsigned, :signature, :hashes])
|
||||||
|> EncodableMap.from_map()
|
|> EncodableMap.from_map()
|
||||||
|
|
||||||
|
@ -233,11 +215,10 @@ defmodule Architex.Event do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec redact(t()) :: map()
|
|
||||||
defp redact(%Event{type: type, content: content} = event) do
|
defp redact(%Event{type: type, content: content} = event) do
|
||||||
redacted_event =
|
redacted_event =
|
||||||
event
|
event
|
||||||
|> Architex.to_serializable_map()
|
|> Architex.Event.Formatters.as_pdu()
|
||||||
|> Map.take([
|
|> Map.take([
|
||||||
:id,
|
:id,
|
||||||
:type,
|
:type,
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
defmodule Architex.Event.Formatters do
|
defmodule Architex.Event.Formatters do
|
||||||
|
@moduledoc """
|
||||||
|
Functions to format events in order to convert them to JSON.
|
||||||
|
"""
|
||||||
alias Architex.Event
|
alias Architex.Event
|
||||||
|
|
||||||
|
@spec for_client(Event.t()) :: map()
|
||||||
def for_client(%Event{
|
def for_client(%Event{
|
||||||
content: content,
|
content: content,
|
||||||
type: type,
|
type: type,
|
||||||
|
@ -23,4 +27,42 @@ defmodule Architex.Event.Formatters do
|
||||||
|
|
||||||
data
|
data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@spec as_pdu(Event.t()) :: map()
|
||||||
|
def as_pdu(%Event{
|
||||||
|
auth_events: auth_events,
|
||||||
|
content: content,
|
||||||
|
depth: depth,
|
||||||
|
hashes: hashes,
|
||||||
|
origin: origin,
|
||||||
|
origin_server_ts: origin_server_ts,
|
||||||
|
prev_events: prev_events,
|
||||||
|
redacts: redacts,
|
||||||
|
room_id: room_id,
|
||||||
|
sender: sender,
|
||||||
|
signatures: signatures,
|
||||||
|
state_key: state_key,
|
||||||
|
type: type,
|
||||||
|
unsigned: unsigned
|
||||||
|
}) do
|
||||||
|
data = %{
|
||||||
|
auth_events: auth_events,
|
||||||
|
content: content,
|
||||||
|
depth: depth,
|
||||||
|
hashes: hashes,
|
||||||
|
origin: origin,
|
||||||
|
origin_server_ts: origin_server_ts,
|
||||||
|
prev_events: prev_events,
|
||||||
|
room_id: room_id,
|
||||||
|
sender: to_string(sender),
|
||||||
|
signatures: signatures,
|
||||||
|
type: type
|
||||||
|
}
|
||||||
|
|
||||||
|
data = if redacts, do: Map.put(data, :redacts, redacts), else: data
|
||||||
|
data = if state_key, do: Map.put(data, :state_key, state_key), else: data
|
||||||
|
data = if unsigned, do: Map.put(data, :unsigned, unsigned), else: data
|
||||||
|
|
||||||
|
data
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
defmodule Architex.Event.Join do
|
defmodule Architex.Event.Join do
|
||||||
alias Architex.{Event, Account, Room}
|
alias Architex.{Event, Account, Room}
|
||||||
|
|
||||||
@spec new(Room.t(), Account.t()) :: Event.t()
|
@spec new(Room.t(), Account.t()) :: %Event{}
|
||||||
def new(room, %Account{localpart: localpart} = sender) do
|
def new(room, %Account{localpart: localpart} = sender) do
|
||||||
mxid = Architex.get_mxid(localpart)
|
mxid = Architex.get_mxid(localpart)
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ end
|
||||||
defmodule Architex.Event.CreateRoom do
|
defmodule Architex.Event.CreateRoom do
|
||||||
alias Architex.{Event, Account, Room}
|
alias Architex.{Event, Account, Room}
|
||||||
|
|
||||||
@spec new(Room.t(), Account.t(), String.t()) :: Event.t()
|
@spec new(Room.t(), Account.t(), String.t()) :: %Event{}
|
||||||
def new(room, %Account{localpart: localpart} = creator, room_version) do
|
def new(room, %Account{localpart: localpart} = creator, room_version) do
|
||||||
mxid = Architex.get_mxid(localpart)
|
mxid = Architex.get_mxid(localpart)
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ end
|
||||||
defmodule Architex.Event.PowerLevels do
|
defmodule Architex.Event.PowerLevels do
|
||||||
alias Architex.{Event, Account, Room}
|
alias Architex.{Event, Account, Room}
|
||||||
|
|
||||||
@spec new(Room.t(), Account.t()) :: Event.t()
|
@spec new(Room.t(), Account.t()) :: %Event{}
|
||||||
def new(room, %Account{localpart: localpart} = sender) do
|
def new(room, %Account{localpart: localpart} = sender) do
|
||||||
mxid = Architex.get_mxid(localpart)
|
mxid = Architex.get_mxid(localpart)
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ end
|
||||||
defmodule Architex.Event.Name do
|
defmodule Architex.Event.Name do
|
||||||
alias Architex.{Event, Account, Room}
|
alias Architex.{Event, Account, Room}
|
||||||
|
|
||||||
@spec new(Room.t(), Account.t(), String.t()) :: Event.t()
|
@spec new(Room.t(), Account.t(), String.t()) :: %Event{}
|
||||||
def new(room, sender, name) do
|
def new(room, sender, name) do
|
||||||
%Event{
|
%Event{
|
||||||
Event.new(room, sender)
|
Event.new(room, sender)
|
||||||
|
@ -85,7 +85,7 @@ end
|
||||||
defmodule Architex.Event.Topic do
|
defmodule Architex.Event.Topic do
|
||||||
alias Architex.{Event, Account, Room}
|
alias Architex.{Event, Account, Room}
|
||||||
|
|
||||||
@spec new(Room.t(), Account.t(), String.t()) :: Event.t()
|
@spec new(Room.t(), Account.t(), String.t()) :: %Event{}
|
||||||
def new(room, sender, topic) do
|
def new(room, sender, topic) do
|
||||||
%Event{
|
%Event{
|
||||||
Event.new(room, sender)
|
Event.new(room, sender)
|
||||||
|
@ -101,7 +101,7 @@ end
|
||||||
defmodule Architex.Event.JoinRules do
|
defmodule Architex.Event.JoinRules do
|
||||||
alias Architex.{Event, Account, Room}
|
alias Architex.{Event, Account, Room}
|
||||||
|
|
||||||
@spec new(Room.t(), Account.t(), String.t()) :: Event.t()
|
@spec new(Room.t(), Account.t(), String.t()) :: %Event{}
|
||||||
def new(room, sender, join_rule) do
|
def new(room, sender, join_rule) do
|
||||||
%Event{
|
%Event{
|
||||||
Event.new(room, sender)
|
Event.new(room, sender)
|
||||||
|
@ -117,7 +117,7 @@ end
|
||||||
defmodule Architex.Event.HistoryVisibility do
|
defmodule Architex.Event.HistoryVisibility do
|
||||||
alias Architex.{Event, Account, Room}
|
alias Architex.{Event, Account, Room}
|
||||||
|
|
||||||
@spec new(Room.t(), Account.t(), String.t()) :: Event.t()
|
@spec new(Room.t(), Account.t(), String.t()) :: %Event{}
|
||||||
def new(room, sender, history_visibility) do
|
def new(room, sender, history_visibility) do
|
||||||
%Event{
|
%Event{
|
||||||
Event.new(room, sender)
|
Event.new(room, sender)
|
||||||
|
@ -133,7 +133,7 @@ end
|
||||||
defmodule Architex.Event.GuestAccess do
|
defmodule Architex.Event.GuestAccess do
|
||||||
alias Architex.{Event, Account, Room}
|
alias Architex.{Event, Account, Room}
|
||||||
|
|
||||||
@spec new(Room.t(), Account.t(), String.t()) :: Event.t()
|
@spec new(Room.t(), Account.t(), String.t()) :: %Event{}
|
||||||
def new(room, sender, guest_access) do
|
def new(room, sender, guest_access) do
|
||||||
%Event{
|
%Event{
|
||||||
Event.new(room, sender)
|
Event.new(room, sender)
|
||||||
|
@ -149,7 +149,7 @@ end
|
||||||
defmodule Architex.Event.Invite do
|
defmodule Architex.Event.Invite do
|
||||||
alias Architex.{Event, Account, Room}
|
alias Architex.{Event, Account, Room}
|
||||||
|
|
||||||
@spec new(Room.t(), Account.t(), String.t()) :: Event.t()
|
@spec new(Room.t(), Account.t(), String.t()) :: %Event{}
|
||||||
def new(room, sender, user_id) do
|
def new(room, sender, user_id) do
|
||||||
%Event{
|
%Event{
|
||||||
Event.new(room, sender)
|
Event.new(room, sender)
|
||||||
|
@ -165,7 +165,7 @@ end
|
||||||
defmodule Architex.Event.Leave do
|
defmodule Architex.Event.Leave do
|
||||||
alias Architex.{Event, Account, Room}
|
alias Architex.{Event, Account, Room}
|
||||||
|
|
||||||
@spec new(Room.t(), Account.t()) :: Event.t()
|
@spec new(Room.t(), Account.t()) :: %Event{}
|
||||||
def new(room, sender) do
|
def new(room, sender) do
|
||||||
%Event{
|
%Event{
|
||||||
Event.new(room, sender)
|
Event.new(room, sender)
|
||||||
|
@ -181,7 +181,7 @@ end
|
||||||
defmodule Architex.Event.Kick do
|
defmodule Architex.Event.Kick do
|
||||||
alias Architex.{Event, Account, Room}
|
alias Architex.{Event, Account, Room}
|
||||||
|
|
||||||
@spec new(Room.t(), Account.t(), String.t(), String.t() | nil) :: Event.t()
|
@spec new(Room.t(), Account.t(), String.t(), String.t() | nil) :: %Event{}
|
||||||
def new(room, sender, user_id, reason \\ nil) do
|
def new(room, sender, user_id, reason \\ nil) do
|
||||||
content = %{"membership" => "leave"}
|
content = %{"membership" => "leave"}
|
||||||
content = if reason, do: Map.put(content, "reason", reason), else: content
|
content = if reason, do: Map.put(content, "reason", reason), else: content
|
||||||
|
@ -198,7 +198,7 @@ end
|
||||||
defmodule Architex.Event.Ban do
|
defmodule Architex.Event.Ban do
|
||||||
alias Architex.{Event, Account, Room}
|
alias Architex.{Event, Account, Room}
|
||||||
|
|
||||||
@spec new(Room.t(), Account.t(), String.t(), String.t() | nil) :: Event.t()
|
@spec new(Room.t(), Account.t(), String.t(), String.t() | nil) :: %Event{}
|
||||||
def new(room, sender, user_id, reason \\ nil) do
|
def new(room, sender, user_id, reason \\ nil) do
|
||||||
content = %{"membership" => "ban"}
|
content = %{"membership" => "ban"}
|
||||||
content = if reason, do: Map.put(content, "reason", reason), else: content
|
content = if reason, do: Map.put(content, "reason", reason), else: content
|
||||||
|
@ -214,7 +214,7 @@ end
|
||||||
|
|
||||||
defmodule Architex.Event.Unban do
|
defmodule Architex.Event.Unban do
|
||||||
alias Architex.{Event, Account, Room}
|
alias Architex.{Event, Account, Room}
|
||||||
@spec new(Room.t(), Account.t(), String.t()) :: Event.t()
|
@spec new(Room.t(), Account.t(), String.t()) :: %Event{}
|
||||||
def new(room, sender, user_id) do
|
def new(room, sender, user_id) do
|
||||||
%Event{
|
%Event{
|
||||||
Event.new(room, sender)
|
Event.new(room, sender)
|
||||||
|
|
|
@ -4,7 +4,7 @@ defmodule Architex.Room do
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
|
||||||
alias Architex.{Repo, Room, Event, Alias, RoomServer}
|
alias Architex.{Repo, Room, Event, Alias, RoomServer, Account}
|
||||||
alias ArchitexWeb.Client.Request.{CreateRoom, Messages}
|
alias ArchitexWeb.Client.Request.{CreateRoom, Messages}
|
||||||
|
|
||||||
@type t :: %__MODULE__{
|
@type t :: %__MODULE__{
|
||||||
|
@ -22,7 +22,7 @@ defmodule Architex.Room do
|
||||||
has_many :aliases, Alias, foreign_key: :room_id
|
has_many :aliases, Alias, foreign_key: :room_id
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec changeset(Room.t(), map()) :: Ecto.Changeset.t()
|
@spec changeset(%Room{}, map()) :: Ecto.Changeset.t()
|
||||||
def changeset(room, params \\ %{}) do
|
def changeset(room, params \\ %{}) do
|
||||||
cast(room, params, [:visibility])
|
cast(room, params, [:visibility])
|
||||||
end
|
end
|
||||||
|
|
|
@ -229,11 +229,7 @@ defmodule ArchitexWeb.Client.RoomController do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# GET /_matrix/client/r0/rooms/!atYDsyowueiToUvuqY:localhost:4000/messages
|
|
||||||
# Parameters: %{"dir" => "b", "from" => "", "limit" => "727", "path" => ["_matrix", "client", "r0", "rooms", "!atYDsyowueiToUvuqY:localhost:4000", "messages"]}
|
|
||||||
def messages(%Conn{assigns: %{account: account}} = conn, %{"room_id" => room_id} = params) do
|
def messages(%Conn{assigns: %{account: account}} = conn, %{"room_id" => room_id} = params) do
|
||||||
# IO.inspect(Messages.changeset(%Messages{}, params))
|
|
||||||
|
|
||||||
with {:ok, request} <- Messages.parse(params) do
|
with {:ok, request} <- Messages.parse(params) do
|
||||||
room_query =
|
room_query =
|
||||||
account
|
account
|
||||||
|
|
|
@ -6,13 +6,13 @@ defmodule ArchitexWeb.Client.Request.CreateRoom do
|
||||||
alias Ecto.Changeset
|
alias Ecto.Changeset
|
||||||
|
|
||||||
@type t :: %__MODULE__{
|
@type t :: %__MODULE__{
|
||||||
visibility: String.t(),
|
visibility: String.t() | nil,
|
||||||
room_alias_name: String.t(),
|
room_alias_name: String.t() | nil,
|
||||||
name: String.t(),
|
name: String.t() | nil,
|
||||||
topic: String.t(),
|
topic: String.t() | nil,
|
||||||
invite: list(String.t()),
|
invite: list(String.t()) | nil,
|
||||||
room_version: String.t(),
|
room_version: String.t() | nil,
|
||||||
preset: String.t()
|
preset: String.t() | nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@primary_key false
|
@primary_key false
|
||||||
|
|
|
@ -90,16 +90,23 @@ defmodule ArchitexWeb.Federation.EventController do
|
||||||
case RoomServer.get_room_server(room) do
|
case RoomServer.get_room_server(room) do
|
||||||
{:ok, pid} ->
|
{:ok, pid} ->
|
||||||
if RoomServer.server_in_room?(pid, origin) do
|
if RoomServer.server_in_room?(pid, origin) do
|
||||||
{state_events, auth_chain} =
|
# {state_events, auth_chain} =
|
||||||
|
data =
|
||||||
case state_or_state_ids do
|
case state_or_state_ids do
|
||||||
:state -> RoomServer.get_state_at_event(pid, event)
|
:state ->
|
||||||
:state_ids -> RoomServer.get_state_ids_at_event(pid, event)
|
{state_events, auth_chain} = RoomServer.get_state_at_event(pid, event)
|
||||||
end
|
|
||||||
|
|
||||||
data = %{
|
%{
|
||||||
auth_chain: auth_chain,
|
auth_chain: Enum.map(auth_chain, &Architex.Event.Formatters.as_pdu/1),
|
||||||
pdus: state_events
|
pdus: Enum.map(state_events, &Architex.Event.Formatters.as_pdu/1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
:state_ids ->
|
||||||
|
{state_event_ids, auth_chain_ids} =
|
||||||
|
RoomServer.get_state_ids_at_event(pid, event)
|
||||||
|
|
||||||
|
%{auth_chain: auth_chain_ids, pdus: state_event_ids}
|
||||||
|
end
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_status(200)
|
|> put_status(200)
|
||||||
|
|
|
@ -8,7 +8,7 @@ defmodule ArchitexWeb.Federation.Transaction do
|
||||||
@type t :: %__MODULE__{
|
@type t :: %__MODULE__{
|
||||||
origin: String.t(),
|
origin: String.t(),
|
||||||
origin_server_ts: integer(),
|
origin_server_ts: integer(),
|
||||||
pdus: [Event.t()],
|
pdus: [map()],
|
||||||
edus: [edu()] | nil
|
edus: [edu()] | nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ defmodule ArchitexWeb.Federation.Transaction do
|
||||||
%Transaction{
|
%Transaction{
|
||||||
origin: Architex.server_name(),
|
origin: Architex.server_name(),
|
||||||
origin_server_ts: System.os_time(:millisecond),
|
origin_server_ts: System.os_time(:millisecond),
|
||||||
pdus: Enum.map(pdu_events, &Architex.to_serializable_map/1),
|
pdus: Enum.map(pdu_events, &Architex.Event.Formatters.as_pdu/1),
|
||||||
edus: edus
|
edus: edus
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,19 +27,23 @@ defmodule Architex.Repo.Migrations.CreateInitialTables do
|
||||||
|
|
||||||
create table(:events, primary_key: false) do
|
create table(:events, primary_key: false) do
|
||||||
add :nid, :serial, primary_key: true
|
add :nid, :serial, primary_key: true
|
||||||
|
|
||||||
add :origin_server_ts, :bigint, null: false
|
|
||||||
add :unsigned, :map, default: %{}, null: true
|
|
||||||
add :hashes, :map, null: false
|
|
||||||
add :signatures, :map, null: false
|
|
||||||
add :id, :string, null: false
|
add :id, :string, null: false
|
||||||
add :content, :map
|
|
||||||
add :type, :string, null: false
|
# PDU Fields
|
||||||
add :state_key, :string
|
|
||||||
add :sender, :string, null: false
|
|
||||||
add :prev_events, {:array, :string}, null: false
|
|
||||||
add :auth_events, {:array, :string}, null: false
|
add :auth_events, {:array, :string}, null: false
|
||||||
|
add :content, :map, null: false
|
||||||
|
add :depth, :integer, null: false
|
||||||
|
add :hashes, :map, null: false
|
||||||
|
add :origin, :string, null: false
|
||||||
|
add :origin_server_ts, :bigint, null: false
|
||||||
|
add :prev_events, {:array, :string}, null: false
|
||||||
|
add :redacts, :string, null: true
|
||||||
add :room_id, references(:rooms, type: :string), null: false
|
add :room_id, references(:rooms, type: :string), null: false
|
||||||
|
add :sender, :string, null: false
|
||||||
|
add :signatures, :map, null: false
|
||||||
|
add :state_key, :string, null: true
|
||||||
|
add :type, :string, null: false
|
||||||
|
add :unsigned, :map, default: %{}, null: true
|
||||||
end
|
end
|
||||||
|
|
||||||
create index(:events, [:id], unique: true)
|
create index(:events, [:id], unique: true)
|
||||||
|
|
|
@ -40,7 +40,7 @@ defmodule ArchitexWeb.LoginControllerTest do
|
||||||
test "handles unknown matrix user id", %{conn: conn} do
|
test "handles unknown matrix user id", %{conn: conn} do
|
||||||
conn = post_json(conn, Routes.login_path(Endpoint, :login), @basic_params)
|
conn = post_json(conn, Routes.login_path(Endpoint, :login), @basic_params)
|
||||||
|
|
||||||
assert %{"errcode" => "M_FORBIDDEN"} = json_response(conn, 400)
|
assert %{"errcode" => "M_FORBIDDEN"} = json_response(conn, 403)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "handles wrong password", %{conn: conn} do
|
test "handles wrong password", %{conn: conn} do
|
||||||
|
@ -48,7 +48,7 @@ defmodule ArchitexWeb.LoginControllerTest do
|
||||||
|
|
||||||
conn = post_json(conn, Routes.login_path(Endpoint, :login), @basic_params)
|
conn = post_json(conn, Routes.login_path(Endpoint, :login), @basic_params)
|
||||||
|
|
||||||
assert %{"errcode" => "M_FORBIDDEN"} = json_response(conn, 400)
|
assert %{"errcode" => "M_FORBIDDEN"} = json_response(conn, 403)
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: Test display name
|
# TODO: Test display name
|
||||||
|
|
Loading…
Reference in a new issue