mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-12 02:01:39 -05:00
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
51 lines
1.4 KiB
Ruby
51 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Trip < ApplicationRecord
|
|
include Calculateable
|
|
include DistanceConvertible
|
|
include Shareable
|
|
|
|
has_rich_text :notes
|
|
|
|
belongs_to :user
|
|
|
|
validates :name, :started_at, :ended_at, presence: true
|
|
|
|
after_create :enqueue_calculation_jobs
|
|
after_update :enqueue_calculation_jobs, if: -> { saved_change_to_started_at? || saved_change_to_ended_at? }
|
|
|
|
def enqueue_calculation_jobs
|
|
Trips::CalculateAllJob.perform_later(id, user.safe_settings.distance_unit)
|
|
end
|
|
|
|
def points
|
|
user.points.where(timestamp: started_at.to_i..ended_at.to_i).order(:timestamp)
|
|
end
|
|
|
|
def photo_previews
|
|
@photo_previews ||= select_dominant_orientation(photos).sample(12)
|
|
end
|
|
|
|
def photo_sources
|
|
@photo_sources ||= photos.map { _1[:source] }.uniq
|
|
end
|
|
|
|
def calculate_countries
|
|
self.visited_countries = points.pluck(:country_name).uniq.compact
|
|
end
|
|
|
|
private
|
|
|
|
def photos
|
|
@photos ||= Trips::Photos.new(self, user).call
|
|
end
|
|
|
|
def select_dominant_orientation(photos)
|
|
vertical_photos = photos.select { |photo| photo[:orientation] == 'portrait' }
|
|
horizontal_photos = photos.select { |photo| photo[:orientation] == 'landscape' }
|
|
|
|
# this is ridiculous, but I couldn't find my way around frontend
|
|
# to show all photos in the same height
|
|
vertical_photos.count > horizontal_photos.count ? vertical_photos : horizontal_photos
|
|
end
|
|
end
|