+
+ defp blank?(""), do: nil
+ defp blank?(n), do: n
+
+ def insert_or_update_user(data) do
+ data =
+ data
+ |> Map.put(:name, blank?(data[:name]) || data[:nickname])
+
+ cs = User.remote_user_creation(data)
+ Repo.insert(cs, on_conflict: :replace_all, conflict_target: :nickname)
+ end
+
+ def ap_enabled?(%User{local: true}), do: true
+ def ap_enabled?(%User{info: info}), do: info["ap_enabled"]
+ def ap_enabled?(_), do: false
+
+ def get_or_fetch(uri_or_nickname) do
+ if String.starts_with?(uri_or_nickname, "http") do
+ get_or_fetch_by_ap_id(uri_or_nickname)
+ else
+ get_or_fetch_by_nickname(uri_or_nickname)
+ end
+ end
+
+ # wait a period of time and return newest version of the User structs
+ # this is because we have synchronous follow APIs and need to simulate them
+ # with an async handshake
+ def wait_and_refresh(_, %User{local: true} = a, %User{local: true} = b) do
+ with %User{} = a <- Repo.get(User, a.id),
+ %User{} = b <- Repo.get(User, b.id) do
+ {:ok, a, b}
+ else
+ _e ->
+ :error
+ end
+ end
+
+ def wait_and_refresh(timeout, %User{} = a, %User{} = b) do
+ with :ok <- :timer.sleep(timeout),
+ %User{} = a <- Repo.get(User, a.id),
+ %User{} = b <- Repo.get(User, b.id) do
+ {:ok, a, b}
+ else
+ _e ->
+ :error
+ end
+ end