From 06ea8140aaf42384e39fcc2ee9ea159c6a8b93d4 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 11 Sep 2013 15:14:18 -0400 Subject: [PATCH] New job (default off) to detect whether users have uploaded custom avatars by contacting Gravatar. --- app/models/site_setting.rb | 3 +++ config/locales/server.en.yml | 4 ++++ lib/jobs/detect_avatars.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 lib/jobs/detect_avatars.rb diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index 5a9f4b89f21..6ceb9291f19 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -254,6 +254,9 @@ class SiteSetting < ActiveRecord::Base client_setting(:allow_uploaded_avatars, true) client_setting(:allow_animated_avatars, false) + setting(:detect_custom_avatars, false) + setting(:max_daily_gravatar_crawls, 500) + def self.generate_api_key! self.api_key = SecureRandom.hex(32) end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 18e75e4ae83..5f5d324f851 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -672,6 +672,10 @@ en: allow_animated_avatars: "Allow users to use animated gif for avatars. WARNING: it is highly recommended to run the avatars:regenerate rake task after changing that setting." default_digest_email_frequency: "How often users receive digest emails by default. They can change this setting in their preferences." + detect_custom_avatars: "Whether or not to check that users have uploaded custom avatars" + max_daily_gravatar_crawls: "The maximum amount of times Discourse will check gravatar for custom avatars in a day" + + notification_types: mentioned: "%{display_username} mentioned you in %{link}" liked: "%{display_username} liked your post in %{link}" diff --git a/lib/jobs/detect_avatars.rb b/lib/jobs/detect_avatars.rb new file mode 100644 index 00000000000..25987ac8794 --- /dev/null +++ b/lib/jobs/detect_avatars.rb @@ -0,0 +1,26 @@ +require_dependency 'avatar_detector' + +module Jobs + + class DetectAvatars < Jobs::Scheduled + recurrence { daily.hour_of_day(8) } + + def execute(args) + return unless SiteSetting.detect_custom_avatars? + + # Find a random sampling of users of trust level 1 or higher who don't have a custom avatar. + user_stats = UserStat.where('user_stats.has_custom_avatar = false AND users.trust_level > 0') + .includes(:user) + .order("random()") + .limit(SiteSetting.max_daily_gravatar_crawls) + + if user_stats.present? + user_stats.each do |us| + us.update_column(:has_custom_avatar, true) if AvatarDetector.new(us.user).has_custom_avatar? + end + end + end + + end + +end \ No newline at end of file