FIX: TopicQuery.list_private_messages_unread ignore notification level

This commit is contained in:
Guo Xiang Tan
2020-09-15 12:55:50 +08:00
parent d83e3f9ce8
commit 2ff16b3650
2 changed files with 28 additions and 11 deletions

View File

@ -328,7 +328,15 @@ class TopicQuery
def list_private_messages_unread(user) def list_private_messages_unread(user)
list = private_messages_for(user, :user) list = private_messages_for(user, :user)
list = list.where("tu.last_read_post_number IS NULL OR tu.last_read_post_number < topics.highest_post_number")
list = TopicQuery.unread_filter(
list,
user.id,
staff: user.staff?
)
first_unread_pm_at = UserStat.where(user_id: user.id).pluck(:first_unread_pm_at).first
list = list.where("topics.updated_at >= ?", first_unread_pm_at) if first_unread_pm_at
create_list(:private_messages, {}, list) create_list(:private_messages, {}, list)
end end

View File

@ -638,26 +638,35 @@ RSpec.describe ListController do
end end
end end
describe "private_messages_unread" do describe "#private_messages_unread" do
before do fab!(:pm_user) { Fabricate(:user) }
u = Fabricate(:user)
pm = Fabricate(:private_message_topic, user: u) fab!(:pm) do
Fabricate(:post, user: u, topic: pm, post_number: 1) Fabricate(:private_message_topic).tap do |t|
pm.topic_allowed_users.create!(user: user) t.allowed_users << pm_user
create_post(user: pm_user, topic_id: t.id)
end
end end
it "returns 403 error when the user can't see private message" do it "returns 403 error when the user can't see private message" do
sign_in(Fabricate(:user)) sign_in(Fabricate(:user))
get "/topics/private-messages-unread/#{user.username}.json" get "/topics/private-messages-unread/#{pm_user.username}.json"
expect(response).to be_forbidden expect(response.status).to eq(403)
end end
it "succeeds when the user can see private messages" do it "succeeds when the user can see private messages" do
sign_in(user) TopicUser.find_by(topic: pm, user: pm_user).update!(
get "/topics/private-messages-unread/#{user.username}.json" notification_level: TopicUser.notification_levels[:tracking],
last_read_post_number: 0,
)
sign_in(pm_user)
get "/topics/private-messages-unread/#{pm_user.username}.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
json = response.parsed_body json = response.parsed_body
expect(json["topic_list"]["topics"].size).to eq(1) expect(json["topic_list"]["topics"].size).to eq(1)
expect(json["topic_list"]["topics"][0]["id"]).to eq(pm.id)
end end
end end