Separate extra automatic verification logic to existing module

This commit is contained in:
Liam 2024-06-22 16:37:22 -04:00
parent 1a7fbb29eb
commit f29fdae22f
2 changed files with 46 additions and 23 deletions

View file

@ -14,30 +14,11 @@ defmodule Philomena.ArtistLinks do
alias Philomena.Tags.Tag alias Philomena.Tags.Tag
@doc """ @doc """
Check links pending verification to see if the user placed Updates all links pending verification to transition to link verified or reset
the appropriate code on the page. next update time.
""" """
def automatic_verify! do def automatic_verify! do
now = DateTime.utc_now() |> DateTime.truncate(:second) Enum.each(AutomaticVerifier.generate_updates(), &Repo.update!/1)
# 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)
end end
@doc """ @doc """

View file

@ -1,5 +1,47 @@
defmodule Philomena.ArtistLinks.AutomaticVerifier do 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 artist_link.uri
|> PhilomenaProxy.Http.get() |> PhilomenaProxy.Http.get()
|> contains_verification_code?(artist_link.verification_code) |> contains_verification_code?(artist_link.verification_code)