Skip to content

Commit

Permalink
Make merge_accounts/2 accept IDs, usernames, emails or User structs
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed May 2, 2024
1 parent c1e9634 commit fbaf139
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/asciinema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ defmodule Asciinema do
defdelegate verify_login_token(token), to: Accounts

def merge_accounts(src_user, dst_user) do
src_user = Accounts.find_user(src_user)
dst_user = Accounts.find_user(dst_user)

Repo.transact(fn ->
Recordings.reassign_asciicasts(src_user.id, dst_user.id)
Streaming.reassign_live_streams(src_user.id, dst_user.id)
Expand Down
10 changes: 10 additions & 0 deletions lib/asciinema/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ defmodule Asciinema.Accounts do

def get_user(id), do: Repo.get(User, id)

def find_user(%User{} = user), do: user

def find_user(id) when is_integer(id), do: get_user(id)

def find_user(id) when is_binary(id) do
{_, user} = lookup_user(id)

user
end

def find_user_by_username(username) do
Repo.one(
from(u in User,
Expand Down

0 comments on commit fbaf139

Please sign in to comment.