mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 08:53:41 +08:00
1630dae2db
* Enable or disable read state based on group attribute * When read state needs to be published, the minimum unread count is calculated in the topic query. This way, we can know if someone reads the last post * The option can be enabled/disabled from the UI * The read indicator will live-updated using message bus * Show read indicator on every post * The read indicator now shows read count and can be expanded to see user avatars * Read count gets updated everytime someone reads a message * Simplify topic-list read indicator logic * Unsubscribe from message bus on willDestroyElement, removed unnecesarry values from post-menu, and added a comment to explain where does minimum_unread_count comes from
91 lines
2.9 KiB
Ruby
91 lines
2.9 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'rails_helper'
|
|
|
|
describe PostReadersController do
|
|
describe '#index' do
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
fab!(:reader) { Fabricate(:user) }
|
|
|
|
before { sign_in(admin) }
|
|
|
|
before do
|
|
@group = Fabricate(:group)
|
|
@group_message = Fabricate(:private_message_topic, allowed_groups: [@group])
|
|
@post = Fabricate(:post, topic: @group_message, post_number: 3)
|
|
end
|
|
|
|
context 'When the user has access to readers data' do
|
|
before do
|
|
@group.update!(publish_read_state: true)
|
|
@group.add(admin)
|
|
@group.add(reader)
|
|
end
|
|
|
|
it 'returns an empty list when nobody has read the topic' do
|
|
get '/post_readers.json', params: { id: @post.id }
|
|
|
|
readers = JSON.parse(response.body)['post_readers']
|
|
|
|
expect(readers).to be_empty
|
|
end
|
|
|
|
it 'returns an user who read until that post' do
|
|
TopicUser.create!(user: reader, topic: @group_message, last_read_post_number: 3)
|
|
|
|
get '/post_readers.json', params: { id: @post.id }
|
|
reader_data = JSON.parse(response.body)['post_readers'].first
|
|
|
|
assert_reader_is_correctly_serialized(reader_data, reader, @post)
|
|
end
|
|
|
|
it 'returns an user who read pass that post' do
|
|
TopicUser.create!(user: reader, topic: @group_message, last_read_post_number: 4)
|
|
|
|
get '/post_readers.json', params: { id: @post.id }
|
|
reader_data = JSON.parse(response.body)['post_readers'].first
|
|
|
|
assert_reader_is_correctly_serialized(reader_data, reader, @post)
|
|
end
|
|
|
|
it 'return an empty list when nodobody read unti that post' do
|
|
TopicUser.create!(user: reader, topic: @group_message, last_read_post_number: 1)
|
|
|
|
get '/post_readers.json', params: { id: @post.id }
|
|
readers = JSON.parse(response.body)['post_readers']
|
|
|
|
expect(readers).to be_empty
|
|
end
|
|
|
|
it "doesn't include current_user in the readers list" do
|
|
TopicUser.create!(user: admin, topic: @group_message, last_read_post_number: 3)
|
|
|
|
get '/post_readers.json', params: { id: @post.id }
|
|
reader = JSON.parse(response.body)['post_readers'].detect { |r| r['username'] == admin.username }
|
|
|
|
expect(reader).to be_nil
|
|
end
|
|
end
|
|
|
|
def assert_reader_is_correctly_serialized(reader_data, reader, post)
|
|
expect(reader_data['avatar_template']).to eq reader.avatar_template
|
|
expect(reader_data['username']).to eq reader.username
|
|
expect(reader_data['username_lower']).to eq reader.username_lower
|
|
end
|
|
|
|
it 'returns forbidden if no group has publish_read_state enabled' do
|
|
get '/post_readers.json', params: { id: @post.id }
|
|
|
|
expect(response).to be_forbidden
|
|
end
|
|
|
|
it 'returns forbidden if current_user is not a member of a group with publish_read_state enabled' do
|
|
@group.update!(publish_read_state: true)
|
|
|
|
get '/post_readers.json', params: { id: @post.id }
|
|
|
|
expect(response).to be_forbidden
|
|
end
|
|
end
|
|
end
|