Merge branch 'resilient-user-view-2' into 'develop'

MastoAPI: Don't break on missing users.

See merge request pleroma/pleroma!675
This commit is contained in:
kaniini 2019-01-17 03:19:55 +00:00
commit ce2efd1ee2
4 changed files with 57 additions and 14 deletions

View file

@ -1040,4 +1040,14 @@ defp local_nickname_regex() do
@strict_local_nickname_regex @strict_local_nickname_regex
end end
end end
def error_user(ap_id) do
%User{
name: ap_id,
ap_id: ap_id,
info: %User.Info{},
nickname: "erroruser@example.com",
inserted_at: NaiveDateTime.utc_now()
}
end
end end

View file

@ -32,6 +32,19 @@ defp get_replied_to_activities(activities) do
end) end)
end end
defp get_user(ap_id) do
cond do
user = User.get_cached_by_ap_id(ap_id) ->
user
user = User.get_by_guessed_nickname(ap_id) ->
user
true ->
User.error_user(ap_id)
end
end
def render("index.json", opts) do def render("index.json", opts) do
replied_to_activities = get_replied_to_activities(opts.activities) replied_to_activities = get_replied_to_activities(opts.activities)
@ -48,7 +61,7 @@ def render(
"status.json", "status.json",
%{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts
) do ) do
user = User.get_cached_by_ap_id(activity.data["actor"]) user = get_user(activity.data["actor"])
created_at = Utils.to_masto_date(activity.data["published"]) created_at = Utils.to_masto_date(activity.data["published"])
reblogged = Activity.get_create_activity_by_object_ap_id(object) reblogged = Activity.get_create_activity_by_object_ap_id(object)
@ -93,7 +106,7 @@ def render(
end end
def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
user = User.get_cached_by_ap_id(activity.data["actor"]) user = get_user(activity.data["actor"])
like_count = object["like_count"] || 0 like_count = object["like_count"] || 0
announcement_count = object["announcement_count"] || 0 announcement_count = object["announcement_count"] || 0
@ -116,7 +129,7 @@ def render("status.json", %{activity: %{data: %{"object" => object}} = activity}
created_at = Utils.to_masto_date(object["published"]) created_at = Utils.to_masto_date(object["published"])
reply_to = get_reply_to(activity, opts) reply_to = get_reply_to(activity, opts)
reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"]) reply_to_user = reply_to && get_user(reply_to.data["actor"])
content = content =
object object

View file

@ -101,20 +101,10 @@ defp get_user(ap_id, opts) do
user user
true -> true ->
error_user(ap_id) User.error_user(ap_id)
end end
end end
defp error_user(ap_id) do
%User{
name: ap_id,
ap_id: ap_id,
info: %User.Info{},
nickname: "erroruser@example.com",
inserted_at: NaiveDateTime.utc_now()
}
end
def render("index.json", opts) do def render("index.json", opts) do
context_ids = collect_context_ids(opts.activities) context_ids = collect_context_ids(opts.activities)
users = collect_users(opts.activities) users = collect_users(opts.activities)

View file

@ -19,6 +19,36 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
:ok :ok
end end
test "returns a temporary ap_id based user for activities missing db users" do
user = insert(:user)
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
Repo.delete(user)
Cachex.clear(:user_cache)
%{account: ms_user} = StatusView.render("status.json", activity: activity)
assert ms_user.acct == "erroruser@example.com"
end
test "tries to get a user by nickname if fetching by ap_id doesn't work" do
user = insert(:user)
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
{:ok, user} =
user
|> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
|> Repo.update()
Cachex.clear(:user_cache)
result = StatusView.render("status.json", activity: activity)
assert result[:account][:id] == to_string(user.id)
end
test "a note with null content" do test "a note with null content" do
note = insert(:note_activity) note = insert(:note_activity)