2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
class UserOption < ActiveRecord::Base
|
2024-02-15 23:31:42 -05:00
|
|
|
HOMEPAGES = {
|
2024-04-02 11:05:08 -04:00
|
|
|
# -1 => reserved for "custom homepage"
|
2024-02-15 23:31:42 -05:00
|
|
|
1 => "latest",
|
|
|
|
2 => "categories",
|
|
|
|
3 => "unread",
|
|
|
|
4 => "new",
|
|
|
|
5 => "top",
|
|
|
|
6 => "bookmarks",
|
|
|
|
7 => "unseen",
|
2024-08-08 13:57:42 -04:00
|
|
|
8 => "hot",
|
2024-02-15 23:31:42 -05:00
|
|
|
}
|
|
|
|
|
2024-05-06 14:18:53 -04:00
|
|
|
self.ignored_columns = [
|
2024-05-06 23:06:31 -04:00
|
|
|
"sidebar_list_destination", # TODO: Remove when 20240212034010_drop_deprecated_columns has been promoted to pre-deploy
|
2024-05-06 14:18:53 -04:00
|
|
|
]
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
self.primary_key = :user_id
|
|
|
|
belongs_to :user
|
|
|
|
before_create :set_defaults
|
|
|
|
|
2024-11-12 20:22:58 -05:00
|
|
|
before_save :update_hide_profile_and_presence
|
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
|
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
|
2024-10-28 00:18:15 -04:00
|
|
|
self.enable_smart_lists = SiteSetting.default_other_enable_smart_lists
|
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
|
|
|
|
|
2024-05-22 04:23:03 -04:00
|
|
|
self.email_digests = SiteSetting.default_email_digest_frequency.to_i > 0
|
|
|
|
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
|
|
|
|
|
2024-11-12 20:22:58 -05:00
|
|
|
self.hide_profile = SiteSetting.default_hide_profile
|
|
|
|
self.hide_presence = SiteSetting.default_hide_presence
|
2023-07-06 21:52:10 -04:00
|
|
|
self.sidebar_link_to_filtered_list = SiteSetting.default_sidebar_link_to_filtered_list
|
|
|
|
self.sidebar_show_count_of_new_items = SiteSetting.default_sidebar_show_count_of_new_items
|
2022-11-06 06:14:17 -05:00
|
|
|
|
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
|
2024-05-22 04:23:03 -04:00
|
|
|
SiteSetting.disable_mailing_list_mode ? false : super
|
2016-03-04 16:53:28 -05:00
|
|
|
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
|
2024-02-15 23:31:42 -05:00
|
|
|
return HOMEPAGES[homepage_id] if HOMEPAGES.keys.include?(homepage_id)
|
|
|
|
|
2024-04-02 11:05:08 -04:00
|
|
|
"hot" if homepage_id == 8 && SiteSetting.top_menu_map.include?("hot")
|
2017-11-09 14:45:19 -05:00
|
|
|
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
|
|
|
|
|
2024-11-12 20:22:58 -05:00
|
|
|
def update_hide_profile_and_presence
|
|
|
|
if hide_profile_changed? || hide_presence_changed?
|
|
|
|
self.hide_profile_and_presence = hide_profile || hide_presence
|
|
|
|
elsif hide_profile_and_presence_changed?
|
|
|
|
self.hide_profile = hide_profile_and_presence
|
|
|
|
self.hide_presence = hide_profile_and_presence
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-06 03:56:58 -04:00
|
|
|
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
|
2024-11-12 20:22:58 -05:00
|
|
|
# hide_profile :boolean default(FALSE), not null
|
|
|
|
# hide_presence :boolean default(FALSE), not null
|
2022-11-02 09:41:30 -04:00
|
|
|
# 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
|
2023-02-28 20:01:44 -05:00
|
|
|
# chat_header_indicator_preference :integer default(0), not null
|
2023-06-22 12:04:13 -04:00
|
|
|
# sidebar_link_to_filtered_list :boolean default(FALSE), not null
|
|
|
|
# sidebar_show_count_of_new_items :boolean default(FALSE), not null
|
2023-07-04 01:08:29 -04:00
|
|
|
# watched_precedence_over_muted :boolean
|
2023-08-18 14:33:07 -04:00
|
|
|
# chat_separate_sidebar_mode :integer default(0), not null
|
2024-03-08 17:14:46 -05:00
|
|
|
# topics_unread_when_closed :boolean default(TRUE), not null
|
2024-04-29 05:20:01 -04:00
|
|
|
# show_thread_title_prompts :boolean default(TRUE), not null
|
2024-10-28 00:18:15 -04:00
|
|
|
# enable_smart_lists :boolean default(TRUE), not null
|
2016-02-22 18:33:53 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2023-07-12 19:02:23 -04:00
|
|
|
# index_user_options_on_user_id (user_id) UNIQUE
|
|
|
|
# index_user_options_on_user_id_and_default_calendar (user_id,default_calendar)
|
|
|
|
# index_user_options_on_watched_precedence_over_muted (watched_precedence_over_muted)
|
2016-02-22 18:33:53 -05:00
|
|
|
#
|