Move event generators to seperate modules

This commit is contained in:
Pim Kunis 2021-08-30 14:12:28 +02:00
parent e2cb61e2d8
commit 072ef27480
3 changed files with 242 additions and 192 deletions

View file

@ -246,7 +246,7 @@ defmodule MatrixServer.RoomServer do
end end
def handle_call({:invite, account, user_id}, _from, %{room: room, state_set: state_set} = state) do def handle_call({:invite, account, user_id}, _from, %{room: room, state_set: state_set} = state) do
invite_event = Event.invite(room, account, user_id) invite_event = Event.Invite.new(room, account, user_id)
case insert_single_event(room, state_set, invite_event) do case insert_single_event(room, state_set, invite_event) do
{:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}} {:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}}
@ -259,7 +259,7 @@ defmodule MatrixServer.RoomServer do
_from, _from,
%{room: %Room{id: room_id} = room, state_set: state_set} = state %{room: %Room{id: room_id} = room, state_set: state_set} = state
) do ) do
join_event = Event.join(room, account) join_event = Event.Join.new(room, account)
case insert_single_event(room, state_set, join_event) do case insert_single_event(room, state_set, join_event) do
{:ok, {state_set, room}} -> {:ok, {state_set, room}} ->
@ -271,7 +271,7 @@ defmodule MatrixServer.RoomServer do
end end
def handle_call({:leave, account}, _from, %{room: room, state_set: state_set} = state) do def handle_call({:leave, account}, _from, %{room: room, state_set: state_set} = state) do
leave_event = Event.leave(room, account) leave_event = Event.Leave.new(room, account)
case insert_single_event(room, state_set, leave_event) do case insert_single_event(room, state_set, leave_event) do
{:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}} {:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}}
@ -284,7 +284,7 @@ defmodule MatrixServer.RoomServer do
_from, _from,
%{room: room, state_set: state_set} = state %{room: room, state_set: state_set} = state
) do ) do
kick_event = Event.kick(room, account, user_id, reason) kick_event = Event.Kick.new(room, account, user_id, reason)
case insert_single_event(room, state_set, kick_event) do case insert_single_event(room, state_set, kick_event) do
{:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}} {:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}}
@ -297,7 +297,7 @@ defmodule MatrixServer.RoomServer do
_from, _from,
%{room: room, state_set: state_set} = state %{room: room, state_set: state_set} = state
) do ) do
ban_event = Event.ban(room, account, user_id, reason) ban_event = Event.Ban.new(room, account, user_id, reason)
case insert_single_event(room, state_set, ban_event) do case insert_single_event(room, state_set, ban_event) do
{:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}} {:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}}
@ -306,7 +306,7 @@ defmodule MatrixServer.RoomServer do
end end
def handle_call({:unban, account, user_id}, _from, %{room: room, state_set: state_set} = state) do def handle_call({:unban, account, user_id}, _from, %{room: room, state_set: state_set} = state) do
unban_event = Event.unban(room, account, user_id) unban_event = Event.Unban.new(room, account, user_id)
case insert_single_event(room, state_set, unban_event) do case insert_single_event(room, state_set, unban_event) do
{:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}} {:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}}
@ -359,14 +359,14 @@ defmodule MatrixServer.RoomServer do
}) do }) do
events = events =
([ ([
Event.create_room(room, account, room_version), Event.CreateRoom.new(room, account, room_version),
Event.join(room, account), Event.Join.new(room, account),
Event.power_levels(room, account) Event.PowerLevels.new(room, account)
] ++ ] ++
room_creation_preset(account, preset, room) ++ room_creation_preset(account, preset, room) ++
[ [
if(name, do: Event.name(room, account, name)), if(name, do: Event.Name.new(room, account, name)),
if(topic, do: Event.topic(room, account, topic)) if(topic, do: Event.Topic.new(room, account, topic))
]) ])
|> Enum.reject(&Kernel.is_nil/1) |> Enum.reject(&Kernel.is_nil/1)
@ -427,9 +427,9 @@ defmodule MatrixServer.RoomServer do
end end
[ [
Event.join_rules(room, account, join_rule), Event.JoinRules.new(room, account, join_rule),
Event.history_visibility(room, account, his_vis), Event.HistoryVisibility.new(room, account, his_vis),
Event.guest_access(room, account, guest_access) Event.GuestAccess.new(room, account, guest_access)
] ]
end end

View file

@ -73,184 +73,6 @@ defmodule MatrixServer.Event do
} }
end end
@spec create_room(Room.t(), Account.t(), String.t()) :: t()
def create_room(room, %Account{localpart: localpart} = creator, room_version) do
mxid = MatrixServer.get_mxid(localpart)
%Event{
new(room, creator)
| type: "m.room.create",
state_key: "",
content: %{
"creator" => mxid,
"room_version" => room_version || MatrixServer.default_room_version()
}
}
end
@spec join(Room.t(), Account.t()) :: t()
def join(room, %Account{localpart: localpart} = sender) do
mxid = MatrixServer.get_mxid(localpart)
%Event{
new(room, sender)
| type: "m.room.member",
state_key: mxid,
content: %{
"membership" => "join"
}
}
end
@spec power_levels(Room.t(), Account.t()) :: t()
def power_levels(room, %Account{localpart: localpart} = sender) do
mxid = MatrixServer.get_mxid(localpart)
%Event{
new(room, 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" => %{
mxid => 50
},
"users_default" => 0,
"notifications" => %{
"room" => 50
}
}
}
end
@spec name(Room.t(), Account.t(), String.t()) :: %Event{}
def name(room, sender, name) do
%Event{
new(room, sender)
| type: "m.room.name",
state_key: "",
content: %{
"name" => name
}
}
end
@spec topic(Room.t(), Account.t(), String.t()) :: t()
def topic(room, sender, topic) do
%Event{
new(room, sender)
| type: "m.room.topic",
state_key: "",
content: %{
"topic" => topic
}
}
end
@spec join_rules(Room.t(), Account.t(), String.t()) :: t()
def join_rules(room, sender, join_rule) do
%Event{
new(room, sender)
| type: "m.room.join_rules",
state_key: "",
content: %{
"join_rule" => join_rule
}
}
end
@spec history_visibility(Room.t(), Account.t(), String.t()) :: t()
def history_visibility(room, sender, history_visibility) do
%Event{
new(room, sender)
| type: "m.room.history_visibility",
state_key: "",
content: %{
"history_visibility" => history_visibility
}
}
end
@spec guest_access(Room.t(), Account.t(), String.t()) :: t()
def guest_access(room, sender, guest_access) do
%Event{
new(room, sender)
| type: "m.room.guest_access",
state_key: "",
content: %{
"guest_access" => guest_access
}
}
end
@spec invite(Room.t(), Account.t(), String.t()) :: t()
def invite(room, sender, user_id) do
%Event{
new(room, sender)
| type: "m.room.member",
state_key: user_id,
content: %{
"membership" => "invite"
}
}
end
@spec leave(Room.t(), Account.t()) :: t()
def leave(room, sender) do
%Event{
new(room, sender)
| type: "m.room.member",
state_key: Account.get_mxid(sender),
content: %{
"membership" => "leave"
}
}
end
@spec kick(Room.t(), Account.t(), String.t(), String.t() | nil) :: t()
def kick(room, sender, user_id, reason \\ nil) do
content = %{"membership" => "leave"}
content = if reason, do: Map.put(content, "reason", reason), else: content
%Event{
new(room, sender)
| type: "m.room.member",
state_key: user_id,
content: content
}
end
@spec ban(Room.t(), Account.t(), String.t(), String.t() | nil) :: t()
def ban(room, sender, user_id, reason \\ nil) do
content = %{"membership" => "ban"}
content = if reason, do: Map.put(content, "reason", reason), else: content
%Event{
new(room, sender)
| type: "m.room.member",
state_key: user_id,
content: content
}
end
@spec unban(Room.t(), Account.t(), String.t()) :: t()
def unban(room, sender, user_id) do
%Event{
new(room, sender)
| type: "m.room.member",
state_key: user_id,
content: %{
"membership" => "leave"
}
}
end
@spec is_control_event(t()) :: boolean() @spec is_control_event(t()) :: boolean()
def is_control_event(%Event{type: "m.room.power_levels", state_key: ""}), do: true 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.join_rules", state_key: ""}), do: true

View file

@ -0,0 +1,228 @@
defmodule MatrixServer.Event.Join do
alias MatrixServer.{Event, Account, Room}
@spec new(Room.t(), Account.t()) :: Event.t()
def new(room, %Account{localpart: localpart} = sender) do
mxid = MatrixServer.get_mxid(localpart)
%Event{
Event.new(room, sender)
| type: "m.room.member",
state_key: mxid,
content: %{
"membership" => "join"
}
}
end
end
defmodule MatrixServer.Event.CreateRoom do
alias MatrixServer.{Event, Account, Room}
@spec new(Room.t(), Account.t(), String.t()) :: Event.t()
def new(room, %Account{localpart: localpart} = creator, room_version) do
mxid = MatrixServer.get_mxid(localpart)
%Event{
Event.new(room, creator)
| type: "m.room.create",
state_key: "",
content: %{
"creator" => mxid,
"room_version" => room_version || MatrixServer.default_room_version()
}
}
end
end
defmodule MatrixServer.Event.PowerLevels do
alias MatrixServer.{Event, Account, Room}
@spec new(Room.t(), Account.t()) :: Event.t()
def new(room, %Account{localpart: localpart} = sender) do
mxid = MatrixServer.get_mxid(localpart)
%Event{
Event.new(room, 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" => %{
mxid => 50
},
"users_default" => 0,
"notifications" => %{
"room" => 50
}
}
}
end
end
defmodule MatrixServer.Event.Name do
alias MatrixServer.{Event, Account, Room}
@spec new(Room.t(), Account.t(), String.t()) :: Event.t()
def new(room, sender, name) do
%Event{
Event.new(room, sender)
| type: "m.room.name",
state_key: "",
content: %{
"name" => name
}
}
end
end
defmodule MatrixServer.Event.Topic do
alias MatrixServer.{Event, Account, Room}
@spec new(Room.t(), Account.t(), String.t()) :: Event.t()
def new(room, sender, topic) do
%Event{
Event.new(room, sender)
| type: "m.room.topic",
state_key: "",
content: %{
"topic" => topic
}
}
end
end
defmodule MatrixServer.Event.JoinRules do
alias MatrixServer.{Event, Account, Room}
@spec new(Room.t(), Account.t(), String.t()) :: Event.t()
def new(room, sender, join_rule) do
%Event{
Event.new(room, sender)
| type: "m.room.join_rules",
state_key: "",
content: %{
"join_rule" => join_rule
}
}
end
end
defmodule MatrixServer.Event.HistoryVisibility do
alias MatrixServer.{Event, Account, Room}
@spec new(Room.t(), Account.t(), String.t()) :: Event.t()
def new(room, sender, history_visibility) do
%Event{
Event.new(room, sender)
| type: "m.room.history_visibility",
state_key: "",
content: %{
"history_visibility" => history_visibility
}
}
end
end
defmodule MatrixServer.Event.GuestAccess do
alias MatrixServer.{Event, Account, Room}
@spec new(Room.t(), Account.t(), String.t()) :: Event.t()
def new(room, sender, guest_access) do
%Event{
Event.new(room, sender)
| type: "m.room.guest_access",
state_key: "",
content: %{
"guest_access" => guest_access
}
}
end
end
defmodule MatrixServer.Event.Invite do
alias MatrixServer.{Event, Account, Room}
@spec new(Room.t(), Account.t(), String.t()) :: Event.t()
def new(room, sender, user_id) do
%Event{
Event.new(room, sender)
| type: "m.room.member",
state_key: user_id,
content: %{
"membership" => "invite"
}
}
end
end
defmodule MatrixServer.Event.Leave do
alias MatrixServer.{Event, Account, Room}
@spec new(Room.t(), Account.t()) :: Event.t()
def new(room, sender) do
%Event{
Event.new(room, sender)
| type: "m.room.member",
state_key: Account.get_mxid(sender),
content: %{
"membership" => "leave"
}
}
end
end
defmodule MatrixServer.Event.Kick do
alias MatrixServer.{Event, Account, Room}
@spec new(Room.t(), Account.t(), String.t(), String.t() | nil) :: Event.t()
def new(room, sender, user_id, reason \\ nil) do
content = %{"membership" => "leave"}
content = if reason, do: Map.put(content, "reason", reason), else: content
%Event{
Event.new(room, sender)
| type: "m.room.member",
state_key: user_id,
content: content
}
end
end
defmodule MatrixServer.Event.Ban do
alias MatrixServer.{Event, Account, Room}
@spec new(Room.t(), Account.t(), String.t(), String.t() | nil) :: Event.t()
def new(room, sender, user_id, reason \\ nil) do
content = %{"membership" => "ban"}
content = if reason, do: Map.put(content, "reason", reason), else: content
%Event{
Event.new(room, sender)
| type: "m.room.member",
state_key: user_id,
content: content
}
end
end
defmodule MatrixServer.Event.Unban do
alias MatrixServer.{Event, Account, Room}
@spec new(Room.t(), Account.t(), String.t()) :: Event.t()
def new(room, sender, user_id) do
%Event{
Event.new(room, sender)
| type: "m.room.member",
state_key: user_id,
content: %{
"membership" => "leave"
}
}
end
end