Fix/1019 correct count remote users

This commit is contained in:
Alexander Strizhakov 2019-07-09 17:36:35 +00:00 committed by kaniini
commit d6b0fce6e9
16 changed files with 564 additions and 7 deletions

View file

@ -0,0 +1,7 @@
{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "http://localhost:4001/users/masto_closed/followers",
"type": "OrderedCollection",
"totalItems": 437,
"first": "http://localhost:4001/users/masto_closed/followers?page=1"
}

View file

@ -0,0 +1,7 @@
{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "http://localhost:4001/users/masto_closed/following",
"type": "OrderedCollection",
"totalItems": 152,
"first": "http://localhost:4001/users/masto_closed/following?page=1"
}

View file

@ -0,0 +1,20 @@
{
"type": "OrderedCollection",
"totalItems": 527,
"id": "http://localhost:4001/users/fuser2/followers",
"first": {
"type": "OrderedCollectionPage",
"totalItems": 527,
"partOf": "http://localhost:4001/users/fuser2/followers",
"orderedItems": [],
"next": "http://localhost:4001/users/fuser2/followers?page=2",
"id": "http://localhost:4001/users/fuser2/followers?page=1"
},
"@context": [
"https://www.w3.org/ns/activitystreams",
"http://localhost:4001/schemas/litepub-0.1.jsonld",
{
"@language": "und"
}
]
}

View file

@ -0,0 +1,20 @@
{
"type": "OrderedCollection",
"totalItems": 267,
"id": "http://localhost:4001/users/fuser2/following",
"first": {
"type": "OrderedCollectionPage",
"totalItems": 267,
"partOf": "http://localhost:4001/users/fuser2/following",
"orderedItems": [],
"next": "http://localhost:4001/users/fuser2/following?page=2",
"id": "http://localhost:4001/users/fuser2/following?page=1"
},
"@context": [
"https://www.w3.org/ns/activitystreams",
"http://localhost:4001/schemas/litepub-0.1.jsonld",
{
"@language": "und"
}
]
}

View file

@ -759,6 +759,54 @@ defmodule HttpRequestMock do
{:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")}}
end
def get("http://localhost:4001/users/masto_closed/followers", _, _, _) do
{:ok,
%Tesla.Env{
status: 200,
body: File.read!("test/fixtures/users_mock/masto_closed_followers.json")
}}
end
def get("http://localhost:4001/users/masto_closed/following", _, _, _) do
{:ok,
%Tesla.Env{
status: 200,
body: File.read!("test/fixtures/users_mock/masto_closed_following.json")
}}
end
def get("http://localhost:4001/users/fuser2/followers", _, _, _) do
{:ok,
%Tesla.Env{
status: 200,
body: File.read!("test/fixtures/users_mock/pleroma_followers.json")
}}
end
def get("http://localhost:4001/users/fuser2/following", _, _, _) do
{:ok,
%Tesla.Env{
status: 200,
body: File.read!("test/fixtures/users_mock/pleroma_following.json")
}}
end
def get("http://domain-with-errors:4001/users/fuser1/followers", _, _, _) do
{:ok,
%Tesla.Env{
status: 504,
body: ""
}}
end
def get("http://domain-with-errors:4001/users/fuser1/following", _, _, _) do
{:ok,
%Tesla.Env{
status: 504,
body: ""
}}
end
def get("http://example.com/ogp-missing-data", _, _, _) do
{:ok,
%Tesla.Env{

View file

@ -0,0 +1,104 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.User.SynchronizationTest do
use Pleroma.DataCase
import Pleroma.Factory
alias Pleroma.User
alias Pleroma.User.Synchronization
setup do
Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
:ok
end
test "update following/followers counters" do
user1 =
insert(:user,
local: false,
ap_id: "http://localhost:4001/users/masto_closed"
)
user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
users = User.external_users()
assert length(users) == 2
{user, %{}} = Synchronization.call(users, %{})
assert user == List.last(users)
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user1)
assert followers == 437
assert following == 152
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user2)
assert followers == 527
assert following == 267
end
test "don't check host if errors exist" do
user1 = insert(:user, local: false, ap_id: "http://domain-with-errors:4001/users/fuser1")
user2 = insert(:user, local: false, ap_id: "http://domain-with-errors:4001/users/fuser2")
users = User.external_users()
assert length(users) == 2
{user, %{"domain-with-errors" => 2}} =
Synchronization.call(users, %{"domain-with-errors" => 2}, max_retries: 2)
assert user == List.last(users)
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user1)
assert followers == 0
assert following == 0
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user2)
assert followers == 0
assert following == 0
end
test "don't check host if errors appeared" do
user1 = insert(:user, local: false, ap_id: "http://domain-with-errors:4001/users/fuser1")
user2 = insert(:user, local: false, ap_id: "http://domain-with-errors:4001/users/fuser2")
users = User.external_users()
assert length(users) == 2
{user, %{"domain-with-errors" => 2}} = Synchronization.call(users, %{}, max_retries: 2)
assert user == List.last(users)
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user1)
assert followers == 0
assert following == 0
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user2)
assert followers == 0
assert following == 0
end
test "other users after error appeared" do
user1 = insert(:user, local: false, ap_id: "http://domain-with-errors:4001/users/fuser1")
user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
users = User.external_users()
assert length(users) == 2
{user, %{"domain-with-errors" => 2}} = Synchronization.call(users, %{}, max_retries: 2)
assert user == List.last(users)
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user1)
assert followers == 0
assert following == 0
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user2)
assert followers == 527
assert following == 267
end
end

View file

@ -0,0 +1,49 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.User.SynchronizationWorkerTest do
use Pleroma.DataCase
import Pleroma.Factory
setup do
Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
config = Pleroma.Config.get([:instance, :external_user_synchronization])
for_update = [enabled: true, interval: 1000]
Pleroma.Config.put([:instance, :external_user_synchronization], for_update)
on_exit(fn ->
Pleroma.Config.put([:instance, :external_user_synchronization], config)
end)
:ok
end
test "sync follow counters" do
user1 =
insert(:user,
local: false,
ap_id: "http://localhost:4001/users/masto_closed"
)
user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
{:ok, _} = Pleroma.User.SynchronizationWorker.start_link()
:timer.sleep(1500)
%{follower_count: followers, following_count: following} =
Pleroma.User.get_cached_user_info(user1)
assert followers == 437
assert following == 152
%{follower_count: followers, following_count: following} =
Pleroma.User.get_cached_user_info(user2)
assert followers == 527
assert following == 267
end
end

View file

@ -1183,4 +1183,121 @@ defmodule Pleroma.UserTest do
assert user_two.ap_id in ap_ids
end
end
describe "sync followers count" do
setup do
user1 = insert(:user, local: false, ap_id: "http://localhost:4001/users/masto_closed")
user2 = insert(:user, local: false, ap_id: "http://localhost:4001/users/fuser2")
insert(:user, local: true)
insert(:user, local: false, info: %{deactivated: true})
{:ok, user1: user1, user2: user2}
end
test "external_users/1 external active users with limit", %{user1: user1, user2: user2} do
[fdb_user1] = User.external_users(limit: 1)
assert fdb_user1.ap_id
assert fdb_user1.ap_id == user1.ap_id
assert fdb_user1.id == user1.id
[fdb_user2] = User.external_users(max_id: fdb_user1.id, limit: 1)
assert fdb_user2.ap_id
assert fdb_user2.ap_id == user2.ap_id
assert fdb_user2.id == user2.id
assert User.external_users(max_id: fdb_user2.id, limit: 1) == []
end
test "sync_follow_counters/1", %{user1: user1, user2: user2} do
{:ok, _pid} = Agent.start_link(fn -> %{} end, name: :domain_errors)
:ok = User.sync_follow_counters()
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user1)
assert followers == 437
assert following == 152
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user2)
assert followers == 527
assert following == 267
Agent.stop(:domain_errors)
end
test "sync_follow_counters/1 in separate batches", %{user1: user1, user2: user2} do
{:ok, _pid} = Agent.start_link(fn -> %{} end, name: :domain_errors)
:ok = User.sync_follow_counters(limit: 1)
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user1)
assert followers == 437
assert following == 152
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user2)
assert followers == 527
assert following == 267
Agent.stop(:domain_errors)
end
test "perform/1 with :sync_follow_counters", %{user1: user1, user2: user2} do
:ok = User.perform(:sync_follow_counters)
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user1)
assert followers == 437
assert following == 152
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user2)
assert followers == 527
assert following == 267
end
end
describe "set_info_cache/2" do
setup do
user = insert(:user)
{:ok, user: user}
end
test "update from args", %{user: user} do
User.set_info_cache(user, %{following_count: 15, follower_count: 18})
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
assert followers == 18
assert following == 15
end
test "without args", %{user: user} do
User.set_info_cache(user, %{})
%{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
assert followers == 0
assert following == 0
end
end
describe "user_info/2" do
setup do
user = insert(:user)
{:ok, user: user}
end
test "update from args", %{user: user} do
%{follower_count: followers, following_count: following} =
User.user_info(user, %{following_count: 15, follower_count: 18})
assert followers == 18
assert following == 15
end
test "without args", %{user: user} do
%{follower_count: followers, following_count: following} = User.user_info(user)
assert followers == 0
assert following == 0
end
end
end