diff --git a/config/config.exs b/config/config.exs index 4df8951b..740e247d 100644 --- a/config/config.exs +++ b/config/config.exs @@ -25,6 +25,9 @@ config :bcrypt_elixir, config :elastix, json_codec: Jason +config :canary, + repo: Philomena.Repo + # Configures the endpoint config :philomena, PhilomenaWeb.Endpoint, url: [host: "localhost"], diff --git a/lib/philomena/images/elasticsearch.ex b/lib/philomena/images/elasticsearch.ex index dfef3855..fdd58e46 100644 --- a/lib/philomena/images/elasticsearch.ex +++ b/lib/philomena/images/elasticsearch.ex @@ -8,7 +8,7 @@ defmodule Philomena.Images.Elasticsearch do } }, mappings: %{ - _doc: %{ + image: %{ _all: %{enabled: false}, dynamic: false, properties: %{ @@ -76,6 +76,10 @@ defmodule Philomena.Images.Elasticsearch do } end + # preload([ + # :user, :deleter, :gallery_interactions, :upvoters, :downvoters, :favers, + # :hiders, tags: [:aliases, :aliased_tag] + # ]) def as_json(image) do %{ id: image.id, diff --git a/lib/philomena/tags.ex b/lib/philomena/tags.ex index 511d265e..5b9ff477 100644 --- a/lib/philomena/tags.ex +++ b/lib/philomena/tags.ex @@ -35,7 +35,7 @@ defmodule Philomena.Tags do ** (Ecto.NoResultsError) """ - def get_tag!(id), do: Repo.get!(Tag, id) + def get_tag!(slug), do: Repo.get_by!(Tag, slug: slug) @doc """ Creates a tag. diff --git a/lib/philomena/users/ability.ex b/lib/philomena/users/ability.ex index d7c0009e..cbe81b1a 100644 --- a/lib/philomena/users/ability.ex +++ b/lib/philomena/users/ability.ex @@ -12,8 +12,8 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do when action in [:new, :create, :index], do: true - def can?(_user, :read, %Image{hidden_from_users: true}), do: false - def can?(_user, :read, %Image{hidden_from_users: false}), do: true + def can?(_user, :show, %Image{hidden_from_users: true}), do: false + def can?(_user, :show, %Image{hidden_from_users: false}), do: true # Otherwise... def can?(_user, _action, _model), do: false diff --git a/lib/philomena_web.ex b/lib/philomena_web.ex index 5d83f853..bcc00039 100644 --- a/lib/philomena_web.ex +++ b/lib/philomena_web.ex @@ -23,6 +23,7 @@ defmodule PhilomenaWeb do import Plug.Conn import PhilomenaWeb.Gettext + import Canary.Plugs alias PhilomenaWeb.Router.Helpers, as: Routes alias PhilomenaWeb.Plugs.ImageFilter end diff --git a/lib/philomena_web/controllers/image_controller.ex b/lib/philomena_web/controllers/image_controller.ex index 92b479cf..29606749 100644 --- a/lib/philomena_web/controllers/image_controller.ex +++ b/lib/philomena_web/controllers/image_controller.ex @@ -1,10 +1,11 @@ defmodule PhilomenaWeb.ImageController do use PhilomenaWeb, :controller - alias Philomena.{Images, Images.Image} + alias Philomena.{Images.Image} import Ecto.Query plug ImageFilter + plug :load_and_authorize_resource, model: Image, only: :show, preload: :tags def index(conn, _params) do query = conn.assigns.compiled_filter @@ -21,8 +22,7 @@ defmodule PhilomenaWeb.ImageController do render(conn, "index.html", images: images) end - def show(conn, %{"id" => id}) do - image = Images.get_image!(id) - render(conn, "show.html", image: image) + def show(conn, %{"id" => _id}) do + render(conn, "show.html", image: conn.assigns.image) end end diff --git a/lib/philomena_web/controllers/tag_controller.ex b/lib/philomena_web/controllers/tag_controller.ex index b91137b4..18a2c3b7 100644 --- a/lib/philomena_web/controllers/tag_controller.ex +++ b/lib/philomena_web/controllers/tag_controller.ex @@ -1,15 +1,35 @@ defmodule PhilomenaWeb.TagController do use PhilomenaWeb, :controller - alias Philomena.Tags + alias Philomena.{Images.Image, Tags} + import Ecto.Query + + plug ImageFilter def index(conn, _params) do tags = Tags.list_tags() render(conn, "index.html", tags: tags) end - def show(conn, %{"id" => id}) do - tag = Tags.get_tag!(id) - render(conn, "show.html", tag: tag) + def show(conn, %{"id" => slug}) do + tag = Tags.get_tag!(slug) + + query = conn.assigns.compiled_filter + + images = + Image.search_records( + %{ + query: %{ + bool: %{ + must_not: query, + must: %{term: %{"namespaced_tags.name": tag.name}} + } + }, + sort: %{created_at: :desc} + }, + Image |> preload(:tags) + ) + + render(conn, "show.html", tag: tag, images: images) end end diff --git a/lib/philomena_web/templates/tag/show.html.eex b/lib/philomena_web/templates/tag/show.html.eex deleted file mode 100644 index fe485512..00000000 --- a/lib/philomena_web/templates/tag/show.html.eex +++ /dev/null @@ -1,8 +0,0 @@ -