REFACTOR: Add basic tests for `TopicTrackingState#publish_*`.
* Ensure we don't publish events for PMs.
This commit is contained in:
parent
821f3d0c15
commit
d576056cff
|
@ -99,7 +99,7 @@ class Topic < ActiveRecord::Base
|
|||
if: Proc.new { |t|
|
||||
(t.new_record? || t.category_id_changed?) &&
|
||||
!SiteSetting.allow_uncategorized_topics &&
|
||||
(t.archetype.nil? || t.archetype == Archetype.default) &&
|
||||
(t.archetype.nil? || t.regular?) &&
|
||||
(!t.user_id || !t.user.staff?)
|
||||
}
|
||||
|
||||
|
@ -273,7 +273,7 @@ class Topic < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def ensure_topic_has_a_category
|
||||
if category_id.nil? && (archetype.nil? || archetype == Archetype.default)
|
||||
if category_id.nil? && (archetype.nil? || self.regular?)
|
||||
self.category_id = SiteSetting.uncategorized_category_id
|
||||
end
|
||||
end
|
||||
|
@ -463,6 +463,10 @@ class Topic < ActiveRecord::Base
|
|||
archetype == Archetype.private_message
|
||||
end
|
||||
|
||||
def regular?
|
||||
self.archetype == Archetype.default
|
||||
end
|
||||
|
||||
MAX_SIMILAR_BODY_LENGTH ||= 200
|
||||
|
||||
def self.similar_to(title, raw, user = nil)
|
||||
|
|
|
@ -10,6 +10,8 @@ class TopicTrackingState
|
|||
include ActiveModel::SerializerSupport
|
||||
|
||||
CHANNEL = "/user-tracking"
|
||||
UNREAD_MESSAGE_TYPE = "unread".freeze
|
||||
LATEST_MESSAGE_TYPE = "latest".freeze
|
||||
|
||||
attr_accessor :user_id,
|
||||
:topic_id,
|
||||
|
@ -20,6 +22,7 @@ class TopicTrackingState
|
|||
:notification_level
|
||||
|
||||
def self.publish_new(topic)
|
||||
return unless topic.regular?
|
||||
|
||||
message = {
|
||||
topic_id: topic.id,
|
||||
|
@ -41,11 +44,11 @@ class TopicTrackingState
|
|||
end
|
||||
|
||||
def self.publish_latest(topic, staff_only = false)
|
||||
return unless topic.archetype == "regular"
|
||||
return unless topic.regular?
|
||||
|
||||
message = {
|
||||
topic_id: topic.id,
|
||||
message_type: "latest",
|
||||
message_type: LATEST_MESSAGE_TYPE,
|
||||
payload: {
|
||||
bumped_at: topic.bumped_at,
|
||||
topic_id: topic.id,
|
||||
|
@ -63,7 +66,12 @@ class TopicTrackingState
|
|||
MessageBus.publish("/latest", message.as_json, group_ids: group_ids)
|
||||
end
|
||||
|
||||
def self.unread_channel_key(user_id)
|
||||
"/unread/#{user_id}"
|
||||
end
|
||||
|
||||
def self.publish_unread(post)
|
||||
return unless post.topic.regular?
|
||||
# TODO at high scale we are going to have to defer this,
|
||||
# perhaps cut down to users that are around in the last 7 days as well
|
||||
|
||||
|
@ -81,7 +89,7 @@ class TopicTrackingState
|
|||
|
||||
message = {
|
||||
topic_id: post.topic_id,
|
||||
message_type: "unread",
|
||||
message_type: UNREAD_MESSAGE_TYPE,
|
||||
payload: {
|
||||
last_read_post_number: tu.last_read_post_number,
|
||||
highest_post_number: post.post_number,
|
||||
|
@ -93,7 +101,7 @@ class TopicTrackingState
|
|||
}
|
||||
}
|
||||
|
||||
MessageBus.publish("/unread/#{tu.user_id}", message.as_json, group_ids: group_ids)
|
||||
MessageBus.publish(self.unread_channel_key(tu.user_id), message.as_json, group_ids: group_ids)
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -128,7 +136,6 @@ class TopicTrackingState
|
|||
end
|
||||
|
||||
def self.publish_read(topic_id, last_read_post_number, user_id, notification_level = nil)
|
||||
|
||||
highest_post_number = Topic.where(id: topic_id).pluck(:highest_post_number).first
|
||||
|
||||
message = {
|
||||
|
@ -142,8 +149,7 @@ class TopicTrackingState
|
|||
}
|
||||
}
|
||||
|
||||
MessageBus.publish("/unread/#{user_id}", message.as_json, user_ids: [user_id])
|
||||
|
||||
MessageBus.publish(self.unread_channel_key(user_id), message.as_json, user_ids: [user_id])
|
||||
end
|
||||
|
||||
def self.treat_as_new_topic_clause
|
||||
|
|
|
@ -10,9 +10,67 @@ describe TopicTrackingState do
|
|||
create_post
|
||||
end
|
||||
|
||||
it "can correctly publish unread" do
|
||||
# TODO setup stuff and look at messages
|
||||
TopicTrackingState.publish_unread(post)
|
||||
let(:topic) { post.topic }
|
||||
|
||||
describe '#publish_latest' do
|
||||
it 'can correctly publish latest' do
|
||||
message = MessageBus.track_publish("/latest") do
|
||||
described_class.publish_latest(topic)
|
||||
end.first
|
||||
|
||||
data = message.data
|
||||
|
||||
expect(data["topic_id"]).to eq(topic.id)
|
||||
expect(data["message_type"]).to eq(described_class::LATEST_MESSAGE_TYPE)
|
||||
expect(data["payload"]["archetype"]).to eq(Archetype.default)
|
||||
end
|
||||
|
||||
describe 'private message' do
|
||||
let(:topic) { Fabricate(:private_message_topic) }
|
||||
|
||||
it 'should not publish any message' do
|
||||
messages = MessageBus.track_publish do
|
||||
described_class.publish_latest(topic)
|
||||
end
|
||||
|
||||
expect(messages).to eq([])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#publish_unread' do
|
||||
it "can correctly publish unread" do
|
||||
message = MessageBus.track_publish(described_class.unread_channel_key(post.user.id)) do
|
||||
TopicTrackingState.publish_unread(post)
|
||||
end.first
|
||||
|
||||
data = message.data
|
||||
|
||||
expect(data["topic_id"]).to eq(topic.id)
|
||||
expect(data["message_type"]).to eq(described_class::UNREAD_MESSAGE_TYPE)
|
||||
expect(data["payload"]["archetype"]).to eq(Archetype.default)
|
||||
end
|
||||
|
||||
describe 'for a private message' do
|
||||
let(:private_message_post) { Fabricate(:private_message_post) }
|
||||
let(:topic) { private_message_post.topic }
|
||||
|
||||
before do
|
||||
TopicUser.change(
|
||||
topic.allowed_users.first.id,
|
||||
topic.id,
|
||||
notification_level: TopicUser.notification_levels[:tracking]
|
||||
)
|
||||
end
|
||||
|
||||
it 'should not publish any message' do
|
||||
messages = MessageBus.track_publish do
|
||||
TopicTrackingState.publish_unread(private_message_post)
|
||||
end
|
||||
|
||||
expect(messages).to eq([])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "correctly handles muted categories" do
|
||||
|
|
Loading…
Reference in New Issue