DEV: Apply syntax_tree formatting to app/*

This commit is contained in:
David Taylor
2023-01-09 12:20:10 +00:00
parent a641ce4b62
commit 5a003715d3
696 changed files with 18447 additions and 15481 deletions

View File

@ -2,7 +2,7 @@
class TopicUser < ActiveRecord::Base
self.ignored_columns = [
:highest_seen_post_number # Remove after 01 Jan 2022
:highest_seen_post_number, # Remove after 01 Jan 2022
]
belongs_to :user
@ -11,20 +11,18 @@ class TopicUser < ActiveRecord::Base
# used for serialization
attr_accessor :post_action_data
scope :level, lambda { |topic_id, level|
where(topic_id: topic_id)
.where("COALESCE(topic_users.notification_level, :regular) >= :level",
regular: TopicUser.notification_levels[:regular],
level: TopicUser.notification_levels[level])
}
scope :level,
lambda { |topic_id, level|
where(topic_id: topic_id).where(
"COALESCE(topic_users.notification_level, :regular) >= :level",
regular: TopicUser.notification_levels[:regular],
level: TopicUser.notification_levels[level],
)
}
scope :tracking, lambda { |topic_id|
level(topic_id, :tracking)
}
scope :tracking, lambda { |topic_id| level(topic_id, :tracking) }
scope :watching, lambda { |topic_id|
level(topic_id, :watching)
}
scope :watching, lambda { |topic_id| level(topic_id, :watching) }
def topic_bookmarks
Bookmark.where(topic: topic, user: user)
@ -32,38 +30,62 @@ class TopicUser < ActiveRecord::Base
# Class methods
class << self
# Enums
def notification_levels
NotificationLevels.topic_levels
end
def notification_reasons
@notification_reasons ||= Enum.new(created_topic: 1,
user_changed: 2,
user_interacted: 3,
created_post: 4,
auto_watch: 5,
auto_watch_category: 6,
auto_mute_category: 7,
auto_track_category: 8,
plugin_changed: 9,
auto_watch_tag: 10,
auto_mute_tag: 11,
auto_track_tag: 12)
@notification_reasons ||=
Enum.new(
created_topic: 1,
user_changed: 2,
user_interacted: 3,
created_post: 4,
auto_watch: 5,
auto_watch_category: 6,
auto_mute_category: 7,
auto_track_category: 8,
plugin_changed: 9,
auto_watch_tag: 10,
auto_mute_tag: 11,
auto_track_tag: 12,
)
end
def auto_notification(user_id, topic_id, reason, notification_level)
should_change = TopicUser
.where(user_id: user_id, topic_id: topic_id)
.where("notifications_reason_id IS NULL OR (notification_level < :max AND notification_level > :min)", max: notification_level, min: notification_levels[:regular])
.exists?
should_change =
TopicUser
.where(user_id: user_id, topic_id: topic_id)
.where(
"notifications_reason_id IS NULL OR (notification_level < :max AND notification_level > :min)",
max: notification_level,
min: notification_levels[:regular],
)
.exists?
change(user_id, topic_id, notification_level: notification_level, notifications_reason_id: reason) if should_change
if should_change
change(
user_id,
topic_id,
notification_level: notification_level,
notifications_reason_id: reason,
)
end
end
def auto_notification_for_staging(user_id, topic_id, reason, notification_level = notification_levels[:watching])
change(user_id, topic_id, notification_level: notification_level, notifications_reason_id: reason)
def auto_notification_for_staging(
user_id,
topic_id,
reason,
notification_level = notification_levels[:watching]
)
change(
user_id,
topic_id,
notification_level: notification_level,
notifications_reason_id: reason,
)
end
def unwatch_categories!(user, category_ids)
@ -80,14 +102,15 @@ class TopicUser < ActiveRecord::Base
WHERE t.id = tu.topic_id AND tu.notification_level <> :muted AND category_id IN (:category_ids) AND tu.user_id = :user_id
SQL
DB.exec(sql,
DB.exec(
sql,
watching: notification_levels[:watching],
tracking: notification_levels[:tracking],
regular: notification_levels[:regular],
muted: notification_levels[:muted],
category_ids: category_ids,
user_id: user.id,
track_threshold: track_threshold
track_threshold: track_threshold,
)
end
@ -119,9 +142,7 @@ class TopicUser < ActiveRecord::Base
# it then creates the row instead.
def change(user_id, topic_id, attrs)
# For plugin compatibility, remove after 01 Jan 2022
if attrs[:highest_seen_post_number]
attrs.delete(:highest_seen_post_number)
end
attrs.delete(:highest_seen_post_number) if attrs[:highest_seen_post_number]
# Sometimes people pass objs instead of the ids. We can handle that.
topic_id = topic_id.id if topic_id.is_a?(::Topic)
@ -143,15 +164,17 @@ class TopicUser < ActiveRecord::Base
rows = TopicUser.where(topic_id: topic_id, user_id: user_id).update_all([attrs_sql, *vals])
if rows == 0
create_missing_record(user_id, topic_id, attrs)
end
create_missing_record(user_id, topic_id, attrs) if rows == 0
end
if attrs[:notification_level]
notification_level_change(user_id, topic_id, attrs[:notification_level], attrs[:notifications_reason_id])
notification_level_change(
user_id,
topic_id,
attrs[:notification_level],
attrs[:notifications_reason_id],
)
end
rescue ActiveRecord::RecordNotUnique
# In case of a race condition to insert, do nothing
end
@ -161,67 +184,90 @@ class TopicUser < ActiveRecord::Base
message[:notifications_reason_id] = reason_id if reason_id
MessageBus.publish("/topic/#{topic_id}", message, user_ids: [user_id])
DiscourseEvent.trigger(:topic_notification_level_changed,
DiscourseEvent.trigger(
:topic_notification_level_changed,
notification_level,
user_id,
topic_id
topic_id,
)
end
def create_missing_record(user_id, topic_id, attrs)
now = DateTime.now
unless attrs[:notification_level]
category_notification_level = CategoryUser.where(user_id: user_id)
.where("category_id IN (SELECT category_id FROM topics WHERE id = :id)", id: topic_id)
.where("notification_level IN (:levels)", levels: [CategoryUser.notification_levels[:watching],
CategoryUser.notification_levels[:tracking]])
.order("notification_level DESC")
.limit(1)
.pluck(:notification_level)
.first
category_notification_level =
CategoryUser
.where(user_id: user_id)
.where("category_id IN (SELECT category_id FROM topics WHERE id = :id)", id: topic_id)
.where(
"notification_level IN (:levels)",
levels: [
CategoryUser.notification_levels[:watching],
CategoryUser.notification_levels[:tracking],
],
)
.order("notification_level DESC")
.limit(1)
.pluck(:notification_level)
.first
tag_notification_level = TagUser.where(user_id: user_id)
.where("tag_id IN (SELECT tag_id FROM topic_tags WHERE topic_id = :id)", id: topic_id)
.where("notification_level IN (:levels)", levels: [CategoryUser.notification_levels[:watching],
CategoryUser.notification_levels[:tracking]])
.order("notification_level DESC")
.limit(1)
.pluck(:notification_level)
.first
tag_notification_level =
TagUser
.where(user_id: user_id)
.where("tag_id IN (SELECT tag_id FROM topic_tags WHERE topic_id = :id)", id: topic_id)
.where(
"notification_level IN (:levels)",
levels: [
CategoryUser.notification_levels[:watching],
CategoryUser.notification_levels[:tracking],
],
)
.order("notification_level DESC")
.limit(1)
.pluck(:notification_level)
.first
if category_notification_level && !(tag_notification_level && (tag_notification_level > category_notification_level))
if category_notification_level &&
!(tag_notification_level && (tag_notification_level > category_notification_level))
attrs[:notification_level] = category_notification_level
attrs[:notifications_changed_at] = DateTime.now
attrs[:notifications_reason_id] = category_notification_level == CategoryUser.notification_levels[:watching] ?
TopicUser.notification_reasons[:auto_watch_category] :
attrs[:notifications_reason_id] = (
if category_notification_level == CategoryUser.notification_levels[:watching]
TopicUser.notification_reasons[:auto_watch_category]
else
TopicUser.notification_reasons[:auto_track_category]
end
)
elsif tag_notification_level
attrs[:notification_level] = tag_notification_level
attrs[:notifications_changed_at] = DateTime.now
attrs[:notifications_reason_id] = tag_notification_level == TagUser.notification_levels[:watching] ?
TopicUser.notification_reasons[:auto_watch_tag] :
attrs[:notifications_reason_id] = (
if tag_notification_level == TagUser.notification_levels[:watching]
TopicUser.notification_reasons[:auto_watch_tag]
else
TopicUser.notification_reasons[:auto_track_tag]
end
)
end
end
unless attrs[:notification_level]
if Topic.private_messages.where(id: topic_id).exists? &&
Notification.where(
user_id: user_id,
topic_id: topic_id,
notification_type: Notification.types[:invited_to_private_message]
).exists?
group_notification_level = Group
.joins("LEFT OUTER JOIN group_users gu ON gu.group_id = groups.id AND gu.user_id = #{user_id}")
.joins("LEFT OUTER JOIN topic_allowed_groups tag ON tag.topic_id = #{topic_id}")
.where("gu.id IS NOT NULL AND tag.id IS NOT NULL")
.pluck(:default_notification_level)
.first
Notification.where(
user_id: user_id,
topic_id: topic_id,
notification_type: Notification.types[:invited_to_private_message],
).exists?
group_notification_level =
Group
.joins(
"LEFT OUTER JOIN group_users gu ON gu.group_id = groups.id AND gu.user_id = #{user_id}",
)
.joins("LEFT OUTER JOIN topic_allowed_groups tag ON tag.topic_id = #{topic_id}")
.where("gu.id IS NOT NULL AND tag.id IS NOT NULL")
.pluck(:default_notification_level)
.first
if group_notification_level.present?
attrs[:notification_level] = group_notification_level
@ -229,7 +275,8 @@ class TopicUser < ActiveRecord::Base
attrs[:notification_level] = notification_levels[:watching]
end
else
auto_track_after = UserOption.where(user_id: user_id).pluck_first(:auto_track_topics_after_msecs)
auto_track_after =
UserOption.where(user_id: user_id).pluck_first(:auto_track_topics_after_msecs)
auto_track_after ||= SiteSetting.default_other_auto_track_topics_after_msecs
if auto_track_after >= 0 && auto_track_after <= (attrs[:total_msecs_viewed].to_i || 0)
@ -238,12 +285,14 @@ class TopicUser < ActiveRecord::Base
end
end
TopicUser.create!(attrs.merge!(
user_id: user_id,
topic_id: topic_id,
first_visited_at: now ,
last_visited_at: now
))
TopicUser.create!(
attrs.merge!(
user_id: user_id,
topic_id: topic_id,
first_visited_at: now,
last_visited_at: now,
),
)
DiscourseEvent.trigger(:topic_first_visited_by_user, topic_id, user_id)
end
@ -252,9 +301,7 @@ class TopicUser < ActiveRecord::Base
now = DateTime.now
rows = TopicUser.where(topic_id: topic_id, user_id: user_id).update_all(last_visited_at: now)
if rows == 0
change(user_id, topic_id, last_visited_at: now, first_visited_at: now)
end
change(user_id, topic_id, last_visited_at: now, first_visited_at: now) if rows == 0
end
# Update the last read and the last seen post count, but only if it doesn't exist.
@ -289,7 +336,8 @@ class TopicUser < ActiveRecord::Base
t.archetype
SQL
INSERT_TOPIC_USER_SQL = "INSERT INTO topic_users (user_id, topic_id, last_read_post_number, last_visited_at, first_visited_at, notification_level)
INSERT_TOPIC_USER_SQL =
"INSERT INTO topic_users (user_id, topic_id, last_read_post_number, last_visited_at, first_visited_at, notification_level)
SELECT :user_id, :topic_id, :post_number, :now, :now, :new_status
FROM topics AS ft
JOIN users u on u.id = :user_id
@ -309,7 +357,7 @@ class TopicUser < ActiveRecord::Base
now: DateTime.now,
msecs: msecs,
tracking: notification_levels[:tracking],
threshold: SiteSetting.default_other_auto_track_topics_after_msecs
threshold: SiteSetting.default_other_auto_track_topics_after_msecs,
}
rows = DB.query(UPDATE_TOPIC_USER_SQL, args)
@ -327,23 +375,22 @@ class TopicUser < ActiveRecord::Base
post_number: post_number,
user: user,
notification_level: after,
private_message: archetype == Archetype.private_message
private_message: archetype == Archetype.private_message,
)
end
if new_posts_read > 0
user.update_posts_read!(new_posts_read, mobile: opts[:mobile])
end
user.update_posts_read!(new_posts_read, mobile: opts[:mobile]) if new_posts_read > 0
if before != after
notification_level_change(user.id, topic_id, after, nil)
end
notification_level_change(user.id, topic_id, after, nil) if before != after
end
if rows.length == 0
# The user read at least one post in a topic that they haven't viewed before.
args[:new_status] = notification_levels[:regular]
if (user.user_option.auto_track_topics_after_msecs || SiteSetting.default_other_auto_track_topics_after_msecs) == 0
if (
user.user_option.auto_track_topics_after_msecs ||
SiteSetting.default_other_auto_track_topics_after_msecs
) == 0
args[:new_status] = notification_levels[:tracking]
end
@ -352,10 +399,7 @@ class TopicUser < ActiveRecord::Base
post_number: post_number,
user: user,
notification_level: args[:new_status],
private_message: Topic.exists?(
archetype: Archetype.private_message,
id: topic_id
)
private_message: Topic.exists?(archetype: Archetype.private_message, id: topic_id),
)
user.update_posts_read!(new_posts_read, mobile: opts[:mobile])
@ -387,14 +431,8 @@ class TopicUser < ActiveRecord::Base
TopicTrackingState
end
klass.publish_read(
topic_id,
post_number,
user,
notification_level
)
klass.publish_read(topic_id, post_number, user, notification_level)
end
end
# Update the cached topic_user.liked column based on data
@ -441,16 +479,17 @@ class TopicUser < ActiveRecord::Base
WHERE x.topic_id = tu.topic_id AND x.user_id = tu.user_id AND x.state != tu.#{action_type_name}
SQL
if user_id
builder.where("tu2.user_id IN (:user_id)", user_id: user_id)
end
builder.where("tu2.user_id IN (:user_id)", user_id: user_id) if user_id
if topic_id
builder.where("tu2.topic_id IN (:topic_id)", topic_id: topic_id)
end
builder.where("tu2.topic_id IN (:topic_id)", topic_id: topic_id) if topic_id
if post_id
builder.where("tu2.topic_id IN (SELECT topic_id FROM posts WHERE id IN (:post_id))", post_id: post_id) if !topic_id
if !topic_id
builder.where(
"tu2.topic_id IN (SELECT topic_id FROM posts WHERE id IN (:post_id))",
post_id: post_id,
)
end
builder.where(<<~SQL, post_id: post_id)
tu2.user_id IN (
SELECT user_id FROM post_actions
@ -515,13 +554,10 @@ SQL
)
SQL
if topic_id
builder.where("t.topic_id = :topic_id", topic_id: topic_id)
end
builder.where("t.topic_id = :topic_id", topic_id: topic_id) if topic_id
builder.exec
end
end
# == Schema Information