From 7b2eb82d6fb2d701afcc6ed3a96dddc9ddcf24a4 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 7 Nov 2022 18:59:46 +0100 Subject: [PATCH] Handle nothing found by unsplash for location Closes #1187 Signed-off-by: Thomas Citharel --- lib/service/pictures/unsplash.ex | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/service/pictures/unsplash.ex b/lib/service/pictures/unsplash.ex index 307ef2920..4a403a19b 100644 --- a/lib/service/pictures/unsplash.ex +++ b/lib/service/pictures/unsplash.ex @@ -24,7 +24,9 @@ defmodule Mobilizon.Service.Pictures.Unsplash do GenericJSONClient.client(headers: [{:Authorization, "Client-ID #{unsplash_access_key()}"}]) with {:ok, %{status: 200, body: body}} <- GenericJSONClient.get(client, url), - selected_picture <- Enum.random(body["results"]) do + results <- body["results"], + {:empty, false} <- {:empty, Enum.empty?(results)}, + selected_picture <- Enum.random(results) do %Information{ url: selected_picture["urls"]["small"], author: %{ @@ -37,6 +39,9 @@ defmodule Mobilizon.Service.Pictures.Unsplash do } } else + {:empty, false} -> + nil + _ -> nil end