X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fpleroma%2Fweb%2Fadmin_api%2Fcontrollers%2Fmedia_proxy_cache_controller.ex;h=a6d7aaf54a49e28568ed9c69e7fbd1bb196dee45;hb=f2134e605b44c300505f81adab01354a1652b2e5;hp=e3fa0ac284651f50745fbe916abf9c6ba9a10e1e;hpb=3462d4b995a995ad523139a865b34a824e02674d;p=akkoma
diff --git a/lib/pleroma/web/admin_api/controllers/media_proxy_cache_controller.ex b/lib/pleroma/web/admin_api/controllers/media_proxy_cache_controller.ex
index e3fa0ac28..a6d7aaf54 100644
--- a/lib/pleroma/web/admin_api/controllers/media_proxy_cache_controller.ex
+++ b/lib/pleroma/web/admin_api/controllers/media_proxy_cache_controller.ex
@@ -1,24 +1,26 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.AdminAPI.MediaProxyCacheController do
use Pleroma.Web, :controller
- alias Pleroma.Plugs.OAuthScopesPlug
alias Pleroma.Web.ApiSpec.Admin, as: Spec
alias Pleroma.Web.MediaProxy
+ alias Pleroma.Web.Plugs.OAuthScopesPlug
+
+ @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
plug(Pleroma.Web.ApiSpec.CastAndValidate)
plug(
OAuthScopesPlug,
- %{scopes: ["read:media_proxy_caches"], admin: true} when action in [:index]
+ %{scopes: ["admin:read:media_proxy_caches"]} when action in [:index]
)
plug(
OAuthScopesPlug,
- %{scopes: ["write:media_proxy_caches"], admin: true} when action in [:purge, :delete]
+ %{scopes: ["admin:write:media_proxy_caches"]} when action in [:purge, :delete]
)
action_fallback(Pleroma.Web.AdminAPI.FallbackController)
@@ -26,38 +28,49 @@ defmodule Pleroma.Web.AdminAPI.MediaProxyCacheController do
defdelegate open_api_operation(action), to: Spec.MediaProxyCacheOperation
def index(%{assigns: %{user: _}} = conn, params) do
- cursor =
- :deleted_urls_cache
- |> :ets.table([{:traverse, {:select, Cachex.Query.create(true, :key)}}])
- |> :qlc.cursor()
+ entries = fetch_entries(params)
+ urls = paginate_entries(entries, params.page, params.page_size)
+
+ render(conn, "index.json",
+ urls: urls,
+ page_size: params.page_size,
+ count: length(entries)
+ )
+ end
- urls =
- case params.page do
- 1 ->
- :qlc.next_answers(cursor, params.page_size)
+ defp fetch_entries(params) do
+ MediaProxy.cache_table()
+ |> @cachex.stream!(Cachex.Query.create(true, :key))
+ |> filter_entries(params[:query])
+ end
- _ ->
- :qlc.next_answers(cursor, (params.page - 1) * params.page_size)
- :qlc.next_answers(cursor, params.page_size)
- end
+ defp filter_entries(stream, query) when is_binary(query) do
+ regex = ~r/#{query}/i
+
+ stream
+ |> Enum.filter(fn url -> String.match?(url, regex) end)
+ |> Enum.to_list()
+ end
- :qlc.delete_cursor(cursor)
+ defp filter_entries(stream, _), do: Enum.to_list(stream)
- render(conn, "index.json", urls: urls)
+ defp paginate_entries(entries, page, page_size) do
+ offset = page_size * (page - 1)
+ Enum.slice(entries, offset, page_size)
end
def delete(%{assigns: %{user: _}, body_params: %{urls: urls}} = conn, _) do
- MediaProxy.remove_from_deleted_urls(urls)
- render(conn, "index.json", urls: urls)
+ MediaProxy.remove_from_banned_urls(urls)
+ json(conn, %{})
end
def purge(%{assigns: %{user: _}, body_params: %{urls: urls, ban: ban}} = conn, _) do
MediaProxy.Invalidation.purge(urls)
if ban do
- MediaProxy.put_in_deleted_urls(urls)
+ MediaProxy.put_in_banned_urls(urls)
end
- render(conn, "index.json", urls: urls)
+ json(conn, %{})
end
end