From c677f8ee6a14c9f62a10d7bd48034a72265f9986 Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Tue, 21 Jan 2020 10:50:44 +1000 Subject: [PATCH] DEV: Add debug lines for mystery no_user_selected error (#8759) On some customer forums we are randomly getting a "You must select a valid user" error when sending a PM even when all parameters seem to be OK. This is an attempt to track it down with more data. --- app/controllers/posts_controller.rb | 4 ++++ lib/topic_creator.rb | 2 ++ spec/requests/posts_controller_spec.rb | 16 ++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index caef113e502..ba7d7bf0b72 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -777,6 +777,10 @@ class PostsController < ApplicationController result[:target_group_names] = groups.join(",") end + if recipients.blank? || result[:target_usernames].blank? + Rails.logger.debug("Missing recipients for PM! result: #{result.inspect} | params: #{params.inspect}") + end + result.permit! result.to_h end diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb index 5f4aa7f16e9..5e6d0e0de5d 100644 --- a/lib/topic_creator.rb +++ b/lib/topic_creator.rb @@ -171,6 +171,8 @@ class TopicCreator topic.subtype = TopicSubtype.user_to_user unless topic.subtype unless @opts[:target_usernames].present? || @opts[:target_emails].present? || @opts[:target_group_names].present? + Rails.logger.debug("Topic PM cannot be created without recipients! opts: #{@opts.inspect}") + rollback_with!(topic, :no_user_selected) end diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb index 82990338847..d2818f50de2 100644 --- a/spec/requests/posts_controller_spec.rb +++ b/spec/requests/posts_controller_spec.rb @@ -1110,6 +1110,22 @@ describe PostsController do expect(new_topic.allowed_users).to contain_exactly(user, user_2, user_3) end + context "when target_recipients not provided" do + it "errors when creating a private post" do + post "/posts.json", params: { + raw: 'this is the test content', + archetype: 'private_message', + title: "this is some post", + target_recipients: "" + } + + expect(response.status).to eq(422) + expect(JSON.parse(response.body)["errors"]).to include( + I18n.t("activerecord.errors.models.topic.attributes.base.no_user_selected") + ) + end + end + context "errors" do it "does not succeed" do post "/posts.json", params: { raw: 'test' }