diff --git a/app/models/user_feedback.rb b/app/models/user_feedback.rb index c4cac9f81..738adb30c 100644 --- a/app/models/user_feedback.rb +++ b/app/models/user_feedback.rb @@ -2,6 +2,7 @@ class UserFeedback < ApplicationRecord self.table_name = "user_feedback" belongs_to :user belongs_to_creator + belongs_to_updater validates :body, :category, presence: true validates :category, inclusion: { in: %w[positive negative neutral] } validates :body, length: { minimum: 1, maximum: Danbooru.config.user_feedback_max_size } @@ -75,7 +76,7 @@ class UserFeedback < ApplicationRecord return unless should_send action = saved_change_to_id? ? "created" : "updated" - body = %(#{creator_name} #{action} a "#{category} record":/user_feedbacks?search[user_id]=#{user_id} for your account:\n\n#{self.body}) + body = %(#{updater_name} #{action} a "#{category} record":/user_feedbacks?search[user_id]=#{user_id} for your account:\n\n#{self.body}) Dmail.create_automated(to_id: user_id, title: "Your user record has been updated", body: body) end diff --git a/db/migrate/20240101042716_user_feedback_add_updater_id.rb b/db/migrate/20240101042716_user_feedback_add_updater_id.rb new file mode 100644 index 000000000..676adff91 --- /dev/null +++ b/db/migrate/20240101042716_user_feedback_add_updater_id.rb @@ -0,0 +1,6 @@ +class UserFeedbackAddUpdaterId < ActiveRecord::Migration[7.0] + def change + add_column :user_feedback, :updater_id, :integer + add_foreign_key :user_feedback, :users, column: :updater_id + end +end diff --git a/db/structure.sql b/db/structure.sql index b7b2f1d21..5d42035ac 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2096,7 +2096,8 @@ CREATE TABLE public.user_feedback ( body text NOT NULL, created_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL, - creator_ip_addr inet + creator_ip_addr inet, + updater_id integer ); @@ -4437,6 +4438,14 @@ ALTER TABLE ONLY public.tickets ADD CONSTRAINT fk_rails_45cd696dba FOREIGN KEY (accused_id) REFERENCES public.users(id); +-- +-- Name: user_feedback fk_rails_9329a36823; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.user_feedback + ADD CONSTRAINT fk_rails_9329a36823 FOREIGN KEY (updater_id) REFERENCES public.users(id); + + -- -- Name: mascots fk_rails_9901e810fa; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -4743,6 +4752,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20230513074838'), ('20230517155547'), ('20230518182034'), -('20230531080817'); +('20230531080817'), +('20240101042716'); diff --git a/test/unit/user_feedback_test.rb b/test/unit/user_feedback_test.rb index 6662e268c..0366b0a27 100644 --- a/test/unit/user_feedback_test.rb +++ b/test/unit/user_feedback_test.rb @@ -5,18 +5,36 @@ class UserFeedbackTest < ActiveSupport::TestCase setup do @user = create(:user) @mod = create(:moderator_user) + @admin = create(:admin_user) CurrentUser.user = @mod end should "create a dmail" do - dmail = <<~EOS.chomp + dmail = <<~DMAIL.chomp #{@mod.name} created a "positive record":/user_feedbacks?search[user_id]=#{@user.id} for your account: good job! - EOS + DMAIL assert_difference("Dmail.count", 1) do create(:user_feedback, user: @user, body: "good job!") - assert_equal(dmail, @user.dmails.last.body) + assert_equal(dmail, @user.dmails.first.body) + end + end + + should "correctly credit the updater" do + feedback = create(:user_feedback, user: @user, body: "good job!") + + dmail = <<~DMAIL.chomp + #{@admin.name} updated a "positive record":/user_feedbacks?search[user_id]=#{@user.id} for your account: + + great job! + DMAIL + + assert_difference("Dmail.count", 1) do + CurrentUser.scoped(@admin) do + feedback.update(body: "great job!", send_update_dmail: true) + end + assert_equal(dmail, @user.dmails.first.body) end end