+ def make_user_from_ap_id(ap_id) do
+ with {:ok, %{status_code: 200, body: body}} <- @httpoison.get(ap_id, ["Accept": "application/activity+json"]),
+ {:ok, data} <- Poison.decode(body)
+ do
+ user_data = %{
+ ap_id: data["id"],
+ info: %{
+ "ap_enabled" => true,
+ "source_data" => data
+ },
+ nickname: "#{data["preferredUsername"]}@#{URI.parse(ap_id).host}",
+ name: data["name"]
+ }
+
+ User.insert_or_update_user(user_data)
+ if user = User.get_by_ap_id(ap_id) do
+ User.info_changeset(user, user_data}
+ |> Repo.update
+ else
+ User.insert_or_update_user(user_data)
+ end
+ end
+ end
+
+ def make_user_from_nickname(nickname) do
+ with {:ok, %{"ap_id" => ap_id}} when not is_nil(ap_id) <- WebFinger.finger(nickname) do
+ make_user_from_ap_id(ap_id)
+ end
+ end
+
+ 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}