From a7b1ecf20408e40992c752a02de6abfc1594e0f2 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Fri, 23 Oct 2020 00:34:14 -0400 Subject: [PATCH] peform alias resolution in user links (fixes philomena-dev/philomena#52) --- lib/philomena/user_links.ex | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/philomena/user_links.ex b/lib/philomena/user_links.ex index cd71402e..3e2ae7b8 100644 --- a/lib/philomena/user_links.ex +++ b/lib/philomena/user_links.ex @@ -54,7 +54,7 @@ defmodule Philomena.UserLinks 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.creation_changeset(attrs, user, tag) @@ -74,7 +74,7 @@ defmodule Philomena.UserLinks 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 |> UserLink.edit_changeset(attrs, tag) @@ -159,4 +159,15 @@ defmodule Philomena.UserLinks do nil 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