2016-02-16 23:46:19 -05:00
|
|
|
class UserOption < ActiveRecord::Base
|
|
|
|
self.primary_key = :user_id
|
|
|
|
belongs_to :user
|
|
|
|
before_create :set_defaults
|
|
|
|
|
2016-02-18 00:57:22 -05:00
|
|
|
after_save :update_tracked_topics
|
|
|
|
|
2016-03-16 19:03:56 -04:00
|
|
|
def self.ensure_consistency!
|
|
|
|
exec_sql("SELECT u.id FROM users u
|
|
|
|
LEFT JOIN user_options o ON o.user_id = u.id
|
2017-07-27 21:20:09 -04:00
|
|
|
WHERE o.user_id IS NULL").values.each do |id, _|
|
2016-03-16 19:03:56 -04:00
|
|
|
UserOption.create(user_id: id.to_i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-18 21:56:52 -05:00
|
|
|
def self.previous_replies_type
|
|
|
|
@previous_replies_type ||= Enum.new(always: 0, unless_emailed: 1, never: 2)
|
|
|
|
end
|
|
|
|
|
2016-03-02 07:16:52 -05:00
|
|
|
def self.like_notification_frequency_type
|
2016-03-05 17:12:59 -05:00
|
|
|
@like_notification_frequency_type ||= Enum.new(always: 0, first_time_and_daily: 1, first_time: 2, never: 3)
|
2016-03-02 07:16:52 -05:00
|
|
|
end
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
def set_defaults
|
|
|
|
self.email_always = SiteSetting.default_email_always
|
|
|
|
self.mailing_list_mode = SiteSetting.default_email_mailing_list_mode
|
2016-05-21 09:17:54 -04:00
|
|
|
self.mailing_list_mode_frequency = SiteSetting.default_email_mailing_list_mode_frequency
|
2016-02-16 23:46:19 -05:00
|
|
|
self.email_direct = SiteSetting.default_email_direct
|
|
|
|
self.automatically_unpin_topics = SiteSetting.default_topics_automatic_unpin
|
|
|
|
self.email_private_messages = SiteSetting.default_email_private_messages
|
2016-02-18 21:56:52 -05:00
|
|
|
self.email_previous_replies = SiteSetting.default_email_previous_replies
|
2016-02-25 08:05:40 -05:00
|
|
|
self.email_in_reply_to = SiteSetting.default_email_in_reply_to
|
2016-02-16 23:46:19 -05:00
|
|
|
|
|
|
|
self.enable_quoting = SiteSetting.default_other_enable_quoting
|
|
|
|
self.external_links_in_new_tab = SiteSetting.default_other_external_links_in_new_tab
|
|
|
|
self.dynamic_favicon = SiteSetting.default_other_dynamic_favicon
|
|
|
|
self.disable_jump_reply = SiteSetting.default_other_disable_jump_reply
|
|
|
|
|
2016-02-18 00:57:22 -05:00
|
|
|
self.new_topic_duration_minutes = SiteSetting.default_other_new_topic_duration_minutes
|
|
|
|
self.auto_track_topics_after_msecs = SiteSetting.default_other_auto_track_topics_after_msecs
|
2016-09-30 12:36:43 -04:00
|
|
|
self.notification_level_when_replying = SiteSetting.default_other_notification_level_when_replying
|
2016-02-18 00:57:22 -05:00
|
|
|
|
2016-03-02 07:16:52 -05:00
|
|
|
self.like_notification_frequency = SiteSetting.default_other_like_notification_frequency
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
if SiteSetting.default_email_digest_frequency.to_i <= 0
|
|
|
|
self.email_digests = false
|
|
|
|
else
|
|
|
|
self.email_digests = true
|
2016-03-02 15:26:27 -05:00
|
|
|
self.digest_after_minutes ||= SiteSetting.default_email_digest_frequency.to_i
|
2016-02-16 23:46:19 -05:00
|
|
|
end
|
|
|
|
|
2016-03-17 17:35:23 -04:00
|
|
|
self.include_tl0_in_digests = SiteSetting.default_include_tl0_in_digests
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
true
|
|
|
|
end
|
2016-02-18 00:57:22 -05:00
|
|
|
|
2016-03-04 16:53:28 -05:00
|
|
|
def mailing_list_mode
|
|
|
|
return false if SiteSetting.disable_mailing_list_mode
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2016-02-18 00:57:22 -05:00
|
|
|
def redirected_to_top_yet?
|
|
|
|
last_redirected_to_top_at.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_last_redirected_to_top!
|
|
|
|
key = "user:#{id}:update_last_redirected_to_top"
|
|
|
|
delay = SiteSetting.active_user_rate_limit_secs
|
|
|
|
|
|
|
|
# only update last_redirected_to_top_at once every minute
|
|
|
|
return unless $redis.setnx(key, "1")
|
|
|
|
$redis.expire(key, delay)
|
|
|
|
|
|
|
|
# delay the update
|
2017-10-04 16:08:41 -04:00
|
|
|
Jobs.enqueue_in(delay / 2, :update_top_redirection, user_id: self.user_id, redirected_at: Time.zone.now)
|
2016-02-18 00:57:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def should_be_redirected_to_top
|
|
|
|
redirected_to_top.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirected_to_top
|
|
|
|
# redirect is enabled
|
|
|
|
return unless SiteSetting.redirect_users_to_top_page
|
2016-12-05 01:37:43 -05:00
|
|
|
|
|
|
|
# PERF: bypass min_redirected_to_top query for users that were seen already
|
|
|
|
return if user.trust_level > 0 && user.last_seen_at && user.last_seen_at > 1.month.ago
|
|
|
|
|
2016-02-18 00:57:22 -05:00
|
|
|
# top must be in the top_menu
|
2017-10-04 16:08:41 -04:00
|
|
|
return unless SiteSetting.top_menu[/\btop\b/i]
|
2016-12-05 01:37:43 -05:00
|
|
|
|
2016-02-18 00:57:22 -05:00
|
|
|
# not enough topics
|
2017-10-04 16:08:41 -04:00
|
|
|
return unless period = SiteSetting.min_redirected_to_top_period(1.day.ago)
|
2016-02-18 00:57:22 -05:00
|
|
|
|
|
|
|
if !user.seen_before? || (user.trust_level == 0 && !redirected_to_top_yet?)
|
|
|
|
update_last_redirected_to_top!
|
|
|
|
return {
|
|
|
|
reason: I18n.t('redirected_to_top_reasons.new_user'),
|
|
|
|
period: period
|
|
|
|
}
|
|
|
|
elsif user.last_seen_at < 1.month.ago
|
|
|
|
update_last_redirected_to_top!
|
|
|
|
return {
|
|
|
|
reason: I18n.t('redirected_to_top_reasons.not_seen_in_a_month'),
|
|
|
|
period: period
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# don't redirect to top
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def treat_as_new_topic_start_date
|
|
|
|
duration = new_topic_duration_minutes || SiteSetting.default_other_new_topic_duration_minutes.to_i
|
2017-07-27 21:20:09 -04:00
|
|
|
times = [
|
|
|
|
case duration
|
2016-02-18 00:57:22 -05:00
|
|
|
when User::NewTopicDuration::ALWAYS
|
|
|
|
user.created_at
|
|
|
|
when User::NewTopicDuration::LAST_VISIT
|
|
|
|
user.previous_visit_at || user.user_stat.new_since
|
|
|
|
else
|
|
|
|
duration.minutes.ago
|
2017-07-27 21:20:09 -04:00
|
|
|
end,
|
|
|
|
user.user_stat.new_since,
|
|
|
|
Time.at(SiteSetting.min_new_topics_time).to_datetime
|
|
|
|
]
|
2016-02-18 00:57:22 -05:00
|
|
|
|
|
|
|
times.max
|
|
|
|
end
|
|
|
|
|
2017-11-09 14:45:19 -05:00
|
|
|
def homepage
|
|
|
|
case homepage_id
|
|
|
|
when 1 then "latest"
|
|
|
|
when 2 then "categories"
|
|
|
|
when 3 then "unread"
|
|
|
|
when 4 then "new"
|
|
|
|
when 5 then "top"
|
|
|
|
else SiteSetting.homepage
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-06 03:56:58 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def update_tracked_topics
|
|
|
|
return unless saved_change_to_auto_track_topics_after_msecs?
|
|
|
|
TrackedTopicsUpdater.new(id, auto_track_topics_after_msecs).call
|
|
|
|
end
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
end
|
2016-02-22 18:33:53 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_options
|
|
|
|
#
|
2016-09-30 12:36:43 -04:00
|
|
|
# user_id :integer not null, primary key
|
|
|
|
# email_always :boolean default(FALSE), not null
|
|
|
|
# mailing_list_mode :boolean default(FALSE), not null
|
|
|
|
# email_digests :boolean
|
|
|
|
# email_direct :boolean default(TRUE), not null
|
|
|
|
# email_private_messages :boolean default(TRUE), not null
|
|
|
|
# external_links_in_new_tab :boolean default(FALSE), not null
|
|
|
|
# enable_quoting :boolean default(TRUE), not null
|
|
|
|
# dynamic_favicon :boolean default(FALSE), not null
|
|
|
|
# disable_jump_reply :boolean default(FALSE), not null
|
|
|
|
# automatically_unpin_topics :boolean default(TRUE), not null
|
|
|
|
# digest_after_minutes :integer
|
|
|
|
# auto_track_topics_after_msecs :integer
|
|
|
|
# new_topic_duration_minutes :integer
|
|
|
|
# last_redirected_to_top_at :datetime
|
|
|
|
# email_previous_replies :integer default(2), not null
|
|
|
|
# email_in_reply_to :boolean default(TRUE), not null
|
|
|
|
# like_notification_frequency :integer default(1), not null
|
2017-05-05 00:11:20 -04:00
|
|
|
# mailing_list_mode_frequency :integer default(1), not null
|
2016-09-30 12:36:43 -04:00
|
|
|
# include_tl0_in_digests :boolean default(FALSE)
|
2017-05-12 14:46:57 -04:00
|
|
|
# notification_level_when_replying :integer
|
|
|
|
# theme_key :string
|
2017-05-15 12:48:08 -04:00
|
|
|
# theme_key_seq :integer default(0), not null
|
2017-10-06 03:56:58 -04:00
|
|
|
# allow_private_messages :boolean default(TRUE), not null
|
2017-11-23 15:55:44 -05:00
|
|
|
# homepage_id :integer
|
2016-02-22 18:33:53 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_user_options_on_user_id (user_id) UNIQUE
|
|
|
|
#
|