2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-09-13 05:46:43 +08:00
|
|
|
class ComposerMessagesFinder
|
|
|
|
def initialize(user, details)
|
|
|
|
@user = user
|
|
|
|
@details = details
|
2015-11-28 02:29:44 +08:00
|
|
|
@topic = Topic.find_by(id: details[:topic_id]) if details[:topic_id]
|
2013-09-13 05:46:43 +08:00
|
|
|
end
|
|
|
|
|
2017-02-04 05:59:05 +08:00
|
|
|
def self.check_methods
|
2023-01-21 02:52:49 +08:00
|
|
|
@check_methods ||= instance_methods.find_all { |m| m =~ /\Acheck\_/ }
|
2017-02-04 05:59:05 +08:00
|
|
|
end
|
|
|
|
|
2013-09-13 05:46:43 +08:00
|
|
|
def find
|
2018-10-04 08:21:53 +08:00
|
|
|
return if editing_post?
|
|
|
|
|
2017-02-04 05:59:05 +08:00
|
|
|
self.class.check_methods.each do |m|
|
2019-05-07 10:22:37 +08:00
|
|
|
msg = public_send(m)
|
2017-02-04 05:59:05 +08:00
|
|
|
return msg if msg.present?
|
|
|
|
end
|
2018-10-04 08:21:53 +08:00
|
|
|
|
2017-02-04 05:59:05 +08:00
|
|
|
nil
|
2013-09-13 05:46:43 +08: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
|
|
|
|
2013-09-13 05:46:43 +08:00
|
|
|
if creating_topic?
|
|
|
|
count = @user.created_topic_count
|
2015-11-28 06:00:26 +08:00
|
|
|
education_key = "education.new-topic"
|
2013-09-13 05:46:43 +08:00
|
|
|
else
|
2013-09-14 12:32:18 +08:00
|
|
|
count = @user.post_count
|
2015-11-28 06:00:26 +08:00
|
|
|
education_key = "education.new-reply"
|
2013-09-13 05:46:43 +08:00
|
|
|
end
|
|
|
|
|
2013-09-18 00:11:17 +08:00
|
|
|
if count < SiteSetting.educate_until_posts
|
2015-01-02 19:37:17 +08:00
|
|
|
return(
|
|
|
|
{
|
2016-06-07 01:52:24 +08:00
|
|
|
id: "education",
|
2016-06-07 04:58:35 +08:00
|
|
|
templateName: "education",
|
2015-01-02 19:37:17 +08:00
|
|
|
wait_for_typing: true,
|
2018-11-08 00:59:42 +08: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 19:37:17 +08:00
|
|
|
}
|
2023-01-09 20:10:19 +08:00
|
|
|
)
|
2013-09-13 05:46:43 +08:00
|
|
|
end
|
|
|
|
|
2016-11-09 11:13:34 +08:00
|
|
|
nil
|
2013-09-13 05:46:43 +08:00
|
|
|
end
|
|
|
|
|
2013-12-20 02:45:55 +08: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 19:37:17 +08:00
|
|
|
|
|
|
|
{
|
2016-06-07 01:52:24 +08:00
|
|
|
id: "too_many_replies",
|
2016-06-07 04:58:35 +08:00
|
|
|
templateName: "education",
|
2015-01-02 19:37:17 +08:00
|
|
|
body:
|
|
|
|
PrettyText.cook(
|
|
|
|
I18n.t(
|
|
|
|
"education.too_many_replies",
|
|
|
|
newuser_max_replies_per_topic: SiteSetting.newuser_max_replies_per_topic,
|
2023-01-09 20:10:19 +08:00
|
|
|
),
|
|
|
|
),
|
2015-01-02 19:37:17 +08:00
|
|
|
}
|
2013-12-20 02:45:55 +08:00
|
|
|
end
|
|
|
|
|
2013-09-13 05:46:43 +08:00
|
|
|
# Should a user be contacted to update their avatar?
|
|
|
|
def check_avatar_notification
|
|
|
|
# A user has to be basic at least to be considered for an avatar notification
|
2014-09-05 13:20:39 +08:00
|
|
|
return unless @user.has_trust_level?(TrustLevel[1])
|
2013-09-13 05:46:43 +08:00
|
|
|
|
|
|
|
# We don't notify users who have avatars or who have been notified already.
|
2014-05-22 15:37:02 +08:00
|
|
|
if @user.uploaded_avatar_id || UserHistory.exists_for_user?(@user, :notified_about_avatar)
|
|
|
|
return
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2013-09-13 05:46:43 +08:00
|
|
|
|
2015-12-15 01:02:40 +08:00
|
|
|
# Do not notify user if any of the following is true:
|
|
|
|
# - "disable avatar education message" is enabled
|
|
|
|
# - "sso overrides avatar" is enabled
|
|
|
|
# - "allow uploaded avatars" is disabled
|
2021-08-24 15:46:28 +08:00
|
|
|
if SiteSetting.disable_avatar_education_message ||
|
|
|
|
SiteSetting.discourse_connect_overrides_avatar ||
|
2023-12-13 07:53:19 +08:00
|
|
|
!@user.in_any_groups?(SiteSetting.uploaded_avatars_allowed_groups_map)
|
2021-08-24 15:46:28 +08:00
|
|
|
return
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2015-12-15 01:02:40 +08:00
|
|
|
|
2013-09-13 05:46:43 +08:00
|
|
|
# If we got this far, log that we've nagged them about the avatar
|
|
|
|
UserHistory.create!(
|
|
|
|
action: UserHistory.actions[:notified_about_avatar],
|
|
|
|
target_user_id: @user.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Return the message
|
2015-01-02 19:37:17 +08:00
|
|
|
{
|
2016-06-07 01:52:24 +08:00
|
|
|
id: "avatar",
|
2016-06-07 04:58:35 +08:00
|
|
|
templateName: "education",
|
2019-03-22 17:20:48 +08:00
|
|
|
body:
|
|
|
|
PrettyText.cook(
|
|
|
|
I18n.t(
|
|
|
|
"education.avatar",
|
|
|
|
profile_path: "/u/#{@user.username_lower}/preferences/account#profile-picture",
|
2023-01-09 20:10:19 +08:00
|
|
|
),
|
|
|
|
),
|
2015-01-02 19:37:17 +08:00
|
|
|
}
|
2013-09-13 05:46:43 +08:00
|
|
|
end
|
|
|
|
|
2013-09-14 01:49:34 +08:00
|
|
|
# Is a user replying too much in succession?
|
|
|
|
def check_sequential_replies
|
2017-02-04 05:59:05 +08:00
|
|
|
return unless educate_reply?(:notified_about_sequential_replies)
|
2013-09-14 01:49:34 +08:00
|
|
|
|
2018-07-05 04:51:19 +08:00
|
|
|
# Count the posts made by this user in the last day
|
2013-09-14 01:49:34 +08:00
|
|
|
recent_posts_user_ids =
|
|
|
|
Post
|
|
|
|
.where(topic_id: @details[:topic_id])
|
|
|
|
.where("created_at > ?", 1.day.ago)
|
2018-07-05 04:51:19 +08:00
|
|
|
.where(post_type: Post.types[:regular])
|
2013-09-14 01:49:34 +08:00
|
|
|
.order("created_at desc")
|
|
|
|
.limit(SiteSetting.sequential_replies_threshold)
|
|
|
|
.pluck(:user_id)
|
|
|
|
|
|
|
|
# Did we get back as many posts as we asked for, and are they all by the current user?
|
|
|
|
if recent_posts_user_ids.size != SiteSetting.sequential_replies_threshold ||
|
|
|
|
recent_posts_user_ids.detect { |u| u != @user.id }
|
2023-01-09 20:10:19 +08:00
|
|
|
return
|
|
|
|
end
|
2013-09-14 01:49:34 +08:00
|
|
|
|
|
|
|
# If we got this far, log that we've nagged them about the sequential replies
|
2013-09-18 02:38:39 +08:00
|
|
|
UserHistory.create!(
|
|
|
|
action: UserHistory.actions[:notified_about_sequential_replies],
|
|
|
|
target_user_id: @user.id,
|
|
|
|
topic_id: @details[:topic_id],
|
|
|
|
)
|
2013-09-14 01:49:34 +08:00
|
|
|
|
2015-01-02 19:37:17 +08:00
|
|
|
{
|
2016-06-07 01:52:24 +08:00
|
|
|
id: "sequential_replies",
|
2016-06-07 04:58:35 +08:00
|
|
|
templateName: "education",
|
2015-01-02 19:37:17 +08:00
|
|
|
wait_for_typing: true,
|
2015-12-04 11:49:55 +08:00
|
|
|
extraClass: "education-message",
|
2021-08-19 12:02:32 +08:00
|
|
|
hide_if_whisper: true,
|
2015-01-02 19:37:17 +08:00
|
|
|
body: PrettyText.cook(I18n.t("education.sequential_replies")),
|
|
|
|
}
|
2013-09-14 01:49:34 +08:00
|
|
|
end
|
|
|
|
|
2013-09-18 02:38:39 +08:00
|
|
|
def check_dominating_topic
|
2017-02-04 05:59:05 +08:00
|
|
|
return unless educate_reply?(:notified_about_dominating_topic)
|
2013-09-18 02:38:39 +08:00
|
|
|
|
2015-11-28 02:29:44 +08:00
|
|
|
if @topic.blank? || @topic.user_id == @user.id ||
|
|
|
|
@topic.posts_count < SiteSetting.summary_posts_required || @topic.private_message?
|
2023-01-09 20:10:19 +08:00
|
|
|
return
|
|
|
|
end
|
2013-09-18 02:38:39 +08:00
|
|
|
|
2015-11-28 02:29:44 +08:00
|
|
|
posts_by_user = @user.posts.where(topic_id: @topic.id).count
|
2013-09-18 02:38:39 +08:00
|
|
|
|
2015-11-28 02:29:44 +08:00
|
|
|
ratio = (posts_by_user.to_f / @topic.posts_count.to_f)
|
2013-09-18 02:38:39 +08:00
|
|
|
return if ratio < (SiteSetting.dominating_topic_minimum_percent.to_f / 100.0)
|
|
|
|
|
|
|
|
# Log the topic notification
|
2014-02-07 08:19:45 +08:00
|
|
|
UserHistory.create!(
|
|
|
|
action: UserHistory.actions[:notified_about_dominating_topic],
|
2013-09-18 02:38:39 +08:00
|
|
|
target_user_id: @user.id,
|
|
|
|
topic_id: @details[:topic_id],
|
|
|
|
)
|
|
|
|
|
2015-01-02 19:37:17 +08:00
|
|
|
{
|
2016-06-07 01:52:24 +08:00
|
|
|
id: "dominating_topic",
|
2021-07-21 01:58:38 +08:00
|
|
|
templateName: "dominating-topic",
|
2015-01-02 19:37:17 +08:00
|
|
|
wait_for_typing: true,
|
2021-07-21 01:58:38 +08:00
|
|
|
extraClass: "education-message dominating-topic-message",
|
2022-11-23 04:11:53 +08:00
|
|
|
body: PrettyText.cook(I18n.t("education.dominating_topic")),
|
2015-01-02 19:37:17 +08:00
|
|
|
}
|
2013-09-18 02:38:39 +08:00
|
|
|
end
|
|
|
|
|
2017-03-01 05:47:16 +08:00
|
|
|
def check_get_a_room(min_users_posted: 5)
|
2017-02-04 05:59:05 +08:00
|
|
|
return unless educate_reply?(:notified_about_get_a_room)
|
|
|
|
return unless @details[:post_id].present?
|
2021-04-15 01:34:13 +08:00
|
|
|
return if @topic.category&.read_restricted
|
2017-02-04 05:59:05 +08: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-20 05:00:28 +08:00
|
|
|
.order("created_at desc")
|
2017-02-04 05:59:05 +08: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 17:40:11 +08: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-04 05:59:05 +08: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-09 04:27:07 +08:00
|
|
|
|
2017-02-04 05:59:05 +08:00
|
|
|
{
|
|
|
|
id: "get_a_room",
|
2021-08-24 06:34:23 +08:00
|
|
|
templateName: "get-a-room",
|
2017-02-04 05:59:05 +08:00
|
|
|
wait_for_typing: true,
|
2021-08-24 06:34:23 +08:00
|
|
|
reply_username: reply_username,
|
|
|
|
extraClass: "education-message get-a-room",
|
2017-02-04 05:59:05 +08:00
|
|
|
body:
|
|
|
|
PrettyText.cook(
|
2017-02-09 04:27:07 +08:00
|
|
|
I18n.t(
|
|
|
|
"education.get_a_room",
|
|
|
|
count: SiteSetting.get_a_room_threshold,
|
2018-11-08 00:59:42 +08:00
|
|
|
reply_username: reply_username,
|
|
|
|
base_path: Discourse.base_path,
|
2017-02-09 04:27:07 +08:00
|
|
|
),
|
2017-02-04 05:59:05 +08: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 23:08:00 +08: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
|
|
|
|
|
2014-03-12 22:44:08 +08:00
|
|
|
def check_reviving_old_topic
|
2014-03-20 08:04:42 +08:00
|
|
|
return unless replying?
|
2014-03-12 22:44:08 +08:00
|
|
|
if @topic.nil? || SiteSetting.warn_reviving_old_topic_age < 1 || @topic.last_posted_at.nil? ||
|
2015-11-28 02:29:44 +08:00
|
|
|
@topic.last_posted_at > SiteSetting.warn_reviving_old_topic_age.days.ago
|
2023-01-09 20:10:19 +08:00
|
|
|
return
|
|
|
|
end
|
2014-03-12 22:44:08 +08:00
|
|
|
|
2015-01-02 19:37:17 +08:00
|
|
|
{
|
2016-06-07 01:52:24 +08:00
|
|
|
id: "reviving_old",
|
2016-06-07 04:58:35 +08:00
|
|
|
templateName: "education",
|
2015-01-02 19:37:17 +08:00
|
|
|
wait_for_typing: false,
|
2015-12-04 11:49:55 +08:00
|
|
|
extraClass: "education-message",
|
2018-11-08 00:59:42 +08:00
|
|
|
body:
|
|
|
|
PrettyText.cook(
|
|
|
|
I18n.t(
|
|
|
|
"education.reviving_old_topic",
|
|
|
|
time_ago:
|
2023-05-25 20:53:59 +08:00
|
|
|
AgeWords.time_ago_in_words(
|
2018-11-08 00:59:42 +08:00
|
|
|
@topic.last_posted_at,
|
|
|
|
false,
|
|
|
|
scope: :"datetime.distance_in_words_verbose",
|
|
|
|
),
|
|
|
|
),
|
2023-01-09 20:10:19 +08:00
|
|
|
),
|
2015-01-02 19:37:17 +08:00
|
|
|
}
|
2014-03-12 22:44:08 +08:00
|
|
|
end
|
|
|
|
|
2022-09-28 00:36:40 +08:00
|
|
|
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-13 05:46:43 +08:00
|
|
|
private
|
|
|
|
|
2017-02-04 05:59:05 +08: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-13 05:46:43 +08:00
|
|
|
def creating_topic?
|
2016-06-07 04:58:35 +08:00
|
|
|
@details[:composer_action] == "createTopic"
|
2013-09-13 05:46:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def replying?
|
2016-06-07 04:58:35 +08:00
|
|
|
@details[:composer_action] == "reply"
|
2013-09-13 05:46:43 +08:00
|
|
|
end
|
|
|
|
|
2018-10-04 08:21:53 +08:00
|
|
|
def editing_post?
|
|
|
|
@details[:composer_action] == "edit"
|
|
|
|
end
|
2014-02-07 08:19:45 +08:00
|
|
|
end
|