From a71a90941ac14902ae4c3c358fe55235b1b79f41 Mon Sep 17 00:00:00 2001 From: Roman Deev Date: Sat, 6 Dec 2025 02:29:03 +0300 Subject: [PATCH] stricter restrictions on creating anonymous notes --- app/assets/javascripts/index/new_note.js | 2 +- app/helpers/note_helper.rb | 8 +++++ app/views/notes/new.html.erb | 7 ++-- test/system/create_note_test.rb | 46 ++++++++++++++++++++---- 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/index/new_note.js b/app/assets/javascripts/index/new_note.js index 8030369d8..0beff16be 100644 --- a/app/assets/javascripts/index/new_note.js +++ b/app/assets/javascripts/index/new_note.js @@ -149,7 +149,7 @@ OSM.NewNote = function (map) { createNote(location, text, (feature) => { if (typeof OSM.user === "undefined") { const anonymousNotesCount = Number(OSM.cookies.get("_osm_anonymous_notes_count")) || 0; - OSM.cookies.set("_osm_anonymous_notes_count", anonymousNotesCount + 1, { expires: 30 }); + OSM.cookies.set("_osm_anonymous_notes_count", anonymousNotesCount + 1, { expires: 14 }); } content.find("textarea").val(""); addCreatedNoteMarker(feature); diff --git a/app/helpers/note_helper.rb b/app/helpers/note_helper.rb index 3632e1d03..87af60bee 100644 --- a/app/helpers/note_helper.rb +++ b/app/helpers/note_helper.rb @@ -34,4 +34,12 @@ module NoteHelper :class => "mw-100 d-inline-block align-bottom text-truncate text-wrap", :dir => "auto" end end + + def soft_anonymous_notes_limit_reached?(anonymous_notes_count) + !current_user && anonymous_notes_count >= 5 + end + + def hard_anonymous_notes_limit_reached?(anonymous_notes_count) + !current_user && anonymous_notes_count >= 10 + end end diff --git a/app/views/notes/new.html.erb b/app/views/notes/new.html.erb index 5a4e4cbb1..15cd4ae96 100644 --- a/app/views/notes/new.html.erb +++ b/app/views/notes/new.html.erb @@ -1,15 +1,14 @@ <% set_title(t(".title")) %> <%= render "sidebar_header", :title => t(".title") %> -

<%= t(".intro") %>

<% if !current_user %> -
+
pb-0">

<%= t ".anonymous_warning_html", :log_in => link_to(t(".anonymous_warning_log_in"), login_path(:referer => new_note_path)), :sign_up => link_to(t(".anonymous_warning_sign_up"), new_user_path) %>

- <% if @anonymous_notes_count >= 10 %> + <% if soft_anonymous_notes_limit_reached?(@anonymous_notes_count) %>

<%= t ".counter_warning_html", :x_anonymous_notes => t(".x_anonymous_notes", :count => @anonymous_notes_count), :contribute_by_yourself => link_to(t(".counter_warning_guide_link.text"), t(".counter_warning_guide_link.url")), @@ -17,6 +16,7 @@ <% end %>

<% end %> + <% if !hard_anonymous_notes_limit_reached?(@anonymous_notes_count) %>
@@ -28,4 +28,5 @@ <%= submit_tag t(".add"), :name => "add", :disabled => 1, :class => "btn btn-primary" %>
+ <% end %>
diff --git a/test/system/create_note_test.rb b/test/system/create_note_test.rb index f096c4055..f74b1a64c 100644 --- a/test/system/create_note_test.rb +++ b/test/system/create_note_test.rb @@ -100,30 +100,38 @@ class CreateNoteTest < ApplicationSystemTestCase end end - test "encouragement to contribute appears after 10 created notes and disappears after login" do - check_encouragement_while_creating_notes(10) + test "encouragement to contribute appears after 5 created notes and disappears after login" do + check_encouragement_while_creating_notes(5) sign_in_as(create(:user)) check_no_encouragement_while_logging_out end - test "encouragement to contribute appears after 10 created notes and disappears after email signup" do - check_encouragement_while_creating_notes(10) + test "encouragement to contribute appears after 5 created notes and disappears after email signup" do + check_encouragement_while_creating_notes(5) sign_up_with_email check_no_encouragement_while_logging_out end - test "encouragement to contribute appears after 10 created notes and disappears after google signup" do - check_encouragement_while_creating_notes(10) + test "encouragement to contribute appears after 5 created notes and disappears after google signup" do + check_encouragement_while_creating_notes(5) sign_up_with_google check_no_encouragement_while_logging_out end + test "strict encouragement to contribute appears after 10 created notes and disappears after login" do + check_strict_encouragement_while_creating_notes(5, 10) + + sign_in_as(create(:user)) + + check_no_encouragement_while_logging_out + end + private def check_encouragement_while_creating_notes(encouragement_threshold) @@ -147,6 +155,32 @@ class CreateNoteTest < ApplicationSystemTestCase end end + def check_strict_encouragement_while_creating_notes(encouragement_threshold, strict_encouragement_threshold) + strict_encouragement_threshold.times do |n| + visit new_note_path(:anchor => "map=16/0/#{0.001 * n}") + + within_sidebar do + if n < encouragement_threshold + assert_no_content(/already posted at least \d+ anonymous note/) + else + assert_content(/already posted at least \d+ anonymous note/) + end + + fill_in "text", :with => "new note ##{n + 1}" + click_on "Add Note" + + assert_content "new note ##{n + 1}" + end + end + + visit new_note_path(:anchor => "map=16/0/#{0.001 * strict_encouragement_threshold}") + + within_sidebar do + assert_content(/already posted at least #{strict_encouragement_threshold} anonymous note/) + assert_no_button "Add Note" + end + end + def check_no_encouragement_while_logging_out visit new_note_path(:anchor => "map=16/0/0") -- 2.39.5