From: Ivan Tashkinov Date: Thu, 22 Aug 2019 17:59:58 +0000 (+0300) Subject: [#1149] Merge remote-tracking branch 'remotes/upstream/develop' into 1149-oban-job... X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=256ff09aa8068166a9f83e3159f060fd4598af76;p=akkoma [#1149] Merge remote-tracking branch 'remotes/upstream/develop' into 1149-oban-job-queue # Conflicts: # lib/pleroma/application.ex # lib/pleroma/scheduled_activity_worker.ex # lib/pleroma/web/federator/retry_queue.ex # lib/pleroma/web/oauth/token/clean_worker.ex # test/user_test.exs # test/web/federator_test.exs --- 256ff09aa8068166a9f83e3159f060fd4598af76 diff --cc lib/pleroma/application.ex index 7cf60f44a,25e56b9e2..2e2922d28 --- a/lib/pleroma/application.ex +++ b/lib/pleroma/application.ex @@@ -29,108 -30,18 +30,21 @@@ defmodule Pleroma.Application d # Define workers and child supervisors to be supervised children = [ - # Start the Ecto repository - %{id: Pleroma.Repo, start: {Pleroma.Repo, :start_link, []}, type: :supervisor}, - %{id: Pleroma.Config.TransferTask, start: {Pleroma.Config.TransferTask, :start_link, []}}, - %{id: Pleroma.Emoji, start: {Pleroma.Emoji, :start_link, []}}, - %{id: Pleroma.Captcha, start: {Pleroma.Captcha, :start_link, []}}, - %{ - id: :cachex_used_captcha_cache, - start: - {Cachex, :start_link, - [ - :used_captcha_cache, - [ - ttl_interval: - :timer.seconds(Pleroma.Config.get!([Pleroma.Captcha, :seconds_valid])) - ] - ]} - }, - %{ - id: :cachex_user, - start: - {Cachex, :start_link, - [ - :user_cache, - [ - default_ttl: 25_000, - ttl_interval: 1000, - limit: 2500 - ] - ]} - }, - %{ - id: :cachex_object, - start: - {Cachex, :start_link, - [ - :object_cache, - [ - default_ttl: 25_000, - ttl_interval: 1000, - limit: 2500 - ] - ]} - }, - %{ - id: :cachex_rich_media, - start: - {Cachex, :start_link, - [ - :rich_media_cache, - [ - default_ttl: :timer.minutes(120), - limit: 5000 - ] - ]} - }, - %{ - id: :cachex_scrubber, - start: - {Cachex, :start_link, - [ - :scrubber_cache, - [ - limit: 2500 - ] - ]} - }, - %{ - id: :cachex_idem, - start: - {Cachex, :start_link, - [ - :idempotency_cache, - [ - expiration: - expiration( - default: :timer.seconds(6 * 60 * 60), - interval: :timer.seconds(60) - ), - limit: 2500 - ] - ]} - }, - %{id: Pleroma.FlakeId, start: {Pleroma.FlakeId, :start_link, []}}, - %{ - id: Pleroma.ScheduledActivityWorker, - start: {Pleroma.ScheduledActivityWorker, :start_link, []} - } + Pleroma.Repo, + Pleroma.Config.TransferTask, + Pleroma.Emoji, + Pleroma.Captcha, + Pleroma.FlakeId, + Pleroma.ScheduledActivityWorker ] ++ + cachex_children() ++ hackney_pool_children() ++ [ - Pleroma.Web.Federator.RetryQueue, + Pleroma.Stats, + %{ + id: Oban, + start: {Oban, :start_link, [Application.get_env(:pleroma, Oban)]} + }, - %{ - id: Pleroma.Web.OAuth.Token.CleanWorker, - start: {Pleroma.Web.OAuth.Token.CleanWorker, :start_link, []} - }, - %{ - id: Pleroma.Stats, - start: {Pleroma.Stats, :start_link, []} - }, %{ id: :web_push_init, start: {Task, :start_link, [&Pleroma.Web.Push.init/0]}, diff --cc lib/pleroma/digest_email_worker.ex index 6e44cc955,5644d6a67..ffc48bfab --- a/lib/pleroma/digest_email_worker.ex +++ b/lib/pleroma/digest_email_worker.ex @@@ -1,10 -1,11 +1,14 @@@ + # Pleroma: A lightweight social networking server + # Copyright © 2017-2019 Pleroma Authors + # SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.DigestEmailWorker do + alias Pleroma.Repo + alias Pleroma.Workers.Mailer, as: MailerWorker + import Ecto.Query - @queue_name :digest_emails + defdelegate worker_args(queue), to: Pleroma.Workers.Helper def perform do config = Pleroma.Config.get([:email_notifications, :digest]) diff --cc lib/pleroma/scheduled_activity_worker.ex index cabea51ca,8578cab5e..a01fb4fcb --- a/lib/pleroma/scheduled_activity_worker.ex +++ b/lib/pleroma/scheduled_activity_worker.ex @@@ -18,9 -16,7 +18,9 @@@ defmodule Pleroma.ScheduledActivityWork @schedule_interval :timer.minutes(1) + defdelegate worker_args(queue), to: Pleroma.Workers.Helper + - def start_link do + def start_link(_) do GenServer.start_link(__MODULE__, nil) end diff --cc lib/pleroma/web/oauth/token/clean_worker.ex index c0c9c3653,f50098302..943e73289 --- a/lib/pleroma/web/oauth/token/clean_worker.ex +++ b/lib/pleroma/web/oauth/token/clean_worker.ex @@@ -6,30 -6,23 +6,27 @@@ defmodule Pleroma.Web.OAuth.Token.Clean @moduledoc """ The module represents functions to clean an expired oauth tokens. """ + use GenServer + + @ten_seconds 10_000 + @one_day 86_400_000 - # 10 seconds - @start_interval 10_000 @interval Pleroma.Config.get( - # 24 hours [:oauth2, :clean_expired_tokens_interval], - 86_400_000 + @one_day ) + alias Pleroma.Repo alias Pleroma.Web.OAuth.Token + alias Pleroma.Workers.BackgroundWorker + + defdelegate worker_args(queue), to: Pleroma.Workers.Helper - def start_link, do: GenServer.start_link(__MODULE__, nil) + def start_link(_), do: GenServer.start_link(__MODULE__, %{}) def init(_) do - if Pleroma.Config.get([:oauth2, :clean_expired_tokens], false) do - Process.send_after(self(), :perform, @start_interval) - {:ok, nil} - else - :ignore - end + Process.send_after(self(), :perform, @ten_seconds) + {:ok, nil} end @doc false @@@ -41,7 -32,4 +38,6 @@@ Process.send_after(self(), :perform, @interval) {:noreply, state} end + - # Job Worker Callbacks + def perform(:clean), do: Token.delete_expired_tokens() end diff --cc test/user_test.exs index 2b955ced0,b70133a94..3185cc1fb --- a/test/user_test.exs +++ b/test/user_test.exs @@@ -13,9 -12,9 +13,10 @@@ defmodule Pleroma.UserTest d alias Pleroma.Web.CommonAPI use Pleroma.DataCase + use Oban.Testing, repo: Pleroma.Repo - import Pleroma.Factory + import Mock + import Pleroma.Factory setup_all do Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end) @@@ -1006,6 -1047,13 +1053,15 @@@ refute Activity.get_by_id(activity.id) end + test "it deletes deactivated user" do + {:ok, user} = insert(:user, info: %{deactivated: true}) |> User.set_cache() + - assert {:ok, _} = User.delete(user) ++ {:ok, job} = User.delete(user) ++ {:ok, _user} = ObanHelpers.perform(job) ++ + refute User.get_by_id(user.id) + end + test "it deletes a user, all follow relationships and all activities", %{user: user} do follower = insert(:user) {:ok, follower} = User.follow(follower, user) @@@ -1051,21 -1100,13 +1109,19 @@@ {:ok, follower} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin") {:ok, _} = User.follow(follower, user) - {:ok, _user} = User.delete(user) - - assert called( - Pleroma.Web.ActivityPub.Publisher.publish_one(%{ - inbox: "http://mastodon.example.org/inbox" - }) + {:ok, job} = User.delete(user) + {:ok, _user} = ObanHelpers.perform(job) + + assert ObanHelpers.member?( + %{ + "op" => "publish_one", + "params" => %{ + "inbox" => "http://mastodon.example.org/inbox", + "id" => "pleroma:fakeid" + } + }, + all_enqueued(worker: Pleroma.Workers.Publisher) ) - - Pleroma.Config.put(config_path, initial_setting) end end diff --cc test/web/activity_pub/activity_pub_test.exs index d723f331f,f20cd2840..1515f4eb6 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@@ -538,6 -538,29 +538,29 @@@ defmodule Pleroma.Web.ActivityPub.Activ assert Enum.member?(activities, activity_one) end + test "doesn't return thread muted activities" do + user = insert(:user) + _activity_one = insert(:note_activity) + note_two = insert(:note, data: %{"context" => "suya.."}) + activity_two = insert(:note_activity, note: note_two) + + {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two) + + assert [_activity_one] = ActivityPub.fetch_activities([], %{"muting_user" => user}) + end + + test "returns thread muted activities when with_muted is set" do + user = insert(:user) + _activity_one = insert(:note_activity) + note_two = insert(:note, data: %{"context" => "suya.."}) + activity_two = insert(:note_activity, note: note_two) + - {:ok, activity_two} = CommonAPI.add_mute(user, activity_two) ++ {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two) + + assert [_activity_two, _activity_one] = + ActivityPub.fetch_activities([], %{"muting_user" => user, "with_muted" => true}) + end + test "does include announces on request" do activity_three = insert(:note_activity) user = insert(:user) diff --cc test/web/digest_email_worker_test.exs index 000000000,15002330f..5dfd920fa mode 000000,100644..100644 --- a/test/web/digest_email_worker_test.exs +++ b/test/web/digest_email_worker_test.exs @@@ -1,0 -1,31 +1,35 @@@ + # Pleroma: A lightweight social networking server + # Copyright © 2017-2019 Pleroma Authors + # SPDX-License-Identifier: AGPL-3.0-only + + defmodule Pleroma.DigestEmailWorkerTest do + use Pleroma.DataCase + import Pleroma.Factory + + alias Pleroma.DigestEmailWorker ++ alias Pleroma.Tests.ObanHelpers + alias Pleroma.User + alias Pleroma.Web.CommonAPI + + test "it sends digest emails" do + user = insert(:user) + + date = + Timex.now() + |> Timex.shift(days: -10) + |> Timex.to_naive_datetime() + + user2 = insert(:user, last_digest_emailed_at: date) + User.switch_email_notifications(user2, "digest", true) + CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}!"}) + + DigestEmailWorker.perform() ++ ObanHelpers.perform_all() ++ # Performing job(s) enqueued at previous step ++ ObanHelpers.perform_all() + + assert_received {:email, email} + assert email.to == [{user2.name, user2.email}] + assert email.subject == "Your digest from #{Pleroma.Config.get(:instance)[:name]}" + end + end diff --cc test/web/federator_test.exs index 9ca341b6d,09e54533f..5724672fd --- a/test/web/federator_test.exs +++ b/test/web/federator_test.exs @@@ -21,12 -16,23 +21,14 @@@ defmodule Pleroma.Web.FederatorTest d :ok end + clear_config_all([:instance, :federating]) do + Pleroma.Config.put([:instance, :federating], true) + end + + clear_config([:instance, :allow_relay]) + clear_config([:instance, :rewrite_policy]) + clear_config([:mrf_keyword]) + - describe "Publisher.perform" do - test "call `perform` with unknown task" do - assert { - :error, - "Don't know what to do with this" - } = Pleroma.Web.Federator.Publisher.perform("test", :ok, :ok) - end - end - describe "Publish an activity" do setup do user = insert(:user) @@@ -249,11 -242,7 +251,8 @@@ File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!() - assert Federator.incoming_ap_doc(params) == :error + assert {:ok, job} = Federator.incoming_ap_doc(params) + assert :error = ObanHelpers.perform(job) - - Pleroma.Config.put([:instance, :rewrite_policy], policies) - Pleroma.Config.put(:mrf_keyword, mrf_keyword_policy) end end end