- def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object) do
- data = %{
- "type" => "Announce",
- "actor" => ap_id,
- "object" => id,
- "to" => [User.ap_followers(user), object.data["actor"]]
- }
-
- {:ok, activity} = insert(data)
-
- announcements = [ap_id | (object.data["announcements"] || [])] |> Enum.uniq
-
- new_data = object.data
- |> Map.put("announcement_count", length(announcements))
- |> Map.put("announcements", announcements)
-
- changeset = Ecto.Changeset.change(object, data: new_data)
- {:ok, object} = Repo.update(changeset)
-
- update_object_in_activities(object)
-
- {:ok, activity, object}
+ # 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