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
10 changes: 10 additions & 0 deletions app/models/moment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Moment < ApplicationRecord
validates :note, length: { maximum: 1000 }
validate :photo_present
validate :acceptable_photo
validate :captured_at_a_visited_place, on: :create

# A moment is never visible on publish alone — going public re-enters
# moderation, so a curator must approve it before anyone else can see it.
Expand Down Expand Up @@ -85,6 +86,15 @@ def require_moderation_when_published
self.moderation_status = :pending if visibility_public_moment? && visibility_changed?
end

# Capture is earned by being there: a check-in at this place on any of the
# traveller's plans. Asked on create only, so a moment outlives its check-in.
def captured_at_a_visited_place
return if user.nil? || location.nil?
return if user.plan_visits.exists?(location_id: location_id)

errors.add(:base, I18n.t("plans.moments.not_visited"))
end

def photo_present
errors.add(:photo, :blank) unless photo.attached?
end
Expand Down
12 changes: 8 additions & 4 deletions app/views/plans/_moment_gallery.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@
</div>

<div class="<%= layout %>">
<div class="<%= tile[:wrapper] %>" data-action="submit->photo-gallery#closeLightbox">
<%= render "plans/moment_instant_form", plan: plan, location: location,
context: context, compact: true, tile: tile %>
</div>
<%# Capture is earned by being there. A guest's check-ins live on the
device until sign-in, so the guest keeps the sign-in tile. %>
<% if !logged_in? || visited_location?(location) %>
<div class="<%= tile[:wrapper] %>" data-action="submit->photo-gallery#closeLightbox">
<%= render "plans/moment_instant_form", plan: plan, location: location,
context: context, compact: true, tile: tile %>
</div>
<% end %>

<div class="contents" data-load-more-target="container">
<%= render "plans/moment_gallery_items", moments: moments,
Expand Down
1 change: 1 addition & 0 deletions config/locales/bs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ bs:
note_save: Sačuvaj bilješku
note_saved: Bilješka sačuvana
note_failed: Bilješka nije sačuvana
not_visited: Prvo ovdje označi posjetu. Uspomena se bilježi na mjestu do kojeg si stigao/la.
public: Javno
pending: Čeka pregled
add_for_plan: Dodaj uspomene
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ en:
note_save: Save note
note_saved: Note saved
note_failed: That note could not be saved
not_visited: Mark your visit here first. A moment is captured at a place you have reached.
public: Public
pending: Awaiting review
add_for_plan: Add moments
Expand Down
7 changes: 7 additions & 0 deletions test/application_system_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ def load_profile_moments
assert_selector "#my-moments-frame [data-photo-gallery-target='thumbnail']", minimum: 1, wait: 10
end

# A traveller with check-ins is congratulated on the badges the profile's
# server sync awards, and the celebration covers the page until dismissed.
def dismiss_badge_celebration
find("div.fixed.inset-0.bg-black\\/50 > div > button", wait: 10).click
assert_no_selector "div.fixed.inset-0.bg-black\\/50"
end

# Submitting before the field values have landed posts a blank form, which
# re-renders the login page — no error raised, the test simply never left it.
# Assert the value is in before pressing, and press again if the page did not
Expand Down
1 change: 1 addition & 0 deletions test/controllers/curator/moments_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def login_as(user)
end

def build_public_pending_moment
@user.plan_visits.find_or_create_by!(plan: @plan, location: @location)
moment = @user.moments.build(plan: @plan, location: @location)
moment.photo.attach(
io: File.open("test/fixtures/files/test_image.jpg"),
Expand Down
1 change: 1 addition & 0 deletions test/controllers/moments/likes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def login_as(user)
end

def moment_for(visibility:, moderation:)
@owner.plan_visits.find_or_create_by!(plan: @plan, location: @location)
moment = @owner.moments.build(plan: @plan, location: @location)
moment.photo.attach(io: StringIO.new("fake image data"), filename: "heart.jpg", content_type: "image/jpeg")
moment.save!
Expand Down
13 changes: 13 additions & 0 deletions test/controllers/moments_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MomentsControllerTest < ActionDispatch::IntegrationTest
@experience.locations << @location
@plan = Plan.create!(title: "Moment Trip", city_name: "Sarajevo", visibility: :public_plan, user: @owner)
@plan.plan_experiences.create!(experience: @experience, day_number: 1)
@owner.plan_visits.create!(plan: @plan, location: @location)
end

teardown do
Expand Down Expand Up @@ -44,6 +45,17 @@ class MomentsControllerTest < ActionDispatch::IntegrationTest
assert moment.photo.attached?
end

test "create is refused before a check-in at the place" do
@owner.plan_visits.destroy_all
login_as(@owner)

assert_no_difference "Moment.count" do
post plan_moments_path(@plan), params: moment_params
end

assert_equal I18n.t("plans.moments.not_visited"), flash[:alert]
end

test "create rejects a non-image upload" do
login_as(@owner)

Expand Down Expand Up @@ -125,6 +137,7 @@ class MomentsControllerTest < ActionDispatch::IntegrationTest
end

test "create allows a public plan the user does not own" do
@stranger.plan_visits.create!(plan: @plan, location: @location)
login_as(@stranger)

assert_difference "Moment.count", 1 do
Expand Down
4 changes: 4 additions & 0 deletions test/controllers/new_design_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ class NewDesignControllerTest < ActionDispatch::IntegrationTest

test "explore returns approved public moments under the moment type" do
user = User.create!(username: "explorer_sharer", email: "explorer_sharer@example.com", password: "password123")
user.plan_visits.find_or_create_by!(plan: @plan, location: @location)
moment = user.moments.build(plan: @plan, location: @location)
moment.photo.attach(io: File.open(file_fixture("real_image.jpg")), filename: "m.jpg", content_type: "image/jpeg")
moment.save!
Expand Down Expand Up @@ -241,6 +242,7 @@ class NewDesignControllerTest < ActionDispatch::IntegrationTest

test "explore does not surface a pending or private moment" do
user = User.create!(username: "private_sharer", email: "private_sharer@example.com", password: "password123")
user.plan_visits.find_or_create_by!(plan: @plan, location: @mostar_location)
moment = user.moments.build(plan: @plan, location: @mostar_location)
moment.photo.attach(io: File.open(file_fixture("real_image.jpg")), filename: "m.jpg", content_type: "image/jpeg")
moment.save!
Expand Down Expand Up @@ -800,6 +802,7 @@ class NewDesignControllerTest < ActionDispatch::IntegrationTest
private

def own_moment_for(user, location)
user.plan_visits.find_or_create_by!(plan: @plan, location: location)
moment = user.moments.build(plan: @plan, location: location)
moment.photo.attach(io: File.open(file_fixture("real_image.jpg")), filename: "m.jpg", content_type: "image/jpeg")
moment.save!
Expand Down Expand Up @@ -830,6 +833,7 @@ def sync_browse_records
end

def publish_moment(user, location)
user.plan_visits.find_or_create_by!(plan: @plan, location: location)
moment = user.moments.build(plan: @plan, location: location)
moment.photo.attach(io: File.open(file_fixture("real_image.jpg")), filename: "m.jpg", content_type: "image/jpeg")
moment.save!
Expand Down
1 change: 1 addition & 0 deletions test/controllers/travel_profiles_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ class TravelProfilesControllerTest < ActionDispatch::IntegrationTest
private

def own_moment
@user.plan_visits.find_or_create_by!(plan: @plan, location: @location)
moment = @user.moments.build(plan: @plan, location: @location)
moment.photo.attach(io: File.open(file_fixture("test_image.jpg")), filename: "m.jpg", content_type: "image/jpeg")
moment.save!
Expand Down
31 changes: 28 additions & 3 deletions test/integration/explore_bosnia_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -527,21 +527,29 @@ class ExploreBosniaTest < ActionDispatch::IntegrationTest
Location.where("name LIKE 'Spot %'").destroy_all
end

# A place with a moment of your own is a place you have visited, so it is no
# longer dealt; the moment that can still ride along is another traveller's.
test "the deck ships no moments until a panel is opened" do
moment = @user.moments.new(plan: Plan.explore_bosnia_for(@user), location: @near, visibility: :private_moment)
other = User.create!(username: "deck_wanderer", email: "deck_wanderer@example.com", password: "password123")
other.plan_visits.create!(plan: Plan.explore_bosnia_for(other), location: @near)
moment = other.moments.new(plan: Plan.explore_bosnia_for(other), location: @near, visibility: :public_moment)
moment.photo.attach(io: File.open(Rails.root.join("test/fixtures/files/real_image.jpg")), filename: "real_image.jpg", content_type: "image/jpeg")
moment.save!
moment.update!(moderation_status: :approved)
login_as(@user)

get explore_bosnia_experience_path("history", **SARAJEVO)

assert_response :success
assert_select "turbo-frame[id=?][loading='lazy']",
ActionView::RecordIdentifier.dom_id(@near, :moments_frame), count: 1
refute_includes response.body, photo_plan_moment_path(moment.plan, moment, size: "thumb")
refute_includes response.body, moment.photo.blob.signed_id
ensure
other&.destroy
end

test "the moments frame renders the gallery for its location" do
@user.plan_visits.find_or_create_by!(plan: Plan.explore_bosnia_for(@user), location: @near)
moment = @user.moments.new(plan: Plan.explore_bosnia_for(@user), location: @near, visibility: :private_moment)
moment.photo.attach(io: File.open(Rails.root.join("test/fixtures/files/real_image.jpg")), filename: "real_image.jpg", content_type: "image/jpeg")
moment.save!
Expand All @@ -560,6 +568,7 @@ class ExploreBosniaTest < ActionDispatch::IntegrationTest
test "the gallery shows every moment in rows of three, not a sideways strip" do
plan = Plan.explore_bosnia_for(@user)
moments = 5.times.map do
@user.plan_visits.find_or_create_by!(plan: plan, location: @near)
moment = @user.moments.new(plan: plan, location: @near, visibility: :private_moment)
moment.photo.attach(io: File.open(Rails.root.join("test/fixtures/files/real_image.jpg")),
filename: "real_image.jpg", content_type: "image/jpeg")
Expand Down Expand Up @@ -630,7 +639,19 @@ class ExploreBosniaTest < ActionDispatch::IntegrationTest
assert_select "form[action=?]", publish_plan_moment_path(moment.plan, moment), count: 1
end

test "the moments panel offers the upload tile without a visit" do
test "the moments panel offers no upload tile before a visit" do
login_as(@user)

get plan_moments_path(Plan.explore_bosnia_for(@user), location_id: @near.uuid, context: "explore"),
headers: { "Turbo-Frame" => ActionView::RecordIdentifier.dom_id(@near, :moments_frame) }

assert_response :success
assert_select "label[aria-label=?]", I18n.t("plans.moments.add"), count: 0
end

test "the moments panel offers the upload tile once the place is visited on any plan" do
other_plan = Plan.create!(title: "Trip", visibility: :private_plan, user: @user)
@user.plan_visits.create!(plan: other_plan, location: @near)
login_as(@user)

get plan_moments_path(Plan.explore_bosnia_for(@user), location_id: @near.uuid, context: "explore"),
Expand All @@ -642,6 +663,7 @@ class ExploreBosniaTest < ActionDispatch::IntegrationTest

test "the moments panel shows another traveller's approved public moment" do
other = User.create!(username: "other_wanderer", email: "other_wanderer@example.com", password: "password123")
other.plan_visits.find_or_create_by!(plan: Plan.explore_bosnia_for(other), location: @near)
moment = other.moments.new(plan: Plan.explore_bosnia_for(other), location: @near, visibility: :public_moment)
moment.photo.attach(io: File.open(Rails.root.join("test/fixtures/files/real_image.jpg")), filename: "real_image.jpg", content_type: "image/jpeg")
moment.save!
Expand All @@ -658,6 +680,7 @@ class ExploreBosniaTest < ActionDispatch::IntegrationTest
end

test "an own approved public moment removes the be-first invitation" do
@user.plan_visits.find_or_create_by!(plan: Plan.explore_bosnia_for(@user), location: @near)
moment = @user.moments.new(plan: Plan.explore_bosnia_for(@user), location: @near,
visibility: :public_moment)
moment.photo.attach(io: File.open(Rails.root.join("test/fixtures/files/real_image.jpg")), filename: "real_image.jpg", content_type: "image/jpeg")
Expand All @@ -677,6 +700,7 @@ class ExploreBosniaTest < ActionDispatch::IntegrationTest
end

test "publishing from the story actually publishes and streams the carousel back" do
@user.plan_visits.find_or_create_by!(plan: Plan.explore_bosnia_for(@user), location: @near)
moment = @user.moments.new(plan: Plan.explore_bosnia_for(@user), location: @near, visibility: :private_moment)
moment.photo.attach(io: File.open(Rails.root.join("test/fixtures/files/real_image.jpg")), filename: "real_image.jpg", content_type: "image/jpeg")
moment.save!
Expand All @@ -694,6 +718,7 @@ class ExploreBosniaTest < ActionDispatch::IntegrationTest
end

test "deleting a moment from the story destroys it and its photo everywhere" do
@user.plan_visits.find_or_create_by!(plan: Plan.explore_bosnia_for(@user), location: @near)
moment = @user.moments.new(plan: Plan.explore_bosnia_for(@user), location: @near, visibility: :private_moment)
moment.photo.attach(io: File.open(Rails.root.join("test/fixtures/files/real_image.jpg")), filename: "real_image.jpg", content_type: "image/jpeg")
moment.save!
Expand Down
1 change: 1 addition & 0 deletions test/integration/guest_walk_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class GuestWalkTest < ActionDispatch::IntegrationTest

test "a signed-in traveller reading a place outside a plan can still upload" do
user = User.create!(username: "browsing", email: "browsing@example.com", password: "password123")
user.plan_visits.create!(plan: Plan.explore_bosnia_for(user), location: @location)
post user_session_path, params: { user: { email: user.email, password: "password123" } }

get location_moments_path(@location.uuid)
Expand Down
2 changes: 2 additions & 0 deletions test/integration/moment_like_lifecycle_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class MomentLikeLifecycleTest < ActionDispatch::IntegrationTest
end

test "nobody can like a private moment, its author least of all" do
@author.plan_visits.find_or_create_by!(plan: @plan, location: @location)
private_one = @author.moments.build(plan: @plan, location: @location, note: "mine alone")
private_one.photo.attach(io: File.open(file_fixture("real_image.jpg")), filename: "mine.jpg",
content_type: "image/jpeg")
Expand All @@ -127,6 +128,7 @@ class MomentLikeLifecycleTest < ActionDispatch::IntegrationTest
private

def public_moment
@author.plan_visits.find_or_create_by!(plan: @plan, location: @location)
moment = @author.moments.build(plan: @plan, location: @location, note: "lifecycle")
moment.photo.attach(io: File.open(file_fixture("real_image.jpg")), filename: "life.jpg",
content_type: "image/jpeg")
Expand Down
2 changes: 2 additions & 0 deletions test/integration/moment_photo_access_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class MomentPhotoAccessTest < ActionDispatch::IntegrationTest
end

test "a blob whose bytes are not an image is refused rather than 500ing" do
@owner.plan_visits.find_or_create_by!(plan: @plan, location: @location)
junk = @owner.moments.build(plan: @plan, location: @location)
junk.photo.attach(io: StringIO.new("not an image"), filename: "junk.jpg", content_type: "image/jpeg")
junk.save!
Expand All @@ -109,6 +110,7 @@ def login_as(user)
end

def build_moment
@owner.plan_visits.find_or_create_by!(plan: @plan, location: @location)
moment = @owner.moments.build(plan: @plan, location: @location, note: "private moment")
moment.photo.attach(
io: File.open("test/fixtures/files/real_image.jpg"),
Expand Down
1 change: 1 addition & 0 deletions test/integration/moments_walk_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def login_as(user)
end

def add_moment(note: nil)
@user.plan_visits.find_or_create_by!(plan: @plan, location: @location)
moment = @user.moments.build(plan: @plan, location: @location, note: note)
moment.photo.attach(
io: File.open("test/fixtures/files/test_image.jpg"),
Expand Down
2 changes: 2 additions & 0 deletions test/integration/plan_start_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class PlanStartTest < ActionDispatch::IntegrationTest
end

test "capturing a photo on the walk adds it and shows it on the step" do
@user.plan_visits.create!(plan: @plan, location: @location)
login_as(@user)

post plan_moments_path(@plan), params: {
Expand Down Expand Up @@ -165,6 +166,7 @@ class PlanStartTest < ActionDispatch::IntegrationTest

test "another traveller's private moment never surfaces as a shared moment on the walk" do
stranger = User.create!(username: "stranger", email: "stranger@example.com", password: "password123")
stranger.plan_visits.create!(plan: @plan, location: @location)
hidden = Moment.create!(user: stranger, plan: @plan, location: @location,
photo: fixture_file_upload("test/fixtures/files/real_image.jpg", "image/jpeg"))
login_as(@user)
Expand Down
1 change: 1 addition & 0 deletions test/models/like_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class LikeTest < ActiveSupport::TestCase
private

def public_moment
@owner.plan_visits.find_or_create_by!(plan: @plan, location: @location)
moment = @owner.moments.build(plan: @plan, location: @location)
moment.photo.attach(io: StringIO.new("fake image data"), filename: "like.jpg", content_type: "image/jpeg")
moment.save!
Expand Down
31 changes: 31 additions & 0 deletions test/models/moment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class MomentTest < ActiveSupport::TestCase
@other_user = User.create!(username: "moment_stranger", email: "moment_stranger@example.com", password: "password123")
@location = Location.create!(name: "Moment Location", city: "Mostar", lat: 43.34, lng: 17.81)
@plan = Plan.create!(title: "Moment Plan", city_name: "Mostar", visibility: :private_plan, user: @user)
@user.plan_visits.create!(plan: @plan, location: @location)
end

teardown do
Expand Down Expand Up @@ -40,6 +41,36 @@ class MomentTest < ActiveSupport::TestCase
assert_includes moment.errors[:photo], "must be JPEG, PNG, GIF, or WebP"
end

test "is refused at a place the traveller has not checked in at" do
@user.plan_visits.destroy_all
moment = build_moment

assert_not moment.valid?
assert_includes moment.errors[:base], I18n.t("plans.moments.not_visited")
end

test "a moment with no place is refused for the place, not for the check-in" do
moment = build_moment
moment.location = nil

assert_not moment.valid?
assert_not_includes moment.errors[:base], I18n.t("plans.moments.not_visited")
end

test "a check-in on any plan earns the capture" do
explore = Plan.explore_bosnia_for(@user)

assert build_moment(plan: explore).valid?
end

test "an existing moment still saves after its check-in is gone" do
moment = build_moment
moment.save!
@user.plan_visits.destroy_all

assert moment.update(note: "Still mine")
end

test "rejects a note longer than 1000 characters" do
moment = build_moment
moment.note = "a" * 1001
Expand Down
1 change: 1 addition & 0 deletions test/models/plan_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@ def count_location_selects
end

def build_moment(traveller, plan)
traveller.plan_visits.find_or_create_by!(plan: plan, location: @location)
moment = traveller.moments.build(plan: plan, location: @location)
moment.photo.attach(
io: File.open("test/fixtures/files/test_image.jpg"),
Expand Down
Loading
Loading