|> where([s], s.actor == ^actor)
|> Repo.all()
end
-
- def increase_replies_count(nil), do: nil
-
- def increase_replies_count(object_ap_id) do
- from(a in create_by_object_ap_id(object_ap_id),
- update: [
- set: [
- data:
- fragment(
- """
- jsonb_set(?, '{object, repliesCount}',
- (coalesce((?->'object'->>'repliesCount')::int, 0) + 1)::varchar::jsonb, true)
- """,
- a.data,
- a.data
- )
- ]
- ]
- )
- |> Repo.update_all([])
- |> case do
- {1, [activity]} -> activity
- _ -> {:error, "Not found"}
- end
- end
-
- def decrease_replies_count(nil), do: nil
-
- def decrease_replies_count(object_ap_id) do
- from(a in create_by_object_ap_id(object_ap_id),
- update: [
- set: [
- data:
- fragment(
- """
- jsonb_set(?, '{object, repliesCount}',
- (greatest(0, (?->'object'->>'repliesCount')::int - 1))::varchar::jsonb, true)
- """,
- a.data,
- a.data
- )
- ]
- ]
- )
- |> Repo.update_all([])
- |> case do
- {1, [activity]} -> activity
- _ -> {:error, "Not found"}
- end
- end
end
"type" => "Create"
}) do
if is_public?(object) do
- Activity.increase_replies_count(reply_ap_id)
Object.increase_replies_count(reply_ap_id)
end
end
data: %{"inReplyTo" => reply_ap_id} = object
}) do
if is_public?(object) do
- Activity.decrease_replies_count(reply_ap_id)
Object.decrease_replies_count(reply_ap_id)
end
end
assert activity == found_activity
end
-
- test "reply count" do
- %{id: id, data: %{"object" => %{"id" => object_ap_id}}} = activity = insert(:note_activity)
-
- replies_count = activity.data["object"]["repliesCount"] || 0
- expected_increase = replies_count + 1
- Activity.increase_replies_count(object_ap_id)
- %{data: %{"object" => %{"repliesCount" => actual_increase}}} = Activity.get_by_id(id)
- assert expected_increase == actual_increase
- expected_decrease = expected_increase - 1
- Activity.decrease_replies_count(object_ap_id)
- %{data: %{"object" => %{"repliesCount" => actual_decrease}}} = Activity.get_by_id(id)
- assert expected_decrease == actual_decrease
- end
end