- 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)
- )
+ def get_bookmark(%Activity{} = activity, %User{} = user) do
+ if Ecto.assoc_loaded?(activity.bookmark) do
+ activity.bookmark
+ else
+ Bookmark.get(user.id, activity.id)
+ end
+ end
+
+ def get_bookmark(_, _), do: nil
+
+ def change(struct, params \\ %{}) do
+ struct
+ |> cast(params, [:data, :recipients])
+ |> validate_required([:data])
+ |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
+ end
+
+ def get_by_ap_id_with_object(ap_id) do
+ ap_id
+ |> Queries.by_ap_id()
+ |> with_preloaded_object(:left)
+ |> Repo.one()
+ end
+
+ @spec get_by_id(String.t()) :: Activity.t() | nil
+ def get_by_id(id) do
+ case FlakeId.flake_id?(id) do
+ true ->
+ Activity
+ |> where([a], a.id == ^id)
+ |> restrict_deactivated_users()
+ |> Repo.one()
+
+ _ ->
+ nil
+ end
+ end
+
+ def get_by_id_with_object(id) do
+ Activity
+ |> where(id: ^id)
+ |> with_preloaded_object()
+ |> Repo.one()