2018-01-16 22:49:35 -06:00
|
|
|
# frozen_string_literal: true
|
2017-11-17 15:08:31 -06:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
class PostTiming < ActiveRecord::Base
|
|
|
|
belongs_to :topic
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
validates_presence_of :post_number
|
|
|
|
validates_presence_of :msecs
|
|
|
|
|
2024-03-08 16:14:46 -06:00
|
|
|
def self.pretend_read(topic_id, actual_read_post_number, pretend_read_post_number, user_ids = nil)
|
2013-07-03 20:47:12 -05:00
|
|
|
# This is done in SQL cause the logic is quite tricky and we want to do this in one db hit
|
|
|
|
#
|
2024-03-08 16:14:46 -06:00
|
|
|
user_ids_condition = user_ids.present? ? "AND user_id = ANY(ARRAY[:user_ids]::int[])" : ""
|
|
|
|
sql_query = <<-SQL
|
|
|
|
INSERT INTO post_timings(topic_id, user_id, post_number, msecs)
|
2013-07-03 20:47:12 -05:00
|
|
|
SELECT :topic_id, user_id, :pretend_read_post_number, 1
|
|
|
|
FROM post_timings pt
|
|
|
|
WHERE topic_id = :topic_id AND
|
2024-03-08 16:14:46 -06:00
|
|
|
post_number = :actual_read_post_number
|
|
|
|
#{user_ids_condition}
|
|
|
|
AND NOT EXISTS (
|
2013-07-03 20:47:12 -05:00
|
|
|
SELECT 1 FROM post_timings pt1
|
|
|
|
WHERE pt1.topic_id = pt.topic_id AND
|
|
|
|
pt1.post_number = :pretend_read_post_number AND
|
|
|
|
pt1.user_id = pt.user_id
|
|
|
|
)
|
2024-03-08 16:14:46 -06:00
|
|
|
SQL
|
|
|
|
|
|
|
|
params = {
|
2023-01-09 06:20:10 -06:00
|
|
|
pretend_read_post_number: pretend_read_post_number,
|
|
|
|
topic_id: topic_id,
|
|
|
|
actual_read_post_number: actual_read_post_number,
|
2024-03-08 16:14:46 -06:00
|
|
|
}
|
|
|
|
params[:user_ids] = user_ids if user_ids.present?
|
|
|
|
|
|
|
|
DB.exec(sql_query, params)
|
2013-07-03 20:47:12 -05:00
|
|
|
|
2024-05-27 06:56:33 -05:00
|
|
|
TopicUser.update_last_read_post_number(topic_id:)
|
2013-07-03 20:47:12 -05:00
|
|
|
end
|
|
|
|
|
2015-06-18 02:02:10 -05:00
|
|
|
def self.record_new_timing(args)
|
2023-01-09 06:20:10 -06:00
|
|
|
row_count =
|
|
|
|
DB.exec(
|
|
|
|
"INSERT INTO post_timings (topic_id, user_id, post_number, msecs)
|
2019-04-29 14:34:42 -05:00
|
|
|
SELECT :topic_id, :user_id, :post_number, :msecs
|
|
|
|
ON CONFLICT DO NOTHING",
|
2023-01-09 06:20:10 -06:00
|
|
|
args,
|
|
|
|
)
|
2020-01-31 07:23:24 -06:00
|
|
|
|
2019-04-29 14:34:42 -05:00
|
|
|
# concurrency is hard, we are not running serialized so this can possibly
|
|
|
|
# still happen, if it happens we just don't care, its an invalid record anyway
|
2020-01-31 07:23:24 -06:00
|
|
|
return if row_count == 0
|
2023-01-09 06:20:10 -06:00
|
|
|
Post.where(
|
|
|
|
["topic_id = :topic_id and post_number = :post_number", args],
|
|
|
|
).update_all "reads = reads + 1"
|
2021-02-23 08:36:00 -06:00
|
|
|
|
|
|
|
return if Topic.exists?(id: args[:topic_id], archetype: Archetype.private_message)
|
2023-01-09 06:20:10 -06:00
|
|
|
UserStat.where(user_id: args[:user_id]).update_all "posts_read_count = posts_read_count + 1"
|
2015-06-18 02:02:10 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
# Increases a timer if a row exists, otherwise create it
|
|
|
|
def self.record_timing(args)
|
2018-06-19 01:13:14 -05:00
|
|
|
rows = DB.exec(<<~SQL, args)
|
|
|
|
UPDATE post_timings
|
|
|
|
SET msecs = msecs + :msecs
|
|
|
|
WHERE topic_id = :topic_id
|
|
|
|
AND user_id = :user_id
|
|
|
|
AND post_number = :post_number
|
|
|
|
SQL
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2015-06-18 02:02:10 -05:00
|
|
|
record_new_timing(args) if rows == 0
|
2013-02-07 09:45:24 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2022-02-21 14:45:01 -06:00
|
|
|
def self.destroy_last_for(user, topic_id: nil, topic: nil)
|
|
|
|
topic ||= Topic.find(topic_id)
|
2022-06-29 19:18:12 -05:00
|
|
|
post_number = user.whisperer? ? topic.highest_staff_post_number : topic.highest_post_number
|
2018-11-12 23:07:48 -06:00
|
|
|
|
|
|
|
last_read = post_number - 1
|
|
|
|
|
|
|
|
PostTiming.transaction do
|
2023-01-09 06:20:10 -06:00
|
|
|
PostTiming.where(
|
|
|
|
"topic_id = ? AND user_id = ? AND post_number > ?",
|
|
|
|
topic.id,
|
|
|
|
user.id,
|
|
|
|
last_read,
|
|
|
|
).delete_all
|
|
|
|
last_read = nil if last_read < 1
|
2018-11-12 23:07:48 -06:00
|
|
|
|
|
|
|
TopicUser.where(user_id: user.id, topic_id: topic.id).update_all(
|
2023-01-09 06:20:10 -06:00
|
|
|
last_read_post_number: last_read,
|
2018-11-12 23:07:48 -06:00
|
|
|
)
|
2019-04-04 20:44:36 -05:00
|
|
|
|
2019-10-08 13:39:23 -05:00
|
|
|
topic.posts.find_by(post_number: post_number).decrement!(:reads)
|
|
|
|
|
2020-09-03 01:02:15 -05:00
|
|
|
if topic.private_message?
|
|
|
|
set_minimum_first_unread_pm!(topic: topic, user_id: user.id, date: topic.updated_at)
|
|
|
|
else
|
2019-04-04 20:44:36 -05:00
|
|
|
set_minimum_first_unread!(user_id: user.id, date: topic.updated_at)
|
|
|
|
end
|
2018-11-12 23:07:48 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-21 12:03:50 -06:00
|
|
|
def self.destroy_for(user_id, topic_ids)
|
2013-02-05 13:16:51 -06:00
|
|
|
PostTiming.transaction do
|
2023-01-09 06:20:10 -06:00
|
|
|
PostTiming.where("user_id = ? and topic_id in (?)", user_id, topic_ids).delete_all
|
2017-08-30 23:06:56 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
TopicUser.where("user_id = ? and topic_id in (?)", user_id, topic_ids).delete_all
|
2019-04-04 20:44:36 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
Post.where(topic_id: topic_ids).update_all("reads = reads - 1")
|
2019-10-08 13:39:23 -05:00
|
|
|
|
2019-04-04 20:44:36 -05:00
|
|
|
date = Topic.listable_topics.where(id: topic_ids).minimum(:updated_at)
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
set_minimum_first_unread!(user_id: user_id, date: date) if date
|
2013-02-07 09:45:24 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2020-09-03 01:02:15 -05:00
|
|
|
def self.set_minimum_first_unread_pm!(topic:, user_id:, date:)
|
|
|
|
if topic.topic_allowed_users.exists?(user_id: user_id)
|
2023-01-09 06:20:10 -06:00
|
|
|
UserStat.where("first_unread_pm_at > ? AND user_id = ?", date, user_id).update_all(
|
|
|
|
first_unread_pm_at: date,
|
|
|
|
)
|
2020-09-03 01:02:15 -05:00
|
|
|
else
|
|
|
|
DB.exec(<<~SQL, date: date, user_id: user_id, topic_id: topic.id)
|
|
|
|
UPDATE group_users gu
|
|
|
|
SET first_unread_pm_at = :date
|
|
|
|
FROM (
|
|
|
|
SELECT
|
|
|
|
gu2.user_id,
|
|
|
|
gu2.group_id
|
|
|
|
FROM group_users gu2
|
|
|
|
INNER JOIN topic_allowed_groups tag ON tag.group_id = gu2.group_id AND tag.topic_id = :topic_id
|
|
|
|
WHERE gu2.user_id = :user_id
|
|
|
|
) Y
|
|
|
|
WHERE gu.user_id = Y.user_id AND gu.group_id = Y.group_id
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-04 20:44:36 -05:00
|
|
|
def self.set_minimum_first_unread!(user_id:, date:)
|
|
|
|
DB.exec(<<~SQL, date: date, user_id: user_id)
|
|
|
|
UPDATE user_stats
|
|
|
|
SET first_unread_at = :date
|
|
|
|
WHERE first_unread_at > :date AND
|
|
|
|
user_id = :user_id
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
MAX_READ_TIME_PER_BATCH = 60 * 1000.0
|
2013-02-25 01:42:42 -06:00
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.process_timings(current_user, topic_id, topic_time, timings, opts = {})
|
2024-04-19 03:10:50 -05:00
|
|
|
lookup_column = current_user.whisperer? ? "highest_staff_post_number" : "highest_post_number"
|
|
|
|
highest_post_number = DB.query_single(<<~SQL, topic_id: topic_id).first
|
|
|
|
SELECT #{lookup_column}
|
|
|
|
FROM topics
|
|
|
|
WHERE id = :topic_id
|
|
|
|
SQL
|
|
|
|
|
|
|
|
# does not exist log nothing
|
|
|
|
return if highest_post_number.nil?
|
|
|
|
|
2018-01-16 22:49:35 -06:00
|
|
|
UserStat.update_time_read!(current_user.id)
|
2013-02-25 10:42:20 -06:00
|
|
|
|
2015-06-18 19:35:55 -05:00
|
|
|
max_time_per_post = ((Time.now - current_user.created_at) * 1000.0)
|
|
|
|
max_time_per_post = MAX_READ_TIME_PER_BATCH if max_time_per_post > MAX_READ_TIME_PER_BATCH
|
2015-04-14 10:49:44 -05:00
|
|
|
|
2013-04-07 20:12:52 -05:00
|
|
|
highest_seen = 1
|
2017-11-17 15:08:31 -06:00
|
|
|
new_posts_read = 0
|
2015-06-18 02:02:10 -05:00
|
|
|
|
|
|
|
join_table = []
|
|
|
|
|
2015-06-18 19:35:55 -05:00
|
|
|
i = timings.length
|
|
|
|
while i > 0
|
|
|
|
i -= 1
|
|
|
|
timings[i][1] = max_time_per_post if timings[i][1] > max_time_per_post
|
|
|
|
timings.delete_at(i) if timings[i][0] < 1
|
2024-04-19 03:10:50 -05:00
|
|
|
timings.delete_at(i) if timings[i][0] > highest_post_number
|
2015-06-18 02:02:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
timings.each_with_index do |(post_number, time), index|
|
2017-07-27 20:20:09 -05:00
|
|
|
join_table << "SELECT #{topic_id.to_i} topic_id, #{post_number.to_i} post_number,
|
|
|
|
#{current_user.id.to_i} user_id, #{time.to_i} msecs, #{index} idx"
|
2013-04-07 20:12:52 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
highest_seen = post_number.to_i > highest_seen ? post_number.to_i : highest_seen
|
2015-06-18 02:02:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if join_table.length > 0
|
2018-01-16 22:49:35 -06:00
|
|
|
sql = <<~SQL
|
2015-06-18 02:02:10 -05:00
|
|
|
UPDATE post_timings t
|
2021-02-18 08:48:15 -06:00
|
|
|
SET msecs = LEAST(t.msecs::bigint + x.msecs, 2^31 - 1)
|
2015-06-18 02:02:10 -05:00
|
|
|
FROM (#{join_table.join(" UNION ALL ")}) x
|
|
|
|
WHERE x.topic_id = t.topic_id AND
|
|
|
|
x.post_number = t.post_number AND
|
|
|
|
x.user_id = t.user_id
|
|
|
|
RETURNING x.idx
|
|
|
|
SQL
|
|
|
|
|
2018-06-19 01:13:14 -05:00
|
|
|
existing = Set.new(DB.query_single(sql))
|
2018-01-16 22:49:35 -06:00
|
|
|
|
|
|
|
sql = <<~SQL
|
|
|
|
SELECT 1 FROM topics
|
|
|
|
WHERE deleted_at IS NULL AND
|
|
|
|
archetype = 'regular' AND
|
|
|
|
id = :topic_id
|
|
|
|
SQL
|
|
|
|
|
2018-06-19 01:13:14 -05:00
|
|
|
is_regular = DB.exec(sql, topic_id: topic_id) == 1
|
2018-01-16 22:49:35 -06:00
|
|
|
new_posts_read = timings.size - existing.size if is_regular
|
2015-06-18 02:02:10 -05:00
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
timings.each_with_index do |(post_number, time), index|
|
2024-05-27 05:27:13 -05:00
|
|
|
if existing.exclude?(index)
|
2023-01-09 06:20:10 -06:00
|
|
|
PostTiming.record_new_timing(
|
|
|
|
topic_id: topic_id,
|
|
|
|
post_number: post_number,
|
|
|
|
user_id: current_user.id,
|
|
|
|
msecs: time,
|
|
|
|
)
|
2015-06-18 02:02:10 -05:00
|
|
|
end
|
2013-02-25 01:42:42 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
total_changed = 0
|
|
|
|
if timings.length > 0
|
2017-07-27 20:20:09 -05:00
|
|
|
total_changed = Notification.mark_posts_read(current_user, topic_id, timings.map { |t| t[0] })
|
2013-02-25 01:42:42 -06:00
|
|
|
end
|
2013-02-25 10:42:20 -06:00
|
|
|
|
2015-06-18 19:35:55 -05:00
|
|
|
topic_time = max_time_per_post if topic_time > max_time_per_post
|
2015-07-07 11:31:07 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
TopicUser.update_last_read(
|
|
|
|
current_user,
|
|
|
|
topic_id,
|
|
|
|
highest_seen,
|
|
|
|
new_posts_read,
|
|
|
|
topic_time,
|
|
|
|
opts,
|
|
|
|
)
|
2019-08-27 07:09:00 -05:00
|
|
|
TopicGroup.update_last_read(current_user, topic_id, highest_seen)
|
2013-02-25 01:42:42 -06:00
|
|
|
|
|
|
|
if total_changed > 0
|
|
|
|
current_user.reload
|
|
|
|
current_user.publish_notifications_state
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2013-05-23 21:48:32 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: post_timings
|
|
|
|
#
|
|
|
|
# topic_id :integer not null
|
|
|
|
# post_number :integer not null
|
|
|
|
# user_id :integer not null
|
|
|
|
# msecs :integer not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2014-08-03 21:35:55 -05:00
|
|
|
# index_post_timings_on_user_id (user_id)
|
|
|
|
# post_timings_unique (topic_id,post_number,user_id) UNIQUE
|
2013-05-23 21:48:32 -05:00
|
|
|
#
|