2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
class UserOption < ActiveRecord::Base
|
2021-07-06 05:47:17 -04:00
|
|
|
self.ignored_columns = [
|
|
|
|
"disable_jump_reply", # Remove once 20210706091905 is promoted from post_deploy to regular migration
|
|
|
|
]
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
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
|
|
|
|
|
2022-05-23 09:20:51 -04:00
|
|
|
scope :human_users, -> { where("user_id > 0") }
|
|
|
|
|
2021-11-26 10:34:07 -05:00
|
|
|
enum default_calendar: { none_selected: 0, ics: 1, google: 2 }, _scopes: false
|
2022-10-17 22:21:52 -04:00
|
|
|
enum sidebar_list_destination: {
|
|
|
|
none_selected: 0,
|
|
|
|
default: 0,
|
|
|
|
unread_new: 1,
|
|
|
|
},
|
|
|
|
_prefix: "sidebar_list"
|
2021-10-05 23:11:52 -04:00
|
|
|
|
2016-03-16 19:03:56 -04:00
|
|
|
def self.ensure_consistency!
|
2018-06-19 02:13:14 -04:00
|
|
|
sql = <<~SQL
|
|
|
|
SELECT u.id FROM users u
|
|
|
|
LEFT JOIN user_options o ON o.user_id = u.id
|
|
|
|
WHERE o.user_id IS NULL
|
|
|
|
SQL
|
|
|
|
|
|
|
|
DB.query_single(sql).each { |id| UserOption.create(user_id: id) }
|
2016-03-16 19:03:56 -04:00
|
|
|
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
|
|
|
|
|
2019-01-14 08:21:46 -05:00
|
|
|
def self.text_sizes
|
2020-07-07 13:08:19 -04:00
|
|
|
@text_sizes ||= Enum.new(normal: 0, larger: 1, largest: 2, smaller: 3, smallest: 4)
|
2019-01-14 08:21:46 -05:00
|
|
|
end
|
|
|
|
|
2019-04-11 19:02:18 -04:00
|
|
|
def self.title_count_modes
|
|
|
|
@title_count_modes ||= Enum.new(notifications: 0, contextual: 1)
|
|
|
|
end
|
|
|
|
|
2019-03-15 10:55:11 -04:00
|
|
|
def self.email_level_types
|
|
|
|
@email_level_type ||= Enum.new(always: 0, only_when_away: 1, never: 2)
|
|
|
|
end
|
|
|
|
|
2019-01-14 08:21:46 -05:00
|
|
|
validates :text_size_key, inclusion: { in: UserOption.text_sizes.values }
|
2019-03-15 10:55:11 -04:00
|
|
|
validates :email_level, inclusion: { in: UserOption.email_level_types.values }
|
|
|
|
validates :email_messages_level, inclusion: { in: UserOption.email_level_types.values }
|
2019-11-24 19:49:27 -05:00
|
|
|
validates :timezone, timezone: true
|
2019-01-14 08:21:46 -05:00
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
def set_defaults
|
|
|
|
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
|
2019-03-15 10:55:11 -04:00
|
|
|
self.email_level = SiteSetting.default_email_level
|
|
|
|
self.email_messages_level = SiteSetting.default_email_messages_level
|
2016-02-16 23:46:19 -05:00
|
|
|
self.automatically_unpin_topics = SiteSetting.default_topics_automatic_unpin
|
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
|
2019-05-31 01:43:17 -04:00
|
|
|
self.enable_defer = SiteSetting.default_other_enable_defer
|
2016-02-16 23:46:19 -05:00
|
|
|
self.external_links_in_new_tab = SiteSetting.default_other_external_links_in_new_tab
|
|
|
|
self.dynamic_favicon = SiteSetting.default_other_dynamic_favicon
|
2020-08-14 09:40:56 -04:00
|
|
|
self.skip_new_user_tips = SiteSetting.default_other_skip_new_user_tips
|
2016-02-16 23:46:19 -05:00
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2020-06-01 17:39:16 -04:00
|
|
|
self.digest_after_minutes ||= SiteSetting.default_email_digest_frequency.to_i
|
|
|
|
|
2016-03-17 17:35:23 -04:00
|
|
|
self.include_tl0_in_digests = SiteSetting.default_include_tl0_in_digests
|
|
|
|
|
2019-01-14 08:21:46 -05:00
|
|
|
self.text_size = SiteSetting.default_text_size
|
|
|
|
|
2019-04-11 19:02:18 -04:00
|
|
|
self.title_count_mode = SiteSetting.default_title_count_mode
|
|
|
|
|
2022-11-06 06:14:17 -05:00
|
|
|
self.hide_profile_and_presence = SiteSetting.default_hide_profile_and_presence
|
|
|
|
|
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
|
2019-12-03 04:05:53 -05:00
|
|
|
return unless Discourse.redis.setnx(key, "1")
|
|
|
|
Discourse.redis.expire(key, delay)
|
2016-02-18 00:57:22 -05:00
|
|
|
|
|
|
|
# delay the update
|
2022-02-04 18:14:35 -05:00
|
|
|
Jobs.enqueue_in(
|
|
|
|
delay / 2,
|
|
|
|
:update_top_redirection,
|
|
|
|
user_id: self.user_id,
|
|
|
|
redirected_at: Time.zone.now.to_s,
|
|
|
|
)
|
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
|
|
|
|
times = [
|
|
|
|
case duration
|
|
|
|
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
|
|
|
|
end,
|
2021-02-14 16:50:33 -05:00
|
|
|
user.created_at,
|
2016-02-18 00:57:22 -05:00
|
|
|
Time.at(SiteSetting.min_new_topics_time).to_datetime,
|
|
|
|
]
|
|
|
|
|
|
|
|
times.max
|
|
|
|
end
|
|
|
|
|
2017-11-09 14:45:19 -05:00
|
|
|
def homepage
|
|
|
|
case homepage_id
|
|
|
|
when 1
|
|
|
|
"latest"
|
|
|
|
when 2
|
|
|
|
"categories"
|
|
|
|
when 3
|
|
|
|
"unread"
|
|
|
|
when 4
|
|
|
|
"new"
|
|
|
|
when 5
|
|
|
|
"top"
|
2020-09-10 11:13:12 -04:00
|
|
|
when 6
|
|
|
|
"bookmarks"
|
2021-09-06 13:51:52 -04:00
|
|
|
when 7
|
|
|
|
"unseen"
|
2017-11-09 14:45:19 -05:00
|
|
|
else
|
|
|
|
SiteSetting.homepage
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-14 08:21:46 -05:00
|
|
|
def text_size
|
|
|
|
UserOption.text_sizes[text_size_key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def text_size=(value)
|
|
|
|
self.text_size_key = UserOption.text_sizes[value.to_sym]
|
|
|
|
end
|
|
|
|
|
2019-04-11 19:02:18 -04:00
|
|
|
def title_count_mode
|
|
|
|
UserOption.title_count_modes[title_count_mode_key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def title_count_mode=(value)
|
|
|
|
self.title_count_mode_key = UserOption.title_count_modes[value.to_sym]
|
|
|
|
end
|
|
|
|
|
2020-07-23 02:20:10 -04:00
|
|
|
def unsubscribed_from_all?
|
|
|
|
!mailing_list_mode && !email_digests && email_level == UserOption.email_level_types[:never] &&
|
|
|
|
email_messages_level == UserOption.email_level_types[:never]
|
|
|
|
end
|
|
|
|
|
2022-07-25 08:19:53 -04:00
|
|
|
def likes_notifications_disabled?
|
|
|
|
like_notification_frequency == UserOption.like_notification_frequency_type[:never]
|
|
|
|
end
|
|
|
|
|
2021-08-19 15:56:14 -04:00
|
|
|
def self.user_tzinfo(user_id)
|
|
|
|
timezone = UserOption.where(user_id: user_id).pluck(:timezone).first || "UTC"
|
|
|
|
|
|
|
|
tzinfo = nil
|
|
|
|
begin
|
|
|
|
tzinfo = ActiveSupport::TimeZone.find_tzinfo(timezone)
|
|
|
|
rescue TZInfo::InvalidTimezoneIdentifier
|
|
|
|
Rails.logger.warn(
|
|
|
|
"#{User.find_by(id: user_id)&.username} has the timezone #{timezone} set, we do not know how to parse it in Rails, fallback to UTC",
|
|
|
|
)
|
|
|
|
tzinfo = ActiveSupport::TimeZone.find_tzinfo("UTC")
|
|
|
|
end
|
|
|
|
|
|
|
|
tzinfo
|
|
|
|
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
|
|
|
|
#
|
2022-11-02 09:41:30 -04:00
|
|
|
# user_id :integer not null, primary key
|
|
|
|
# mailing_list_mode :boolean default(FALSE), not null
|
|
|
|
# email_digests :boolean
|
|
|
|
# external_links_in_new_tab :boolean default(FALSE), not null
|
|
|
|
# enable_quoting :boolean default(TRUE), not null
|
|
|
|
# dynamic_favicon :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
|
|
|
|
# mailing_list_mode_frequency :integer default(1), not null
|
|
|
|
# include_tl0_in_digests :boolean default(FALSE)
|
|
|
|
# notification_level_when_replying :integer
|
|
|
|
# theme_key_seq :integer default(0), not null
|
|
|
|
# allow_private_messages :boolean default(TRUE), not null
|
|
|
|
# homepage_id :integer
|
|
|
|
# theme_ids :integer default([]), not null, is an Array
|
|
|
|
# hide_profile_and_presence :boolean default(FALSE), not null
|
|
|
|
# text_size_key :integer default(0), not null
|
|
|
|
# text_size_seq :integer default(0), not null
|
|
|
|
# email_level :integer default(1), not null
|
|
|
|
# email_messages_level :integer default(0), not null
|
|
|
|
# title_count_mode_key :integer default(0), not null
|
|
|
|
# enable_defer :boolean default(FALSE), not null
|
|
|
|
# timezone :string
|
|
|
|
# enable_allowed_pm_users :boolean default(FALSE), not null
|
|
|
|
# dark_scheme_id :integer
|
|
|
|
# skip_new_user_tips :boolean default(FALSE), not null
|
|
|
|
# color_scheme_id :integer
|
|
|
|
# default_calendar :integer default("none_selected"), not null
|
|
|
|
# chat_enabled :boolean default(TRUE), not null
|
|
|
|
# only_chat_push_notifications :boolean
|
|
|
|
# oldest_search_log_date :datetime
|
|
|
|
# chat_sound :string
|
|
|
|
# dismissed_channel_retention_reminder :boolean
|
|
|
|
# dismissed_dm_retention_reminder :boolean
|
|
|
|
# bookmark_auto_delete_preference :integer default(3), not null
|
|
|
|
# ignore_channel_wide_mention :boolean
|
|
|
|
# chat_email_frequency :integer default(1), not null
|
|
|
|
# enable_experimental_sidebar :boolean default(FALSE)
|
|
|
|
# seen_popups :integer is an Array
|
|
|
|
# sidebar_list_destination :integer default("none_selected"), not null
|
2023-02-28 20:01:44 -05:00
|
|
|
# chat_header_indicator_preference :integer default(0), not null
|
2016-02-22 18:33:53 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2021-10-05 23:11:52 -04:00
|
|
|
# index_user_options_on_user_id (user_id) UNIQUE
|
|
|
|
# index_user_options_on_user_id_and_default_calendar (user_id,default_calendar)
|
2016-02-22 18:33:53 -05:00
|
|
|
#
|