Skip to content

Commit

Permalink
Tidy up verification code a little (#4156)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerosol committed May 29, 2024
1 parent 9d77e97 commit bb7b0d9
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 164 deletions.
6 changes: 3 additions & 3 deletions lib/plausible/verification/check.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
defmodule Plausible.Verification.Check do
@moduledoc """
Behaviour to be implemented by specific site verification checks.
`friendly_name()` doesn't necessarily reflect the actual check description,
`report_progress_as()` doesn't necessarily reflect the actual check description,
it serves as a user-facing message grouping mechanism, to prevent frequent message flashing when checks rotate often.
Each check operates on `state()` and is expected to return it, optionally modified, by all means.
`perform_safe/1` is used to guarantee no exceptions are thrown by faulty implementations, not to interrupt LiveView.
"""
@type state() :: Plausible.Verification.State.t()
@callback friendly_name() :: String.t()
@callback report_progress_as() :: String.t()
@callback perform(state()) :: state()

defmacro __using__(_) do
Expand All @@ -30,7 +30,7 @@ defmodule Plausible.Verification.Check do
"Error running check #{inspect(__MODULE__)} on #{state.url}: #{inspect(e)}"
)

put_diagnostics(state, service_error: true)
put_diagnostics(state, service_error: e)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/plausible/verification/checks/csp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Plausible.Verification.Checks.CSP do
use Plausible.Verification.Check

@impl true
def friendly_name, do: "We're visiting your site to ensure that everything is working"
def report_progress_as, do: "We're visiting your site to ensure that everything is working"

@impl true
def perform(%State{assigns: %{headers: headers}} = state) do
Expand Down
2 changes: 1 addition & 1 deletion lib/plausible/verification/checks/fetch_body.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Plausible.Verification.Checks.FetchBody do
use Plausible.Verification.Check

@impl true
def friendly_name, do: "We're visiting your site to ensure that everything is working"
def report_progress_as, do: "We're visiting your site to ensure that everything is working"

@impl true
def perform(%State{url: "https://" <> _ = url} = state) do
Expand Down
2 changes: 1 addition & 1 deletion lib/plausible/verification/checks/installation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Plausible.Verification.Checks.Installation do
use Plausible.Verification.Check

@impl true
def friendly_name, do: "We're verifying that your visitors are being counted correctly"
def report_progress_as, do: "We're verifying that your visitors are being counted correctly"

@impl true
def perform(%State{url: url} = state) do
Expand Down
2 changes: 1 addition & 1 deletion lib/plausible/verification/checks/scan_body.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Plausible.Verification.Checks.ScanBody do
use Plausible.Verification.Check

@impl true
def friendly_name, do: "We're visiting your site to ensure that everything is working"
def report_progress_as, do: "We're visiting your site to ensure that everything is working"

@impl true
def perform(%State{assigns: %{raw_body: body}} = state) when is_binary(body) do
Expand Down
2 changes: 1 addition & 1 deletion lib/plausible/verification/checks/snippet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Plausible.Verification.Checks.Snippet do
use Plausible.Verification.Check

@impl true
def friendly_name, do: "We're looking for the Plausible snippet on your site"
def report_progress_as, do: "We're looking for the Plausible snippet on your site"

@impl true
def perform(%State{assigns: %{document: document}} = state) do
Expand Down
2 changes: 1 addition & 1 deletion lib/plausible/verification/checks/snippet_cache_bust.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Plausible.Verification.Checks.SnippetCacheBust do
use Plausible.Verification.Check

@impl true
def friendly_name, do: "We're looking for the Plausible snippet on your site"
def report_progress_as, do: "We're looking for the Plausible snippet on your site"

@impl true
def perform(
Expand Down
2 changes: 1 addition & 1 deletion lib/plausible/verification/diagnostics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ defmodule Plausible.Verification.Diagnostics do
%Result{
ok?: false,
errors: [error.message],
recommendations: [{error.recommendation, error.url}]
recommendations: [%{text: error.recommendation, url: error.url}]
}
end

Expand Down
27 changes: 7 additions & 20 deletions lib/plausible_web/live/components/verification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule PlausibleWeb.Live.Components.Verification do
:if={not @finished? or (not @modal? and @success?)}
class="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-green-100 dark:bg-gray-700"
>
<div class="block pulsating-circle" }></div>
<div class="block pulsating-circle"></div>
</div>
<div
Expand Down Expand Up @@ -67,10 +67,12 @@ defmodule PlausibleWeb.Live.Components.Verification do
</p>
<p :if={not @finished?} class="mt-2 animate-pulse" id="progress"><%= @message %></p>
<.recommendations
:if={@finished? and not @success? and @interpretation}
interpretation={@interpretation}
/>
<p :if={@finished? and not @success? and @interpretation} class="mt-2" id="recommendation">
<span><%= List.first(@interpretation.recommendations).text %>.&nbsp;</span>
<.styled_link href={List.first(@interpretation.recommendations).url} new_tab={true}>
Learn more
</.styled_link>
</p>
</div>
<div :if={@finished?} class="mt-auto">
Expand Down Expand Up @@ -115,19 +117,4 @@ defmodule PlausibleWeb.Live.Components.Verification do
</div>
"""
end

def recommendations(assigns) do
~H"""
<p class="mt-2" id="recommendations">
<span :for={recommendation <- @interpretation.recommendations} class="recommendation">
<span :if={is_binary(recommendation)}><%= recommendation %></span>
<span :if={is_tuple(recommendation)}><%= elem(recommendation, 0) %>.&nbsp;</span>
<.styled_link :if={is_tuple(recommendation)} href={elem(recommendation, 1)} new_tab={true}>
Learn more
</.styled_link>
<br />
</span>
</p>
"""
end
end
2 changes: 1 addition & 1 deletion lib/plausible_web/live/verification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ defmodule PlausibleWeb.Live.Verification do

def handle_info({:verification_check_start, {check, _state}}, socket) do
update_component(socket,
message: check.friendly_name()
message: check.report_progress_as()
)

{:noreply, socket}
Expand Down
Loading

0 comments on commit bb7b0d9

Please sign in to comment.