Skip to content

Commit

Permalink
Dedup code based on read team schemas switching
Browse files Browse the repository at this point in the history
  • Loading branch information
aerosol committed Nov 14, 2024
1 parent 777f28a commit f5ac923
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 98 deletions.
1 change: 0 additions & 1 deletion lib/plausible/sites.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Plausible.Sites do
import Ecto.Query

alias Plausible.Auth
alias Plausible.Billing.Quota
alias Plausible.Repo
alias Plausible.Site
alias Plausible.Site.SharedLink
Expand Down
38 changes: 38 additions & 0 deletions lib/plausible/teams/adapter.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule Plausible.Teams.Adapter do
@moduledoc """
Commonly used teams-transition functions
"""
alias Plausible.Teams

defmacro __using__(_) do
quote do
alias Plausible.Teams
import Teams.Adapter
end
end

def team_or_user(user) do
switch(
user,
team_fn: &Function.identity/1,
user_fn: &Function.identity/1
)
end

def switch(user, opts \\ []) do
team_fn = Keyword.fetch!(opts, :team_fn)
user_fn = Keyword.fetch!(opts, :user_fn)

if Teams.read_team_schemas?(user) do
team =
case Teams.get_by_owner(user) do
{:ok, team} -> team
{:error, _} -> nil
end

team_fn.(team)
else
user_fn.(user)
end
end
end
65 changes: 20 additions & 45 deletions lib/plausible/teams/adapter/read/billing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,36 @@ defmodule Plausible.Teams.Adapter.Read.Billing do
@moduledoc """
Transition adapter for new schema reads
"""
alias Plausible.Teams
use Plausible.Teams.Adapter

def check_needs_to_upgrade(user) do
if Teams.read_team_schemas?(user) do
team =
case Teams.get_by_owner(user) do
{:ok, team} -> team
{:error, _} -> nil
end

Teams.Billing.check_needs_to_upgrade(team)
else
Plausible.Billing.check_needs_to_upgrade(user)
end
switch(
user,
team_fn: &Teams.Billing.check_needs_to_upgrade/1,
user_fn: &Plausible.Billing.check_needs_to_upgrade/1
)
end

def site_limit(user) do
if Teams.read_team_schemas?(user) do
team =
case Teams.get_by_owner(user) do
{:ok, team} -> team
{:error, _} -> nil
end

Teams.Billing.site_limit(team)
else
Plausible.Billing.Quota.Limits.site_limit(user)
end
switch(
user,
team_fn: &Teams.Billing.site_limit/1,
user_fn: &Plausible.Billing.Quota.Limits.site_limit/1
)
end

def ensure_can_add_new_site(user) do
if Teams.read_team_schemas?(user) do
team =
case Teams.get_by_owner(user) do
{:ok, team} -> team
{:error, _} -> nil
end

Teams.Billing.ensure_can_add_new_site(team)
else
Plausible.Billing.Quota.ensure_can_add_new_site(user)
end
switch(
user,
team_fn: &Teams.Billing.ensure_can_add_new_site/1,
user_fn: &Plausible.Billing.Quota.ensure_can_add_new_site/1
)
end

def site_usage(user) do
if Teams.read_team_schemas?(user) do
team =
case Teams.get_by_owner(user) do
{:ok, team} -> team
{:error, _} -> nil
end

Teams.Billing.site_usage(team)
else
Plausible.Billing.Quota.Usage.site_usage(user)
end
switch(user,
team_fn: &Teams.Billing.site_usage/1,
user_fn: &Plausible.Billing.Quota.Usage.site_usage/1
)
end
end
58 changes: 23 additions & 35 deletions lib/plausible/teams/adapter/read/ownership.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,47 @@ defmodule Plausible.Teams.Adapter.Read.Ownership do
Transition adapter for new schema reads
"""
use Plausible
use Plausible.Teams.Adapter
alias Plausible.Site
alias Plausible.Auth
alias Plausible.Teams
alias Plausible.Site.Memberships.Invitations

def ensure_can_take_ownership(site, user) do
if Teams.read_team_schemas?(user) do
team =
case Teams.get_by_owner(user) do
{:ok, team} -> team
{:error, _} -> nil
end

Teams.Invitations.ensure_can_take_ownership(site, team)
else
Invitations.ensure_can_take_ownership(site, user)
end
switch(
user,
team_fn: &Teams.Invitations.ensure_can_take_ownership(site, &1),
user_fn: &Invitations.ensure_can_take_ownership(site, &1)
)
end

def has_sites?(user) do
if Teams.read_team_schemas?(user) do
Teams.Users.has_sites?(user, include_pending?: true)
else
Site.Memberships.any_or_pending?(user)
end
switch(
user,
team_fn: fn _ -> Teams.Users.has_sites?(user, include_pending?: true) end,
user_fn: &Site.Memberships.any_or_pending?/1
)
end

def owns_sites?(user, sites) do
if Teams.read_team_schemas?(user) do
Teams.Users.owns_sites?(user, include_pending?: true)
else
Enum.any?(sites.entries, fn site ->
length(site.invitations) > 0 && List.first(site.invitations).role == :owner
end) ||
Auth.user_owns_sites?(user)
end
switch(
user,
team_fn: fn _ -> Teams.Users.owns_sites?(user, include_pending?: true) end,
user_fn: fn user ->
Enum.any?(sites.entries, fn site ->
length(site.invitations) > 0 && List.first(site.invitations).role == :owner
end) ||
Auth.user_owns_sites?(user)
end
)
end

on_ee do
def check_feature_access(site, new_owner) do
user_or_team =
if Teams.read_team_schemas?(new_owner) do
case Teams.get_by_owner(new_owner) do
{:ok, team} -> team
{:error, _} -> nil
end
else
new_owner
end
team_or_user = team_or_user(new_owner)

missing_features =
Plausible.Billing.Quota.Usage.features_usage(nil, [site.id])
|> Enum.filter(&(&1.check_availability(user_or_team) != :ok))
|> Enum.filter(&(&1.check_availability(team_or_user) != :ok))

if missing_features == [] do
:ok
Expand Down
32 changes: 15 additions & 17 deletions lib/plausible/teams/adapter/read/sites.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,29 @@ defmodule Plausible.Teams.Adapter.Read.Sites do

import Ecto.Query

alias Plausible.Auth
alias Plausible.Repo
alias Plausible.Site
alias Plausible.Teams
use Plausible.Teams.Adapter

def list(user, pagination_params, opts \\ []) do
if Plausible.Teams.read_team_schemas?(user) do
Plausible.Teams.Sites.list(user, pagination_params, opts)
else
old_list(user, pagination_params, opts)
end
switch(
user,
team_fn: fn _ -> Plausible.Teams.Sites.list(user, pagination_params, opts) end,
user_fn: fn _ -> old_list(user, pagination_params, opts) end
)
end

def list_with_invitations(user, pagination_params, opts \\ []) do
if Plausible.Teams.read_team_schemas?(user) do
Plausible.Teams.Sites.list_with_invitations(user, pagination_params, opts)
else
old_list_with_invitations(user, pagination_params, opts)
end
switch(
user,
team_fn: fn _ ->
Plausible.Teams.Sites.list_with_invitations(user, pagination_params, opts)
end,
user_fn: fn _ -> old_list_with_invitations(user, pagination_params, opts) end
)
end

@type list_opt() :: {:filter_by_domain, String.t()}
@spec old_list(Auth.User.t(), map(), [list_opt()]) :: Scrivener.Page.t()
def old_list(user, pagination_params, opts \\ []) do
defp old_list(user, pagination_params, opts) do
domain_filter = Keyword.get(opts, :filter_by_domain)

from(s in Site,
Expand Down Expand Up @@ -60,8 +59,7 @@ defmodule Plausible.Teams.Adapter.Read.Sites do
|> Repo.paginate(pagination_params)
end

@spec old_list_with_invitations(Auth.User.t(), map(), [list_opt()]) :: Scrivener.Page.t()
def old_list_with_invitations(user, pagination_params, opts \\ []) do
defp old_list_with_invitations(user, pagination_params, opts) do
domain_filter = Keyword.get(opts, :filter_by_domain)

result =
Expand Down

0 comments on commit f5ac923

Please sign in to comment.