From f29fdae22f6b1a19936904d976a07502b191477e Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 22 Jun 2024 16:37:22 -0400 Subject: [PATCH] Separate extra automatic verification logic to existing module --- lib/philomena/artist_links.ex | 25 ++--------- .../artist_links/automatic_verifier.ex | 44 ++++++++++++++++++- 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/lib/philomena/artist_links.ex b/lib/philomena/artist_links.ex index 47f4d286..14a17e42 100644 --- a/lib/philomena/artist_links.ex +++ b/lib/philomena/artist_links.ex @@ -14,30 +14,11 @@ defmodule Philomena.ArtistLinks do alias Philomena.Tags.Tag @doc """ - Check links pending verification to see if the user placed - the appropriate code on the page. + Updates all links pending verification to transition to link verified or reset + next update time. """ def automatic_verify! do - now = DateTime.utc_now() |> DateTime.truncate(:second) - - # Automatically retry in an hour if we don't manage to - # successfully verify any given link - recheck_time = DateTime.add(now, 3600, :second) - - recheck_query = - from ul in ArtistLink, - where: ul.aasm_state == "unverified", - where: ul.next_check_at < ^now - - recheck_query - |> Repo.all() - |> Enum.map(fn link -> - ArtistLink.automatic_verify_changeset( - link, - AutomaticVerifier.check_link(link, recheck_time) - ) - end) - |> Enum.map(&Repo.update!/1) + Enum.each(AutomaticVerifier.generate_updates(), &Repo.update!/1) end @doc """ diff --git a/lib/philomena/artist_links/automatic_verifier.ex b/lib/philomena/artist_links/automatic_verifier.ex index f2a5bebd..de9afcc9 100644 --- a/lib/philomena/artist_links/automatic_verifier.ex +++ b/lib/philomena/artist_links/automatic_verifier.ex @@ -1,5 +1,47 @@ defmodule Philomena.ArtistLinks.AutomaticVerifier do - def check_link(artist_link, recheck_time) do + @moduledoc """ + Artist link automatic verification. + + Artist links contain a random code which is generated when the link is created. If the user + places the code on their linked page and this verifier finds it, this expedites the process + of verifying a link for the moderator, as they can simply use the presence of the code in a + field controlled by the artist to ascertain the validity of the artist link. + """ + + alias Philomena.ArtistLinks.ArtistLink + alias Philomena.Repo + import Ecto.Query + + @doc """ + Check links pending verification to see if the user placed the appropriate code on the page. + + Polls each artist link in unverified state and generates a changeset to either set it to + link verified, if the code was found on the page, or reset the next check time, if the code + was not found. + + Returns a list of changesets with updated links. + """ + def generate_updates do + # Automatically retry in an hour if we don't manage to + # successfully verify any given link + now = DateTime.utc_now(:second) + recheck_time = DateTime.add(now, 3600, :second) + + Enum.map(links_to_check(now), fn link -> + ArtistLink.automatic_verify_changeset(link, check_link(link, recheck_time)) + end) + end + + defp links_to_check(now) do + recheck_query = + from ul in ArtistLink, + where: ul.aasm_state == "unverified", + where: ul.next_check_at < ^now + + Repo.all(recheck_query) + end + + defp check_link(artist_link, recheck_time) do artist_link.uri |> PhilomenaProxy.Http.get() |> contains_verification_code?(artist_link.verification_code)