- defp update_object_in_activities(%{data: %{"id" => id}} = object) do
- # Update activities that already had this. Could be done in a seperate process.
- relevant_activities = Activity.all_by_object_ap_id(id)
- Enum.map(relevant_activities, fn (activity) ->
- new_activity_data = activity.data |> Map.put("object", object.data)
- changeset = Ecto.Changeset.change(activity, data: new_activity_data)
- Repo.update(changeset)
- end)
- end
-
- def unlike(%User{ap_id: ap_id}, %Object{data: %{ "id" => id}} = object) do
- query = from activity in Activity,
- where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id, type: "Like"})
-
- activity = Repo.one(query)
-
- if activity do
- # just delete for now...
- {:ok, _activity} = Repo.delete(activity)
-
- likes = (object.data["likes"] || []) |> List.delete(ap_id)
-
- 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, object}
+ def announce(%User{ap_id: _} = user, %Object{data: %{"id" => _}} = object, activity_id \\ nil, local \\ true) do
+ with announce_data <- make_announce_data(user, object, activity_id),
+ {:ok, activity} <- insert(announce_data, local),
+ {:ok, object} <- add_announce_to_object(activity, object),
+ :ok <- maybe_federate(activity) do
+ {:ok, activity, object}