FEATURE: mark last notification unread when removing timings

This calls a bit more attention to the deferred topic by amending the "read"
state of the last notification on the topic.
This commit is contained in:
Sam Saffron 2019-04-09 12:42:21 +10:00
parent f0bb492d24
commit f6db30a5da
2 changed files with 46 additions and 6 deletions

View File

@ -254,10 +254,25 @@ class TopicsController < ApplicationController
end
def destroy_timings
topic_id = params[:topic_id].to_i
if params[:last].to_s == "1"
PostTiming.destroy_last_for(current_user, params[:topic_id])
PostTiming.destroy_last_for(current_user, topic_id)
else
PostTiming.destroy_for(current_user.id, [params[:topic_id].to_i])
PostTiming.destroy_for(current_user.id, [topic_id])
end
last_notification = Notification
.where(
user_id: current_user.id,
topic_id: topic_id
)
.order(created_at: :desc)
.limit(1)
.first
if last_notification
last_notification.update!(read: false)
end
render body: nil

View File

@ -726,22 +726,42 @@ RSpec.describe TopicsController do
freeze_time
post1 = create_post
user = post1.user
topic = post1.topic
post2 = create_post(topic_id: topic.id)
PostTiming.create!(topic: topic, user: user, post_number: 1, msecs: 100)
PostTiming.create!(topic: topic, user: user, post_number: 2, msecs: 100)
user.user_stat.update!(first_unread_at: Time.now + 1.week)
TopicUser.create!(
topic: topic,
user: user,
topic_user = TopicUser.find_by(
topic_id: topic.id,
user_id: user.id,
)
topic_user.update!(
last_read_post_number: 2,
highest_seen_post_number: 2
)
# ensure we have 2 notifications
# fake notification on topic but it is read
first_notification = Notification.create!(
user_id: user.id,
topic_id: topic.id,
data: "{}",
read: true,
notification_type: 1
)
freeze_time 1.minute.from_now
PostAlerter.post_created(post2)
second_notification = user.notifications.where(topic_id: topic.id).order(created_at: :desc).first
second_notification.update!(read: true)
sign_in(user)
delete "/t/#{topic.id}/timings.json?last=1"
@ -754,6 +774,11 @@ RSpec.describe TopicsController do
user.user_stat.reload
expect(user.user_stat.first_unread_at).to eq_time(topic.updated_at)
first_notification.reload
second_notification.reload
expect(first_notification.read).to eq(true)
expect(second_notification.read).to eq(false)
PostDestroyer.new(Fabricate(:admin), post2).destroy
delete "/t/#{topic.id}/timings.json?last=1"