architex/lib/matrix_server/event.ex

126 lines
2.9 KiB
Elixir
Raw Normal View History

2021-07-03 10:30:57 +00:00
defmodule MatrixServer.Event do
2021-07-10 21:16:00 +00:00
use Ecto.Schema
2021-07-03 10:30:57 +00:00
2021-07-10 21:16:00 +00:00
import Ecto.Changeset
2021-07-03 10:30:57 +00:00
alias MatrixServer.{Room, Event}
2021-07-03 10:30:57 +00:00
2021-07-17 15:38:20 +00:00
@primary_key {:event_id, :string, []}
2021-07-10 21:16:00 +00:00
schema "events" do
field :type, :string
2021-07-17 15:38:20 +00:00
field :origin_server_ts, :integer
2021-07-10 21:16:00 +00:00
field :state_key, :string
field :sender, :string
2021-07-17 15:38:20 +00:00
field :content, :map
2021-07-10 21:16:00 +00:00
field :prev_events, {:array, :string}
field :auth_events, {:array, :string}
2021-07-17 15:38:20 +00:00
belongs_to :room, Room, type: :string
2021-07-03 10:30:57 +00:00
end
2021-07-10 21:16:00 +00:00
def changeset(event, params \\ %{}) do
2021-07-05 14:12:44 +00:00
event
2021-07-10 21:16:00 +00:00
|> cast(params, [:type, :timestamp, :state_key, :sender, :content])
|> validate_required([:type, :timestamp, :sender])
2021-07-03 10:30:57 +00:00
end
2021-07-17 15:38:20 +00:00
def new(room_id, sender) do
%Event{
room_id: room_id,
sender: sender,
event_id: generate_event_id(),
origin_server_ts: DateTime.utc_now() |> DateTime.to_unix(),
prev_events: [],
auth_events: []
}
end
def create_room(room_id, creator, room_version) do
%Event{
new(room_id, creator)
| type: "m.room.create",
state_key: "",
content: %{
2021-07-17 16:54:49 +00:00
"creator" => creator,
"room_version" => room_version || MatrixServer.default_room_version()
2021-07-17 15:38:20 +00:00
}
}
end
def join(room_id, sender) do
%Event{
new(room_id, sender)
| type: "m.room.member",
state_key: sender,
content: %{
"membership" => "join"
2021-07-17 15:38:20 +00:00
}
}
end
2021-07-17 16:54:49 +00:00
def power_levels(room_id, sender) do
%Event{
new(room_id, sender)
| type: "m.room.power_levels",
state_key: "",
content: %{
"ban" => 50,
"events" => %{},
"events_default" => 0,
"invite" => 50,
"kick" => 50,
"redact" => 50,
"state_default" => 50,
"users" => %{
sender => 50
},
"users_default" => 0,
"notifications" => %{
"room" => 50
}
}
}
end
def room_name(room_id, sender, name) do
%Event{
new(room_id, sender)
| type: "m.room.name",
state_key: "",
content: %{
"name" => name
}
}
end
def room_topic(room_id, sender, topic) do
%Event{
new(room_id, sender)
| type: "m.room.topic",
state_key: "",
content: %{
"topic" => topic
}
}
end
2021-07-17 15:38:20 +00:00
def generate_event_id do
"$" <> MatrixServer.random_string(17) <> ":" <> MatrixServer.server_name()
end
2021-07-25 12:57:52 +00:00
def is_control_event(%Event{type: "m.room.power_levels", state_key: ""}), do: true
def is_control_event(%Event{type: "m.room.join_rules", state_key: ""}), do: true
def is_control_event(%Event{
type: "m.room.member",
state_key: state_key,
sender: sender,
content: %{membership: membership}
})
when sender != state_key and membership in ["leave", "ban"],
do: true
def is_control_event(_), do: false
def is_state_event(%Event{state_key: state_key}), do: state_key != nil
2021-07-03 10:30:57 +00:00
end