+ )
+ end
+
+ def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
+ from(
+ activity in Activity,
+ where:
+ fragment(
+ "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
+ activity.data,
+ activity.data,
+ ^to_string(ap_id)
+ ),
+ where: fragment("(?)->>'type' = 'Create'", activity.data)
+ )
+ end
+
+ def create_by_object_ap_id(_), do: nil
+
+ def get_all_create_by_object_ap_id(ap_id) do
+ Repo.all(create_by_object_ap_id(ap_id))
+ end
+
+ def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
+ create_by_object_ap_id(ap_id)
+ |> Repo.one()
+ end
+
+ def get_create_by_object_ap_id(_), do: nil
+
+ def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
+ from(
+ activity in Activity,
+ where:
+ fragment(
+ "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
+ activity.data,
+ activity.data,
+ ^to_string(ap_id)
+ ),
+ where: fragment("(?)->>'type' = 'Create'", activity.data),
+ inner_join: o in Object,
+ on:
+ fragment(
+ "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
+ o.data,
+ activity.data,
+ activity.data
+ ),
+ preload: [object: o]
+ )
+ end
+
+ def create_by_object_ap_id_with_object(_), do: nil
+
+ def get_create_by_object_ap_id_with_object(ap_id) do
+ ap_id
+ |> create_by_object_ap_id_with_object()
+ |> Repo.one()
+ end
+
+ def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
+ def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
+ def normalize(_), do: nil
+
+ def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
+ get_create_by_object_ap_id(ap_id)
+ end
+
+ def get_in_reply_to_activity(_), do: nil
+
+ def delete_by_ap_id(id) when is_binary(id) do
+ by_object_ap_id(id)
+ |> select([u], u)
+ |> Repo.delete_all()
+ |> elem(1)
+ |> Enum.find(fn
+ %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
+ _ -> nil
+ end)