{:ok, user} <- User.ensure_keys_present(user) do
conn
|> put_resp_content_type("application/activity+json")
- |> json(UserView.render("user.json", %{user: user}))
+ |> put_view(UserView)
+ |> render("user.json", %{user: user})
else
nil -> {:error, :not_found}
end
conn
|> 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
{:public?, false} ->
{:error, :not_found}
likes <- Utils.get_object_likes(object) do
conn
|> 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
{:public?, false} ->
{:error, :not_found}
def following(%{assigns: %{relay: true}} = conn, _params) do
conn
|> 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
def following(%{assigns: %{user: for_user}} = conn, %{"nickname" => nickname, "page" => page}) do
conn
|> 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
{:show_follows, _} ->
conn
{user, for_user} <- ensure_user_keys_present_and_maybe_refresh_for_user(user, for_user) do
conn
|> 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
def followers(%{assigns: %{relay: true}} = conn, _params) do
conn
|> 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
def followers(%{assigns: %{user: for_user}} = conn, %{"nickname" => nickname, "page" => page}) do
conn
|> 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
{:show_followers, _} ->
conn
{user, for_user} <- ensure_user_keys_present_and_maybe_refresh_for_user(user, for_user) do
conn
|> 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
{:ok, user} <- User.ensure_keys_present(user) do
conn
|> 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
with {:ok, user} <- User.ensure_keys_present(user) do
conn
|> put_resp_content_type("application/activity+json")
- |> json(UserView.render("user.json", %{user: user}))
+ |> put_view(UserView)
+ |> render("user.json", %{user: user})
else
nil -> {:error, :not_found}
end
def whoami(%{assigns: %{user: %User{} = user}} = conn, _params) do
conn
|> put_resp_content_type("application/activity+json")
- |> json(UserView.render("user.json", %{user: user}))
+ |> put_view(UserView)
+ |> render("user.json", %{user: user})
end
def whoami(_conn, _params), do: {:error, :not_found}
Map.merge(base, additional)
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)
|> Map.merge(Pleroma.Web.ActivityPub.Utils.make_json_ld_header())
end
- def render("likes.json", ap_id, likes) do
+ def render("likes.json", %{ap_id: ap_id, likes: likes}) do
%{
"id" => "#{ap_id}/likes",
"type" => "OrderedCollection",
alias Pleroma.Web.AdminAPI.Config
alias Pleroma.Web.AdminAPI.ConfigView
alias Pleroma.Web.AdminAPI.ModerationLogView
+ alias Pleroma.Web.AdminAPI.Report
alias Pleroma.Web.AdminAPI.ReportView
alias Pleroma.Web.AdminAPI.Search
alias Pleroma.Web.CommonAPI
def user_show(conn, %{"nickname" => nickname}) do
with %User{} = user <- User.get_cached_by_nickname_or_id(nickname) do
conn
- |> json(AccountView.render("show.json", %{user: user}))
+ |> put_view(AccountView)
+ |> render("show.json", %{user: user})
else
_ -> {:error, :not_found}
end
})
conn
- |> json(StatusView.render("index.json", %{activities: activities, as: :activity}))
+ |> put_view(StatusView)
+ |> render("index.json", %{activities: activities, as: :activity})
else
_ -> {:error, :not_found}
end
})
conn
- |> json(AccountView.render("show.json", %{user: updated_user}))
+ |> put_view(AccountView)
+ |> render("show.json", %{user: updated_user})
end
def tag_users(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames, "tags" => tags}) do
invites = UserInviteToken.list_invites()
conn
- |> json(AccountView.render("invites.json", %{invites: invites}))
+ |> put_view(AccountView)
+ |> render("invites.json", %{invites: invites})
end
@doc "Revokes invite by token"
with {:ok, invite} <- UserInviteToken.find_by_token(token),
{:ok, updated_invite} = UserInviteToken.update_invite(invite, %{used: true}) do
conn
- |> json(AccountView.render("invite.json", %{invite: updated_invite}))
+ |> put_view(AccountView)
+ |> render("invite.json", %{invite: updated_invite})
else
nil -> {:error, :not_found}
end
with %Activity{} = report <- Activity.get_by_id(id) do
conn
|> put_view(ReportView)
- |> render("show.json", %{report: report})
+ |> render("show.json", Report.extract_report_info(report))
else
_ -> {:error, :not_found}
end
conn
|> put_view(ReportView)
- |> render("show.json", %{report: report})
+ |> render("show.json", Report.extract_report_info(report))
end
end
--- /dev/null
+# 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
defmodule Pleroma.Web.AdminAPI.ReportView do
use Pleroma.Web, :view
- alias Pleroma.Activity
alias Pleroma.HTML
alias Pleroma.User
+ alias Pleroma.Web.AdminAPI.Report
alias Pleroma.Web.CommonAPI.Utils
alias Pleroma.Web.MastodonAPI.StatusView
def render("index.json", %{reports: reports}) do
%{
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]
}
end
- def render("show.json", %{report: report}) do
- user = User.get_cached_by_ap_id(report.data["actor"])
+ def render("show.json", %{report: report, user: user, account: account, statuses: statuses}) do
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 =
unless is_nil(report.data["content"]) do
HTML.filter_tags(report.data["content"])
nil
end
- statuses =
- Enum.map(status_ap_ids, fn ap_id ->
- Activity.get_by_ap_id_with_object(ap_id)
- end)
-
%{
id: report.id,
account: merge_account_views(account),
def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
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
def search2(conn, params), do: do_search(:v2, conn, params)
conn
|> put_resp_header("content-type", "application/activity+json")
- |> json(ObjectView.render("object.json", %{object: object}))
+ |> put_view(ObjectView)
+ |> render("object.json", %{object: object})
end
defp represent_activity(_conn, "activity+json", _, _) do
defmodule Pleroma.Web.AdminAPI.ReportViewTest do
use Pleroma.DataCase
import Pleroma.Factory
+ alias Pleroma.Web.AdminAPI.Report
alias Pleroma.Web.AdminAPI.ReportView
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.MastodonAPI.AccountView
}
result =
- ReportView.render("show.json", %{report: activity})
+ ReportView.render("show.json", Report.extract_report_info(activity))
|> Map.delete(:created_at)
assert result == expected
}
result =
- ReportView.render("show.json", %{report: report_activity})
+ ReportView.render("show.json", Report.extract_report_info(report_activity))
|> Map.delete(:created_at)
assert result == expected
{:ok, activity} = CommonAPI.report(user, %{"account_id" => other_user.id})
{: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
test "renders report description" do
})
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
test "sanitizes report description" do
activity = Map.put(activity, :data, data)
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
test "doesn't error out when the user doesn't exists" do
Pleroma.User.delete(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