Change primary keys for accounts and devices table

This commit is contained in:
Pim Kunis 2021-08-30 22:36:01 +02:00
parent dfc3fa450d
commit 91a06aff1b
9 changed files with 81 additions and 77 deletions

View file

@ -530,6 +530,7 @@ defmodule MatrixServer.RoomServer do
room = Room.update_forward_extremities(event, room) room = Room.update_forward_extremities(event, room)
event = Repo.insert!(event) event = Repo.insert!(event)
state_set = StateResolution.resolve_forward_extremities(event) state_set = StateResolution.resolve_forward_extremities(event)
# TODO: Do this as a background job, and not after every insert...
_ = update_joined_rooms(room, state_set) _ = update_joined_rooms(room, state_set)
{:ok, state_set, room} {:ok, state_set, room}
@ -539,7 +540,6 @@ defmodule MatrixServer.RoomServer do
end end
# Update local accounts' room membership if applicable. # Update local accounts' room membership if applicable.
# TODO: Could be a background job.
@spec update_joined_rooms(Room.t(), t()) :: JoinedRoom.t() | nil @spec update_joined_rooms(Room.t(), t()) :: JoinedRoom.t() | nil
defp update_joined_rooms(%Room{id: room_id}, state_set) do defp update_joined_rooms(%Room{id: room_id}, state_set) do
server_name = MatrixServer.server_name() server_name = MatrixServer.server_name()
@ -553,19 +553,27 @@ defmodule MatrixServer.RoomServer do
membership == "join" membership == "join"
end) end)
joined_assocs = map_localparts =
Enum.map(joined, fn {{_, state_key}, _} -> &Enum.map(&1, fn {{_, state_key}, _} -> MatrixServer.get_localpart(state_key) end)
%{localpart: MatrixServer.get_localpart(state_key), room_id: room_id}
end)
not_joined_localparts = joined_localparts = map_localparts.(joined)
Enum.map(not_joined, fn {{_, state_key}, _} -> MatrixServer.get_localpart(state_key) end) not_joined_localparts = map_localparts.(not_joined)
_ = Repo.insert_all(JoinedRoom, joined_assocs, on_conflict: :nothing) _ =
Repo.insert_all(
JoinedRoom,
from(a in Account,
where: a.localpart in ^joined_localparts,
select: %{account_id: a.id, room_id: ^room_id}
),
on_conflict: :nothing
)
Repo.delete_all( Repo.delete_all(
from jr in JoinedRoom, from jr in JoinedRoom,
where: jr.room_id == ^room_id and jr.localpart in ^not_joined_localparts join: a in Account,
on: a.id == jr.account_id,
where: jr.room_id == ^room_id and a.localpart in ^not_joined_localparts
) )
end end
end end

View file

@ -13,14 +13,14 @@ defmodule MatrixServer.Account do
@max_mxid_length 255 @max_mxid_length 255
@primary_key {:localpart, :string, []}
schema "accounts" do schema "accounts" do
field :localpart, :string
field :password_hash, :string, redact: true field :password_hash, :string, redact: true
has_many :devices, Device, foreign_key: :localpart has_many :devices, Device
many_to_many :joined_rooms, Room, many_to_many :joined_rooms, Room,
join_through: JoinedRoom, join_through: JoinedRoom,
join_keys: [localpart: :localpart, room_id: :id] join_keys: [account_id: :id, room_id: :id]
timestamps(updated_at: false) timestamps(updated_at: false)
end end
@ -50,31 +50,35 @@ defmodule MatrixServer.Account do
Return an multi to register a new user. Return an multi to register a new user.
""" """
@spec register(Register.t()) :: Multi.t() @spec register(Register.t()) :: Multi.t()
def register(%Register{} = input) do def register(input) do
localpart = input.username || MatrixServer.random_string(10, ?a..?z)
account_params = %{ account_params = %{
localpart: input.username || MatrixServer.random_string(10, ?a..?z), localpart: localpart,
password_hash: Bcrypt.hash_pwd_salt(input.password) password_hash: Bcrypt.hash_pwd_salt(input.password)
} }
Multi.new() Multi.new()
|> Multi.insert(:account, changeset(%Account{}, account_params)) |> Multi.insert(:account, changeset(%Account{}, account_params))
|> Multi.insert(:device, fn %{account: account} -> |> Multi.insert(:device, fn %{account: account} ->
device_id = input.device_id || Device.generate_device_id(account.localpart)
access_token = Device.generate_access_token(localpart, device_id)
device_params = %{ device_params = %{
display_name: input.initial_device_display_name, display_name: input.initial_device_display_name,
device_id: input.device_id || Device.generate_device_id(account.localpart) device_id: device_id
} }
Ecto.build_assoc(account, :devices) Ecto.build_assoc(account, :devices, access_token: access_token)
|> Device.changeset(device_params) |> Device.changeset(device_params)
end) end)
|> Multi.run(:device_with_access_token, &Device.insert_new_access_token/2)
end end
@doc """ @doc """
Return a function to log a user in. Return a function to log a user in.
""" """
@spec login(Login.t()) :: (Ecto.Repo.t() -> {:error, any()} | {:ok, Device.t()}) @spec login(Login.t()) :: (Ecto.Repo.t() -> {:error, any()} | {:ok, {Account.t(), Device.t()}})
def login(%Login{} = input) do def login(input) do
localpart = try_get_localpart(input.identifier.user) localpart = try_get_localpart(input.identifier.user)
fn repo -> fn repo ->
@ -83,7 +87,7 @@ defmodule MatrixServer.Account do
if Bcrypt.verify_pass(input.password, hash) do if Bcrypt.verify_pass(input.password, hash) do
case Device.login(input, account) do case Device.login(input, account) do
{:ok, device} -> {:ok, device} ->
device {account, device}
{:error, _cs} -> {:error, _cs} ->
repo.rollback(:forbidden) repo.rollback(:forbidden)
@ -119,7 +123,7 @@ defmodule MatrixServer.Account do
|> validate_length(:password_hash, max: 60) |> validate_length(:password_hash, max: 60)
|> validate_format(:localpart, MatrixServer.localpart_regex()) |> validate_format(:localpart, MatrixServer.localpart_regex())
|> validate_length(:localpart, max: localpart_length()) |> validate_length(:localpart, max: localpart_length())
|> unique_constraint(:localpart, name: :accounts_pkey) |> unique_constraint(:localpart, name: :accounts_localpart_index)
end end
@spec localpart_length :: integer() @spec localpart_length :: integer()

View file

@ -4,43 +4,27 @@ defmodule MatrixServer.Device do
import Ecto.{Changeset, Query} import Ecto.{Changeset, Query}
alias MatrixServer.{Account, Device, Repo} alias MatrixServer.{Account, Device, Repo}
alias MatrixServerWeb.Client.Request.Login
@type t :: %__MODULE__{ @type t :: %__MODULE__{
device_id: String.t(), device_id: String.t(),
access_token: String.t(), access_token: String.t(),
display_name: String.t(), display_name: String.t(),
localpart: String.t() account_id: integer()
} }
@primary_key false
schema "devices" do schema "devices" do
field :device_id, :string, primary_key: true field :device_id, :string
field :access_token, :string, redact: true field :access_token, :string, redact: true
field :display_name, :string field :display_name, :string
belongs_to :account, Account, belongs_to :account, Account
foreign_key: :localpart,
references: :localpart,
type: :string,
primary_key: true
end end
def changeset(device, params \\ %{}) do def changeset(device, params \\ %{}) do
device device
|> cast(params, [:display_name, :device_id]) |> cast(params, [:display_name, :device_id])
|> validate_required([:localpart, :device_id]) |> validate_required([:device_id])
|> unique_constraint([:localpart, :device_id], name: :devices_pkey) |> unique_constraint([:device_id, :account_id], name: :devices_device_id_account_id_index)
end
def insert_new_access_token(repo, %{
device: %Device{localpart: localpart, device_id: device_id} = device
}) do
access_token = generate_access_token(localpart, device_id)
device
|> change(%{access_token: access_token})
|> repo.update()
end end
def generate_access_token(localpart, device_id) do def generate_access_token(localpart, device_id) do
@ -52,9 +36,9 @@ defmodule MatrixServer.Device do
"#{localpart}_#{System.os_time(:millisecond)}" "#{localpart}_#{System.os_time(:millisecond)}"
end end
def login(%Login{} = input, account) do def login(input, %Account{localpart: localpart} = account) do
device_id = input.device_id || generate_device_id(account.localpart) device_id = input.device_id || generate_device_id(localpart)
access_token = generate_access_token(account.localpart, device_id) access_token = generate_access_token(localpart, device_id)
update_query = update_query =
from(d in Device) from(d in Device)
@ -75,6 +59,6 @@ defmodule MatrixServer.Device do
Ecto.build_assoc(account, :devices) Ecto.build_assoc(account, :devices)
|> Device.changeset(device_params) |> Device.changeset(device_params)
|> put_change(:access_token, access_token) |> put_change(:access_token, access_token)
|> Repo.insert(on_conflict: update_query, conflict_target: [:localpart, :device_id]) |> Repo.insert(on_conflict: update_query, conflict_target: [:account_id, :device_id])
end end
end end

View file

@ -4,17 +4,13 @@ defmodule MatrixServer.JoinedRoom do
alias MatrixServer.{Account, Room} alias MatrixServer.{Account, Room}
@type t :: %__MODULE__{ @type t :: %__MODULE__{
localpart: String.t(), account_id: integer(),
room_id: String.t() room_id: String.t()
} }
@primary_key false @primary_key false
schema "joined_rooms" do schema "joined_rooms" do
belongs_to :account, Account, belongs_to :account, Account, primary_key: true
foreign_key: :localpart,
references: :localpart,
type: :string,
primary_key: true
belongs_to :room, Room, primary_key: true, type: :string belongs_to :room, Room, primary_key: true, type: :string
end end

View file

@ -4,7 +4,7 @@ defmodule MatrixServerWeb.Client.LoginController do
import MatrixServerWeb.Error import MatrixServerWeb.Error
import Ecto.Changeset import Ecto.Changeset
alias MatrixServer.{Repo, Account} alias MatrixServer.{Repo, Account, Device}
alias MatrixServerWeb.Client.Request.Login alias MatrixServerWeb.Client.Request.Login
alias Ecto.Changeset alias Ecto.Changeset
@ -38,11 +38,13 @@ defmodule MatrixServerWeb.Client.LoginController do
input = apply_changes(cs) input = apply_changes(cs)
case Account.login(input) |> Repo.transaction() do case Account.login(input) |> Repo.transaction() do
{:ok, device} -> {:ok,
{%Account{localpart: localpart},
%Device{access_token: access_token, device_id: device_id}}} ->
data = %{ data = %{
user_id: MatrixServer.get_mxid(device.localpart), user_id: MatrixServer.get_mxid(localpart),
access_token: device.access_token, access_token: access_token,
device_id: device.device_id device_id: device_id
} }
conn conn

View file

@ -4,7 +4,7 @@ defmodule MatrixServerWeb.Client.RegisterController do
import MatrixServerWeb.Error import MatrixServerWeb.Error
import Ecto.Changeset import Ecto.Changeset
alias MatrixServer.{Repo, Account} alias MatrixServer.{Repo, Account, Device}
alias MatrixServerWeb.Client.Request.Register alias MatrixServerWeb.Client.Request.Register
alias Ecto.Changeset alias Ecto.Changeset
@ -21,14 +21,18 @@ defmodule MatrixServerWeb.Client.RegisterController do
input = apply_changes(cs) input = apply_changes(cs)
case Account.register(input) |> Repo.transaction() do case Account.register(input) |> Repo.transaction() do
{:ok, %{device_with_access_token: device}} -> {:ok,
data = %{user_id: MatrixServer.get_mxid(device.localpart)} %{
account: %Account{localpart: localpart},
device: %Device{device_id: device_id, access_token: access_token}
}} ->
data = %{user_id: MatrixServer.get_mxid(localpart)}
data = data =
if not input.inhibit_login do if not input.inhibit_login do
data data
|> Map.put(:device_id, device.device_id) |> Map.put(:device_id, device_id)
|> Map.put(:access_token, device.access_token) |> Map.put(:access_token, access_token)
else else
data data
end end

View file

@ -207,4 +207,14 @@ defmodule MatrixServerWeb.Client.RoomController do
end end
def unban(conn, _), do: put_error(conn, :missing_param) def unban(conn, _), do: put_error(conn, :missing_param)
def send_message(%Conn{assigns: %{account: account}, body_params: body_params} = conn, %{
"room_id" => room_id,
"event_type" => event_type,
"txn_id" => txn_id
}) do
conn
|> send_resp(200, [])
|> halt()
end
end end

View file

@ -66,6 +66,7 @@ defmodule MatrixServerWeb.Router do
post "/kick", RoomController, :kick post "/kick", RoomController, :kick
post "/ban", RoomController, :ban post "/ban", RoomController, :ban
post "/unban", RoomController, :unban post "/unban", RoomController, :unban
put "/send/:event_type/:txn_id", RoomController, :send_message
end end
end end
end end

View file

@ -2,12 +2,14 @@ defmodule MatrixServer.Repo.Migrations.CreateInitialTables do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:accounts, primary_key: false) do create table(:accounts) do
add :localpart, :string, primary_key: true, null: false add :localpart, :string, null: false
add :password_hash, :string, size: 60, null: false add :password_hash, :string, size: 60, null: false
timestamps(updated_at: false) timestamps(updated_at: false)
end end
create index(:accounts, [:localpart], unique: true)
create table(:rooms, primary_key: false) do create table(:rooms, primary_key: false) do
add :state, {:array, {:array, :string}}, default: [], null: false add :state, {:array, {:array, :string}}, default: [], null: false
add :forward_extremities, {:array, :string}, default: [], null: false add :forward_extremities, {:array, :string}, default: [], null: false
@ -16,10 +18,7 @@ defmodule MatrixServer.Repo.Migrations.CreateInitialTables do
end end
create table(:joined_rooms, primary_key: false) do create table(:joined_rooms, primary_key: false) do
add :localpart, add :account_id, references(:accounts), primary_key: true, null: false
references(:accounts, column: :localpart, type: :string),
primary_key: true,
null: false
add :room_id, references(:rooms, type: :string), add :room_id, references(:rooms, type: :string),
primary_key: true, primary_key: true,
@ -62,20 +61,16 @@ defmodule MatrixServer.Repo.Migrations.CreateInitialTables do
create index(:aliases, [:room_id]) create index(:aliases, [:room_id])
create table(:devices, primary_key: false) do create table(:devices) do
add :device_id, :string, primary_key: true, null: false add :device_id, :string, null: false
add :access_token, :string add :access_token, :string
add :display_name, :string add :display_name, :string
add :localpart, add :account_id, references(:accounts, on_delete: :delete_all), null: false
references(:accounts, column: :localpart, on_delete: :delete_all, type: :string),
primary_key: true,
null: false
end end
# Compound primary already indexes device_id. create index(:devices, [:device_id, :account_id], unique: true)
create index(:devices, [:localpart]) create index(:devices, [:account_id])
create index(:devices, [:access_token], unique: true) create index(:devices, [:access_token], unique: true)
end end
end end