From 46486595ff96e0af0c3193e60879a8e67f77b933 Mon Sep 17 00:00:00 2001 From: raeno Date: Fri, 14 Dec 2018 19:55:40 +0100 Subject: [PATCH] Handle "users/:id" links as well. Fix comments in MR. --- lib/pleroma/user.ex | 13 +++++++++++++ lib/pleroma/web/ostatus/metadata.ex | 5 ++--- lib/pleroma/web/ostatus/ostatus_controller.ex | 4 +++- lib/pleroma/web/router.ex | 8 ++++++-- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 49928bc13..28ff08a39 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -294,6 +294,10 @@ def locked?(%User{} = user) do user.info.locked || false end + def get_by_id(id) do + Repo.get_by(User, id: id) + end + def get_by_ap_id(ap_id) do Repo.get_by(User, ap_id: ap_id) end @@ -320,11 +324,20 @@ def get_cached_by_ap_id(ap_id) do Cachex.fetch!(:user_cache, key, fn _ -> get_by_ap_id(ap_id) end) end + def get_cached_by_id(id) do + key = "id:#{id}" + Cachex.fetch!(:user_cache, key, fn _ -> get_by_id(id) end) + end + def get_cached_by_nickname(nickname) do key = "nickname:#{nickname}" Cachex.fetch!(:user_cache, key, fn _ -> get_or_fetch_by_nickname(nickname) end) end + def get_cached_by_nickname_or_id(nickname_or_id) do + get_cached_by_nickname(nickname_or_id) || get_cached_by_id(nickname_or_id) + end + def get_by_nickname(nickname) do Repo.get_by(User, nickname: nickname) end diff --git a/lib/pleroma/web/ostatus/metadata.ex b/lib/pleroma/web/ostatus/metadata.ex index 792b4a4bd..120a89a8b 100644 --- a/lib/pleroma/web/ostatus/metadata.ex +++ b/lib/pleroma/web/ostatus/metadata.ex @@ -11,8 +11,7 @@ def build_tags(params) do end def meta_enabled?(type) do - config = Pleroma.Config.get(:metadata, []) - Keyword.get(config, type, false) + Pleroma.Config.get([:metadata, type], false) end # opengraph for single status @@ -70,6 +69,6 @@ defp user_avatar_url(user) do end def pleroma_domain do - Pleroma.Config.get([:instance, :domain], "UNKNOWN_DOMAIN") + Pleroma.Web.Endpoint.host() end end diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex index 55dbcab93..5dbee20e1 100644 --- a/lib/pleroma/web/ostatus/ostatus_controller.ex +++ b/lib/pleroma/web/ostatus/ostatus_controller.ex @@ -16,8 +16,10 @@ defmodule Pleroma.Web.OStatus.OStatusController do def feed_redirect(conn, %{"nickname" => nickname}) do case get_format(conn) do "html" -> - with %User{} = user <- User.get_cached_by_nickname(nickname) do + with %User{} = user <- User.get_cached_by_nickname_or_id(nickname) do Fallback.RedirectController.redirector_with_meta(conn, %{user: user}) + else + nil -> {:error, :not_found} end "activity+json" -> diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 914cd6a6d..ad97698dd 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -449,11 +449,11 @@ defmodule Fallback.RedirectController do def redirector(conn, _params) do conn |> put_resp_content_type("text/html") - |> send_file(200, Application.app_dir(:pleroma, "priv/static/index.html")) + |> send_file(200, index_file_path()) end def redirector_with_meta(conn, params) do - {:ok, index_content} = File.read(Application.app_dir(:pleroma, "priv/static/index.html")) + {:ok, index_content} = File.read(index_file_path()) tags = Metadata.build_tags(params) response = String.replace(index_content, "", tags) @@ -462,6 +462,10 @@ def redirector_with_meta(conn, params) do |> send_resp(200, response) end + def index_file_path do + Application.app_dir(:pleroma, "priv/static/index.html") + end + def registration_page(conn, params) do redirector(conn, params) end