Clean up views

This commit is contained in:
Steven Fuchs 2019-09-23 22:33:59 +00:00 committed by kaniini
parent db0c00db8b
commit e1d2d69c87
8 changed files with 81 additions and 42 deletions

View file

@ -49,7 +49,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
{:ok, user} <- User.ensure_keys_present(user) do {:ok, user} <- User.ensure_keys_present(user) do
conn conn
|> put_resp_content_type("application/activity+json") |> put_resp_content_type("application/activity+json")
|> json(UserView.render("user.json", %{user: user})) |> put_view(UserView)
|> render("user.json", %{user: user})
else else
nil -> {:error, :not_found} nil -> {:error, :not_found}
end end
@ -90,7 +91,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
conn conn
|> put_resp_content_type("application/activity+json") |> put_resp_content_type("application/activity+json")
|> json(ObjectView.render("likes.json", ap_id, likes, page)) |> put_view(ObjectView)
|> render("likes.json", %{ap_id: ap_id, likes: likes, page: page})
else else
{:public?, false} -> {:public?, false} ->
{:error, :not_found} {:error, :not_found}
@ -104,7 +106,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
likes <- Utils.get_object_likes(object) do likes <- Utils.get_object_likes(object) do
conn conn
|> put_resp_content_type("application/activity+json") |> put_resp_content_type("application/activity+json")
|> json(ObjectView.render("likes.json", ap_id, likes)) |> put_view(ObjectView)
|> render("likes.json", %{ap_id: ap_id, likes: likes})
else else
{:public?, false} -> {:public?, false} ->
{:error, :not_found} {:error, :not_found}
@ -158,7 +161,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
def following(%{assigns: %{relay: true}} = conn, _params) do def following(%{assigns: %{relay: true}} = conn, _params) do
conn conn
|> put_resp_content_type("application/activity+json") |> put_resp_content_type("application/activity+json")
|> json(UserView.render("following.json", %{user: Relay.get_actor()})) |> put_view(UserView)
|> render("following.json", %{user: Relay.get_actor()})
end end
def following(%{assigns: %{user: for_user}} = conn, %{"nickname" => nickname, "page" => page}) do def following(%{assigns: %{user: for_user}} = conn, %{"nickname" => nickname, "page" => page}) do
@ -170,7 +174,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
conn conn
|> put_resp_content_type("application/activity+json") |> put_resp_content_type("application/activity+json")
|> json(UserView.render("following.json", %{user: user, page: page, for: for_user})) |> put_view(UserView)
|> render("following.json", %{user: user, page: page, for: for_user})
else else
{:show_follows, _} -> {:show_follows, _} ->
conn conn
@ -184,7 +189,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
{user, for_user} <- ensure_user_keys_present_and_maybe_refresh_for_user(user, for_user) do {user, for_user} <- ensure_user_keys_present_and_maybe_refresh_for_user(user, for_user) do
conn conn
|> put_resp_content_type("application/activity+json") |> put_resp_content_type("application/activity+json")
|> json(UserView.render("following.json", %{user: user, for: for_user})) |> put_view(UserView)
|> render("following.json", %{user: user, for: for_user})
end end
end end
@ -192,7 +198,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
def followers(%{assigns: %{relay: true}} = conn, _params) do def followers(%{assigns: %{relay: true}} = conn, _params) do
conn conn
|> put_resp_content_type("application/activity+json") |> put_resp_content_type("application/activity+json")
|> json(UserView.render("followers.json", %{user: Relay.get_actor()})) |> put_view(UserView)
|> render("followers.json", %{user: Relay.get_actor()})
end end
def followers(%{assigns: %{user: for_user}} = conn, %{"nickname" => nickname, "page" => page}) do def followers(%{assigns: %{user: for_user}} = conn, %{"nickname" => nickname, "page" => page}) do
@ -204,7 +211,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
conn conn
|> put_resp_content_type("application/activity+json") |> put_resp_content_type("application/activity+json")
|> json(UserView.render("followers.json", %{user: user, page: page, for: for_user})) |> put_view(UserView)
|> render("followers.json", %{user: user, page: page, for: for_user})
else else
{:show_followers, _} -> {:show_followers, _} ->
conn conn
@ -218,7 +226,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
{user, for_user} <- ensure_user_keys_present_and_maybe_refresh_for_user(user, for_user) do {user, for_user} <- ensure_user_keys_present_and_maybe_refresh_for_user(user, for_user) do
conn conn
|> put_resp_content_type("application/activity+json") |> put_resp_content_type("application/activity+json")
|> json(UserView.render("followers.json", %{user: user, for: for_user})) |> put_view(UserView)
|> render("followers.json", %{user: user, for: for_user})
end end
end end
@ -227,7 +236,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
{:ok, user} <- User.ensure_keys_present(user) do {:ok, user} <- User.ensure_keys_present(user) do
conn conn
|> put_resp_content_type("application/activity+json") |> put_resp_content_type("application/activity+json")
|> json(UserView.render("outbox.json", %{user: user, max_id: params["max_id"]})) |> put_view(UserView)
|> render("outbox.json", %{user: user, max_id: params["max_id"]})
end end
end end
@ -275,7 +285,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
with {:ok, user} <- User.ensure_keys_present(user) do with {:ok, user} <- User.ensure_keys_present(user) do
conn conn
|> put_resp_content_type("application/activity+json") |> put_resp_content_type("application/activity+json")
|> json(UserView.render("user.json", %{user: user})) |> put_view(UserView)
|> render("user.json", %{user: user})
else else
nil -> {:error, :not_found} nil -> {:error, :not_found}
end end
@ -296,7 +307,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
def whoami(%{assigns: %{user: %User{} = user}} = conn, _params) do def whoami(%{assigns: %{user: %User{} = user}} = conn, _params) do
conn conn
|> put_resp_content_type("application/activity+json") |> put_resp_content_type("application/activity+json")
|> json(UserView.render("user.json", %{user: user})) |> put_view(UserView)
|> render("user.json", %{user: user})
end end
def whoami(_conn, _params), do: {:error, :not_found} def whoami(_conn, _params), do: {:error, :not_found}

View file

@ -37,12 +37,12 @@ defmodule Pleroma.Web.ActivityPub.ObjectView do
Map.merge(base, additional) Map.merge(base, additional)
end end
def render("likes.json", ap_id, likes, page) do def render("likes.json", %{ap_id: ap_id, likes: likes, page: page}) do
collection(likes, "#{ap_id}/likes", page) collection(likes, "#{ap_id}/likes", page)
|> Map.merge(Pleroma.Web.ActivityPub.Utils.make_json_ld_header()) |> Map.merge(Pleroma.Web.ActivityPub.Utils.make_json_ld_header())
end end
def render("likes.json", ap_id, likes) do def render("likes.json", %{ap_id: ap_id, likes: likes}) do
%{ %{
"id" => "#{ap_id}/likes", "id" => "#{ap_id}/likes",
"type" => "OrderedCollection", "type" => "OrderedCollection",

View file

@ -14,6 +14,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
alias Pleroma.Web.AdminAPI.Config alias Pleroma.Web.AdminAPI.Config
alias Pleroma.Web.AdminAPI.ConfigView alias Pleroma.Web.AdminAPI.ConfigView
alias Pleroma.Web.AdminAPI.ModerationLogView alias Pleroma.Web.AdminAPI.ModerationLogView
alias Pleroma.Web.AdminAPI.Report
alias Pleroma.Web.AdminAPI.ReportView alias Pleroma.Web.AdminAPI.ReportView
alias Pleroma.Web.AdminAPI.Search alias Pleroma.Web.AdminAPI.Search
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
@ -139,7 +140,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
def user_show(conn, %{"nickname" => nickname}) do def user_show(conn, %{"nickname" => nickname}) do
with %User{} = user <- User.get_cached_by_nickname_or_id(nickname) do with %User{} = user <- User.get_cached_by_nickname_or_id(nickname) do
conn conn
|> json(AccountView.render("show.json", %{user: user})) |> put_view(AccountView)
|> render("show.json", %{user: user})
else else
_ -> {:error, :not_found} _ -> {:error, :not_found}
end end
@ -158,7 +160,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
}) })
conn conn
|> json(StatusView.render("index.json", %{activities: activities, as: :activity})) |> put_view(StatusView)
|> render("index.json", %{activities: activities, as: :activity})
else else
_ -> {:error, :not_found} _ -> {:error, :not_found}
end end
@ -178,7 +181,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
}) })
conn conn
|> json(AccountView.render("show.json", %{user: updated_user})) |> put_view(AccountView)
|> render("show.json", %{user: updated_user})
end end
def tag_users(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames, "tags" => tags}) do def tag_users(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames, "tags" => tags}) do
@ -424,7 +428,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
invites = UserInviteToken.list_invites() invites = UserInviteToken.list_invites()
conn conn
|> json(AccountView.render("invites.json", %{invites: invites})) |> put_view(AccountView)
|> render("invites.json", %{invites: invites})
end end
@doc "Revokes invite by token" @doc "Revokes invite by token"
@ -432,7 +437,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
with {:ok, invite} <- UserInviteToken.find_by_token(token), with {:ok, invite} <- UserInviteToken.find_by_token(token),
{:ok, updated_invite} = UserInviteToken.update_invite(invite, %{used: true}) do {:ok, updated_invite} = UserInviteToken.update_invite(invite, %{used: true}) do
conn conn
|> json(AccountView.render("invite.json", %{invite: updated_invite})) |> put_view(AccountView)
|> render("invite.json", %{invite: updated_invite})
else else
nil -> {:error, :not_found} nil -> {:error, :not_found}
end end
@ -465,7 +471,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
with %Activity{} = report <- Activity.get_by_id(id) do with %Activity{} = report <- Activity.get_by_id(id) do
conn conn
|> put_view(ReportView) |> put_view(ReportView)
|> render("show.json", %{report: report}) |> render("show.json", Report.extract_report_info(report))
else else
_ -> {:error, :not_found} _ -> {:error, :not_found}
end end
@ -481,7 +487,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
conn conn
|> put_view(ReportView) |> put_view(ReportView)
|> render("show.json", %{report: report}) |> render("show.json", Report.extract_report_info(report))
end end
end end

View file

@ -0,0 +1,22 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.AdminAPI.Report do
alias Pleroma.Activity
alias Pleroma.User
def extract_report_info(
%{data: %{"actor" => actor, "object" => [account_ap_id | status_ap_ids]}} = report
) do
user = User.get_cached_by_ap_id(actor)
account = User.get_cached_by_ap_id(account_ap_id)
statuses =
Enum.map(status_ap_ids, fn ap_id ->
Activity.get_by_ap_id_with_object(ap_id)
end)
%{report: report, user: user, account: account, statuses: statuses}
end
end

View file

@ -4,27 +4,26 @@
defmodule Pleroma.Web.AdminAPI.ReportView do defmodule Pleroma.Web.AdminAPI.ReportView do
use Pleroma.Web, :view use Pleroma.Web, :view
alias Pleroma.Activity
alias Pleroma.HTML alias Pleroma.HTML
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.AdminAPI.Report
alias Pleroma.Web.CommonAPI.Utils alias Pleroma.Web.CommonAPI.Utils
alias Pleroma.Web.MastodonAPI.StatusView alias Pleroma.Web.MastodonAPI.StatusView
def render("index.json", %{reports: reports}) do def render("index.json", %{reports: reports}) do
%{ %{
reports: reports:
render_many(reports[:items], __MODULE__, "show.json", as: :report) |> Enum.reverse(), reports[:items]
|> Enum.map(&Report.extract_report_info(&1))
|> Enum.map(&render(__MODULE__, "show.json", &1))
|> Enum.reverse(),
total: reports[:total] total: reports[:total]
} }
end end
def render("show.json", %{report: report}) do def render("show.json", %{report: report, user: user, account: account, statuses: statuses}) do
user = User.get_cached_by_ap_id(report.data["actor"])
created_at = Utils.to_masto_date(report.data["published"]) created_at = Utils.to_masto_date(report.data["published"])
[account_ap_id | status_ap_ids] = report.data["object"]
account = User.get_cached_by_ap_id(account_ap_id)
content = content =
unless is_nil(report.data["content"]) do unless is_nil(report.data["content"]) do
HTML.filter_tags(report.data["content"]) HTML.filter_tags(report.data["content"])
@ -32,11 +31,6 @@ defmodule Pleroma.Web.AdminAPI.ReportView do
nil nil
end end
statuses =
Enum.map(status_ap_ids, fn ap_id ->
Activity.get_by_ap_id_with_object(ap_id)
end)
%{ %{
id: report.id, id: report.id,
account: merge_account_views(account), account: merge_account_views(account),

View file

@ -19,9 +19,10 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do
def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
accounts = User.search(query, search_options(params, user)) accounts = User.search(query, search_options(params, user))
res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
json(conn, res) conn
|> put_view(AccountView)
|> render("accounts.json", users: accounts, for: user, as: :user)
end end
def search2(conn, params), do: do_search(:v2, conn, params) def search2(conn, params), do: do_search(:v2, conn, params)

View file

@ -216,7 +216,8 @@ defmodule Pleroma.Web.OStatus.OStatusController do
conn conn
|> put_resp_header("content-type", "application/activity+json") |> put_resp_header("content-type", "application/activity+json")
|> json(ObjectView.render("object.json", %{object: object})) |> put_view(ObjectView)
|> render("object.json", %{object: object})
end end
defp represent_activity(_conn, "activity+json", _, _) do defp represent_activity(_conn, "activity+json", _, _) do

View file

@ -5,6 +5,7 @@
defmodule Pleroma.Web.AdminAPI.ReportViewTest do defmodule Pleroma.Web.AdminAPI.ReportViewTest do
use Pleroma.DataCase use Pleroma.DataCase
import Pleroma.Factory import Pleroma.Factory
alias Pleroma.Web.AdminAPI.Report
alias Pleroma.Web.AdminAPI.ReportView alias Pleroma.Web.AdminAPI.ReportView
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
alias Pleroma.Web.MastodonAPI.AccountView alias Pleroma.Web.MastodonAPI.AccountView
@ -34,7 +35,7 @@ defmodule Pleroma.Web.AdminAPI.ReportViewTest do
} }
result = result =
ReportView.render("show.json", %{report: activity}) ReportView.render("show.json", Report.extract_report_info(activity))
|> Map.delete(:created_at) |> Map.delete(:created_at)
assert result == expected assert result == expected
@ -66,7 +67,7 @@ defmodule Pleroma.Web.AdminAPI.ReportViewTest do
} }
result = result =
ReportView.render("show.json", %{report: report_activity}) ReportView.render("show.json", Report.extract_report_info(report_activity))
|> Map.delete(:created_at) |> Map.delete(:created_at)
assert result == expected assert result == expected
@ -78,7 +79,9 @@ defmodule Pleroma.Web.AdminAPI.ReportViewTest do
{:ok, activity} = CommonAPI.report(user, %{"account_id" => other_user.id}) {:ok, activity} = CommonAPI.report(user, %{"account_id" => other_user.id})
{:ok, activity} = CommonAPI.update_report_state(activity.id, "closed") {:ok, activity} = CommonAPI.update_report_state(activity.id, "closed")
assert %{state: "closed"} = ReportView.render("show.json", %{report: activity})
assert %{state: "closed"} =
ReportView.render("show.json", Report.extract_report_info(activity))
end end
test "renders report description" do test "renders report description" do
@ -92,7 +95,7 @@ defmodule Pleroma.Web.AdminAPI.ReportViewTest do
}) })
assert %{content: "posts are too good for this instance"} = assert %{content: "posts are too good for this instance"} =
ReportView.render("show.json", %{report: activity}) ReportView.render("show.json", Report.extract_report_info(activity))
end end
test "sanitizes report description" do test "sanitizes report description" do
@ -109,7 +112,7 @@ defmodule Pleroma.Web.AdminAPI.ReportViewTest do
activity = Map.put(activity, :data, data) activity = Map.put(activity, :data, data)
refute "<script> alert('hecked :D:D:D:D:D:D:D') </script>" == refute "<script> alert('hecked :D:D:D:D:D:D:D') </script>" ==
ReportView.render("show.json", %{report: activity})[:content] ReportView.render("show.json", Report.extract_report_info(activity))[:content]
end end
test "doesn't error out when the user doesn't exists" do test "doesn't error out when the user doesn't exists" do
@ -125,6 +128,6 @@ defmodule Pleroma.Web.AdminAPI.ReportViewTest do
Pleroma.User.delete(other_user) Pleroma.User.delete(other_user)
Pleroma.User.invalidate_cache(other_user) Pleroma.User.invalidate_cache(other_user)
assert %{} = ReportView.render("show.json", %{report: activity}) assert %{} = ReportView.render("show.json", Report.extract_report_info(activity))
end end
end end