Merge branch 'develop' into fix/majic-nits
This commit is contained in:
commit
28d2917c3a
120 changed files with 707 additions and 142 deletions
|
|
@ -2248,4 +2248,43 @@ defmodule Pleroma.UserTest do
|
|||
user = insert(:user, ap_id: "https://lain.com/users/lain", nickname: "lain")
|
||||
assert User.get_host(user) == "lain.com"
|
||||
end
|
||||
|
||||
test "update_last_active_at/1" do
|
||||
user = insert(:user)
|
||||
assert is_nil(user.last_active_at)
|
||||
|
||||
test_started_at = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
|
||||
|
||||
assert {:ok, user} = User.update_last_active_at(user)
|
||||
|
||||
assert user.last_active_at >= test_started_at
|
||||
assert user.last_active_at <= NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
|
||||
|
||||
last_active_at =
|
||||
NaiveDateTime.utc_now()
|
||||
|> NaiveDateTime.add(-:timer.hours(24), :millisecond)
|
||||
|> NaiveDateTime.truncate(:second)
|
||||
|
||||
assert {:ok, user} =
|
||||
user
|
||||
|> cast(%{last_active_at: last_active_at}, [:last_active_at])
|
||||
|> User.update_and_set_cache()
|
||||
|
||||
assert user.last_active_at == last_active_at
|
||||
assert {:ok, user} = User.update_last_active_at(user)
|
||||
assert user.last_active_at >= test_started_at
|
||||
assert user.last_active_at <= NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
|
||||
end
|
||||
|
||||
test "active_user_count/1" do
|
||||
insert(:user)
|
||||
insert(:user, %{local: false})
|
||||
insert(:user, %{last_active_at: Timex.shift(NaiveDateTime.utc_now(), weeks: -5)})
|
||||
insert(:user, %{last_active_at: Timex.shift(NaiveDateTime.utc_now(), weeks: -3)})
|
||||
insert(:user, %{last_active_at: NaiveDateTime.utc_now()})
|
||||
|
||||
assert User.active_user_count() == 2
|
||||
assert User.active_user_count(6) == 3
|
||||
assert User.active_user_count(1) == 1
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -47,6 +47,7 @@ defmodule Pleroma.Web.MastodonAPI.InstanceControllerTest do
|
|||
assert result["pleroma"]["metadata"]["federation"]
|
||||
assert result["pleroma"]["metadata"]["fields_limits"]
|
||||
assert result["pleroma"]["vapid_public_key"]
|
||||
assert result["pleroma"]["stats"]["mau"] == 0
|
||||
|
||||
assert email == from_config_email
|
||||
assert thumbnail == from_config_thumbnail
|
||||
|
|
|
|||
|
|
@ -263,6 +263,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
|
|||
|
||||
fake_conn =
|
||||
conn
|
||||
|> assign(:user, refresh_record(conn.assigns.user))
|
||||
|> put_req_header("content-type", "application/json")
|
||||
|> post("/api/v1/statuses", %{
|
||||
"status" =>
|
||||
|
|
|
|||
|
|
@ -90,6 +90,65 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
|
|||
}
|
||||
] = result
|
||||
end
|
||||
|
||||
test "filtering", %{conn: conn, user: user} do
|
||||
local_user = insert(:user)
|
||||
{:ok, user, local_user} = User.follow(user, local_user)
|
||||
{:ok, local_activity} = CommonAPI.post(local_user, %{status: "Status"})
|
||||
with_media = create_with_media_activity(local_user)
|
||||
|
||||
remote_user = insert(:user, local: false)
|
||||
{:ok, _user, remote_user} = User.follow(user, remote_user)
|
||||
remote_activity = create_remote_activity(remote_user)
|
||||
|
||||
without_filter_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/home")
|
||||
|> json_response_and_validate_schema(200)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
assert local_activity.id in without_filter_ids
|
||||
assert remote_activity.id in without_filter_ids
|
||||
assert with_media.id in without_filter_ids
|
||||
|
||||
only_local_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/home?local=true")
|
||||
|> json_response_and_validate_schema(200)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
assert local_activity.id in only_local_ids
|
||||
refute remote_activity.id in only_local_ids
|
||||
assert with_media.id in only_local_ids
|
||||
|
||||
only_local_media_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/home?local=true&only_media=true")
|
||||
|> json_response_and_validate_schema(200)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
refute local_activity.id in only_local_media_ids
|
||||
refute remote_activity.id in only_local_media_ids
|
||||
assert with_media.id in only_local_media_ids
|
||||
|
||||
remote_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/home?remote=true")
|
||||
|> json_response_and_validate_schema(200)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
refute local_activity.id in remote_ids
|
||||
assert remote_activity.id in remote_ids
|
||||
refute with_media.id in remote_ids
|
||||
|
||||
assert conn
|
||||
|> get("/api/v1/timelines/home?remote=true&only_media=true")
|
||||
|> json_response_and_validate_schema(200) == []
|
||||
|
||||
assert conn
|
||||
|> get("/api/v1/timelines/home?remote=true&local=true")
|
||||
|> json_response_and_validate_schema(200) == []
|
||||
end
|
||||
end
|
||||
|
||||
describe "public" do
|
||||
|
|
@ -98,27 +157,80 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
|
|||
user = insert(:user)
|
||||
|
||||
{:ok, activity} = CommonAPI.post(user, %{status: "test"})
|
||||
with_media = create_with_media_activity(user)
|
||||
|
||||
_activity = insert(:note_activity, local: false)
|
||||
remote = insert(:note_activity, local: false)
|
||||
|
||||
conn = get(conn, "/api/v1/timelines/public?local=False")
|
||||
assert conn
|
||||
|> get("/api/v1/timelines/public?local=False")
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|> length == 3
|
||||
|
||||
assert length(json_response_and_validate_schema(conn, :ok)) == 2
|
||||
local_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/public?local=True")
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
conn = get(build_conn(), "/api/v1/timelines/public?local=True")
|
||||
assert activity.id in local_ids
|
||||
assert with_media.id in local_ids
|
||||
refute remote.id in local_ids
|
||||
|
||||
assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
|
||||
local_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/public?local=True")
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
conn = get(build_conn(), "/api/v1/timelines/public?local=1")
|
||||
assert activity.id in local_ids
|
||||
assert with_media.id in local_ids
|
||||
refute remote.id in local_ids
|
||||
|
||||
assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok)
|
||||
local_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/public?local=True&only_media=true")
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
refute activity.id in local_ids
|
||||
assert with_media.id in local_ids
|
||||
refute remote.id in local_ids
|
||||
|
||||
local_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/public?local=1")
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
assert activity.id in local_ids
|
||||
assert with_media.id in local_ids
|
||||
refute remote.id in local_ids
|
||||
|
||||
remote_id = remote.id
|
||||
|
||||
assert [%{"id" => ^remote_id}] =
|
||||
conn
|
||||
|> get("/api/v1/timelines/public?remote=true")
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|
||||
with_media_id = with_media.id
|
||||
|
||||
assert [%{"id" => ^with_media_id}] =
|
||||
conn
|
||||
|> get("/api/v1/timelines/public?only_media=true")
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|
||||
assert conn
|
||||
|> get("/api/v1/timelines/public?remote=true&only_media=true")
|
||||
|> json_response_and_validate_schema(:ok) == []
|
||||
|
||||
# does not contain repeats
|
||||
{:ok, _} = CommonAPI.repeat(activity.id, user)
|
||||
|
||||
conn = get(build_conn(), "/api/v1/timelines/public?local=true")
|
||||
|
||||
assert [_] = json_response_and_validate_schema(conn, :ok)
|
||||
assert [_, _] =
|
||||
conn
|
||||
|> get("/api/v1/timelines/public?local=true")
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
end
|
||||
|
||||
test "the public timeline includes only public statuses for an authenticated user" do
|
||||
|
|
@ -544,6 +656,77 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
|
|||
}
|
||||
] = result
|
||||
end
|
||||
|
||||
test "filtering", %{user: user, conn: conn} do
|
||||
{:ok, list} = Pleroma.List.create("name", user)
|
||||
|
||||
local_user = insert(:user)
|
||||
{:ok, local_activity} = CommonAPI.post(local_user, %{status: "Marisa is stupid."})
|
||||
with_media = create_with_media_activity(local_user)
|
||||
{:ok, list} = Pleroma.List.follow(list, local_user)
|
||||
|
||||
remote_user = insert(:user, local: false)
|
||||
remote_activity = create_remote_activity(remote_user)
|
||||
{:ok, list} = Pleroma.List.follow(list, remote_user)
|
||||
|
||||
all_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/list/#{list.id}")
|
||||
|> json_response_and_validate_schema(200)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
assert local_activity.id in all_ids
|
||||
assert with_media.id in all_ids
|
||||
assert remote_activity.id in all_ids
|
||||
|
||||
only_local_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/list/#{list.id}?local=true")
|
||||
|> json_response_and_validate_schema(200)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
assert local_activity.id in only_local_ids
|
||||
assert with_media.id in only_local_ids
|
||||
refute remote_activity.id in only_local_ids
|
||||
|
||||
only_local_media_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/list/#{list.id}?local=true&only_media=true")
|
||||
|> json_response_and_validate_schema(200)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
refute local_activity.id in only_local_media_ids
|
||||
assert with_media.id in only_local_media_ids
|
||||
refute remote_activity.id in only_local_media_ids
|
||||
|
||||
remote_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/list/#{list.id}?remote=true")
|
||||
|> json_response_and_validate_schema(200)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
refute local_activity.id in remote_ids
|
||||
refute with_media.id in remote_ids
|
||||
assert remote_activity.id in remote_ids
|
||||
|
||||
assert conn
|
||||
|> get("/api/v1/timelines/list/#{list.id}?remote=true&only_media=true")
|
||||
|> json_response_and_validate_schema(200) == []
|
||||
|
||||
only_media_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/list/#{list.id}?only_media=true")
|
||||
|> json_response_and_validate_schema(200)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
refute local_activity.id in only_media_ids
|
||||
assert with_media.id in only_media_ids
|
||||
refute remote_activity.id in only_media_ids
|
||||
|
||||
assert conn
|
||||
|> get("/api/v1/timelines/list/#{list.id}?only_media=true&local=true&remote=true")
|
||||
|> json_response_and_validate_schema(200) == []
|
||||
end
|
||||
end
|
||||
|
||||
describe "hashtag" do
|
||||
|
|
@ -554,19 +737,85 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
|
|||
following = insert(:user)
|
||||
|
||||
{:ok, activity} = CommonAPI.post(following, %{status: "test #2hu"})
|
||||
with_media = create_with_media_activity(following)
|
||||
|
||||
nconn = get(conn, "/api/v1/timelines/tag/2hu")
|
||||
remote = insert(:user, local: false)
|
||||
remote_activity = create_remote_activity(remote)
|
||||
|
||||
assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
|
||||
all_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/tag/2hu")
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
assert id == to_string(activity.id)
|
||||
assert activity.id in all_ids
|
||||
assert with_media.id in all_ids
|
||||
assert remote_activity.id in all_ids
|
||||
|
||||
# works for different capitalization too
|
||||
nconn = get(conn, "/api/v1/timelines/tag/2HU")
|
||||
all_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/tag/2HU")
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok)
|
||||
assert activity.id in all_ids
|
||||
assert with_media.id in all_ids
|
||||
assert remote_activity.id in all_ids
|
||||
|
||||
assert id == to_string(activity.id)
|
||||
local_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/tag/2hu?local=true")
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
assert activity.id in local_ids
|
||||
assert with_media.id in local_ids
|
||||
refute remote_activity.id in local_ids
|
||||
|
||||
remote_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/tag/2hu?remote=true")
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
refute activity.id in remote_ids
|
||||
refute with_media.id in remote_ids
|
||||
assert remote_activity.id in remote_ids
|
||||
|
||||
media_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/tag/2hu?only_media=true")
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
refute activity.id in media_ids
|
||||
assert with_media.id in media_ids
|
||||
refute remote_activity.id in media_ids
|
||||
|
||||
media_local_ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/tag/2hu?only_media=true&local=true")
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
refute activity.id in media_local_ids
|
||||
assert with_media.id in media_local_ids
|
||||
refute remote_activity.id in media_local_ids
|
||||
|
||||
ids =
|
||||
conn
|
||||
|> get("/api/v1/timelines/tag/2hu?only_media=true&local=true&remote=true")
|
||||
|> json_response_and_validate_schema(:ok)
|
||||
|> Enum.map(& &1["id"])
|
||||
|
||||
refute activity.id in ids
|
||||
refute with_media.id in ids
|
||||
refute remote_activity.id in ids
|
||||
|
||||
assert conn
|
||||
|> get("/api/v1/timelines/tag/2hu?only_media=true&remote=true")
|
||||
|> json_response_and_validate_schema(:ok) == []
|
||||
end
|
||||
|
||||
test "multi-hashtag timeline", %{conn: conn} do
|
||||
|
|
@ -726,4 +975,37 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
|
|||
ensure_authenticated_access(base_uri)
|
||||
end
|
||||
end
|
||||
|
||||
defp create_remote_activity(user) do
|
||||
obj =
|
||||
insert(:note, %{
|
||||
data: %{
|
||||
"to" => [
|
||||
"https://www.w3.org/ns/activitystreams#Public",
|
||||
User.ap_followers(user)
|
||||
]
|
||||
},
|
||||
user: user
|
||||
})
|
||||
|
||||
insert(:note_activity, %{
|
||||
note: obj,
|
||||
recipients: [
|
||||
"https://www.w3.org/ns/activitystreams#Public",
|
||||
User.ap_followers(user)
|
||||
],
|
||||
user: user,
|
||||
local: false
|
||||
})
|
||||
end
|
||||
|
||||
defp create_with_media_activity(user) do
|
||||
obj = insert(:attachment_note, user: user)
|
||||
|
||||
insert(:note_activity, %{
|
||||
note: obj,
|
||||
recipients: ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(user)],
|
||||
user: user
|
||||
})
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -104,7 +104,7 @@ defmodule Pleroma.Web.PleromaAPI.ConversationControllerTest do
|
|||
[participation] = Participation.for_user(user)
|
||||
participation = Repo.preload(participation, :recipients)
|
||||
|
||||
assert user in participation.recipients
|
||||
assert refresh_record(user) in participation.recipients
|
||||
assert other_user in participation.recipients
|
||||
end
|
||||
|
||||
|
|
|
|||
58
test/pleroma/web/plugs/user_tracking_plug_test.exs
Normal file
58
test/pleroma/web/plugs/user_tracking_plug_test.exs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.Plugs.UserTrackingPlugTest do
|
||||
use Pleroma.Web.ConnCase, async: true
|
||||
|
||||
import Pleroma.Factory
|
||||
|
||||
alias Pleroma.Web.Plugs.UserTrackingPlug
|
||||
|
||||
test "updates last_active_at for a new user", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
|
||||
assert is_nil(user.last_active_at)
|
||||
|
||||
test_started_at = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
|
||||
|
||||
%{assigns: %{user: user}} =
|
||||
conn
|
||||
|> assign(:user, user)
|
||||
|> UserTrackingPlug.call(%{})
|
||||
|
||||
assert user.last_active_at >= test_started_at
|
||||
assert user.last_active_at <= NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second)
|
||||
end
|
||||
|
||||
test "doesn't update last_active_at if it was updated recently", %{conn: conn} do
|
||||
last_active_at =
|
||||
NaiveDateTime.utc_now()
|
||||
|> NaiveDateTime.add(-:timer.hours(1), :millisecond)
|
||||
|> NaiveDateTime.truncate(:second)
|
||||
|
||||
user = insert(:user, %{last_active_at: last_active_at})
|
||||
|
||||
%{assigns: %{user: user}} =
|
||||
conn
|
||||
|> assign(:user, user)
|
||||
|> UserTrackingPlug.call(%{})
|
||||
|
||||
assert user.last_active_at == last_active_at
|
||||
end
|
||||
|
||||
test "skips updating last_active_at if user ID is nil", %{conn: conn} do
|
||||
%{assigns: %{user: user}} =
|
||||
conn
|
||||
|> assign(:user, %Pleroma.User{})
|
||||
|> UserTrackingPlug.call(%{})
|
||||
|
||||
assert is_nil(user.last_active_at)
|
||||
end
|
||||
|
||||
test "does nothing if user is not present", %{conn: conn} do
|
||||
%{assigns: assigns} = UserTrackingPlug.call(conn, %{})
|
||||
|
||||
refute Map.has_key?(assigns, :user)
|
||||
end
|
||||
end
|
||||
|
|
@ -104,6 +104,37 @@ defmodule Pleroma.Factory do
|
|||
}
|
||||
end
|
||||
|
||||
def attachment_note_factory(attrs \\ %{}) do
|
||||
user = attrs[:user] || insert(:user)
|
||||
{length, attrs} = Map.pop(attrs, :length, 1)
|
||||
|
||||
data = %{
|
||||
"attachment" =>
|
||||
Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
|
||||
|> Enum.take(length)
|
||||
}
|
||||
|
||||
build(:note, Map.put(attrs, :data, data))
|
||||
end
|
||||
|
||||
defp attachment_data(ap_id, href) do
|
||||
href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
|
||||
|
||||
%{
|
||||
"url" => [
|
||||
%{
|
||||
"href" => href,
|
||||
"type" => "Link",
|
||||
"mediaType" => "image/jpeg"
|
||||
}
|
||||
],
|
||||
"name" => "some name",
|
||||
"type" => "Document",
|
||||
"actor" => ap_id,
|
||||
"mediaType" => "image/jpeg"
|
||||
}
|
||||
end
|
||||
|
||||
def audio_factory(attrs \\ %{}) do
|
||||
text = sequence(:text, &"lain radio episode #{&1}")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue