8000 fix(groups): keep a group's address book with its organizers by mwoods79 · Pull Request #725 · 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
13 changes: 4 additions & 9 deletions lib/huddlz/communities/group_location.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,10 @@ defmodule Huddlz.Communities.GroupLocation do
authorize_if Huddlz.Communities.Huddl.Checks.GroupOwnerOrOrganizer
end

# Locations belong to a group. Mirror the Group read policy so a private
# group's saved meeting addresses are only visible to its members, while
# public groups' locations stay readable by anyone (including anonymous).
policy action_type(:read) do
authorize_if expr(group.is_public == true and is_nil(group.archived_at))
authorize_if relates_to_actor_via([:group, :members])
end

policy action_type([:update, :destroy]) do
# The address book belongs to the people who run the group. An entry may
# be used only by a members-only huddl or a draft, or not at all, so
# everyone else learns a place from a huddl they can see, never from here.
policy action_type([:read, :update, :destroy]) do
authorize_if expr(group.owner_id == ^actor(:id))

authorize_if expr(
Expand Down
10 changes: 10 additions & 0 deletions lib/huddlz/communities/huddl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1381,5 +1381,15 @@ defmodule Huddlz.Communities.Huddl do
sort inserted_at: :desc
filter expr(is_nil(deleted_at))
end

# The address book is readable only by the people who run the group. A
# huddl still names its own place to everyone who can see the huddl.
# Only a location of the huddl's own group is ever named, whatever id an
# older row may carry.
first :location_name, :group_location, :name do
description "The name of the address book location this huddl meets at"
authorize? false
filter expr(group_id == parent(group_id))
end
end
end
10 changes: 3 additions & 7 deletions lib/huddlz/communities/huddl/changes/apply_saved_location.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,9 @@ defmodule Huddlz.Communities.Huddl.Changes.ApplySavedLocation do
|> Ash.Changeset.force_change_attribute(:latitude, location.latitude)
|> Ash.Changeset.force_change_attribute(:longitude, location.longitude)

{:ok, %GroupLocation{}} ->
Ash.Changeset.add_error(changeset,
field: :group_location_id,
message: "saved location must belong to the huddl's group"
)

{:error, _error} ->
# A location in another group's address book answers exactly like one
# that does not exist.
_unavailable ->
Ash.Changeset.add_error(changeset,
field: :group_location_id,
message: "saved location is unavailable"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ defmodule Huddlz.Communities.Huddl.Changes.ClearUnusedLocationFields do
virtual huddlz carry no physical_location, in-person huddlz carry no
virtual_link. Hybrid huddlz keep both.

Only acts on create or when event_type is being set — an unrelated update
(e.g. a title edit) must not touch stored location data, re-trigger
geocoding, or notify attendees of a location change.
Only acts on create, when event_type is being set, or when an address book
location is being set — an unrelated update (e.g. a title edit) must not
touch stored location data, re-trigger geocoding, or notify attendees of a
location change. An online huddl never keeps an address book location, so
one named on it is dropped rather than stored unchecked.

Runs before geocoding so a cleared physical_location is never geocoded.
"""
use Ash.Resource.Change

def change(changeset, _opts, _context) do
if changeset.action_type == :create or
Ash.Changeset.changing_attribute?(changeset, :event_type) do
Ash.Changeset.changing_attribute?(changeset, :event_type) or
Ash.Changeset.changing_attribute?(changeset, :group_location_id) do
clear_unused_field(changeset)
else
changeset
Expand Down
2 changes: 1 addition & 1 deletion lib/huddlz_web/components/structured_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ defmodule HuddlzWeb.StructuredData do
defp physical_location(huddl) do
%{
"@type" => "Place",
"name" => huddl.group_location && huddl.group_location.name,
"name" => huddl.location_name,
"address" => %{"@type" => "PostalAddress", "name" => huddl.physical_location}
}
|> Map.reject(fn {_key, value} -> is_nil(value) end)
Expand Down
7 changes: 5 additions & 2 deletions lib/huddlz_web/live/group_live/locations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,11 @@ defmodule HuddlzWeb.GroupLive.Locations do

@impl true
def handle_event("confirm_delete_location", %{"id" => id}, socket) do
with location when not is_nil(location) <-
Enum.find(socket.assigns.locations, &(&1.id == id)),
# The list on the page may be older than the person's role, so the
# location is read again as them before anything about its use is looked up.
locations = load_group_locations(socket.assigns.group.id, socket.assigns.current_user)

with location when not is_nil(location) <- Enum.find(locations, &(&1.id == id)),
{:ok, references} <- DeletionImpact.active_references(location) do
{:noreply,
socket
Expand Down
6 changes: 3 additions & 3 deletions lib/huddlz_web/live/huddl_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule HuddlzWeb.HuddlLive.Show do
:display_image_url,
:turnout_total,
:show_rate,
:group_location,
:location_name,
group: [:member_count, :current_image_url],
creator: [:current_profile_picture_url]
]
Expand Down Expand Up @@ -430,8 +430,8 @@ defmodule HuddlzWeb.HuddlLive.Show do
<div>
<div class="label">Where</div>
<div class="value">
<span :if={@huddl.group_location && @huddl.group_location.name} class="block">
{@huddl.group_location.name}
<span :if={@huddl.location_name} class="block">
{@huddl.location_name}
</span>
<span class="whitespace-pre-line">{@huddl.physical_location}</span>
<a
Expand Down
52 changes: 52 additions & 0 deletions test/features/address_book_api.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@async @database @conn @address_book_api
Feature: A group's address book is for the people who run the group
As a group organizer
I want the places I save to stay between the people who run my group
So that an address only reaches the people who can see a huddl held there

Scenario Outline: The people who run a group list its address book
Given a "<visibility>" group with "Maple Street House" saved in its address book
When "<viewer>" lists the group's address book through "<api>"
Then the address book lists "Maple Street House"

Examples:
| visibility | viewer | api |
| public | the owner | JSON:API |
| public | an organizer | GraphQL |
| private | the owner | GraphQL |
| private | an organizer | JSON:API |

Scenario Outline: Nobody else can list a group's address book
Given a "<visibility>" group with "Maple Street House" saved in its address book
When "<viewer>" lists the group's address book through "<api>"
Then the address book lists nothing

Examples:
| visibility | viewer | api |
| public | a visitor | JSON:API |
| public | a visitor | GraphQL |
| public | a nonmember | JSON:API |
| public | a member | GraphQL |
| private | a member | JSON:API |
| private | a member | GraphQL |

Scenario Outline: A member cannot change an address book location
Given a "public" group with "Maple Street House" saved in its address book
When "a member" renames that address book location to "Somewhere Else" through "<api>"
Then the rename is refused
And the owner sees that address book location named "Maple Street House"

Examples:
| api |
| JSON:API |
| GraphQL |

Scenario Outline: The owner renames an address book location
Given a "public" group with "Maple Street House" saved in its address book
When "the owner" renames that address book location to "Somewhere Else" through "<api>"
Then the owner sees that address book location named "Somewhere Else"

Examples:
| api |
| JSON:API |
| GraphQL |
8 changes: 5 additions & 3 deletions test/features/group_archival.feature
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ Feature: Group archival
When I open the archived huddl "Cancelled outing"
Then I should see "This group is archived"

Scenario Outline: Archived addresses are available only to members
Scenario Outline: Archived addresses are available only to the people who ran the group
Given the following users exist:
| email | role | display_name |
| owner+group-archival@example.com | user | Owner |
Expand All @@ -321,9 +321,11 @@ Feature: Group archival

Examples:
| actor | api | visibility |
| member+group-archival@example.com | GraphQL | visible |
| owner+group-archival@example.com | GraphQL | visible |
| member+group-archival@example.com | GraphQL | hidden |
| other@example.com | GraphQL | hidden |
| member+group-archival@example.com | JSON:API | visible |
| owner+group-archival@example.com | JSON:API | visible |
| member+group-archival@example.com | JSON:API | hidden |
| other@example.com | JSON:API | hidden |

@archived_overview
Expand Down
149 changes: 149 additions & 0 deletions test/features/step_definitions/address_book_api_steps.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
defmodule AddressBookApiSteps do
use Cucumber.StepDefinition

import ExUnit.Assertions
import Huddlz.Generator
import HuddlzWeb.ApiCase
import Phoenix.ConnTest

alias Huddlz.Communities.GroupLocation

@address "412 Maple Street, Portland, OR"

step "a {string} group with {string} saved in its address book",
%{args: [visibility, name]} = context do
owner = generate(user())
organizer = generate(user())
member = generate(user())

{group, _memberships} =
generate_group_with_members(
owner: owner,
group: [is_public: visibility == "public"],
members: [%{user: organizer, role: :organizer}, %{user: member, role: :member}]
)

entry =
generate(group_location(group_id: group.id, actor: owner, name: name, address: @address))

Map.merge(context, %{
address_book_group: group,
address_book_entry: entry,
address_book_people: %{
"the owner" => owner,
"an organizer" => organizer,
"a member" => member
}
})
end

step "{string} lists the group's address book through {string}",
%{args: [viewer, api]} = context do
response = list_address_book(conn_for(context, viewer), context.address_book_group.id, api)

Map.merge(context, %{address_book_response: response, address_book_api: api})
end

step "{string} renames that address book location to {string} through {string}",
%{args: [viewer, name, api]} = context do
response = rename_entry(conn_for(context, viewer), context.address_book_entry.id, name, api)

Map.merge(context, %{rename_response: response, address_book_api: api})
end

step "the rename is refused", context do
assert rename_refused?(context.address_book_api, context.rename_response)

context
end

step "the address book lists {string}", %{args: [name]} = context do
assert [%{"name" => ^name, "address" => @address}] = listed_entries(context)

context
end

step "the address book lists nothing", context do
assert listed_entries(context) == []
refute context.address_book_response.resp_body =~ to_string(context.address_book_entry.name)
refute context.address_book_response.resp_body =~ @address

context
end

step "the owner sees that address book location named {string}", %{args: [name]} = context do
owner = Map.fetch!(context.address_book_people, "the owner")

assert [%{name: ^name}] =
GroupLocation
|> Ash.Query.for_read(:by_group, %{group_id: context.address_book_group.id})
|> Ash.read!(actor: owner)

context
end

# A member is answered as if the location were not there.
defp rename_refused?("JSON:API", response), do: response.status == 404

defp rename_refused?("GraphQL", response) do
body = json_response(response, 200)
mutation = get_in(body, ["data", "updateGroupLocation"]) || %{}
errors = (body["errors"] || []) ++ (mutation["errors"] || [])

is_nil(mutation["result"]) and Enum.any?(errors, &(&1["code"] == "not_found"))
end

defp conn_for(_context, "a visitor"), do: build_conn()

defp conn_for(_context, "a nonmember"),
do: authenticated_conn(build_conn(), generate(user()))

defp conn_for(context, viewer),
do: authenticated_conn(build_conn(), Map.fetch!(context.address_book_people, viewer))

defp list_address_book(conn, group_id, "JSON:API") do
dispatch(conn, HuddlzWeb.Endpoint, :get, "/api/json/group_locations/by_group", %{
"group_id" => group_id
})
end

defp list_address_book(conn, group_id, "GraphQL") do
gql_post(
conn,
"query ($groupId: ID!) { groupLocations(groupId: $groupId) { id name address } }",
%{"groupId" => group_id}
)
end

defp rename_entry(conn, id, name, "JSON:API") do
conn
|> Plug.Conn.put_req_header("content-type", "application/vnd.api+json")
|> dispatch(HuddlzWeb.Endpoint, :patch, "/api/json/group_locations/#{id}", %{
"data" => %{"type" => "group_location", "attributes" => %{"name" => name}}
})
end

defp rename_entry(conn, id, name, "GraphQL") do
gql_post(
conn,
"""
mutation ($id: ID!, $input: UpdateGroupLocationInput) {
updateGroupLocation(id: $id, input: $input) { result { id name } errors { message code } }
}
""",
%{"id" => id, "input" => %{"name" => name}}
)
end

defp listed_entries(%{address_book_api: "JSON:API", address_book_response: response}) do
body = json_response(response, 200)
refute Map.has_key?(body, "errors")
Enum.map(body["data"], & &1["attributes"])
end

defp listed_entries(%{address_book_api: "GraphQL", address_book_response: response}) do
body = json_response(response, 200)
refute Map.has_key?(body, "errors")
Enum.map(body["data"]["groupLocations"], &Map.take(&1, ["name", "address"]))
end
end
Loading
0