end
def get_by_id(id) do
- Repo.get(Activity, id)
+ Activity
+ |> where([a], a.id == ^id)
+ |> restrict_deactivated_users()
+ |> Repo.one()
end
def get_by_id_with_object(id) do
def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
create_by_object_ap_id(ap_id)
+ |> restrict_deactivated_users()
|> Repo.one()
end
def create_by_object_ap_id_with_object(_), do: nil
- def get_create_by_object_ap_id_with_object(ap_id) do
+ def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
ap_id
|> create_by_object_ap_id_with_object()
|> Repo.one()
end
+ def get_create_by_object_ap_id_with_object(_), do: nil
+
defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
get_create_by_object_ap_id_with_object(ap_id)
end
|> Repo.delete_all()
|> elem(1)
|> Enum.find(fn
+ %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
%{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
_ -> nil
end)
|> 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
- )
- ]
- ]
+ def restrict_deactivated_users(query) do
+ from(activity in query,
+ where:
+ fragment(
+ "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')",
+ activity.actor
+ )
)
- |> Repo.update_all([])
- |> case do
- {1, [activity]} -> activity
- _ -> {:error, "Not found"}
- end
end
end