diff --git a/app/assets/javascripts/index/layers/notes.js b/app/assets/javascripts/index/layers/notes.js index 4676aa425e..49ae566944 100644 --- a/app/assets/javascripts/index/layers/notes.js +++ b/app/assets/javascripts/index/layers/notes.js @@ -40,7 +40,7 @@ OSM.initializeNotesLayer = function (map) { } else { marker = L.marker(feature.geometry.coordinates.reverse(), { icon: noteIcons[feature.properties.status], - title: feature.properties.comments[0].text, + title: feature.properties.description, opacity: 0.8, interactive: true }); diff --git a/app/helpers/note_helper.rb b/app/helpers/note_helper.rb index 2e9850aef0..93c959a36e 100644 --- a/app/helpers/note_helper.rb +++ b/app/helpers/note_helper.rb @@ -1,6 +1,18 @@ module NoteHelper include ActionView::Helpers::TranslationHelper + def note_description(note) + if note.nil? + "" + elsif note.author_deleted? + RichText.new("text", t("notes.show.description_when_author_is_deleted")) + elsif note.user_ip.nil? && note.user_id.nil? + note.all_comments.first.body + else + RichText.new("text", note.description) + end + end + def note_event(event, at, by) if by.nil? t("notes.show.event_#{event}_by_anonymous_html", diff --git a/app/models/note.rb b/app/models/note.rb index 17b57c0fa6..ef72a847eb 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -28,6 +28,8 @@ class Note < ApplicationRecord include GeoRecord + belongs_to :author, :class_name => "User", :foreign_key => "user_id", :optional => true + has_many :comments, -> { left_joins(:author).where(:visible => true, :users => { :status => [nil, "active", "confirmed"] }).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id has_many :all_comments, -> { left_joins(:author).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id, :inverse_of => :note has_many :subscriptions, :class_name => "NoteSubscription" @@ -89,24 +91,18 @@ def freshly_closed_until closed_at + DEFAULT_FRESHLY_CLOSED_LIMIT end - # Return the note's description, derived from the first comment - def description - comments.first.body + def author_deleted? + !author.nil? && author.status == "deleted" end - # Return the note's author object, derived from the first comment + # Return the note's author object, unless record is unavailable and + # it will be derived from the first comment def author - comments.first.author - end - - # Return the note's author ID, derived from the first comment - def author_id - comments.first.author_id - end - - # Return the note's author IP address, derived from the first comment - def author_ip - comments.first.author_ip + if user_ip.nil? && user_id.nil? + all_comments.first.author + else + self[:author] + end end private diff --git a/app/views/api/notes/_note.json.jbuilder b/app/views/api/notes/_note.json.jbuilder index 0884c4426d..74b8462ebd 100644 --- a/app/views/api/notes/_note.json.jbuilder +++ b/app/views/api/notes/_note.json.jbuilder @@ -7,6 +7,7 @@ end json.properties do json.id note.id + json.description note_description(note) json.url api_note_url(note, :format => params[:format]) if note.closed? diff --git a/app/views/api/notes/_note.rss.builder b/app/views/api/notes/_note.rss.builder index fa70536f79..3deca386e5 100644 --- a/app/views/api/notes/_note.rss.builder +++ b/app/views/api/notes/_note.rss.builder @@ -13,7 +13,7 @@ xml.item do xml.guid api_note_url(note) xml.description render(:partial => "description", :object => note, :formats => [:html]) - xml.dc :creator, note.author.display_name if note.author + xml.dc :creator, note.author.display_name unless note.author.nil? || note.author_deleted? xml.pubDate note.created_at.to_fs(:rfc822) xml.geo :lat, note.lat diff --git a/app/views/api/notes/_note.xml.builder b/app/views/api/notes/_note.xml.builder index 0e5ad0007e..7175491d2a 100644 --- a/app/views/api/notes/_note.xml.builder +++ b/app/views/api/notes/_note.xml.builder @@ -1,4 +1,4 @@ -xml.note("lon" => note.lon, "lat" => note.lat) do +xml.note("lon" => note.lon, "lat" => note.lat, "description" => note_description(note)) do xml.id note.id xml.url api_note_url(note, :format => params[:format]) diff --git a/app/views/notes/index.html.erb b/app/views/notes/index.html.erb index f805a10402..f6bd75869a 100644 --- a/app/views/notes/index.html.erb +++ b/app/views/notes/index.html.erb @@ -48,7 +48,7 @@ <%= link_to note.id, note %> <%= note_author(note.author) %> - <%= note.description.to_html %> + <%= note_description(note).to_html %> <%= friendly_date_ago(note.created_at) %> <%= friendly_date_ago(note.updated_at) %> diff --git a/app/views/notes/show.html.erb b/app/views/notes/show.html.erb index a320240488..4e9cf8c95f 100644 --- a/app/views/notes/show.html.erb +++ b/app/views/notes/show.html.erb @@ -5,7 +5,7 @@

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

- <%= h(@note.description.to_html) %> + <%= h(note_description(@note).to_html) %>
@@ -52,10 +52,10 @@ <% end %>
- <% if @note_comments.length > 1 %> + <% if @note_comments.length > (@note.author_deleted? ? 0 : 1) %>