Merge branch 'fav-speedup' into 'develop'
authorlain <lain@soykaf.club>
Wed, 27 May 2020 08:02:00 +0000 (08:02 +0000)
committerlain <lain@soykaf.club>
Wed, 27 May 2020 08:02:00 +0000 (08:02 +0000)
ActivityPub: Change ordering to `nulls last` in favorites query

See merge request pleroma/pleroma!2594

12 files changed:
config/config.exs
lib/pleroma/user.ex
lib/pleroma/web/mastodon_api/controllers/account_controller.ex
lib/pleroma/web/mastodon_api/controllers/status_controller.ex
lib/pleroma/workers/cron/clear_oauth_token_worker.ex
lib/pleroma/workers/cron/digest_emails_worker.ex
lib/pleroma/workers/cron/new_users_digest_worker.ex
lib/pleroma/workers/cron/purge_expired_activities_worker.ex
priv/repo/migrations/20200526144426_add_apps_indexes.exs [new file with mode: 0644]
test/support/factory.ex
test/user_test.exs
test/web/activity_pub/activity_pub_controller_test.exs

index 7385fb6c36c4fd1589d783441b7372ccb3181a2b..d15998715296f48eb58408314c566e326505ab9c 100644 (file)
@@ -274,7 +274,7 @@ config :pleroma, :markup,
 config :pleroma, :frontend_configurations,
   pleroma_fe: %{
     alwaysShowSubjectInput: true,
-    background: "/static/aurora_borealis.jpg",
+    background: "/images/city.jpg",
     collapseMessageWithSubject: false,
     disableChat: false,
     greentext: false,
index d2eeeb479dd07a7fb83b9b0022a421e94da18307..842b28c065db5ed7cb698ae64378288d1b8e03cf 100644 (file)
@@ -1618,12 +1618,19 @@ defmodule Pleroma.User do
   def fetch_by_ap_id(ap_id), do: ActivityPub.make_user_from_ap_id(ap_id)
 
   def get_or_fetch_by_ap_id(ap_id) do
-    user = get_cached_by_ap_id(ap_id)
+    cached_user = get_cached_by_ap_id(ap_id)
 
-    if !is_nil(user) and !needs_update?(user) do
-      {:ok, user}
-    else
-      fetch_by_ap_id(ap_id)
+    maybe_fetched_user = needs_update?(cached_user) && fetch_by_ap_id(ap_id)
+
+    case {cached_user, maybe_fetched_user} do
+      {_, {:ok, %User{} = user}} ->
+        {:ok, user}
+
+      {%User{} = user, _} ->
+        {:ok, user}
+
+      _ ->
+        {:error, :not_found}
     end
   end
 
index 75512442de1c352cda4aeb60fc075566aa8cfb6c..47649d41dff3e7da67771b06728d8867d9091f7c 100644 (file)
@@ -81,7 +81,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
 
   plug(
     RateLimiter,
-    [name: :relation_id_action, params: ["id", "uri"]] when action in @relationship_actions
+    [name: :relation_id_action, params: [:id, :uri]] when action in @relationship_actions
   )
 
   plug(RateLimiter, [name: :relations_actions] when action in @relationship_actions)
index 83d997abdf24ed944944b75dd341fb0a8fd64838..f20157a5f36f47067c9f158ef5dd63a6eb3d39d1 100644 (file)
@@ -84,13 +84,13 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
 
   plug(
     RateLimiter,
-    [name: :status_id_action, bucket_name: "status_id_action:reblog_unreblog", params: ["id"]]
+    [name: :status_id_action, bucket_name: "status_id_action:reblog_unreblog", params: [:id]]
     when action in ~w(reblog unreblog)a
   )
 
   plug(
     RateLimiter,
-    [name: :status_id_action, bucket_name: "status_id_action:fav_unfav", params: ["id"]]
+    [name: :status_id_action, bucket_name: "status_id_action:fav_unfav", params: [:id]]
     when action in ~w(favourite unfavourite)a
   )
 
index 341eff054c17c93fcf8a2260de3681d73a30d214..a4c3b9516a0a7e54a9d0177b3b077c9c37e0a564 100644 (file)
@@ -16,6 +16,8 @@ defmodule Pleroma.Workers.Cron.ClearOauthTokenWorker do
   def perform(_opts, _job) do
     if Config.get([:oauth2, :clean_expired_tokens], false) do
       Token.delete_expired_tokens()
+    else
+      :ok
     end
   end
 end
index dd13c3b17f490f95892cc86187fe7ef10f806add..7f09ff3cf56a05ca043732e9ae333bb25c2c0115 100644 (file)
@@ -37,6 +37,8 @@ defmodule Pleroma.Workers.Cron.DigestEmailsWorker do
       )
       |> Repo.all()
       |> send_emails
+    else
+      :ok
     end
   end
 
index 9bd0a5621e04c2d5fba9e55351d2f7f48d78d71e..5c816b3fe3568aee3e67f1f1bd72dc454f917505 100644 (file)
@@ -55,7 +55,11 @@ defmodule Pleroma.Workers.Cron.NewUsersDigestWorker do
         |> Repo.all()
         |> Enum.map(&Pleroma.Emails.NewUsersDigestEmail.new_users(&1, users_and_statuses))
         |> Enum.each(&Pleroma.Emails.Mailer.deliver/1)
+      else
+        :ok
       end
+    else
+      :ok
     end
   end
 end
index b8953dd7fa858e78038a827ff4a12a2c588d2a77..84b3b84dea00d87a48d19d9d00fb073d28bfa593 100644 (file)
@@ -23,6 +23,8 @@ defmodule Pleroma.Workers.Cron.PurgeExpiredActivitiesWorker do
   def perform(_opts, _job) do
     if Config.get([ActivityExpiration, :enabled]) do
       Enum.each(ActivityExpiration.due_expirations(@interval), &delete_activity/1)
+    else
+      :ok
     end
   end
 
diff --git a/priv/repo/migrations/20200526144426_add_apps_indexes.exs b/priv/repo/migrations/20200526144426_add_apps_indexes.exs
new file mode 100644 (file)
index 0000000..5cb6a04
--- /dev/null
@@ -0,0 +1,7 @@
+defmodule Pleroma.Repo.Migrations.AddAppsIndexes do
+  use Ecto.Migration
+
+  def change do
+    create(index(:apps, [:client_id, :client_secret]))
+  end
+end
index d4284831c0a34ce33d3bfe026f00fc53fa7a80b9..6e3676aca302bd1a6542810e4782aac48d2e2215 100644 (file)
@@ -34,7 +34,8 @@ defmodule Pleroma.Factory do
       last_digest_emailed_at: NaiveDateTime.utc_now(),
       last_refreshed_at: NaiveDateTime.utc_now(),
       notification_settings: %Pleroma.User.NotificationSetting{},
-      multi_factor_authentication_settings: %Pleroma.MFA.Settings{}
+      multi_factor_authentication_settings: %Pleroma.MFA.Settings{},
+      ap_enabled: true
     }
 
     %{
index 45125f7042da9888bad9f0e7e8ff3c2032c9e312..3556ef1b4de5ecc17e149bd669d801071b49ed95 100644 (file)
@@ -586,6 +586,26 @@ defmodule Pleroma.UserTest do
 
       refute user.last_refreshed_at == orig_user.last_refreshed_at
     end
+
+    @tag capture_log: true
+    test "it returns the old user if stale, but unfetchable" do
+      a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800)
+
+      orig_user =
+        insert(
+          :user,
+          local: false,
+          nickname: "admin@mastodon.example.org",
+          ap_id: "http://mastodon.example.org/users/raymoo",
+          last_refreshed_at: a_week_ago
+        )
+
+      assert orig_user.last_refreshed_at == a_week_ago
+
+      {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/raymoo")
+
+      assert user.last_refreshed_at == orig_user.last_refreshed_at
+    end
   end
 
   test "returns an ap_id for a user" do
index dd2a48a613b4ed4d72ef61731add3ba64083e178..24edab41ae4f5595d6aa4baf8c1780dc8b392aad 100644 (file)
@@ -451,6 +451,36 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
       assert Activity.get_by_ap_id(data["id"])
     end
 
+    @tag capture_log: true
+    test "it inserts an incoming activity into the database" <>
+           "even if we can't fetch the user but have it in our db",
+         %{conn: conn} do
+      user =
+        insert(:user,
+          ap_id: "https://mastodon.example.org/users/raymoo",
+          ap_enabled: true,
+          local: false,
+          last_refreshed_at: nil
+        )
+
+      data =
+        File.read!("test/fixtures/mastodon-post-activity.json")
+        |> Poison.decode!()
+        |> Map.put("actor", user.ap_id)
+        |> put_in(["object", "attridbutedTo"], user.ap_id)
+
+      conn =
+        conn
+        |> assign(:valid_signature, true)
+        |> put_req_header("content-type", "application/activity+json")
+        |> post("/inbox", data)
+
+      assert "ok" == json_response(conn, 200)
+
+      ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))
+      assert Activity.get_by_ap_id(data["id"])
+    end
+
     test "it clears `unreachable` federation status of the sender", %{conn: conn} do
       data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()