Still do caching in tests.
This commit is contained in:
parent
d5d91ae689
commit
505a084058
|
@ -42,24 +42,18 @@ def authorize_mutation(%Object{data: %{"actor" => actor}}, %User{ap_id: ap_id}),
|
||||||
# Legacy objects can be mutated by anybody
|
# Legacy objects can be mutated by anybody
|
||||||
def authorize_mutation(%Object{}, %User{}), do: true
|
def authorize_mutation(%Object{}, %User{}), do: true
|
||||||
|
|
||||||
if Mix.env() == :test do
|
def get_cached_by_ap_id(ap_id) do
|
||||||
def get_cached_by_ap_id(ap_id) do
|
key = "object:#{ap_id}"
|
||||||
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 _ ->
|
Cachex.fetch!(:object_cache, key, fn _ ->
|
||||||
object = get_by_ap_id(ap_id)
|
object = get_by_ap_id(ap_id)
|
||||||
|
|
||||||
if object do
|
if object do
|
||||||
{:commit, object}
|
{:commit, object}
|
||||||
else
|
else
|
||||||
{:ignore, object}
|
{:ignore, object}
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def context_mapping(context) do
|
def context_mapping(context) do
|
||||||
|
@ -90,4 +84,17 @@ def delete(%Object{data: %{"id" => id}} = object) do
|
||||||
{:ok, object}
|
{:ok, object}
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -285,7 +285,7 @@ def update_element_in_object(property, element, object) do
|
||||||
|> Map.put("#{property}_count", length(element))
|
|> Map.put("#{property}_count", length(element))
|
||||||
|> Map.put("#{property}s", element),
|
|> Map.put("#{property}s", element),
|
||||||
changeset <- Changeset.change(object, data: new_data),
|
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
|
_ <- update_object_in_activities(object) do
|
||||||
{:ok, object}
|
{:ok, object}
|
||||||
end
|
end
|
||||||
|
|
|
@ -200,12 +200,27 @@ test "upload a file" do
|
||||||
|
|
||||||
test "it favorites a status, returns the updated activity" do
|
test "it favorites a status, returns the updated activity" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
other_user = insert(:user)
|
||||||
note_activity = insert(:note_activity)
|
note_activity = insert(:note_activity)
|
||||||
|
|
||||||
{:ok, status} = TwitterAPI.fav(user, note_activity.id)
|
{:ok, status} = TwitterAPI.fav(user, note_activity.id)
|
||||||
updated_activity = Activity.get_by_ap_id(note_activity.data["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
|
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
|
end
|
||||||
|
|
||||||
test "it unfavorites a status, returns the updated activity" do
|
test "it unfavorites a status, returns the updated activity" do
|
||||||
|
|
Loading…
Reference in New Issue