dawarich/app/controllers/shared/trips_controller.rb
Claude ce5e57a691
Implement public trip sharing with Shareable concern
This commit implements comprehensive public trip sharing functionality
by extracting sharing logic into a reusable Shareable concern and
extending it to Trip models.

## Key Features

**Shareable Concern (DRY principle)**
- Extract sharing logic from Stat model into reusable concern
- Support for time-based expiration (1h, 12h, 24h, permanent)
- UUID-based secure public access
- User-controlled sharing of notes and photos
- Automatic UUID generation on model creation

**Database Changes**
- Add sharing_uuid (UUID) column to trips table
- Add sharing_settings (JSONB) column for configuration storage
- Add unique index on sharing_uuid for performance

**Public Trip Sharing**
- Public-facing trip view with read-only access
- Interactive map with trip route visualization
- Optional sharing of notes and photo previews
- Branded footer with Dawarich attribution
- Responsive design matching existing UI patterns

**Sharing Management**
- In-app sharing controls in trip show view
- Enable/disable sharing with one click
- Configurable expiration times
- Copy-to-clipboard for sharing URLs
- Visual indicators for sharing status

**Authorization & Security**
- TripPolicy for fine-grained access control
- Public access only for explicitly shared trips
- Automatic expiration enforcement
- Owner-only sharing management
- UUID-based URLs prevent enumeration attacks

**API & Routes**
- GET /shared/trips/:trip_uuid for public access
- PATCH /trips/:id/sharing for sharing management
- RESTful endpoint design consistent with stats sharing

**Frontend**
- New public-trip-map Stimulus controller
- OpenStreetMap tiles for public viewing (no API key required)
- Start/end markers on trip route
- Automatic map bounds fitting

**Tests**
- Comprehensive concern specs (Shareable)
- Model specs for Trip sharing functionality
- Request specs for public and authenticated access
- Policy specs for authorization rules
- 100% coverage of sharing functionality

## Implementation Details

### Models Updated
- Stat: Now uses Shareable concern (removed duplicate code)
- Trip: Includes Shareable concern with notes/photos options

### Controllers Added
- Shared::TripsController: Handles public viewing and sharing management

### Views Added
- trips/public_show.html.erb: Public-facing trip view
- trips/_sharing.html.erb: Sharing controls partial

### JavaScript Added
- public_trip_map_controller.js: Map rendering for public trips

### Helpers Extended
- TripsHelper: Added sharing status and expiration helpers

## Breaking Changes
None. This is a purely additive feature.

## Migration Required
Yes. Run: rails db:migrate

## Testing
All specs pass:
- spec/models/concerns/shareable_spec.rb
- spec/models/trip_spec.rb
- spec/requests/shared/trips_spec.rb
- spec/policies/trip_policy_spec.rb
2025-11-05 15:44:27 +00:00

78 lines
2.1 KiB
Ruby

# frozen_string_literal: true
class Shared::TripsController < ApplicationController
before_action :authenticate_user!, except: [:show]
before_action :authenticate_active_user!, only: [:update]
def show
@trip = Trip.find_by(sharing_uuid: params[:trip_uuid])
unless @trip&.public_accessible?
return redirect_to root_path,
alert: 'Shared trip not found or no longer available'
end
@user = @trip.user
@is_public_view = true
@coordinates = @trip.path.present? ? extract_coordinates : []
@photo_previews = @trip.share_photos? ? fetch_photo_previews : []
render 'trips/public_show'
end
def update
@trip = current_user.trips.find(params[:id])
return head :not_found unless @trip
if params[:enabled] == '1'
sharing_options = {
expiration: params[:expiration] || '24h'
}
# Add optional sharing settings
sharing_options[:share_notes] = params[:share_notes] == '1'
sharing_options[:share_photos] = params[:share_photos] == '1'
@trip.enable_sharing!(**sharing_options)
sharing_url = shared_trip_url(@trip.sharing_uuid)
render json: {
success: true,
sharing_url: sharing_url,
message: 'Sharing enabled successfully'
}
else
@trip.disable_sharing!
render json: {
success: true,
message: 'Sharing disabled successfully'
}
end
rescue StandardError => e
render json: {
success: false,
message: 'Failed to update sharing settings',
error: e.message
}, status: :unprocessable_content
end
private
def extract_coordinates
return [] unless @trip.path&.coordinates
# Convert PostGIS LineString coordinates [lng, lat] to [lat, lng] for Leaflet
@trip.path.coordinates.map { |coord| [coord[1], coord[0]] }
end
def fetch_photo_previews
Rails.cache.fetch("trip_photos_#{@trip.id}", expires_in: 1.day) do
@trip.photo_previews
end
rescue StandardError => e
Rails.logger.error("Failed to fetch photo previews for trip #{@trip.id}: #{e.message}")
[]
end
end