+
+ def publish(actor, activity) do
+ followers = if actor.follower_address in activity.recipients do
+ {:ok, followers} = User.get_followers(actor)
+ followers
+ else
+ []
+ end
+
+ 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
+
+ {:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
+ json = Poison.encode!(data)
+ Enum.each remote_inboxes, fn(inbox) ->
+ Logger.info("Federating #{activity.data["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}])
+ end
+ 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
+ with {: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} <- Poison.decode(body),
+ data <- Transmogrifier.fix_object(data),
+ nil <- Object.get_by_ap_id(data["id"]),
+ %User{} = user <- User.get_or_fetch_by_ap_id(data["attributedTo"]),
+ {:ok, activity} = create(%{to: data["to"], actor: user, context: data["context"], object: data, local: false, additional: %{"cc" => data["cc"]}}) do
+ {:ok, Object.get_by_ap_id(activity.data["object"]["id"])}
+ else
+ object = %Object{} -> {:ok, object}
+ e -> e
+ end
+ end
+ end
+
+ def is_public?(activity) do
+ "https://www.w3.org/ns/activitystreams#Public" in (activity.data["to"] ++ (activity.data["cc"] || []))
+ end
+
+ def visible_for_user?(activity, nil) do
+ is_public?(activity)
+ end
+ def visible_for_user?(activity, user) do
+ x = [user.ap_id | user.following]
+ y = (activity.data["to"] ++ (activity.data["cc"] || []))
+ visible_for_user?(activity, nil) || Enum.any?(x, &(&1 in y))
+ end