A platform for community discussion. Free, open, simple.
FIX: Serialize thread membership for user (#21743)
Martin Brennanc3779a371f4f
diff --git a/plugins/chat/app/controllers/chat/api/channel_threads_controller.rb b/plugins/chat/app/controllers/chat/api/channel_threads_controller.rbindex 94ec54ff72c..97a707b7281 100644--- a/plugins/chat/app/controllers/chat/api/channel_threads_controller.rb+++ b/plugins/chat/app/controllers/chat/api/channel_threads_controller.rb@@ -10,6 +10,7 @@ class Chat::Api::ChannelThreadsController < Chat::ApiControllerthreads: result.threads,channel: result.channel,tracking: result.tracking,+ memberships: result.memberships,),::Chat::ThreadListSerializer,root: false,@@ -25,7 +26,14 @@ class Chat::Api::ChannelThreadsController < Chat::ApiControllerdef showwith_service(::Chat::LookupThread) do- on_success { render_serialized(result.thread, ::Chat::ThreadSerializer, root: "thread") }+ on_success do+ render_serialized(+ result.thread,+ ::Chat::ThreadSerializer,+ root: "thread",+ membership: result.membership,+ )+ endon_failed_policy(:threaded_discussions_enabled) { raise Discourse::NotFound }on_failed_policy(:threading_enabled_for_channel) { raise Discourse::NotFound }on_model_not_found(:thread) { raise Discourse::NotFound }diff --git a/plugins/chat/app/models/chat/thread.rb b/plugins/chat/app/models/chat/thread.rbindex bb10e6111e5..2482d61432f 100644--- a/plugins/chat/app/models/chat/thread.rb+++ b/plugins/chat/app/models/chat/thread.rb@@ -36,6 +36,10 @@ module ChatChat::UserChatThreadMembership.find_by(user: user, thread: self)&.destroyend+ def membership_for(user)+ user_chat_thread_memberships.find_by(user: user)+ end+def repliesself.chat_messages.where.not(id: self.original_message_id)enddiff --git a/plugins/chat/app/models/chat/threads_view.rb b/plugins/chat/app/models/chat/threads_view.rbindex d349956b47f..55b9cc22bca 100644--- a/plugins/chat/app/models/chat/threads_view.rb+++ b/plugins/chat/app/models/chat/threads_view.rb@@ -2,13 +2,14 @@module Chatclass ThreadsView- attr_reader :user, :channel, :threads, :tracking+ attr_reader :user, :channel, :threads, :tracking, :memberships- def initialize(channel:, threads:, user:, tracking:)+ def initialize(channel:, threads:, user:, tracking:, memberships:)@channel = channel@threads = threads@user = user@tracking = tracking+ @memberships = membershipsendendenddiff --git a/plugins/chat/app/models/chat/view.rb b/plugins/chat/app/models/chat/view.rbindex 2e6f1b18e0a..a32f946e855 100644--- a/plugins/chat/app/models/chat/view.rb+++ b/plugins/chat/app/models/chat/view.rb@@ -9,7 +9,8 @@ module Chat:can_load_more_future,:unread_thread_ids,:threads,- :tracking+ :tracking,+ :thread_membershipsdef initialize(chat_channel:,@@ -19,7 +20,8 @@ module Chatcan_load_more_future: nil,unread_thread_ids: nil,threads: nil,- tracking: nil+ tracking: nil,+ thread_memberships: nil)@chat_channel = chat_channel@chat_messages = chat_messages@@ -29,6 +31,7 @@ module Chat@unread_thread_ids = unread_thread_ids@threads = threads@tracking = tracking+ @thread_memberships = thread_membershipsenddef reviewable_idsdiff --git a/plugins/chat/app/serializers/chat/base_thread_membership_serializer.rb b/plugins/chat/app/serializers/chat/base_thread_membership_serializer.rbnew file mode 100644index 00000000000..f555d662b74--- /dev/null+++ b/plugins/chat/app/serializers/chat/base_thread_membership_serializer.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++module Chat+ class BaseThreadMembershipSerializer < ApplicationSerializer+ attributes :notification_level, :thread_id, :last_read_message_id+ end+enddiff --git a/plugins/chat/app/serializers/chat/thread_list_serializer.rb b/plugins/chat/app/serializers/chat/thread_list_serializer.rbindex bdeedf92e79..44a8b9225a6 100644--- a/plugins/chat/app/serializers/chat/thread_list_serializer.rb+++ b/plugins/chat/app/serializers/chat/thread_list_serializer.rb@@ -5,11 +5,14 @@ module Chatattributes :meta, :threads, :trackingdef threads- ActiveModel::ArraySerializer.new(- object.threads,- each_serializer: Chat::ThreadSerializer,- scope: scope,- )+ object.threads.map do |thread|+ Chat::ThreadSerializer.new(+ thread,+ scope: scope,+ membership: object.memberships.find { |m| m.thread_id == thread.id },+ root: nil,+ )+ endenddef trackingdiff --git a/plugins/chat/app/serializers/chat/thread_serializer.rb b/plugins/chat/app/serializers/chat/thread_serializer.rbindex b5790415f67..69fc3d41f98 100644--- a/plugins/chat/app/serializers/chat/thread_serializer.rb+++ b/plugins/chat/app/serializers/chat/thread_serializer.rb@@ -5,7 +5,7 @@ module Chathas_one :original_message_user, serializer: BasicUserWithStatusSerializer, embed: :objectshas_one :original_message, serializer: Chat::ThreadOriginalMessageSerializer, embed: :objects- attributes :id, :title, :status, :channel_id, :meta, :reply_count+ attributes :id, :title, :status, :channel_id, :meta, :reply_count, :current_user_membershipdef initialize(object, opts)super(object, opts)@@ -13,6 +13,7 @@ module Chat# Avoids an N1 to re-load the thread in the serializer for original_message.object.original_message.thread = object+ @current_user_membership = opts[:membership]enddef meta@@ -23,6 +24,20 @@ module Chatobject.replies_count_cache || 0end+ def include_current_user_membership?+ @current_user_membership.present?+ end++ def current_user_membership+ @current_user_membership.thread = object++ Chat::BaseThreadMembershipSerializer.new(+ @current_user_membership,+ scope: scope,+ root: false,+ ).as_json+ end+privatedef thread_message_bus_last_iddiff --git a/plugins/chat/app/serializers/chat/view_serializer.rb b/plugins/chat/app/serializers/chat/view_serializer.rbindex ca510465624..1d2a5842718 100644--- a/plugins/chat/app/serializers/chat/view_serializer.rb+++ b/plugins/chat/app/serializers/chat/view_serializer.rb@@ -7,11 +7,14 @@ module Chatdef threadsreturn [] if !object.threads- ActiveModel::ArraySerializer.new(- object.threads,- each_serializer: Chat::ThreadSerializer,- scope: scope,- )+ object.threads.map do |thread|+ Chat::ThreadSerializer.new(+ thread,+ scope: scope,+ membership: object.thread_memberships.find { |m| m.thread_id == thread.id },+ root: nil,+ )+ endenddef trackingdiff --git a/plugins/chat/app/services/chat/channel_view_builder.rb b/plugins/chat/app/services/chat/channel_view_builder.rbindex e8243ac8d49..11adbfe6fc7 100644--- a/plugins/chat/app/services/chat/channel_view_builder.rb+++ b/plugins/chat/app/services/chat/channel_view_builder.rb@@ -35,6 +35,7 @@ module Chatstep :fetch_unread_thread_idsstep :fetch_threads_for_messagesstep :fetch_tracking+ step :fetch_thread_membershipsstep :build_viewclass Contract@@ -171,6 +172,18 @@ module Chatendend+ def fetch_thread_memberships(threads:, guardian:, **)+ if threads.empty?+ context.thread_memberships = []+ else+ context.thread_memberships =+ ::Chat::UserChatThreadMembership.where(+ thread_id: threads.map(&:id),+ user_id: guardian.user.id,+ )+ end+ end+def build_view(guardian:,channel:,@@ -180,6 +193,7 @@ module Chatunread_thread_ids:,can_load_more_past:,can_load_more_future:,+ thread_memberships:,**)context.view =@@ -192,6 +206,7 @@ module Chatunread_thread_ids: unread_thread_ids,threads: threads,tracking: tracking,+ thread_memberships: thread_memberships,)endenddiff --git a/plugins/chat/app/services/chat/lookup_channel_threads.rb b/plugins/chat/app/services/chat/lookup_channel_threads.rbindex e64a05c4eff..35e448a6470 100644--- a/plugins/chat/app/services/chat/lookup_channel_threads.rb+++ b/plugins/chat/app/services/chat/lookup_channel_threads.rb@@ -24,6 +24,7 @@ module Chatpolicy :can_view_channelmodel :threadsstep :fetch_tracking+ step :fetch_memberships# @!visibility privateclass Contract@@ -81,5 +82,13 @@ module Chatinclude_threads: true,).thread_trackingend++ def fetch_memberships(guardian:, threads:, **)+ context.memberships =+ ::Chat::UserChatThreadMembership.where(+ thread_id: threads.map(&:id),+ user_id: guardian.user.id,+ )+ endendenddiff --git a/plugins/chat/app/services/chat/lookup_thread.rb b/plugins/chat/app/services/chat/lookup_thread.rbindex ead2e00e277..c04d5e62ddc 100644--- a/plugins/chat/app/services/chat/lookup_thread.rb+++ b/plugins/chat/app/services/chat/lookup_thread.rb@@ -24,6 +24,7 @@ module Chatmodel :thread, :fetch_threadpolicy :invalid_accesspolicy :threading_enabled_for_channel+ step :fetch_membership# @!visibility privateclass Contract@@ -54,5 +55,9 @@ module Chatdef threading_enabled_for_channel(thread:, **)thread.channel.threading_enabledend++ def fetch_membership(thread:, guardian:, **)+ context.membership = thread.membership_for(guardian.user)+ endendenddiff --git a/plugins/chat/assets/javascripts/discourse/models/chat-thread.js b/plugins/chat/assets/javascripts/discourse/models/chat-thread.jsindex 9617cb8b6c9..f9e1afa6670 100644--- a/plugins/chat/assets/javascripts/discourse/models/chat-thread.js+++ b/plugins/chat/assets/javascripts/discourse/models/chat-thread.js@@ -5,6 +5,7 @@ import { tracked } from "@glimmer/tracking";import guid from "pretty-text/guid";import ChatMessage from "discourse/plugins/chat/discourse/models/chat-message";import ChatTrackingState from "discourse/plugins/chat/discourse/models/chat-tracking-state";+import UserChatThreadMembership from "discourse/plugins/chat/discourse/models/user-chat-thread-membership";export const THREAD_STATUSES = {open: "open",@@ -28,6 +29,7 @@ export default class ChatThread {@tracked threadMessageBusLastId;@tracked replyCount;@tracked tracking;+ @tracked currentUserMembership = null;messagesManager = new ChatMessagesManager(getOwner(this));@@ -41,6 +43,12 @@ export default class ChatThread {this.replyCount = args.reply_count;this.originalMessage = ChatMessage.create(channel, args.original_message);+ if (args.current_user_membership) {+ this.currentUserMembership = UserChatThreadMembership.create(+ args.current_user_membership+ );+ }+this.tracking = new ChatTrackingState(getOwner(this));}diff --git a/plugins/chat/assets/javascripts/discourse/models/user-chat-thread-membership.js b/plugins/chat/assets/javascripts/discourse/models/user-chat-thread-membership.jsnew file mode 100644index 00000000000..f45fd0bba94--- /dev/null+++ b/plugins/chat/assets/javascripts/discourse/models/user-chat-thread-membership.js@@ -0,0 +1,15 @@+import { tracked } from "@glimmer/tracking";++export default class UserChatThreadMembership {+ static create(args = {}) {+ return new UserChatThreadMembership(args);+ }++ @tracked lastReadMessageId = null;+ @tracked notificationLevel = null;++ constructor(args = {}) {+ this.lastReadMessageId = args.last_read_message_id;+ this.notificationLevel = args.notification_level;+ }+}diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-channel-thread-pane-subscriptions-manager.js b/plugins/chat/assets/javascripts/discourse/services/chat-channel-thread-pane-subscriptions-manager.jsindex 0a3114951e2..575fc32644d 100644--- a/plugins/chat/assets/javascripts/discourse/services/chat-channel-thread-pane-subscriptions-manager.js+++ b/plugins/chat/assets/javascripts/discourse/services/chat-channel-thread-pane-subscriptions-manager.js@@ -34,7 +34,10 @@ export default class ChatChannelThreadPaneSubscriptionsManager extends ChatPaneBreturn;}- _afterDeleteMessage() {- // TODO (martin) Handle this once we have lastReadMessageId for thread memberships.+ _afterDeleteMessage(targetMsg, data) {+ if (this.model.currentUserMembership?.lastReadMessageId === targetMsg.id) {+ this.model.currentUserMembership.lastReadMessageId =+ data.latest_not_deleted_message_id;+ }}}diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-subscriptions-manager.js b/plugins/chat/assets/javascripts/discourse/services/chat-subscriptions-manager.jsindex 2caa3b4c095..279586f05cd 100644--- a/plugins/chat/assets/javascripts/discourse/services/chat-subscriptions-manager.js+++ b/plugins/chat/assets/javascripts/discourse/services/chat-subscriptions-manager.js@@ -199,7 +199,6 @@ export default class ChatSubscriptionsManager extends Service {if (this.currentUser.ignored_users.includes(busData.username)) {channel.currentUserMembership.lastReadMessageId = busData.message_id;} else {- // Message from other user. Increment unread for channel tracking state.if (busData.message_id >(channel.currentUserMembership.lastReadMessageId || 0)@@ -209,7 +208,13 @@ export default class ChatSubscriptionsManager extends Service {// Thread should be considered unread if not already.if (busData.thread_id) {- channel.unreadThreadIds.add(busData.thread_id);+ channel.threadsManager+ .find(busData.channel_id, busData.thread_id)+ .then((thread) => {+ if (thread.currentUserMembership) {+ channel.unreadThreadIds.add(busData.thread_id);+ }+ });}}}@@ -225,21 +230,27 @@ export default class ChatSubscriptionsManager extends Service {.then((thread) => {if (busData.user_id === this.currentUser.id) {// Thread should no longer be considered unread.- channel.unreadThreadIds.remove(busData.thread_id);- // TODO (martin) Update lastReadMessageId for thread membership on client.+ if (thread.currentUserMembership) {+ channel.unreadThreadIds.delete(busData.thread_id);+ thread.currentUserMembership.lastReadMessageId =+ busData.message_id;+ }} else {+ // Ignored user sent message, update tracking state to no unreadif (this.currentUser.ignored_users.includes(busData.username)) {- // TODO (martin) Update lastReadMessageId for thread membership on client.+ if (thread.currentUserMembership) {+ thread.currentUserMembership.lastReadMessageId =+ busData.message_id;+ }} else {+ // Message from other user. Increment unread for thread tracking state.if (- this.chat.activeChannel?.activeThread?.id === busData.thread_id+ thread.currentUserMembership &&+ busData.message_id >+ (thread.currentUserMembership.lastReadMessageId || 0)) {- // TODO (martin) HACK: We don't yet have the lastReadMessageId on the client,- // so if the user is looking at the thread don't do anything to mark it unread.- } else {- // Message from other user. Thread should be considered unread if not already.channel.unreadThreadIds.add(busData.thread_id);- thread.tracking.unreadCount += 1;+ thread.tracking.unreadCount++;}}}diff --git a/plugins/chat/spec/services/chat/channel_view_builder_spec.rb b/plugins/chat/spec/services/chat/channel_view_builder_spec.rbindex b124888caf7..f9f6fa49455 100644--- a/plugins/chat/spec/services/chat/channel_view_builder_spec.rb+++ b/plugins/chat/spec/services/chat/channel_view_builder_spec.rb@@ -123,6 +123,19 @@ RSpec.describe Chat::ChannelViewBuilder doexpect(subject.view.threads).to eq([message_1.thread])end+ it "fetches thread memberships for the current user for fetched threads" do+ message_1 =+ Fabricate(+ :chat_message,+ chat_channel: channel,+ thread: Fabricate(:chat_thread, channel: channel),+ )+ message_1.thread.add(current_user)+ expect(subject.view.thread_memberships).to eq(+ [message_1.thread.membership_for(current_user)],+ )+ end+it "calls the tracking state report query for thread overview and tracking" dothread = Fabricate(:chat_thread, channel: channel)message_1 = Fabricate(:chat_message, chat_channel: channel, thread: thread)diff --git a/plugins/chat/spec/system/thread_tracking/full_page_spec.rb b/plugins/chat/spec/system/thread_tracking/full_page_spec.rbindex b941f9bea38..489da27b822 100644--- a/plugins/chat/spec/system/thread_tracking/full_page_spec.rb+++ b/plugins/chat/spec/system/thread_tracking/full_page_spec.rb@@ -54,5 +54,14 @@ describe "Thread tracking state | full page", type: :system, js: true doexpect(channel_page).to have_unread_thread_indicator(count: 1)expect(thread_list_page).to have_unread_item(thread.id)end++ it "does not change the unread indicator for the header icon when the user is not a member of the thread" do+ thread.remove(current_user)+ chat_page.visit_channel(channel)+ channel_page.open_thread_list+ Fabricate(:chat_message, chat_channel: channel, thread: thread)+ expect(channel_page).to have_no_unread_thread_indicator+ expect(thread_list_page).to have_no_unread_item(thread.id)+ endendend