8000 refactor(organize): give Social its own LiveView by mwoods79 · Pull Request #716 · huddlz-hq/huddlz · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/huddlz/communities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ defmodule Huddlz.Communities do
define :drop_in?, action: :drop_in?, args: [:group_id]
define :list_drop_ins, action: :drop_ins
define :get_by_slug, action: :get_by_slug, args: [:slug]
define :get_group_for_organize, action: :get_for_organize, args: [:slug], get?: true
define :get_group_for_organize, action: :get_for_organize, get_by: [:slug]
define :get_group_for_organize_by_id, action: :get_for_organize, get_by: [:id]
define :group_overview, action: :overview, args: [:group_id, {:optional, :period}]

define :update_details,
Expand Down
56 changes: 30 additions & 26 deletions lib/huddlz/communities/group.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Huddlz.Communities.Group do
domain: Huddlz.Communities,
data_layer: AshPostgres.DataLayer,
authorizers: [Ash.Policy.Authorizer],
notifiers: [Ash.Notifier.PubSub],
extensions: [
AshPaperTrail.Resource,
AshJsonApi.Resource,
Expand Down Expand Up @@ -79,20 +80,6 @@ defmodule Huddlz.Communities.Group do
end
end

paper_trail do
change_tracking_mode :snapshot
store_action_name? true
reference_source? false
sensitive_attributes :ignore
ignore_attributes [:inserted_at, :updated_at]
belongs_to_actor :actor, Huddlz.Accounts.User, domain: Huddlz.Accounts, on_delete: :nilify
metadata :impersonation_id, :uuid
10BC8 metadata :impersonator_id, :uuid
metadata :automatic?, :boolean
version_extensions authorizers: [Ash.Policy.Authorizer]
mixin {Huddlz.Audit.Version, :mixin, []}
end

postgres do
table "groups"
repo Huddlz.Repo
Expand All @@ -107,6 +94,20 @@ defmodule Huddlz.Communities.Group do
end
end

paper_trail do
change_tracking_mode :snapshot
store_action_name? true
reference_source? false
sensitive_attributes :ignore
ignore_attributes [:inserted_at, :updated_at]
belongs_to_actor :actor, Huddlz.Accounts.User, domain: Huddlz.Accounts, on_delete: :nilify
metadata :impersonation_id, :uuid
metadata :impersonator_id, :uuid
metadata :automatic?, :boolean
version_extensions authorizers: [Ash.Policy.Authorizer]
mixin {Huddlz.Audit.Version, :mixin, []}
end

archive do
exclude_destroy_actions([:destroy])

Expand Down Expand Up @@ -327,19 +328,10 @@ defmodule Huddlz.Communities.Group do

read :get_for_organize do
description """
Get a group by slug, but only when the actor can manage it as an
organizer — they own the group or are an `:organizer` GroupMember.
Being an administrator does not count. Drives `OrganizeLive`'s
per-slug auth check; nil result means "not organizable by this actor."
Read a group the actor owns or helps organize, including archived
groups. Both opening a workspace by slug and refreshing it by id
use this action's organizer policies.
"""

get? true

argument :slug, :string do
allow_nil? false
end

filter expr(slug == ^arg(:slug))
end

update :update_details do
Expand Down Expand Up @@ -443,6 +435,18 @@ defmodule Huddlz.Communities.Group do
end
end

pub_sub do
module Phoenix.PubSub
name Huddlz.PubSub
prefix "group"
transform fn notification -> {:group_changed, notification.data.id} end

publish :archive, [:id]
publish :unarchive, [:id]
publish :update_details, [:id]
publish :destroy, [:id]
end

validations do
validate string_length(:name, min: @name_length.first, max: @name_length.last) do
message "Must be between 3 and 100 characters"
Expand Down
9 changes: 9 additions & 0 deletions lib/huddlz/communities/social_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ defmodule Huddlz.Communities.SocialConnection do
authorize_if Huddlz.Communities.GroupMember.Checks.GroupOwner
end

# The Social page is read-only when the group closes or becomes private.
# Outside that workspace an owner can still disconnect a place, and an
# organizer can stop posting. Resuming or editing requires an open public group.
policy action([:edit, :pause, :resume, :remove]) do
authorize_if expr(group.is_public == true and is_nil(group.archived_at))
forbid_if context_equals(:social_workspace?, true)
authorize_if action([:pause, :remove])
end

policy action([:edit, :reconnect, :remove, :mark_needs_reconnecting]) do
authorize_if expr(group.owner_id == ^actor(:id))
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Huddlz.Communities.SocialConnection.Checks.OwnsConnectionArgument do
@moduledoc """
Authorizes a generic action whose `id` argument names a social connection
of a group the actor owns. Generic actions have no record to write an
of an open public group the actor owns. Generic actions have no record to write an
expression policy against, so the lookup happens here.
"""

Expand All @@ -12,12 +12,15 @@ defmodule Huddlz.Communities.SocialConnection.Checks.OwnsConnectionArgument do
alias Huddlz.Communities.SocialConnection

@impl true
def describe(_opts), do: "actor owns the group of the connection named by id"
def describe(_opts), do: "actor owns the open public group of the connection named by id"

@impl true
def match?(%{id: actor_id}, %{subject: %{arguments: %{id: id}}}, _opts) when is_binary(id) do
SocialConnection
|> Ash.Query.filter(id == ^id and group.owner_id == ^actor_id)
|> Ash.Query.filter(
id == ^id and group.owner_id == ^actor_id and
group.is_public == true and is_nil(group.archived_at)
)
|> Ash.exists?(authorize?: false)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/huddlz_web/components/layouts.ex
3 changes: 2 additions & 1 deletion lib/huddlz_web/components/modal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule HuddlzWeb.Layouts do

attr :active_organize_section, :atom,
default: nil,
values: [nil, :overview, :huddlz, :members, :settings],
values: [nil, :overview, :huddlz, :members, :social, :settings],
doc: "active sub-tab inside an organize-group section"

attr :active_admin_section, :atom,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule HuddlzWeb.Components.Modal do
attr :id, :string, required: true
attr :show, :boolean, default: false
attr :on_cancel, JS, default: %JS{}
attr :dismiss_on_cancel, :boolean, default: true
attr :class, :string, default: "w-full max-w-xl"
attr :return_focus, :string, default: nil
slot :inner_block, required: true
Expand All @@ -29,7 +30,7 @@ defmodule HuddlzWeb.Components.Modal do
phx-remove={
if @return_focus, do: hide_modal(@id) |> JS.focus(to: @return_focus), else: hide_modal(@id)
}
data-cancel={JS.exec(@on_cancel, "phx-remove")}
data-cancel={if @dismiss_on_cancel, do: JS.exec(@on_cancel, "phx-remove"), else: @on_cancel}
class="relative z-50 hidden"
>
<div
Expand Down
Loading
0