Add more events during room creation

This commit is contained in:
Pim Kunis 2021-07-17 18:54:49 +02:00
parent bbb134679c
commit 8919fe3f14
2 changed files with 107 additions and 39 deletions

View file

@ -42,8 +42,8 @@ defmodule MatrixServer.Event do
| type: "m.room.create", | type: "m.room.create",
state_key: "", state_key: "",
content: %{ content: %{
creator: creator, "creator" => creator,
room_version: room_version || MatrixServer.default_room_version() "room_version" => room_version || MatrixServer.default_room_version()
} }
} }
end end
@ -54,51 +54,117 @@ defmodule MatrixServer.Event do
| type: "m.room.member", | type: "m.room.member",
state_key: sender, state_key: sender,
content: %{ content: %{
membership: "invite" "membership" => "invite"
} }
} }
end end
def room_creation_create_room(%CreateRoom{room_version: room_version}, %Account{ def power_levels(room_id, sender) do
localpart: localpart %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
def room_creation_create_room(repo, %{
input: %CreateRoom{room_version: room_version},
account: %Account{localpart: localpart},
room: %Room{id: room_id}
}) do }) do
fn repo, %{room: %Room{id: room_id}} -> # TODO: state resolution
# TODO: state resolution create_room(room_id, MatrixServer.get_mxid(localpart), room_version)
create_room(room_id, MatrixServer.get_mxid(localpart), room_version) |> repo.insert()
|> repo.insert()
end
end end
def room_creation_join_creator do def room_creation_join_creator(repo, %{
fn repo, room: %Room{id: room_id},
%{ create_room_event: %Event{sender: creator}
create_room_event: %Event{sender: creator, event_id: create_room_event_id}, }) do
room: %Room{id: room_id} # TODO: state resolution
} -> join(room_id, creator)
# TODO: state resolution |> repo.insert()
join(room_id, creator)
|> Map.put(:prev_events, [create_room_event_id])
|> Map.put(:auth_events, [create_room_event_id])
|> repo.insert()
end
end end
def room_creation_power_levels(_input) do def room_creation_power_levels(
fn _repo, %{} -> repo,
{:ok, :ok} %{
end room: %Room{id: room_id},
create_room_event: %Event{sender: creator}
}
) do
# TODO: state resolution
power_levels(room_id, creator)
|> repo.insert()
end end
def room_creation_name(_input) do def room_creation_name(_repo, %{input: %CreateRoom{name: nil}}), do: {:ok, :noop}
fn _repo, %{} ->
{:ok, :ok} def room_creation_name(
end repo,
%{
input: %CreateRoom{name: name},
room: %Room{id: room_id},
create_room_event: %Event{sender: creator}
}
) do
# TODO: state resolution
# TODO: check name length
room_name(room_id, creator, name)
|> repo.insert()
end end
def room_creation_topic(_input) do def room_creation_topic(_repo, %{input: %CreateRoom{topic: nil}}), do: {:ok, :noop}
fn _repo, %{} ->
{:ok, :ok} def room_creation_topic(
end repo,
%{
input: %CreateRoom{topic: topic},
room: %Room{id: room_id},
create_room_event: %Event{sender: creator}
}
) do
# TODO: state resolution
room_topic(room_id, creator, topic)
|> repo.insert()
end end
def generate_event_id do def generate_event_id do

View file

@ -23,12 +23,14 @@ defmodule MatrixServer.RoomServer do
# TODO: preset events, initial_state events, invite, invite_3pid # TODO: preset events, initial_state events, invite, invite_3pid
result = result =
Multi.new() Multi.new()
|> Multi.put(:input, input)
|> Multi.put(:account, account)
|> Multi.insert(:room, Room.create_changeset(input)) |> Multi.insert(:room, Room.create_changeset(input))
|> Multi.run(:create_room_event, Event.room_creation_create_room(input, account)) |> Multi.run(:create_room_event, &Event.room_creation_create_room/2)
|> Multi.run(:join_creator_event, Event.room_creation_join_creator()) |> Multi.run(:join_creator_event, &Event.room_creation_join_creator/2)
|> Multi.run(:power_levels_event, Event.room_creation_power_levels(input)) |> Multi.run(:power_levels_event, &Event.room_creation_power_levels/2)
|> Multi.run(:name_event, Event.room_creation_name(input)) |> Multi.run(:name_event, &Event.room_creation_name/2)
|> Multi.run(:topic_event, Event.room_creation_topic(input)) |> Multi.run(:topic_event, &Event.room_creation_topic/2)
|> Repo.transaction() |> Repo.transaction()
{:reply, result, state} {:reply, result, state}