From 2f65393706bc59f8848ad72a9d5b54969fe5dfeb Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 5 Mar 2018 15:38:05 +0800 Subject: [PATCH] REFACTOR: Use `Topic#private_message?` to reduce duplication. --- app/models/post.rb | 2 +- app/models/topic.rb | 2 +- app/services/user_action_creator.rb | 4 ++-- lib/composer_messages_finder.rb | 2 +- .../lib/discourse_narrative_bot/advanced_user_narrative.rb | 2 +- .../lib/discourse_narrative_bot/new_user_narrative.rb | 2 +- plugins/discourse-presence/plugin.rb | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index d959aadbd65..78551d1f816 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -164,7 +164,7 @@ class Post < ActiveRecord::Base }.merge(options) if Topic.visible_post_types.include?(post_type) - if topic.archetype == Archetype.private_message + if topic.private_message? user_ids = User.where('admin or moderator').pluck(:id) user_ids |= topic.allowed_users.pluck(:id) MessageBus.publish(channel, msg, user_ids: user_ids) diff --git a/app/models/topic.rb b/app/models/topic.rb index 09de70dcfd9..1426a12aabc 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -265,7 +265,7 @@ class Topic < ActiveRecord::Base end def advance_draft_sequence - if archetype == Archetype.private_message + if self.private_message? DraftSequence.next!(user, Draft::NEW_PRIVATE_MESSAGE) else DraftSequence.next!(user, Draft::NEW_TOPIC) diff --git a/app/services/user_action_creator.rb b/app/services/user_action_creator.rb index 7b1c92562bb..6d4b917bb89 100644 --- a/app/services/user_action_creator.rb +++ b/app/services/user_action_creator.rb @@ -85,7 +85,7 @@ class UserActionCreator return unless model.user_id row = { - action_type: model.archetype == Archetype.private_message ? UserAction::NEW_PRIVATE_MESSAGE : UserAction::NEW_TOPIC, + action_type: model.private_message? ? UserAction::NEW_PRIVATE_MESSAGE : UserAction::NEW_TOPIC, user_id: model.user_id, acting_user_id: model.user_id, target_topic_id: model.id, @@ -94,7 +94,7 @@ class UserActionCreator } UserAction.remove_action!(row.merge( - action_type: model.archetype == Archetype.private_message ? UserAction::NEW_TOPIC : UserAction::NEW_PRIVATE_MESSAGE + action_type: model.private_message? ? UserAction::NEW_TOPIC : UserAction::NEW_PRIVATE_MESSAGE )) rows = [row] diff --git a/lib/composer_messages_finder.rb b/lib/composer_messages_finder.rb index 8b765c0e142..76c582dd572 100644 --- a/lib/composer_messages_finder.rb +++ b/lib/composer_messages_finder.rb @@ -20,7 +20,7 @@ class ComposerMessagesFinder # Determines whether to show the user education text def check_education_message - return if @topic && @topic.archetype == Archetype.private_message + return if @topic&.private_message? if creating_topic? count = @user.created_topic_count diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/advanced_user_narrative.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/advanced_user_narrative.rb index a503392f95b..6cf34164852 100644 --- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/advanced_user_narrative.rb +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/advanced_user_narrative.rb @@ -176,7 +176,7 @@ module DiscourseNarrativeBot } if @post && - @post.archetype == Archetype.private_message && + @post.topic.private_message? && @post.topic.topic_allowed_users.pluck(:user_id).include?(@user.id) opts = opts.merge(topic_id: @post.topic_id) diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/new_user_narrative.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/new_user_narrative.rb index 040da62c01f..9bd4819b5fd 100644 --- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/new_user_narrative.rb +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/new_user_narrative.rb @@ -190,7 +190,7 @@ module DiscourseNarrativeBot } if @post && - @post.archetype == Archetype.private_message && + @post.topic.private_message? && @post.topic.topic_allowed_users.pluck(:user_id).include?(@user.id) opts = opts.merge(topic_id: @post.topic_id) diff --git a/plugins/discourse-presence/plugin.rb b/plugins/discourse-presence/plugin.rb index fd6dc6a0540..0c0dfb387fc 100644 --- a/plugins/discourse-presence/plugin.rb +++ b/plugins/discourse-presence/plugin.rb @@ -58,7 +58,7 @@ after_initialize do topic = type == 'post' ? Post.find_by(id: id).topic : Topic.find_by(id: id) - if topic.archetype == Archetype.private_message + if topic.private_message? user_ids = User.where('admin OR moderator').pluck(:id) + topic.allowed_users.pluck(:id) group_ids = topic.allowed_groups.pluck(:id)