- remote_users = Pleroma.Web.Salmon.remote_users(activity)
- data = sanitize_outgoing_activity_data(activity.data)
- Enum.each remote_users, fn(user) ->
- if user.info["ap_enabled"] do
- inbox = user.info["source_data"]["inbox"]
- Logger.info("Federating #{activity.data["id"]} to #{inbox}")
- host = URI.parse(inbox).host
- signature = Pleroma.Web.HTTPSignatures.sign(actor, %{host: host})
- @httpoison.post(inbox, Poison.encode!(data), [{"Content-Type", "application/activity+json"}, {"signature", signature}])
+ followers =
+ if actor.follower_address in activity.recipients do
+ {:ok, followers} = User.get_followers(actor)
+ followers |> Enum.filter(&(!&1.local))
+ else
+ []
+ end
+
+ public = is_public?(activity)
+
+ remote_inboxes =
+ (Pleroma.Web.Salmon.remote_users(activity) ++ followers)
+ |> Enum.filter(fn user -> User.ap_enabled?(user) end)
+ |> Enum.map(fn %{info: %{"source_data" => data}} ->
+ (data["endpoints"] && data["endpoints"]["sharedInbox"]) || data["inbox"]
+ end)
+ |> Enum.uniq()
+ |> Enum.filter(fn inbox -> should_federate?(inbox, public) end)
+
+ {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
+ json = Jason.encode!(data)
+
+ Enum.each(remote_inboxes, fn inbox ->
+ Federator.enqueue(:publish_single_ap, %{
+ inbox: inbox,
+ json: json,
+ actor: actor,
+ id: activity.data["id"]
+ })
+ end)
+ end
+
+ def publish_one(%{inbox: inbox, json: json, actor: actor, id: id}) do
+ Logger.info("Federating #{id} to #{inbox}")
+ host = URI.parse(inbox).host
+
+ signature =
+ Pleroma.Web.HTTPSignatures.sign(actor, %{host: host, "content-length": byte_size(json)})
+
+ @httpoison.post(
+ inbox,
+ json,
+ [{"Content-Type", "application/activity+json"}, {"signature", signature}],
+ hackney: [pool: :default]
+ )
+ end
+
+ # TODO:
+ # This will create a Create activity, which we need internally at the moment.
+ def fetch_object_from_id(id) do
+ if object = Object.get_cached_by_ap_id(id) do
+ {:ok, object}
+ else
+ Logger.info("Fetching #{id} via AP")
+
+ with true <- String.starts_with?(id, "http"),
+ {:ok, %{body: body, status_code: code}} when code in 200..299 <-
+ @httpoison.get(
+ id,
+ [Accept: "application/activity+json"],
+ follow_redirect: true,
+ timeout: 10000,
+ recv_timeout: 20000
+ ),
+ {:ok, data} <- Jason.decode(body),
+ nil <- Object.get_by_ap_id(data["id"]),
+ params <- %{
+ "type" => "Create",
+ "to" => data["to"],
+ "cc" => data["cc"],
+ "actor" => data["attributedTo"],
+ "object" => data
+ },
+ {:ok, activity} <- Transmogrifier.handle_incoming(params) do
+ {:ok, Object.get_by_ap_id(activity.data["object"]["id"])}
+ else
+ object = %Object{} ->
+ {:ok, object}
+
+ _e ->
+ Logger.info("Couldn't get object via AP, trying out OStatus fetching...")
+
+ case OStatus.fetch_activity_from_url(id) do
+ {:ok, [activity | _]} -> {:ok, Object.get_by_ap_id(activity.data["object"]["id"])}
+ e -> e
+ end