alias Pleroma.{Activity, Repo, Object, Upload, User, Web}
alias Ecto.{Changeset, UUID}
import Ecto.Query
+ require Logger
def insert(map, local \\ true) when is_map(map) do
map = map
|> Map.put_new_lazy("id", &generate_activity_id/0)
|> Map.put_new_lazy("published", &make_date/0)
- map = if is_map(map["object"]) do
- object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0)
- Repo.insert!(%Object{data: object})
- Map.put(map, "object", object)
- else
- map
- end
+ with %Activity{} = activity <- Activity.get_by_ap_id(map["id"]) do
+ Logger.debug(fn -> "Already have activity, #{activity.id}, not inserting." end)
+ {:ok, activity}
+ else _e ->
+ map = if is_map(map["object"]) do
+ object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0)
+ Repo.insert!(%Object{data: object})
+ Map.put(map, "object", object)
+ else
+ map
+ end
- Repo.insert(%Activity{data: map, local: local})
+ Repo.insert(%Activity{data: map, local: local})
+ end
end
def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do
end
end
- def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, local \\ true) do
+ def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, activity_id \\ nil, local \\ true) do
cond do
# There's already a like here, so return the original activity.
ap_id in (object.data["likes"] || []) ->
"context" => object.data["context"]
}
+ data = if activity_id, do: Map.put(data, "id", activity_id), else: data
+
{:ok, activity} = insert(data, local)
likes = [ap_id | (object.data["likes"] || [])] |> Enum.uniq
end
defp update_object_in_activities(%{data: %{"id" => id}} = object) do
+ # TODO
# Update activities that already had this. Could be done in a seperate process.
+ # Alternatively, just don't do this and fetch the current object each time. Most
+ # could probably be taken from cache.
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)
Enum.reverse(Repo.all(query))
end
- def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, local \\ true) do
+ def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, activity_id \\ nil, local \\ true) do
data = %{
"type" => "Announce",
"actor" => ap_id,
"context" => object.data["context"]
}
+ data = if activity_id, do: Map.put(data, "id", activity_id), else: data
+
{:ok, activity} = insert(data, local)
announcements = [ap_id | (object.data["announcements"] || [])] |> Enum.uniq
{:ok, activity, object}
end
- def follow(%User{ap_id: follower_id, local: actor_local}, %User{ap_id: followed_id}, local \\ true) do
+ def follow(%User{ap_id: follower_id, local: actor_local}, %User{ap_id: followed_id}, activity_id \\ nil, local \\ true) do
data = %{
"type" => "Follow",
"actor" => follower_id,
"published" => make_date()
}
+ data = if activity_id, do: Map.put(data, "id", activity_id), else: data
+
with {:ok, activity} <- insert(data, local) do
if actor_local do
Pleroma.Web.Federator.enqueue(:publish, activity)