Add fallback for reports that don't have attached activities

This commit is contained in:
FloatingGhost 2022-11-08 11:01:47 +00:00
parent a0b8e3c842
commit 479aacb1b6
2 changed files with 184 additions and 182 deletions

View file

@ -209,212 +209,214 @@ def render(
end end
def render("show.json", %{activity: %{data: %{"object" => _object}} = activity} = opts) do def render("show.json", %{activity: %{data: %{"object" => _object}} = activity} = opts) do
object = Object.normalize(activity, fetch: false) with %Object{} = object <- Object.normalize(activity, fetch: false) do
user = CommonAPI.get_user(activity.data["actor"])
user_follower_address = user.follower_address
user = CommonAPI.get_user(activity.data["actor"]) like_count = object.data["like_count"] || 0
user_follower_address = user.follower_address announcement_count = object.data["announcement_count"] || 0
like_count = object.data["like_count"] || 0 hashtags = Object.hashtags(object)
announcement_count = object.data["announcement_count"] || 0 sensitive = object.data["sensitive"] || Enum.member?(hashtags, "nsfw")
hashtags = Object.hashtags(object) tags = Object.tags(object)
sensitive = object.data["sensitive"] || Enum.member?(hashtags, "nsfw")
tags = Object.tags(object) tag_mentions =
tags
|> Enum.filter(fn tag -> is_map(tag) and tag["type"] == "Mention" end)
|> Enum.map(fn tag -> tag["href"] end)
tag_mentions = mentions =
tags (object.data["to"] ++ tag_mentions)
|> Enum.filter(fn tag -> is_map(tag) and tag["type"] == "Mention" end) |> Enum.uniq()
|> Enum.map(fn tag -> tag["href"] end) |> Enum.map(fn
Pleroma.Constants.as_public() -> nil
^user_follower_address -> nil
ap_id -> User.get_cached_by_ap_id(ap_id)
end)
|> Enum.filter(& &1)
|> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
mentions = favorited = opts[:for] && opts[:for].ap_id in (object.data["likes"] || [])
(object.data["to"] ++ tag_mentions)
|> Enum.uniq()
|> Enum.map(fn
Pleroma.Constants.as_public() -> nil
^user_follower_address -> nil
ap_id -> User.get_cached_by_ap_id(ap_id)
end)
|> Enum.filter(& &1)
|> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
favorited = opts[:for] && opts[:for].ap_id in (object.data["likes"] || []) bookmarked = Activity.get_bookmark(activity, opts[:for]) != nil
bookmarked = Activity.get_bookmark(activity, opts[:for]) != nil client_posted_this_activity = opts[:for] && user.id == opts[:for].id
client_posted_this_activity = opts[:for] && user.id == opts[:for].id expires_at =
with true <- client_posted_this_activity,
%Oban.Job{scheduled_at: scheduled_at} <-
Pleroma.Workers.PurgeExpiredActivity.get_expiration(activity.id) do
scheduled_at
else
_ -> nil
end
expires_at = thread_muted? =
with true <- client_posted_this_activity, cond do
%Oban.Job{scheduled_at: scheduled_at} <- is_nil(opts[:for]) -> false
Pleroma.Workers.PurgeExpiredActivity.get_expiration(activity.id) do is_boolean(activity.thread_muted?) -> activity.thread_muted?
scheduled_at true -> CommonAPI.thread_muted?(opts[:for], activity)
else end
_ -> nil
end
thread_muted? = attachment_data = object.data["attachment"] || []
cond do attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
is_nil(opts[:for]) -> false
is_boolean(activity.thread_muted?) -> activity.thread_muted?
true -> CommonAPI.thread_muted?(opts[:for], activity)
end
attachment_data = object.data["attachment"] || [] created_at = Utils.to_masto_date(object.data["published"])
attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
created_at = Utils.to_masto_date(object.data["published"]) edited_at =
with %{"updated" => updated} <- object.data,
date <- Utils.to_masto_date(updated),
true <- date != "" do
date
else
_ ->
nil
end
edited_at = reply_to = get_reply_to(activity, opts)
with %{"updated" => updated} <- object.data,
date <- Utils.to_masto_date(updated),
true <- date != "" do
date
else
_ ->
nil
end
reply_to = get_reply_to(activity, opts) reply_to_user = reply_to && CommonAPI.get_user(reply_to.data["actor"])
reply_to_user = reply_to && CommonAPI.get_user(reply_to.data["actor"]) history_len =
1 +
(Object.Updater.history_for(object.data)
|> Map.get("orderedItems")
|> length())
history_len = # See render("history.json", ...) for more details
1 + # Here the implicit index of the current content is 0
(Object.Updater.history_for(object.data) chrono_order = history_len - 1
|> Map.get("orderedItems")
|> length())
# See render("history.json", ...) for more details content =
# Here the implicit index of the current content is 0 object
chrono_order = history_len - 1 |> render_content()
content = content_html =
object content
|> render_content() |> Activity.HTML.get_cached_scrubbed_html_for_activity(
User.html_filter_policy(opts[:for]),
content_html = activity,
content "mastoapi:content:#{chrono_order}"
|> Activity.HTML.get_cached_scrubbed_html_for_activity(
User.html_filter_policy(opts[:for]),
activity,
"mastoapi:content:#{chrono_order}"
)
content_plaintext =
content
|> Activity.HTML.get_cached_stripped_html_for_activity(
activity,
"mastoapi:content:#{chrono_order}"
)
summary = object.data["summary"] || ""
card = render("card.json", Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity))
url =
if user.local do
Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity)
else
object.data["url"] || object.data["external_url"] || object.data["id"]
end
direct_conversation_id =
with {_, nil} <- {:direct_conversation_id, opts[:direct_conversation_id]},
{_, true} <- {:include_id, opts[:with_direct_conversation_id]},
{_, %User{} = for_user} <- {:for_user, opts[:for]} do
Activity.direct_conversation_id(activity, for_user)
else
{:direct_conversation_id, participation_id} when is_integer(participation_id) ->
participation_id
_e ->
nil
end
emoji_reactions =
object.data
|> Map.get("reactions", [])
|> EmojiReactionController.filter_allowed_users(
opts[:for],
Map.get(opts, :with_muted, false)
)
|> Stream.map(fn {emoji, users, url} ->
build_emoji_map(emoji, users, url, opts[:for])
end)
|> Enum.to_list()
# Status muted state (would do 1 request per status unless user mutes are preloaded)
muted =
thread_muted? ||
UserRelationship.exists?(
get_in(opts, [:relationships, :user_relationships]),
:mute,
opts[:for],
user,
fn for_user, user -> User.mutes?(for_user, user) end
) )
{pinned?, pinned_at} = pin_data(object, user) content_plaintext =
content
|> Activity.HTML.get_cached_stripped_html_for_activity(
activity,
"mastoapi:content:#{chrono_order}"
)
quote = Activity.get_quoted_activity_from_object(object) summary = object.data["summary"] || ""
%{ card = render("card.json", Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity))
id: to_string(activity.id),
uri: object.data["id"], url =
url: url, if user.local do
account: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity)
AccountView.render("show.json", %{ else
user: user, object.data["url"] || object.data["external_url"] || object.data["id"]
for: opts[:for] end
}),
in_reply_to_id: reply_to && to_string(reply_to.id), direct_conversation_id =
in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id), with {_, nil} <- {:direct_conversation_id, opts[:direct_conversation_id]},
reblog: nil, {_, true} <- {:include_id, opts[:with_direct_conversation_id]},
card: card, {_, %User{} = for_user} <- {:for_user, opts[:for]} do
content: content_html, Activity.direct_conversation_id(activity, for_user)
text: opts[:with_source] && get_source_text(object.data["source"]), else
created_at: created_at, {:direct_conversation_id, participation_id} when is_integer(participation_id) ->
edited_at: edited_at, participation_id
reblogs_count: announcement_count,
replies_count: object.data["repliesCount"] || 0, _e ->
favourites_count: like_count, nil
reblogged: reblogged?(activity, opts[:for]), end
favourited: present?(favorited),
bookmarked: present?(bookmarked), emoji_reactions =
muted: muted, object.data
pinned: pinned?, |> Map.get("reactions", [])
sensitive: sensitive, |> EmojiReactionController.filter_allowed_users(
spoiler_text: summary, opts[:for],
visibility: get_visibility(object), Map.get(opts, :with_muted, false)
media_attachments: attachments, )
poll: render(PollView, "show.json", object: object, for: opts[:for]), |> Stream.map(fn {emoji, users, url} ->
mentions: mentions, build_emoji_map(emoji, users, url, opts[:for])
tags: build_tags(tags), end)
application: build_application(object.data["generator"]), |> Enum.to_list()
language: nil,
emojis: build_emojis(object.data["emoji"]), # Status muted state (would do 1 request per status unless user mutes are preloaded)
quote_id: if(quote, do: quote.id, else: nil), muted =
quote: maybe_render_quote(quote, opts), thread_muted? ||
emoji_reactions: emoji_reactions, UserRelationship.exists?(
pleroma: %{ get_in(opts, [:relationships, :user_relationships]),
local: activity.local, :mute,
conversation_id: get_context_id(activity), opts[:for],
context: object.data["context"], user,
in_reply_to_account_acct: reply_to_user && reply_to_user.nickname, fn for_user, user -> User.mutes?(for_user, user) end
content: %{"text/plain" => content_plaintext}, )
spoiler_text: %{"text/plain" => summary},
expires_at: expires_at, {pinned?, pinned_at} = pin_data(object, user)
direct_conversation_id: direct_conversation_id,
thread_muted: thread_muted?, quote = Activity.get_quoted_activity_from_object(object)
%{
id: to_string(activity.id),
uri: object.data["id"],
url: url,
account:
AccountView.render("show.json", %{
user: user,
for: opts[:for]
}),
in_reply_to_id: reply_to && to_string(reply_to.id),
in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
reblog: nil,
card: card,
content: content_html,
text: opts[:with_source] && get_source_text(object.data["source"]),
created_at: created_at,
edited_at: edited_at,
reblogs_count: announcement_count,
replies_count: object.data["repliesCount"] || 0,
favourites_count: like_count,
reblogged: reblogged?(activity, opts[:for]),
favourited: present?(favorited),
bookmarked: present?(bookmarked),
muted: muted,
pinned: pinned?,
sensitive: sensitive,
spoiler_text: summary,
visibility: get_visibility(object),
media_attachments: attachments,
poll: render(PollView, "show.json", object: object, for: opts[:for]),
mentions: mentions,
tags: build_tags(tags),
application: build_application(object.data["generator"]),
language: nil,
emojis: build_emojis(object.data["emoji"]),
quote_id: if(quote, do: quote.id, else: nil),
quote: maybe_render_quote(quote, opts),
emoji_reactions: emoji_reactions, emoji_reactions: emoji_reactions,
parent_visible: visible_for_user?(reply_to, opts[:for]), pleroma: %{
pinned_at: pinned_at local: activity.local,
}, conversation_id: get_context_id(activity),
akkoma: %{ context: object.data["context"],
source: object.data["source"] in_reply_to_account_acct: reply_to_user && reply_to_user.nickname,
content: %{"text/plain" => content_plaintext},
spoiler_text: %{"text/plain" => summary},
expires_at: expires_at,
direct_conversation_id: direct_conversation_id,
thread_muted: thread_muted?,
emoji_reactions: emoji_reactions,
parent_visible: visible_for_user?(reply_to, opts[:for]),
pinned_at: pinned_at
},
akkoma: %{
source: object.data["source"]
}
} }
} else
nil -> nil
end
end end
def render("show.json", _) do def render("show.json", _) do

View file

@ -3,7 +3,7 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.AdminAPI.ReportViewTest do defmodule Pleroma.Web.AdminAPI.ReportViewTest do
use Pleroma.DataCase, async: true use Pleroma.DataCase, async: false
import Pleroma.Factory import Pleroma.Factory