Skip to content

A platform for community discussion. Free, open, simple.

MT: Set topic tracking for imported event invitees

Gerhard Schlager180256013568
diff --git a/script/bulk_import/generic_bulk.rb b/script/bulk_import/generic_bulk.rb
index 9851bdc42d6..571f5682dd4 100644
--- a/script/bulk_import/generic_bulk.rb
+++ b/script/bulk_import/generic_bulk.rb
@@ -694,6 +694,7 @@ class BulkImport::Generic < BulkImport::Base
import_optimized_images
import_topic_users
+ import_event_invitee_topic_tracking
update_topic_users
import_bookmarks
@@ -3038,6 +3039,38 @@ class BulkImport::Generic < BulkImport::Base
topic_users.close
end
+ # Mirrors Invitee#update_topic_tracking!. Runs after the imported topic users, so an
+ # explicit subscription from the source wins, and before update_topic_users, which
+ # keeps the level because the row has a notification reason.
+ def import_event_invitee_topic_tracking
+ return unless defined?(DiscourseEvents::Events)
+
+ puts "", "Importing topic tracking of event invitees..."
+
+ statuses = DiscourseEvents::Events::Invitee.statuses
+ levels = TopicUser.notification_levels
+
+ DB.exec(
+ <<~SQL,
+ INSERT INTO topic_users (user_id, topic_id, notification_level, notifications_changed_at,
+ notifications_reason_id)
+ SELECT i.user_id, p.topic_id,
+ CASE i.status WHEN :going THEN :watching WHEN :interested THEN :tracking ELSE :regular END,
+ i.created_at, :user_changed
+ FROM discourse_post_event_invitees i
+ JOIN posts p ON p.id = i.post_id
+ WHERE i.status IS NOT NULL
+ ON CONFLICT (user_id, topic_id) DO NOTHING
+ SQL
+ going: statuses[:going],
+ interested: statuses[:interested],
+ watching: levels[:watching],
+ tracking: levels[:tracking],
+ regular: levels[:regular],
+ user_changed: TopicUser.notification_reasons[:user_changed],
+ )
+ end
+
def update_topic_users
puts "", "Updating topic users..."
diff --git a/spec/script/bulk_import/generic_bulk_spec.rb b/spec/script/bulk_import/generic_bulk_spec.rb
index 6a3d224af7a..1a1a3c4af48 100644
--- a/spec/script/bulk_import/generic_bulk_spec.rb
+++ b/spec/script/bulk_import/generic_bulk_spec.rb
@@ -918,6 +918,63 @@ if generic_import_dependencies_available
expect(imported_invitees).to be_empty
end
end
+
+ describe "#import_event_invitee_topic_tracking" do
+ fab!(:not_going_attendee, :user)
+ fab!(:muting_attendee, :user)
+
+ it "sets the topic tracking like an RSVP, keeping imported subscriptions" do
+ statuses = DiscourseEvents::Events::Invitee.statuses
+ levels = TopicUser.notification_levels
+ signed_up_at = Time.utc(2017, 3, 4, 5, 6, 7)
+ DiscourseEvents::Events::Invitee.insert_all!(
+ [
+ [attendee, :going],
+ [other_attendee, :interested],
+ [not_going_attendee, :not_going],
+ [muting_attendee, :going],
+ ].map do |user, status|
+ {
+ post_id: vienna_post.id,
+ user_id: user.id,
+ status: statuses[status],
+ created_at: signed_up_at,
+ updated_at: signed_up_at,
+ }
+ end,
+ )
+ # Posting history alone would make this attendee track the topic.
+ Fabricate(:post, topic: vienna_post.topic, user: attendee)
+ TopicUser.create!(
+ user: muting_attendee,
+ topic: vienna_post.topic,
+ notification_level: levels[:muted],
+ notifications_reason_id: TopicUser.notification_reasons[:user_changed],
+ )
+
+ importer.import_event_invitee_topic_tracking
+ importer.update_topic_users
+
+ expect(
+ TopicUser
+ .where(topic: vienna_post.topic)
+ .where.not(user: vienna_post.user)
+ .pluck(:user_id, :notification_level, :notifications_reason_id),
+ ).to contain_exactly(
+ [attendee.id, levels[:watching], TopicUser.notification_reasons[:user_changed]],
+ [other_attendee.id, levels[:tracking], TopicUser.notification_reasons[:user_changed]],
+ [
+ not_going_attendee.id,
+ levels[:regular],
+ TopicUser.notification_reasons[:user_changed],
+ ],
+ [muting_attendee.id, levels[:muted], TopicUser.notification_reasons[:user_changed]],
+ )
+ expect(TopicUser.find_by(user: attendee).notifications_changed_at).to eq_time(
+ signed_up_at,
+ )
+ end
+ end
end
describe "mapping selection" do