- def like(%User{ap_id: ap_id} = user, %Object{data: %{ "id" => id}} = object) do
- cond do
- # There's already a like here, so return the original activity.
- ap_id in (object.data["likes"] || []) ->
- query = from activity in Activity,
- where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id, type: "Like"})
-
- activity = Repo.one(query)
- {:ok, activity, object}
- true ->
- data = %{
- "type" => "Like",
- "actor" => ap_id,
- "object" => id,
- "to" => [User.ap_followers(user), object.data["actor"]],
- "context" => object.data["context"]
- }
-
- {:ok, activity} = insert(data)
-
- likes = [ap_id | (object.data["likes"] || [])] |> Enum.uniq
-
- new_data = object.data
- |> Map.put("like_count", length(likes))
- |> Map.put("likes", likes)
-
- changeset = Ecto.Changeset.change(object, data: new_data)
- {:ok, object} = Repo.update(changeset)
-
- update_object_in_activities(object)
-
- {:ok, activity, object}
+ # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
+ def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => _}} = object, activity_id \\ nil, local \\ true) do
+ with nil <- get_existing_like(ap_id, object),
+ like_data <- make_like_data(user, object, activity_id),
+ {:ok, activity} <- insert(like_data, local),
+ {:ok, object} <- add_like_to_object(activity, object),
+ :ok <- maybe_federate(activity) do
+ {:ok, activity, object}
+ else
+ %Activity{} = activity -> {:ok, activity, object}
+ error -> {:error, error}