defmodule Pleroma.Web.ActivityPub.ActivityPub do
- alias Pleroma.Repo
- alias Pleroma.{Activity, Object, Upload, User}
+ alias Pleroma.{Activity, Repo, Object, Upload, User, Web}
+ alias Ecto.{Changeset, UUID}
import Ecto.Query
def insert(map, local \\ true) when is_map(map) do
activity = %{
"type" => "Create",
- "to" => to,
+ "to" => to |> Enum.uniq,
"actor" => actor.ap_id,
"object" => object,
"published" => published,
end
end
- def like(%User{ap_id: ap_id} = user, %Object{data: %{ "id" => id}} = object) do
+ def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, local \\ true) do
cond do
# There's already a like here, so return the original activity.
ap_id in (object.data["likes"] || []) ->
"type" => "Like",
"actor" => ap_id,
"object" => id,
- "to" => [User.ap_followers(user), object.data["actor"]]
+ "to" => [User.ap_followers(user), object.data["actor"]],
+ "context" => object.data["context"]
}
- {:ok, activity} = insert(data)
+ {:ok, activity} = insert(data, local)
likes = [ap_id | (object.data["likes"] || [])] |> Enum.uniq
|> Map.put("like_count", length(likes))
|> Map.put("likes", likes)
- changeset = Ecto.Changeset.change(object, data: new_data)
+ changeset = Changeset.change(object, data: new_data)
{:ok, object} = Repo.update(changeset)
update_object_in_activities(object)
+ if user.local do
+ Pleroma.Web.Federator.enqueue(:publish, activity)
+ end
+
{:ok, activity, object}
end
end
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)
+ changeset = Changeset.change(activity, data: new_activity_data)
Repo.update(changeset)
end)
end
|> Map.put("like_count", length(likes))
|> Map.put("likes", likes)
- changeset = Ecto.Changeset.change(object, data: new_data)
+ changeset = Changeset.change(object, data: new_data)
{:ok, object} = Repo.update(changeset)
update_object_in_activities(object)
end
def generate_object_id do
- generate_id("objects")
+ Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :object, Ecto.UUID.generate)
end
def generate_id(type) do
- "#{Pleroma.Web.base_url()}/#{type}/#{Ecto.UUID.generate}"
+ "#{Web.base_url()}/#{type}/#{UUID.generate}"
end
def fetch_public_activities(opts \\ %{}) do
query
end
- Repo.all(query)
- |> Enum.reverse
+ Enum.reverse(Repo.all(query))
end
- def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object) do
+ def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, local \\ true) do
data = %{
"type" => "Announce",
"actor" => ap_id,
"object" => id,
- "to" => [User.ap_followers(user), object.data["actor"]]
+ "to" => [User.ap_followers(user), object.data["actor"]],
+ "context" => object.data["context"]
}
- {:ok, activity} = insert(data)
+ {:ok, activity} = insert(data, local)
announcements = [ap_id | (object.data["announcements"] || [])] |> Enum.uniq
|> Map.put("announcement_count", length(announcements))
|> Map.put("announcements", announcements)
- changeset = Ecto.Changeset.change(object, data: new_data)
+ changeset = Changeset.change(object, data: new_data)
{:ok, object} = Repo.update(changeset)
update_object_in_activities(object)
+ if user.local do
+ Pleroma.Web.Federator.enqueue(:publish, activity)
+ end
+
{:ok, activity, object}
end