clean up 'checked_for_custom_avatars' user history entries

This commit is contained in:
Régis Hanol 2015-01-02 12:37:17 +01:00
parent 9fcaf090ec
commit c57a1b393f
4 changed files with 66 additions and 57 deletions

View File

@ -20,7 +20,7 @@ class UserHistory < ActiveRecord::Base
:change_site_setting, :change_site_setting,
:change_site_customization, :change_site_customization,
:delete_site_customization, :delete_site_customization,
:checked_for_custom_avatar, :checked_for_custom_avatar, # not used anymore
:notified_about_avatar, :notified_about_avatar,
:notified_about_sequential_replies, :notified_about_sequential_replies,
:notified_about_dominating_topic, :notified_about_dominating_topic,

View File

@ -0,0 +1,10 @@
class CleanUpUserHistory < ActiveRecord::Migration
def up
# 'checked_for_custom_avatar' is not used anymore
# was removed in https://github.com/discourse/discourse/commit/6c1c8be79433f87bef9d768da7b8fa4ec9bb18d7
UserHistory.where(action: UserHistory.actions[:checked_for_custom_avatar]).delete_all
end
def down
end
end

View File

@ -26,9 +26,11 @@ class ComposerMessagesFinder
if count < SiteSetting.educate_until_posts if count < SiteSetting.educate_until_posts
education_posts_text = I18n.t('education.until_posts', count: SiteSetting.educate_until_posts) education_posts_text = I18n.t('education.until_posts', count: SiteSetting.educate_until_posts)
return {templateName: 'composer/education', return {
wait_for_typing: true, templateName: 'composer/education',
body: PrettyText.cook(SiteText.text_for(education_key, education_posts_text: education_posts_text)) } wait_for_typing: true,
body: PrettyText.cook(SiteText.text_for(education_key, education_posts_text: education_posts_text))
}
end end
nil nil
@ -37,7 +39,11 @@ class ComposerMessagesFinder
# New users have a limited number of replies in a topic # New users have a limited number of replies in a topic
def check_new_user_many_replies def check_new_user_many_replies
return unless replying? && @user.posted_too_much_in_topic?(@details[:topic_id]) return unless replying? && @user.posted_too_much_in_topic?(@details[:topic_id])
{templateName: 'composer/education', body: PrettyText.cook(I18n.t('education.too_many_replies', newuser_max_replies_per_topic: SiteSetting.newuser_max_replies_per_topic)) }
{
templateName: 'composer/education',
body: PrettyText.cook(I18n.t('education.too_many_replies', newuser_max_replies_per_topic: SiteSetting.newuser_max_replies_per_topic))
}
end end
# Should a user be contacted to update their avatar? # Should a user be contacted to update their avatar?
@ -49,14 +55,14 @@ class ComposerMessagesFinder
# We don't notify users who have avatars or who have been notified already. # We don't notify users who have avatars or who have been notified already.
return if @user.uploaded_avatar_id || UserHistory.exists_for_user?(@user, :notified_about_avatar) return if @user.uploaded_avatar_id || UserHistory.exists_for_user?(@user, :notified_about_avatar)
# Finally, we don't check users whose avatars haven't been examined
return unless UserHistory.exists_for_user?(@user, :checked_for_custom_avatar)
# If we got this far, log that we've nagged them about the avatar # 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 ) UserHistory.create!(action: UserHistory.actions[:notified_about_avatar], target_user_id: @user.id )
# Return the message # Return the message
{templateName: 'composer/education', body: PrettyText.cook(I18n.t('education.avatar', profile_path: "/users/#{@user.username_lower}")) } {
templateName: 'composer/education',
body: PrettyText.cook(I18n.t('education.avatar', profile_path: "/users/#{@user.username_lower}"))
}
end end
# Is a user replying too much in succession? # Is a user replying too much in succession?
@ -87,10 +93,12 @@ class ComposerMessagesFinder
target_user_id: @user.id, target_user_id: @user.id,
topic_id: @details[:topic_id] ) topic_id: @details[:topic_id] )
{templateName: 'composer/education', {
wait_for_typing: true, templateName: 'composer/education',
extraClass: 'urgent', wait_for_typing: true,
body: PrettyText.cook(I18n.t('education.sequential_replies')) } extraClass: 'urgent',
body: PrettyText.cook(I18n.t('education.sequential_replies'))
}
end end
def check_dominating_topic def check_dominating_topic
@ -102,6 +110,7 @@ class ComposerMessagesFinder
!UserHistory.exists_for_user?(@user, :notified_about_dominating_topic, topic_id: @details[:topic_id]) !UserHistory.exists_for_user?(@user, :notified_about_dominating_topic, topic_id: @details[:topic_id])
topic = Topic.find_by(id: @details[:topic_id]) topic = Topic.find_by(id: @details[:topic_id])
return if topic.blank? || return if topic.blank? ||
topic.user_id == @user.id || topic.user_id == @user.id ||
topic.posts_count < SiteSetting.summary_posts_required || topic.posts_count < SiteSetting.summary_posts_required ||
@ -117,11 +126,12 @@ class ComposerMessagesFinder
target_user_id: @user.id, target_user_id: @user.id,
topic_id: @details[:topic_id]) topic_id: @details[:topic_id])
{
{templateName: 'composer/education', templateName: 'composer/education',
wait_for_typing: true, wait_for_typing: true,
extraClass: 'urgent', extraClass: 'urgent',
body: PrettyText.cook(I18n.t('education.dominating_topic', percent: (ratio * 100).round)) } body: PrettyText.cook(I18n.t('education.dominating_topic', percent: (ratio * 100).round))
}
end end
def check_reviving_old_topic def check_reviving_old_topic
@ -136,20 +146,22 @@ class ComposerMessagesFinder
topic.last_posted_at.nil? || topic.last_posted_at.nil? ||
topic.last_posted_at > SiteSetting.warn_reviving_old_topic_age.days.ago topic.last_posted_at > SiteSetting.warn_reviving_old_topic_age.days.ago
{templateName: 'composer/education', {
wait_for_typing: false, templateName: 'composer/education',
extraClass: 'urgent', wait_for_typing: false,
body: PrettyText.cook(I18n.t('education.reviving_old_topic', days: (Time.zone.now - topic.last_posted_at).round / 1.day)) } extraClass: 'urgent',
body: PrettyText.cook(I18n.t('education.reviving_old_topic', days: (Time.zone.now - topic.last_posted_at).round / 1.day))
}
end end
private private
def creating_topic? def creating_topic?
return @details[:composerAction] == "createTopic" @details[:composerAction] == "createTopic"
end end
def replying? def replying?
return @details[:composerAction] == "reply" @details[:composerAction] == "reply"
end end
end end

View File

@ -83,44 +83,31 @@ describe ComposerMessagesFinder do
let(:finder) { ComposerMessagesFinder.new(user, composerAction: 'createTopic') } let(:finder) { ComposerMessagesFinder.new(user, composerAction: 'createTopic') }
let(:user) { Fabricate(:user) } let(:user) { Fabricate(:user) }
context "a user who we haven't checked for an avatar yet" do context "success" do
it "returns no avatar message" do let!(:message) { finder.check_avatar_notification }
finder.check_avatar_notification.should be_blank
it "returns an avatar upgrade message" do
message.should be_present
end
it "creates a notified_about_avatar log" do
UserHistory.exists_for_user?(user, :notified_about_avatar).should == true
end end
end end
context "a user who has been checked for a custom avatar" do it "doesn't return notifications for new users" do
before do user.trust_level = TrustLevel[0]
UserHistory.create!(action: UserHistory.actions[:checked_for_custom_avatar], target_user_id: user.id ) finder.check_avatar_notification.should be_blank
end end
context "success" do it "doesn't return notifications for users who have custom avatars" do
let!(:message) { finder.check_avatar_notification } user.uploaded_avatar_id = 1
finder.check_avatar_notification.should be_blank
it "returns an avatar upgrade message" do end
message.should be_present
end
it "creates a notified_about_avatar log" do
UserHistory.exists_for_user?(user, :notified_about_avatar).should == true
end
end
it "doesn't return notifications for new users" do
user.trust_level = TrustLevel[0]
finder.check_avatar_notification.should be_blank
end
it "doesn't return notifications for users who have custom avatars" do
user.uploaded_avatar_id = 1
finder.check_avatar_notification.should be_blank
end
it "doesn't notify users who have been notified already" do
UserHistory.create!(action: UserHistory.actions[:notified_about_avatar], target_user_id: user.id )
finder.check_avatar_notification.should be_blank
end
it "doesn't notify users who have been notified already" do
UserHistory.create!(action: UserHistory.actions[:notified_about_avatar], target_user_id: user.id )
finder.check_avatar_notification.should be_blank
end end
end end