instance: Prevent loop of updates
authorHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Tue, 7 Jul 2020 10:07:30 +0000 (12:07 +0200)
committerHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Wed, 8 Jul 2020 04:28:40 +0000 (06:28 +0200)
lib/pleroma/instances/instance.ex

index b97e229e5a04ff497cd4359f3b03fde1a1c2cefa..a1f935232e69642362be744881f1db6a311125fa 100644 (file)
@@ -127,30 +127,23 @@ defmodule Pleroma.Instances.Instance do
     existing_record = Repo.get_by(Instance, %{host: host})
     now = NaiveDateTime.utc_now()
 
-    if existing_record && existing_record.favicon &&
+    if existing_record && existing_record.favicon_updated_at &&
          NaiveDateTime.diff(now, existing_record.favicon_updated_at) < 86_400 do
       existing_record.favicon
     else
       favicon = scrape_favicon(instance_uri)
 
-      cond do
-        is_binary(favicon) && existing_record ->
-          existing_record
-          |> changeset(%{favicon: favicon, favicon_updated_at: now})
-          |> Repo.update()
-
-          favicon
-
-        is_binary(favicon) ->
-          %Instance{}
-          |> changeset(%{host: host, favicon: favicon, favicon_updated_at: now})
-          |> Repo.insert()
-
-          favicon
-
-        true ->
-          nil
+      if existing_record do
+        existing_record
+        |> changeset(%{favicon: favicon, favicon_updated_at: now})
+        |> Repo.update()
+      else
+        %Instance{}
+        |> changeset(%{host: host, favicon: favicon, favicon_updated_at: now})
+        |> Repo.insert()
       end
+
+      favicon
     end
   end