FEATURE: Add last visit indication to topic view page. (#13471)

This PR also removes grey old unread bubble from the topic badges by
dropping `TopicUser#highest_seen_post_number`.
This commit is contained in:
Alan Guo Xiang Tan
2021-07-05 14:17:31 +08:00
committed by GitHub
parent 0f688f45bd
commit 37b8ce79c9
64 changed files with 306 additions and 312 deletions

View File

@@ -646,7 +646,6 @@ class PostCreator
@topic.id,
posted: true,
last_read_post_number: @post.post_number,
highest_seen_post_number: @post.post_number,
last_posted_at: Time.zone.now)
# assume it took us 5 seconds of reading time to make a post

View File

@@ -57,8 +57,8 @@ def insert_topic_users
log "Inserting topic users..."
DB.exec <<-SQL
INSERT INTO topic_users (user_id, topic_id, posted, last_read_post_number, highest_seen_post_number, first_visited_at, last_visited_at, total_msecs_viewed)
SELECT user_id, topic_id, 't' , MAX(post_number), MAX(post_number), MIN(created_at), MAX(created_at), COUNT(id) * #{MS_SPEND_CREATING_POST}
INSERT INTO topic_users (user_id, topic_id, posted, last_read_post_number, first_visited_at, last_visited_at, total_msecs_viewed)
SELECT user_id, topic_id, 't' , MAX(post_number), MIN(created_at), MAX(created_at), COUNT(id) * #{MS_SPEND_CREATING_POST}
FROM posts
WHERE user_id > 0
GROUP BY user_id, topic_id

View File

@@ -346,7 +346,6 @@ task 'posts:reorder_posts', [:topic_id] => [:environment] do |_, args|
["post_timings", "post_number"],
["posts", "reply_to_post_number"],
["topic_users", "last_read_post_number"],
["topic_users", "highest_seen_post_number"],
["topic_users", "last_emailed_post_number"],
].each do |table, column|
builder = DB.build <<~SQL

View File

@@ -72,7 +72,7 @@ class TopicsBulkAction
highest_number_source_column = @user.staff? ? 'highest_staff_post_number' : 'highest_post_number'
sql = <<~SQL
UPDATE topic_users tu
SET highest_seen_post_number = t.#{highest_number_source_column} , last_read_post_number = t.#{highest_number_source_column}
SET last_read_post_number = t.#{highest_number_source_column}
FROM topics t
WHERE t.id = tu.topic_id AND tu.user_id = :user_id AND t.id IN (:topic_ids)
SQL

View File

@@ -2,7 +2,7 @@
class Unread
# This module helps us calculate unread and new post counts
# This module helps us calculate unread post counts
def initialize(topic, topic_user, guardian)
@guardian = guardian
@@ -11,29 +11,27 @@ class Unread
end
def unread_posts
return 0 if do_not_notify?(@topic_user.notification_level)
result = ((@topic_user.highest_seen_post_number || 0) - (@topic_user.last_read_post_number || 0))
result = 0 if result < 0
result
end
def new_posts
return 0 if @topic_user.highest_seen_post_number.blank?
return 0 if @topic_user.last_read_post_number.blank?
return 0 if do_not_notify?(@topic_user.notification_level)
highest_post_number = @guardian.is_staff? ? @topic.highest_staff_post_number : @topic.highest_post_number
return 0 if (@topic_user.last_read_post_number || 0) > highest_post_number
return 0 if @topic_user.last_read_post_number > highest_post_number
new_posts = (highest_post_number - @topic_user.highest_seen_post_number)
new_posts = 0 if new_posts < 0
new_posts
unread = (highest_post_number - @topic_user.last_read_post_number)
unread = 0 if unread < 0
unread
end
protected
DO_NOT_NOTIFY_LEVELS = [
TopicUser.notification_levels[:muted],
TopicUser.notification_levels[:regular]
]
def do_not_notify?(notification_level)
[TopicUser.notification_levels[:muted], TopicUser.notification_levels[:regular]].include?(notification_level)
DO_NOT_NOTIFY_LEVELS.include?(notification_level)
end
end