Skip to content

Commit

Permalink
Enable teams sync (plausible#4726)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerosol authored Oct 23, 2024
1 parent 0f93328 commit 1234cbf
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 56 deletions.
9 changes: 0 additions & 9 deletions lib/plausible.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule Plausible do
"""

@ce_builds [:ce, :ce_test, :ce_dev]
@public_builds [:ce, :prod]

defmacro __using__(_) do
quote do
Expand Down Expand Up @@ -59,12 +58,4 @@ defmodule Plausible do
"Plausible Analytics"
end
end

defmacro with_teams(do: do_block) do
if Mix.env() not in @public_builds do
quote do
unquote(do_block)
end
end
end
end
8 changes: 3 additions & 5 deletions lib/plausible/auth/user_admin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,10 @@ defmodule Plausible.Auth.UserAdmin do
]
end

with_teams do
def after_update(_conn, user) do
Plausible.Teams.sync_team(user)
def after_update(_conn, user) do
Plausible.Teams.sync_team(user)

{:ok, user}
end
{:ok, user}
end

defp lock(user) do
Expand Down
12 changes: 3 additions & 9 deletions lib/plausible/site/memberships/accept_invitation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ defmodule Plausible.Site.Memberships.AcceptInvitation do

case Repo.transaction(multi) do
{:ok, changes} ->
with_teams do
Plausible.Teams.Invitations.transfer_site_sync(site, user)
end
Plausible.Teams.Invitations.transfer_site_sync(site, user)

membership = Repo.preload(changes.membership, [:site, :user])

Expand Down Expand Up @@ -77,9 +75,7 @@ defmodule Plausible.Site.Memberships.AcceptInvitation do
site = Repo.preload(invitation.site, :owner)

with :ok <- Invitations.ensure_can_take_ownership(site, user) do
with_teams do
Plausible.Teams.Invitations.accept_transfer_sync(invitation, user)
end
Plausible.Teams.Invitations.accept_transfer_sync(invitation, user)

site
|> add_and_transfer_ownership(membership, user)
Expand All @@ -89,9 +85,7 @@ defmodule Plausible.Site.Memberships.AcceptInvitation do
end

defp do_accept_invitation(invitation, user) do
with_teams do
Plausible.Teams.Invitations.accept_invitation_sync(invitation, user)
end
Plausible.Teams.Invitations.accept_invitation_sync(invitation, user)

membership = get_or_create_membership(invitation, user)

Expand Down
4 changes: 1 addition & 3 deletions lib/plausible/site/memberships/create_invitation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ defmodule Plausible.Site.Memberships.CreateInvitation do
{:ok, invitation} <- Plausible.Repo.insert(changeset) do
send_invitation_email(invitation, invitee)

with_teams do
Plausible.Teams.Invitations.invite_sync(site, invitation)
end
Plausible.Teams.Invitations.invite_sync(site, invitation)

invitation
else
Expand Down
32 changes: 8 additions & 24 deletions lib/plausible/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ defmodule Plausible.Users do
|> Auth.User.changeset(%{accept_traffic_until: accept_traffic_until(user)})
|> Repo.update!()

with_teams do
Plausible.Teams.sync_team(user)
end
Plausible.Teams.sync_team(user)

user
end
Expand Down Expand Up @@ -112,9 +110,7 @@ defmodule Plausible.Users do
|> Auth.User.start_trial()
|> Repo.update!()

with_teams do
Plausible.Teams.sync_team(user)
end
Plausible.Teams.sync_team(user)

user
end
Expand All @@ -125,9 +121,7 @@ defmodule Plausible.Users do
|> Auth.User.changeset(%{allow_next_upgrade_override: true})
|> Repo.update!()

with_teams do
Plausible.Teams.sync_team(user)
end
Plausible.Teams.sync_team(user)

user
end
Expand All @@ -139,9 +133,7 @@ defmodule Plausible.Users do
|> Auth.User.changeset(%{allow_next_upgrade_override: false})
|> Repo.update!()

with_teams do
Plausible.Teams.sync_team(user)
end
Plausible.Teams.sync_team(user)

user
else
Expand All @@ -161,9 +153,7 @@ defmodule Plausible.Users do
|> GracePeriod.start_changeset()
|> Repo.update!()

with_teams do
Plausible.Teams.sync_team(user)
end
Plausible.Teams.sync_team(user)

user
end
Expand All @@ -174,9 +164,7 @@ defmodule Plausible.Users do
|> GracePeriod.start_manual_lock_changeset()
|> Repo.update!()

with_teams do
Plausible.Teams.sync_team(user)
end
Plausible.Teams.sync_team(user)

user
end
Expand All @@ -187,9 +175,7 @@ defmodule Plausible.Users do
|> GracePeriod.end_changeset()
|> Repo.update!()

with_teams do
Plausible.Teams.sync_team(user)
end
Plausible.Teams.sync_team(user)

user
end
Expand All @@ -200,9 +186,7 @@ defmodule Plausible.Users do
|> GracePeriod.remove_changeset()
|> Repo.update!()

with_teams do
Plausible.Teams.sync_team(user)
end
Plausible.Teams.sync_team(user)

user
end
Expand Down
8 changes: 2 additions & 6 deletions lib/plausible_web/controllers/site/membership_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,7 @@ defmodule PlausibleWeb.Site.MembershipController do
|> Membership.set_role(new_role)
|> Repo.update!()

with_teams do
Plausible.Teams.Memberships.update_role_sync(membership)
end
Plausible.Teams.Memberships.update_role_sync(membership)

redirect_target =
if membership.user.id == current_user.id and new_role == :viewer do
Expand Down Expand Up @@ -221,9 +219,7 @@ defmodule PlausibleWeb.Site.MembershipController do
if membership do
Repo.delete!(membership)

with_teams do
Plausible.Teams.Memberships.remove_sync(membership)
end
Plausible.Teams.Memberships.remove_sync(membership)

membership
|> PlausibleWeb.Email.site_member_removed()
Expand Down

0 comments on commit 1234cbf

Please sign in to comment.