From 22df208f9a97419bb8aeff16bf0e93d2220f3c44 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Tue, 22 Apr 2025 14:11:21 +0200 Subject: [PATCH] Render proper content for mentions, don't send mentions to yourself --- app/models/card/mentions.rb | 7 +++---- app/models/mention.rb | 4 ++++ app/models/message/mentions.rb | 7 +++---- app/models/notifier.rb | 6 +++--- app/models/notifier/events/base.rb | 4 ---- app/models/notifier/mention.rb | 6 +++++- .../notification/_mention.html.erb | 6 ++++-- ...50422112857_add_creator_to_notifications.rb | 18 ++++++++++++++++++ db/schema.rb | 2 +- db/schema_cache.yml | 11 ++++++++++- test/models/user/mentionable_test.rb | 2 +- 11 files changed, 52 insertions(+), 21 deletions(-) create mode 100644 db/migrate/20250422112857_add_creator_to_notifications.rb diff --git a/app/models/card/mentions.rb b/app/models/card/mentions.rb index dd135f24c..79334b240 100644 --- a/app/models/card/mentions.rb +++ b/app/models/card/mentions.rb @@ -5,8 +5,7 @@ module Card::Mentions include ::Mentions end - private - def mentionable_content - description.to_plain_text - end + def mentionable_content + description.to_plain_text + end end diff --git a/app/models/mention.rb b/app/models/mention.rb index 515c3071c..fd70e075a 100644 --- a/app/models/mention.rb +++ b/app/models/mention.rb @@ -4,4 +4,8 @@ class Mention < ApplicationRecord belongs_to :container, polymorphic: true belongs_to :mentioner, class_name: "User" belongs_to :mentionee, class_name: "User", inverse_of: :mentions + + def self_mention? + mentioner == mentionee + end end diff --git a/app/models/message/mentions.rb b/app/models/message/mentions.rb index fc8282413..f42ac5e1b 100644 --- a/app/models/message/mentions.rb +++ b/app/models/message/mentions.rb @@ -5,8 +5,7 @@ module Message::Mentions include ::Mentions end - private - def mentionable_content - messageable.try(:body_plain_text) - end + def mentionable_content + messageable.try(:body_plain_text) + end end diff --git a/app/models/notifier.rb b/app/models/notifier.rb index f1a06a0ed..72c16ff3d 100644 --- a/app/models/notifier.rb +++ b/app/models/notifier.rb @@ -4,9 +4,9 @@ class Notifier class << self def for(source) case source - when Event + when Event "Notifier::Events::#{source.action.classify}".safe_constantize&.new(source) - when ::Mention + when ::Mention Notifier::Mention.new(source) end end @@ -26,7 +26,7 @@ class Notifier end def should_notify? - true + !creator.system? end def resource diff --git a/app/models/notifier/events/base.rb b/app/models/notifier/events/base.rb index 13a915098..6852ba2b2 100644 --- a/app/models/notifier/events/base.rb +++ b/app/models/notifier/events/base.rb @@ -2,10 +2,6 @@ class Notifier::Events::Base < Notifier delegate :card, :creator, to: :source private - def should_notify? - !creator.system? - end - def resource card end diff --git a/app/models/notifier/mention.rb b/app/models/notifier/mention.rb index a35e0f4e4..2085c42ed 100644 --- a/app/models/notifier/mention.rb +++ b/app/models/notifier/mention.rb @@ -5,7 +5,11 @@ class Notifier::Mention < Notifier end def recipients - [ source.mentionee ] + if source.self_mention? + [] + else + [ source.mentionee ] + end end def creator diff --git a/app/views/notifications/notification/_mention.html.erb b/app/views/notifications/notification/_mention.html.erb index 5dc61b1af..96abde715 100644 --- a/app/views/notifications/notification/_mention.html.erb +++ b/app/views/notifications/notification/_mention.html.erb @@ -1,5 +1,7 @@ -<%= "OMG" %> +<% mention = notification.source %> + +<%= mention.mentioner.first_name %> mentioned you
- YOU WERE MENTIONED! + <%= mention.container.mentionable_content.truncate(200) %>
<%= local_datetime_tag(notification.created_at, style: :ago) %>
diff --git a/db/migrate/20250422112857_add_creator_to_notifications.rb b/db/migrate/20250422112857_add_creator_to_notifications.rb new file mode 100644 index 000000000..63635565c --- /dev/null +++ b/db/migrate/20250422112857_add_creator_to_notifications.rb @@ -0,0 +1,18 @@ +class AddCreatorToNotifications < ActiveRecord::Migration[8.1] + def change + add_reference :notifications, :creator, null: true, foreign_key: { to_table: :users } + + execute <<~SQL + UPDATE notifications + SET creator_id = ( + SELECT events.creator_id + FROM events + WHERE events.id = notifications.source_id + AND notifications.source_type = 'Event' + ) + WHERE source_type = 'Event'; + SQL + + change_column_null :notifications, :creator_id, true + end +end diff --git a/db/schema.rb b/db/schema.rb index 2da27d16f..2dbc1c529 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -236,7 +236,7 @@ ActiveRecord::Schema[8.1].define(version: 2025_04_22_112857) do create_table "notifications", force: :cascade do |t| t.datetime "created_at", null: false - t.integer "creator_id", null: false + t.integer "creator_id" t.datetime "read_at" t.integer "resource_id", null: false t.string "resource_type", null: false diff --git a/db/schema_cache.yml b/db/schema_cache.yml index 73d81743b..eb67d0f45 100644 --- a/db/schema_cache.yml +++ b/db/schema_cache.yml @@ -761,7 +761,16 @@ columns: default_function: collation: comment: - - *23 + - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column + auto_increment: + name: creator_id + cast_type: *1 + sql_type_metadata: *2 + 'null': true + default: + default_function: + collation: + comment: pins: - *5 - *21 diff --git a/test/models/user/mentionable_test.rb b/test/models/user/mentionable_test.rb index 43e70792c..48fb339a1 100644 --- a/test/models/user/mentionable_test.rb +++ b/test/models/user/mentionable_test.rb @@ -1,4 +1,4 @@ -require 'test_helper' +require "test_helper" class User::MentionableTest < ActiveSupport::TestCase test "mentionable handles" do