2019-05-03 08:17:27 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2013-09-12 17:46:43 -04:00
|
|
|
class ComposerMessagesFinder
|
|
|
|
|
def initialize(user, details)
|
|
|
|
|
@user = user
|
|
|
|
|
@details = details
|
2015-11-27 19:29:44 +01:00
|
|
|
@topic = Topic.find_by(id: details[:topic_id]) if details[:topic_id]
|
2013-09-12 17:46:43 -04:00
|
|
|
end
|
|
|
|
|
|
2017-02-03 16:59:05 -05:00
|
|
|
def self.check_methods
|
2023-01-20 12:52:49 -06:00
|
|
|
@check_methods ||= instance_methods.find_all { |m| m =~ /\Acheck\_/ }
|
2017-02-03 16:59:05 -05:00
|
|
|
end
|
|
|
|
|
|
2013-09-12 17:46:43 -04:00
|
|
|
def find
|
2018-10-03 17:21:53 -07:00
|
|
|
return if editing_post?
|
|
|
|
|
|
2017-02-03 16:59:05 -05:00
|
|
|
self.class.check_methods.each do |m|
|
2019-05-07 12:22:37 +10:00
|
|
|
msg = public_send(m)
|
2017-02-03 16:59:05 -05:00
|
|
|
return msg if msg.present?
|
|
|
|
|
end
|
2018-10-03 17:21:53 -07:00
|
|
|
|
2017-02-03 16:59:05 -05:00
|
|
|
nil
|
2013-09-12 17:46:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Determines whether to show the user education text
|
|
|
|
|
def check_education_message
|
2018-03-05 15:38:05 +08:00
|
|
|
return if @topic&.private_message?
|
2016-11-04 17:06:53 +08:00
|
|
|
|
2025-07-17 18:15:18 +04:00
|
|
|
education_key = creating_topic? ? "education.new-topic" : "education.new-reply"
|
|
|
|
|
count = @user.topic_count + @user.post_count
|
2013-09-12 17:46:43 -04:00
|
|
|
|
2013-09-17 12:11:17 -04:00
|
|
|
if count < SiteSetting.educate_until_posts
|
2015-01-02 12:37:17 +01:00
|
|
|
return(
|
|
|
|
|
{
|
2016-06-06 13:52:24 -04:00
|
|
|
id: "education",
|
2016-06-06 16:58:35 -04:00
|
|
|
templateName: "education",
|
2015-01-02 12:37:17 +01:00
|
|
|
wait_for_typing: true,
|
2018-11-07 17:59:42 +01:00
|
|
|
body:
|
|
|
|
|
PrettyText.cook(
|
|
|
|
|
I18n.t(
|
|
|
|
|
education_key,
|
|
|
|
|
education_posts_text:
|
|
|
|
|
I18n.t("education.until_posts", count: SiteSetting.educate_until_posts),
|
|
|
|
|
site_name: SiteSetting.title,
|
|
|
|
|
base_path: Discourse.base_path,
|
|
|
|
|
),
|
|
|
|
|
),
|
2015-01-02 12:37:17 +01:00
|
|
|
}
|
2023-01-09 12:10:19 +00:00
|
|
|
)
|
2013-09-12 17:46:43 -04:00
|
|
|
end
|
|
|
|
|
|
2016-11-09 11:13:34 +08:00
|
|
|
nil
|
2013-09-12 17:46:43 -04:00
|
|
|
end
|
|
|
|
|
|
2013-12-19 13:45:55 -05:00
|
|
|
# New users have a limited number of replies in a topic
|
|
|
|
|
def check_new_user_many_replies
|
|
|
|
|
return unless replying? && @user.posted_too_much_in_topic?(@details[:topic_id])
|
2015-01-02 12:37:17 +01:00
|
|
|
|
|
|
|
|
{
|
2016-06-06 13:52:24 -04:00
|
|
|
id: "too_many_replies",
|
2016-06-06 16:58:35 -04:00
|
|
|
templateName: "education",
|
2015-01-02 12:37:17 +01:00
|
|
|
body:
|
|
|
|
|
PrettyText.cook(
|
|
|
|
|
I18n.t(
|
|
|
|
|
"education.too_many_replies",
|
|
|
|
|
newuser_max_replies_per_topic: SiteSetting.newuser_max_replies_per_topic,
|
2023-01-09 12:10:19 +00:00
|
|
|
),
|
|
|
|
|
),
|
2015-01-02 12:37:17 +01:00
|
|
|
}
|
2013-12-19 13:45:55 -05:00
|
|
|
end
|
|
|
|
|
|
2013-09-17 14:38:39 -04:00
|
|
|
def check_dominating_topic
|
2017-02-03 16:59:05 -05:00
|
|
|
return unless educate_reply?(:notified_about_dominating_topic)
|
2013-09-17 14:38:39 -04:00
|
|
|
|
2015-11-27 19:29:44 +01:00
|
|
|
if @topic.blank? || @topic.user_id == @user.id ||
|
|
|
|
|
@topic.posts_count < SiteSetting.summary_posts_required || @topic.private_message?
|
2023-01-09 12:10:19 +00:00
|
|
|
return
|
|
|
|
|
end
|
2013-09-17 14:38:39 -04:00
|
|
|
|
2015-11-27 19:29:44 +01:00
|
|
|
posts_by_user = @user.posts.where(topic_id: @topic.id).count
|
2013-09-17 14:38:39 -04:00
|
|
|
|
2015-11-27 19:29:44 +01:00
|
|
|
ratio = (posts_by_user.to_f / @topic.posts_count.to_f)
|
2013-09-17 14:38:39 -04:00
|
|
|
return if ratio < (SiteSetting.dominating_topic_minimum_percent.to_f / 100.0)
|
|
|
|
|
|
|
|
|
|
# Log the topic notification
|
2014-02-07 05:49:45 +05:30
|
|
|
UserHistory.create!(
|
|
|
|
|
action: UserHistory.actions[:notified_about_dominating_topic],
|
2013-09-17 14:38:39 -04:00
|
|
|
target_user_id: @user.id,
|
|
|
|
|
topic_id: @details[:topic_id],
|
|
|
|
|
)
|
|
|
|
|
|
2015-01-02 12:37:17 +01:00
|
|
|
{
|
2016-06-06 13:52:24 -04:00
|
|
|
id: "dominating_topic",
|
2021-07-20 13:58:38 -04:00
|
|
|
templateName: "dominating-topic",
|
2015-01-02 12:37:17 +01:00
|
|
|
wait_for_typing: true,
|
2021-07-20 13:58:38 -04:00
|
|
|
extraClass: "education-message dominating-topic-message",
|
2022-11-22 21:11:53 +01:00
|
|
|
body: PrettyText.cook(I18n.t("education.dominating_topic")),
|
2015-01-02 12:37:17 +01:00
|
|
|
}
|
2013-09-17 14:38:39 -04:00
|
|
|
end
|
|
|
|
|
|
2017-02-28 16:47:16 -05:00
|
|
|
def check_get_a_room(min_users_posted: 5)
|
2024-02-27 11:54:05 +01:00
|
|
|
return unless @user.guardian.can_send_private_messages?
|
2017-02-03 16:59:05 -05:00
|
|
|
return unless educate_reply?(:notified_about_get_a_room)
|
2024-05-27 12:27:13 +02:00
|
|
|
return if @details[:post_id].blank?
|
2021-04-14 12:34:13 -05:00
|
|
|
return if @topic.category&.read_restricted
|
2017-02-03 16:59:05 -05:00
|
|
|
|
|
|
|
|
reply_to_user_id = Post.where(id: @details[:post_id]).pluck(:user_id)[0]
|
|
|
|
|
|
|
|
|
|
# Users's last x posts in the topic
|
|
|
|
|
last_x_replies =
|
|
|
|
|
@topic
|
|
|
|
|
.posts
|
|
|
|
|
.where(user_id: @user.id)
|
2017-02-19 16:00:28 -05:00
|
|
|
.order("created_at desc")
|
2017-02-03 16:59:05 -05:00
|
|
|
.limit(SiteSetting.get_a_room_threshold)
|
|
|
|
|
.pluck(:reply_to_user_id)
|
|
|
|
|
.find_all { |uid| uid != @user.id && uid == reply_to_user_id }
|
|
|
|
|
|
2023-02-16 10:40:11 +01:00
|
|
|
return if last_x_replies.size != SiteSetting.get_a_room_threshold
|
|
|
|
|
return if @topic.posts.count("distinct user_id") < min_users_posted
|
2017-02-03 16:59:05 -05:00
|
|
|
|
|
|
|
|
UserHistory.create!(
|
|
|
|
|
action: UserHistory.actions[:notified_about_get_a_room],
|
|
|
|
|
target_user_id: @user.id,
|
|
|
|
|
topic_id: @details[:topic_id],
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-13 12:39:45 +08:00
|
|
|
reply_username = User.where(id: last_x_replies[0]).pick(:username)
|
2017-02-08 15:27:07 -05:00
|
|
|
|
2017-02-03 16:59:05 -05:00
|
|
|
{
|
|
|
|
|
id: "get_a_room",
|
2021-08-23 18:34:23 -04:00
|
|
|
templateName: "get-a-room",
|
2017-02-03 16:59:05 -05:00
|
|
|
wait_for_typing: true,
|
2021-08-23 18:34:23 -04:00
|
|
|
reply_username: reply_username,
|
|
|
|
|
extraClass: "education-message get-a-room",
|
2017-02-03 16:59:05 -05:00
|
|
|
body:
|
|
|
|
|
PrettyText.cook(
|
2017-02-08 15:27:07 -05:00
|
|
|
I18n.t(
|
|
|
|
|
"education.get_a_room",
|
|
|
|
|
count: SiteSetting.get_a_room_threshold,
|
2018-11-07 17:59:42 +01:00
|
|
|
reply_username: reply_username,
|
|
|
|
|
base_path: Discourse.base_path,
|
2017-02-08 15:27:07 -05:00
|
|
|
),
|
2017-02-03 16:59:05 -05:00
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2023-04-20 15:49:35 +08:00
|
|
|
def check_dont_feed_the_trolls
|
|
|
|
|
return if !replying?
|
|
|
|
|
|
|
|
|
|
post =
|
|
|
|
|
if @details[:post_id]
|
|
|
|
|
Post.find_by(id: @details[:post_id])
|
|
|
|
|
else
|
2023-04-25 10:08:00 -05:00
|
|
|
@topic&.first_post
|
2023-04-20 15:49:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return if post.blank?
|
|
|
|
|
|
2023-07-25 15:12:22 +08:00
|
|
|
flags = post.flags.active.group(:user_id).count
|
2023-04-20 15:49:35 +08:00
|
|
|
flagged_by_replier = flags[@user.id].to_i > 0
|
|
|
|
|
flagged_by_others = flags.values.sum >= SiteSetting.dont_feed_the_trolls_threshold
|
|
|
|
|
|
|
|
|
|
return if !flagged_by_replier && !flagged_by_others
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
id: "dont_feed_the_trolls",
|
|
|
|
|
templateName: "education",
|
|
|
|
|
wait_for_typing: false,
|
|
|
|
|
extraClass: "urgent",
|
|
|
|
|
body: PrettyText.cook(I18n.t("education.dont_feed_the_trolls")),
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2022-09-27 22:06:40 +05:30
|
|
|
def self.user_not_seen_in_a_while(usernames)
|
|
|
|
|
User
|
|
|
|
|
.where(username_lower: usernames)
|
|
|
|
|
.where("last_seen_at < ?", SiteSetting.pm_warn_user_last_seen_months_ago.months.ago)
|
|
|
|
|
.pluck(:username)
|
|
|
|
|
.sort
|
|
|
|
|
end
|
|
|
|
|
|
2013-09-12 17:46:43 -04:00
|
|
|
private
|
|
|
|
|
|
2017-02-03 16:59:05 -05:00
|
|
|
def educate_reply?(type)
|
|
|
|
|
replying? && @details[:topic_id] && (@topic.present? && !@topic.private_message?) &&
|
|
|
|
|
(@user.post_count >= SiteSetting.educate_until_posts) &&
|
|
|
|
|
!UserHistory.exists_for_user?(@user, type, topic_id: @details[:topic_id])
|
|
|
|
|
end
|
|
|
|
|
|
2013-09-12 17:46:43 -04:00
|
|
|
def creating_topic?
|
2016-06-06 16:58:35 -04:00
|
|
|
@details[:composer_action] == "createTopic"
|
2013-09-12 17:46:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def replying?
|
2016-06-06 16:58:35 -04:00
|
|
|
@details[:composer_action] == "reply"
|
2013-09-12 17:46:43 -04:00
|
|
|
end
|
|
|
|
|
|
2018-10-03 17:21:53 -07:00
|
|
|
def editing_post?
|
|
|
|
|
@details[:composer_action] == "edit"
|
|
|
|
|
end
|
2014-02-07 05:49:45 +05:30
|
|
|
end
|