FIX: When dismissing category inform via MessageBus (#8371)

When category is dismissed, `dismiss_new` message is sent to fronted to clean state.

In addition, I noticed that when old dismiss new button is clicked, no message is sent so I decided to kill two birds with one stone.
This commit is contained in:
Krzysztof Kotlarek 2019-11-25 06:17:31 +11:00 committed by GitHub
parent 01e2d5a670
commit 43ddf60cdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 6 deletions

View File

@ -14,7 +14,8 @@ function isNew(topic) {
return (
topic.last_read_post_number === null &&
((topic.notification_level !== 0 && !topic.notification_level) ||
topic.notification_level >= NotificationLevels.TRACKING)
topic.notification_level >= NotificationLevels.TRACKING) &&
isUnseen(topic)
);
}
@ -26,6 +27,10 @@ function isUnread(topic) {
);
}
function isUnseen(topic) {
return !topic.is_seen;
}
const TopicTrackingState = EmberObject.extend({
messageCount: 0,
@ -68,6 +73,22 @@ const TopicTrackingState = EmberObject.extend({
tracker.notify(data);
}
if (data.message_type === "dismiss_new") {
Object.keys(tracker.states).forEach(k => {
const topic = tracker.states[k];
if (
!data.payload.category_id ||
topic.category_id === parseInt(data.payload.category_id, 0)
) {
tracker.states[k] = Object.assign({}, topic, {
is_seen: true
});
}
});
tracker.notifyPropertyChange("states");
tracker.incrementMessageCount();
}
if (["new_topic", "unread", "read"].includes(data.message_type)) {
tracker.notify(data);
const old = tracker.states["t" + data.topic_id];
@ -215,7 +236,11 @@ const TopicTrackingState = EmberObject.extend({
if (state) {
const lastRead = t.get("last_read_post_number");
if (lastRead !== state.last_read_post_number) {
const isSeen = t.get("is_seen");
if (
lastRead !== state.last_read_post_number ||
isSeen !== state.is_seen
) {
const postsCount = t.get("posts_count");
let newPosts = postsCount - state.highest_post_number,
unread = postsCount - state.last_read_post_number;
@ -235,7 +260,8 @@ const TopicTrackingState = EmberObject.extend({
last_read_post_number: state.last_read_post_number,
new_posts: newPosts,
unread: unread,
unseen: !state.last_read_post_number
is_seen: state.is_seen,
unseen: !state.last_read_post_number && isUnseen(state)
});
}
}

View File

@ -849,20 +849,20 @@ class TopicsController < ApplicationController
def reset_new
if params[:category_id].present?
category_ids = [params[:category_id]]
if params[:include_subcategories] == 'true'
category_ids = category_ids.concat(Category.where(parent_category_id: params[:category_id]).pluck(:id))
end
category_ids.each do |category_id|
current_user
.category_users
.where(category_id: category_id)
.first_or_initialize
.update!(last_seen_at: Time.zone.now)
TopicTrackingState.publish_dismiss_new(current_user.id, category_id)
end
else
current_user.user_stat.update_column(:new_since, Time.now)
current_user.user_stat.update_column(:new_since, Time.zone.now)
TopicTrackingState.publish_dismiss_new(current_user.id)
end
render body: nil
end

View File

@ -144,6 +144,15 @@ class TopicTrackingState
MessageBus.publish(self.unread_channel_key(user_id), message.as_json, user_ids: [user_id])
end
def self.publish_dismiss_new(user_id, category_id = nil)
payload = category_id ? { category_id: category_id } : {}
message = {
message_type: "dismiss_new",
payload: payload
}
MessageBus.publish(self.unread_channel_key(user_id), message.as_json, user_ids: [user_id])
end
def self.treat_as_new_topic_clause
User.where("GREATEST(CASE
WHEN COALESCE(uo.new_topic_duration_minutes, :default_duration) = :always THEN u.created_at

View File

@ -2342,6 +2342,8 @@ RSpec.describe TopicsController do
user.user_stat.update_column(:new_since, old_date)
TopicTrackingState.expects(:publish_dismiss_new).with(user.id)
put "/topics/reset-new.json"
expect(response.status).to eq(200)
user.reload
@ -2356,6 +2358,9 @@ RSpec.describe TopicsController do
sign_in(user)
category_user = CategoryUser.create!(category_id: category.id, user_id: user.id)
subcategory_user = CategoryUser.create!(category_id: subcategory.id, user_id: user.id)
TopicTrackingState.expects(:publish_dismiss_new).with(user.id, category.id.to_s)
put "/topics/reset-new.json?category_id=#{category.id}"
expect(category_user.reload.last_seen_at).not_to be_nil