peform alias resolution in user links (fixes philomena-dev/philomena#52)

This commit is contained in:
byte[] 2020-10-23 00:34:14 -04:00
parent b5bc7d2d6b
commit a7b1ecf204

View file

@ -54,7 +54,7 @@ defmodule Philomena.UserLinks do
""" """
def create_user_link(user, attrs \\ %{}) do def create_user_link(user, attrs \\ %{}) do
tag = Repo.get_by(Tag, name: attrs["tag_name"]) tag = fetch_tag(attrs["tag_name"])
%UserLink{} %UserLink{}
|> UserLink.creation_changeset(attrs, user, tag) |> UserLink.creation_changeset(attrs, user, tag)
@ -74,7 +74,7 @@ defmodule Philomena.UserLinks do
""" """
def update_user_link(%UserLink{} = user_link, attrs) do def update_user_link(%UserLink{} = user_link, attrs) do
tag = Repo.get_by(Tag, name: attrs["tag_name"]) tag = fetch_tag(attrs["tag_name"])
user_link user_link
|> UserLink.edit_changeset(attrs, tag) |> UserLink.edit_changeset(attrs, tag)
@ -159,4 +159,15 @@ defmodule Philomena.UserLinks do
nil nil
end end
end end
defp fetch_tag(name) do
Tag
|> preload(:aliased_tag)
|> where(name: ^name)
|> Repo.one()
|> case do
nil -> nil
tag -> tag.aliased_tag || tag
end
end
end end