Ver código fonte

Merge branch '589-fix-cache' into 'develop'

Resolve "Update object cache on favs/boosts"

Closes #589

See merge request pleroma/pleroma!764
tags/v0.9.9
lambda 5 anos atrás
pai
commit
69a38644bc
8 arquivos alterados com 51 adições e 19 exclusões
  1. +1
    -0
      config/test.exs
  2. +25
    -18
      lib/pleroma/object.ex
  3. +1
    -1
      lib/pleroma/web/activity_pub/utils.ex
  4. +1
    -0
      test/support/conn_case.ex
  5. +1
    -0
      test/support/data_case.ex
  6. +5
    -0
      test/web/mastodon_api/mastodon_api_controller_test.exs
  7. +2
    -0
      test/web/ostatus/ostatus_test.exs
  8. +15
    -0
      test/web/twitter_api/twitter_api_test.exs

+ 1
- 0
config/test.exs Ver arquivo

@@ -36,6 +36,7 @@ config :pbkdf2_elixir, rounds: 1
config :pleroma, :websub, Pleroma.Web.WebsubMock
config :pleroma, :ostatus, Pleroma.Web.OStatusMock
config :tesla, adapter: Tesla.Mock
config :pleroma, :rich_media, enabled: false

config :web_push_encryption, :vapid_details,
subject: "mailto:administrator@example.com",


+ 25
- 18
lib/pleroma/object.ex Ver arquivo

@@ -42,24 +42,18 @@ defmodule Pleroma.Object do
# Legacy objects can be mutated by anybody
def authorize_mutation(%Object{}, %User{}), do: true

if Mix.env() == :test do
def get_cached_by_ap_id(ap_id) do
get_by_ap_id(ap_id)
end
else
def get_cached_by_ap_id(ap_id) do
key = "object:#{ap_id}"

Cachex.fetch!(:object_cache, key, fn _ ->
object = get_by_ap_id(ap_id)

if object do
{:commit, object}
else
{:ignore, object}
end
end)
end
def get_cached_by_ap_id(ap_id) do
key = "object:#{ap_id}"

Cachex.fetch!(:object_cache, key, fn _ ->
object = get_by_ap_id(ap_id)

if object do
{:commit, object}
else
{:ignore, object}
end
end)
end

def context_mapping(context) do
@@ -90,4 +84,17 @@ defmodule Pleroma.Object do
{:ok, object}
end
end

def set_cache(%Object{data: %{"id" => ap_id}} = object) do
Cachex.put(:object_cache, "object:#{ap_id}", object)
{:ok, object}
end

def update_and_set_cache(changeset) do
with {:ok, object} <- Repo.update(changeset) do
set_cache(object)
else
e -> e
end
end
end

+ 1
- 1
lib/pleroma/web/activity_pub/utils.ex Ver arquivo

@@ -285,7 +285,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
|> Map.put("#{property}_count", length(element))
|> Map.put("#{property}s", element),
changeset <- Changeset.change(object, data: new_data),
{:ok, object} <- Repo.update(changeset),
{:ok, object} <- Object.update_and_set_cache(changeset),
_ <- update_object_in_activities(object) do
{:ok, object}
end


+ 1
- 0
test/support/conn_case.ex Ver arquivo

@@ -33,6 +33,7 @@ defmodule Pleroma.Web.ConnCase do

setup tags do
Cachex.clear(:user_cache)
Cachex.clear(:object_cache)
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)

unless tags[:async] do


+ 1
- 0
test/support/data_case.ex Ver arquivo

@@ -32,6 +32,7 @@ defmodule Pleroma.DataCase do

setup tags do
Cachex.clear(:user_cache)
Cachex.clear(:object_cache)
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)

unless tags[:async] do


+ 5
- 0
test/web/mastodon_api/mastodon_api_controller_test.exs Ver arquivo

@@ -137,6 +137,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
end

test "posting a status with OGP link preview", %{conn: conn} do
Pleroma.Config.put([:rich_media, :enabled], true)
user = insert(:user)

conn =
@@ -148,6 +149,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do

assert %{"id" => id, "card" => %{"title" => "The Rock"}} = json_response(conn, 200)
assert Repo.get(Activity, id)
Pleroma.Config.put([:rich_media, :enabled], false)
end

test "posting a direct status", %{conn: conn} do
@@ -1667,6 +1669,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
end

test "Status rich-media Card", %{conn: conn, user: user} do
Pleroma.Config.put([:rich_media, :enabled], true)
{:ok, activity} = CommonAPI.post(user, %{"status" => "http://example.com/ogp"})

response =
@@ -1691,6 +1694,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
}
}
}

Pleroma.Config.put([:rich_media, :enabled], false)
end
end



+ 2
- 0
test/web/ostatus/ostatus_test.exs Ver arquivo

@@ -530,6 +530,8 @@ defmodule Pleroma.Web.OStatusTest do
note_object.data
|> Map.put("type", "Article")

Cachex.clear(:object_cache)

cs = Object.change(note_object, %{data: note_data})
{:ok, _article_object} = Repo.update(cs)



+ 15
- 0
test/web/twitter_api/twitter_api_test.exs Ver arquivo

@@ -200,12 +200,27 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do

test "it favorites a status, returns the updated activity" do
user = insert(:user)
other_user = insert(:user)
note_activity = insert(:note_activity)

{:ok, status} = TwitterAPI.fav(user, note_activity.id)
updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
assert ActivityView.render("activity.json", %{activity: updated_activity})["fave_num"] == 1

object = Object.normalize(note_activity.data["object"])

assert object.data["like_count"] == 1

assert status == updated_activity

{:ok, _status} = TwitterAPI.fav(other_user, note_activity.id)

object = Object.normalize(note_activity.data["object"])

assert object.data["like_count"] == 2

updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
assert ActivityView.render("activity.json", %{activity: updated_activity})["fave_num"] == 2
end

test "it unfavorites a status, returns the updated activity" do


Carregando…
Cancelar
Salvar