-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Migration: add country rules * Add CountryRule schema * Implement CountryRule cache * Add country rules context interface * Start country rules cache * Lookup country rules on ingestion * Remove :shields feature flag from test helpers * Add nested sidebar menu for Shields * Fix typo * IP Rules: hide description on mobile view * Prepare SiteController to handle multiple shield types * Seed some country shield * Implement LV for country rules * Remove "YOU" indicator from country rules * Fix small build * Format * Update typespecs * Make docs link point at /countries * Fix flash on top of modal for Safari * Build the rule struct with site_id provided up-front * Clarify why we're messaging the ComboBox component * Re-open combobox suggestions after pressing Escape * Update changelog * Fix font size in country table cells * Pass `added_by` via rule add options * Display site's timezone timestamps in rule tooltips * Display formatted timestamps in site's timezone And simplify+test Timezone module; an input timestamp converted to UTC can never be ambiguous. * Remove no-op atom * Display the maximum number of rules when reached * Improve readability of remove button tests * Credo --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
- Loading branch information
Showing
32 changed files
with
1,142 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
defmodule Plausible.Shield.CountryRule do | ||
@moduledoc """ | ||
Schema for Country Block List | ||
""" | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
@type t() :: %__MODULE__{} | ||
|
||
@primary_key {:id, :binary_id, autogenerate: true} | ||
schema "shield_rules_country" do | ||
belongs_to :site, Plausible.Site | ||
field :country_code, :string | ||
field :action, Ecto.Enum, values: [:deny, :allow], default: :deny | ||
field :added_by, :string | ||
|
||
# If `from_cache?` is set, the struct might be incomplete - see `Plausible.Site.Shield.Rules.Country.Cache` | ||
field :from_cache?, :boolean, virtual: true, default: false | ||
timestamps() | ||
end | ||
|
||
def changeset(rule, attrs) do | ||
rule | ||
|> cast(attrs, [:site_id, :country_code]) | ||
|> validate_required([:site_id, :country_code]) | ||
|> validate_length(:country_code, is: 2) | ||
|> Ecto.Changeset.validate_change(:country_code, fn :country_code, cc -> | ||
if cc in Enum.map(Location.Country.all(), & &1.alpha_2) do | ||
[] | ||
else | ||
[country_code: "is invalid"] | ||
end | ||
end) | ||
|> unique_constraint(:country_code, | ||
name: :shield_rules_country_site_id_country_code_index | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
defmodule Plausible.Shield.CountryRuleCache do | ||
@moduledoc """ | ||
Allows retrieving Country Rules by domain and country code | ||
""" | ||
alias Plausible.Shield.CountryRule | ||
|
||
import Ecto.Query | ||
use Plausible.Cache | ||
|
||
@cache_name :country_blocklist_by_domain | ||
|
||
@cached_schema_fields ~w( | ||
id | ||
country_code | ||
action | ||
)a | ||
|
||
@impl true | ||
def name(), do: @cache_name | ||
|
||
@impl true | ||
def child_id(), do: :cachex_country_blocklist | ||
|
||
@impl true | ||
def count_all() do | ||
Plausible.Repo.aggregate(CountryRule, :count) | ||
end | ||
|
||
@impl true | ||
def base_db_query() do | ||
from rule in CountryRule, | ||
inner_join: s in assoc(rule, :site), | ||
select: { | ||
s.domain, | ||
s.domain_changed_from, | ||
%{struct(rule, ^@cached_schema_fields) | from_cache?: true} | ||
} | ||
end | ||
|
||
@impl true | ||
def get_from_source({domain, country_code}) do | ||
query = | ||
base_db_query() | ||
|> where([rule, site], rule.country_code == ^country_code and site.domain == ^domain) | ||
|
||
case Plausible.Repo.one(query) do | ||
{_, _, rule} -> %CountryRule{rule | from_cache?: false} | ||
_any -> nil | ||
end | ||
end | ||
|
||
@impl true | ||
def unwrap_cache_keys(items) do | ||
Enum.reduce(items, [], fn | ||
{domain, nil, object}, acc -> | ||
[{{domain, String.upcase(object.country_code)}, object} | acc] | ||
|
||
{domain, domain_changed_from, object}, acc -> | ||
[ | ||
{{domain, String.upcase(object.country_code)}, object}, | ||
{{domain_changed_from, String.upcase(object.country_code)}, object} | acc | ||
] | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.