2013-09-12 17:46:43 -04:00
|
|
|
class ComposerMessagesFinder
|
|
|
|
|
|
|
|
def initialize(user, details)
|
|
|
|
@user = user
|
|
|
|
@details = details
|
2015-11-27 13:29:44 -05: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
|
2017-07-27 21:20:09 -04:00
|
|
|
@check_methods ||= instance_methods.find_all { |m| m =~ /^check\_/ }
|
2017-02-03 16:59:05 -05:00
|
|
|
end
|
|
|
|
|
2013-09-12 17:46:43 -04:00
|
|
|
def find
|
2017-02-03 16:59:05 -05:00
|
|
|
self.class.check_methods.each do |m|
|
|
|
|
msg = send(m)
|
|
|
|
return msg if msg.present?
|
|
|
|
end
|
|
|
|
nil
|
2013-09-12 17:46:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Determines whether to show the user education text
|
|
|
|
def check_education_message
|
2016-11-08 22:13:34 -05:00
|
|
|
return if @topic && @topic.archetype == Archetype.private_message
|
2016-11-04 05:06:53 -04:00
|
|
|
|
2013-09-12 17:46:43 -04:00
|
|
|
if creating_topic?
|
|
|
|
count = @user.created_topic_count
|
2015-11-27 17:00:26 -05:00
|
|
|
education_key = 'education.new-topic'
|
2013-09-12 17:46:43 -04:00
|
|
|
else
|
2013-09-14 00:32:18 -04:00
|
|
|
count = @user.post_count
|
2015-11-27 17:00:26 -05:00
|
|
|
education_key = 'education.new-reply'
|
2013-09-12 17:46:43 -04:00
|
|
|
end
|
|
|
|
|
2013-09-17 12:11:17 -04:00
|
|
|
if count < SiteSetting.educate_until_posts
|
2013-09-12 17:46:43 -04:00
|
|
|
education_posts_text = I18n.t('education.until_posts', count: SiteSetting.educate_until_posts)
|
2015-01-02 06:37:17 -05: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 06:37:17 -05:00
|
|
|
wait_for_typing: true,
|
2015-11-27 17:23:43 -05:00
|
|
|
body: PrettyText.cook(I18n.t(education_key, education_posts_text: education_posts_text, site_name: SiteSetting.title))
|
2015-01-02 06:37:17 -05:00
|
|
|
}
|
2013-09-12 17:46:43 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 22:13:34 -05: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 06:37:17 -05: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 06:37:17 -05:00
|
|
|
body: PrettyText.cook(I18n.t('education.too_many_replies', newuser_max_replies_per_topic: SiteSetting.newuser_max_replies_per_topic))
|
|
|
|
}
|
2013-12-19 13:45:55 -05:00
|
|
|
end
|
|
|
|
|
2013-09-12 17:46:43 -04: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 01:20:39 -04:00
|
|
|
return unless @user.has_trust_level?(TrustLevel[1])
|
2013-09-12 17:46:43 -04:00
|
|
|
|
|
|
|
# We don't notify users who have avatars or who have been notified already.
|
2014-05-22 03:37:02 -04:00
|
|
|
return if @user.uploaded_avatar_id || UserHistory.exists_for_user?(@user, :notified_about_avatar)
|
2013-09-12 17:46:43 -04:00
|
|
|
|
2015-12-14 12:02:40 -05: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
|
|
|
|
return if SiteSetting.disable_avatar_education_message || SiteSetting.sso_overrides_avatar || !SiteSetting.allow_uploaded_avatars
|
|
|
|
|
2013-09-12 17:46:43 -04:00
|
|
|
# If we got this far, log that we've nagged them about the avatar
|
2017-07-27 21:20:09 -04:00
|
|
|
UserHistory.create!(action: UserHistory.actions[:notified_about_avatar], target_user_id: @user.id)
|
2013-09-12 17:46:43 -04:00
|
|
|
|
|
|
|
# Return the message
|
2015-01-02 06:37:17 -05:00
|
|
|
{
|
2016-06-06 13:52:24 -04:00
|
|
|
id: 'avatar',
|
2016-06-06 16:58:35 -04:00
|
|
|
templateName: 'education',
|
2017-03-28 14:27:54 -04:00
|
|
|
body: PrettyText.cook(I18n.t('education.avatar', profile_path: "/u/#{@user.username_lower}"))
|
2015-01-02 06:37:17 -05:00
|
|
|
}
|
2013-09-12 17:46:43 -04:00
|
|
|
end
|
|
|
|
|
2013-09-13 13:49:34 -04:00
|
|
|
# Is a user replying too much in succession?
|
|
|
|
def check_sequential_replies
|
2017-02-03 16:59:05 -05:00
|
|
|
return unless educate_reply?(:notified_about_sequential_replies)
|
2013-09-13 13:49:34 -04:00
|
|
|
|
|
|
|
# Count the topics made by this user in the last day
|
|
|
|
recent_posts_user_ids = Post.where(topic_id: @details[:topic_id])
|
2017-07-27 21:20:09 -04:00
|
|
|
.where("created_at > ?", 1.day.ago)
|
|
|
|
.order('created_at desc')
|
|
|
|
.limit(SiteSetting.sequential_replies_threshold)
|
|
|
|
.pluck(:user_id)
|
2013-09-13 13:49:34 -04:00
|
|
|
|
|
|
|
# Did we get back as many posts as we asked for, and are they all by the current user?
|
|
|
|
return if recent_posts_user_ids.size != SiteSetting.sequential_replies_threshold ||
|
2017-07-27 21:20:09 -04:00
|
|
|
recent_posts_user_ids.detect { |u| u != @user.id }
|
2013-09-13 13:49:34 -04:00
|
|
|
|
|
|
|
# If we got this far, log that we've nagged them about the sequential replies
|
2013-09-17 14:38:39 -04:00
|
|
|
UserHistory.create!(action: UserHistory.actions[:notified_about_sequential_replies],
|
|
|
|
target_user_id: @user.id,
|
2017-07-27 21:20:09 -04:00
|
|
|
topic_id: @details[:topic_id])
|
2013-09-13 13:49:34 -04:00
|
|
|
|
2015-01-02 06:37:17 -05:00
|
|
|
{
|
2016-06-06 13:52:24 -04:00
|
|
|
id: 'sequential_replies',
|
2016-06-06 16:58:35 -04:00
|
|
|
templateName: 'education',
|
2015-01-02 06:37:17 -05:00
|
|
|
wait_for_typing: true,
|
2015-12-03 22:49:55 -05:00
|
|
|
extraClass: 'education-message',
|
2015-01-02 06:37:17 -05:00
|
|
|
body: PrettyText.cook(I18n.t('education.sequential_replies'))
|
|
|
|
}
|
2013-09-13 13:49:34 -04: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 13:29:44 -05:00
|
|
|
return if @topic.blank? ||
|
|
|
|
@topic.user_id == @user.id ||
|
|
|
|
@topic.posts_count < SiteSetting.summary_posts_required ||
|
|
|
|
@topic.private_message?
|
2013-09-17 14:38:39 -04:00
|
|
|
|
2015-11-27 13:29:44 -05:00
|
|
|
posts_by_user = @user.posts.where(topic_id: @topic.id).count
|
2013-09-17 14:38:39 -04:00
|
|
|
|
2015-11-27 13:29:44 -05: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-06 19:19:45 -05:00
|
|
|
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 06:37:17 -05:00
|
|
|
{
|
2016-06-06 13:52:24 -04:00
|
|
|
id: 'dominating_topic',
|
2016-06-06 16:58:35 -04:00
|
|
|
templateName: 'education',
|
2015-01-02 06:37:17 -05:00
|
|
|
wait_for_typing: true,
|
2015-12-03 22:49:55 -05:00
|
|
|
extraClass: 'education-message',
|
2015-01-02 06:37:17 -05:00
|
|
|
body: PrettyText.cook(I18n.t('education.dominating_topic', percent: (ratio * 100).round))
|
|
|
|
}
|
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)
|
2017-02-03 16:59:05 -05:00
|
|
|
return unless educate_reply?(:notified_about_get_a_room)
|
|
|
|
return unless @details[:post_id].present?
|
|
|
|
|
|
|
|
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).
|
2017-07-27 21:20:09 -04:00
|
|
|
find_all { |uid| uid != @user.id && uid == reply_to_user_id }
|
2017-02-03 16:59:05 -05:00
|
|
|
|
|
|
|
return unless last_x_replies.size == SiteSetting.get_a_room_threshold
|
2017-02-28 16:47:16 -05:00
|
|
|
return unless @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])
|
|
|
|
|
2017-02-08 15:27:07 -05:00
|
|
|
reply_username = User.where(id: last_x_replies[0]).pluck(:username).first
|
|
|
|
|
2017-02-03 16:59:05 -05:00
|
|
|
{
|
|
|
|
id: 'get_a_room',
|
|
|
|
templateName: 'education',
|
|
|
|
wait_for_typing: true,
|
|
|
|
extraClass: 'education-message',
|
|
|
|
body: PrettyText.cook(
|
2017-02-08 15:27:07 -05:00
|
|
|
I18n.t(
|
|
|
|
'education.get_a_room',
|
|
|
|
count: SiteSetting.get_a_room_threshold,
|
|
|
|
reply_username: reply_username
|
|
|
|
)
|
2017-02-03 16:59:05 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-03-12 10:44:08 -04:00
|
|
|
def check_reviving_old_topic
|
2014-03-19 20:04:42 -04:00
|
|
|
return unless replying?
|
2015-11-27 13:29:44 -05:00
|
|
|
return if @topic.nil? ||
|
2014-03-12 10:44:08 -04:00
|
|
|
SiteSetting.warn_reviving_old_topic_age < 1 ||
|
2015-11-27 13:29:44 -05:00
|
|
|
@topic.last_posted_at.nil? ||
|
|
|
|
@topic.last_posted_at > SiteSetting.warn_reviving_old_topic_age.days.ago
|
2014-03-12 10:44:08 -04:00
|
|
|
|
2015-01-02 06:37:17 -05:00
|
|
|
{
|
2016-06-06 13:52:24 -04:00
|
|
|
id: 'reviving_old',
|
2016-06-06 16:58:35 -04:00
|
|
|
templateName: 'education',
|
2015-01-02 06:37:17 -05:00
|
|
|
wait_for_typing: false,
|
2015-12-03 22:49:55 -05:00
|
|
|
extraClass: 'education-message',
|
2018-01-25 13:36:53 -05:00
|
|
|
body: PrettyText.cook(I18n.t('education.reviving_old_topic', time_ago: FreedomPatches::Rails4.time_ago_in_words(@topic.last_posted_at, false, scope: :'datetime.distance_in_words_verbose')))
|
2015-01-02 06:37:17 -05:00
|
|
|
}
|
2014-03-12 10:44:08 -04:00
|
|
|
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
|
|
|
|
|
2014-02-06 19:19:45 -05:00
|
|
|
end
|