FEATURE: syncs topic tracking with attendance

FEATURE: syncs topic tracking with attendance

diff --git a/app/models/discourse_post_event/invitee.rb b/app/models/discourse_post_event/invitee.rb
index cf6162a..67b10a0 100644
--- a/app/models/discourse_post_event/invitee.rb
+++ b/app/models/discourse_post_event/invitee.rb
@@ -30,12 +30,16 @@ module DiscoursePostEvent
         user_id: user_id,
       )
       invitee.event.publish_update!
+      invitee.update_topic_tracking!
       invitee
     end
 
     def update_attendance!(status)
-      self.update(status: Invitee.statuses[status.to_sym])
+      new_status = Invitee.statuses[status.to_sym]
+      status_changed = self.status != new_status
+      self.update(status: new_status)
       self.event.publish_update!
+      self.update_topic_tracking! if status_changed
       self
     end
 
@@ -46,5 +50,20 @@ module DiscoursePostEvent
         ).select(:user_id)
       )
     end
+
+    def update_topic_tracking!
+      topic_id = self.event.post.topic.id
+      user_id = self.user.id
+      tracking = :regular
+
+      case self.status
+      when Invitee.statuses[:going]
+        tracking = :watching
+      when Invitee.statuses[:interested]
+        tracking = :tracking
+      end
+
+      TopicUser.change(user_id, topic_id, notification_level: TopicUser.notification_levels[tracking])
+    end
   end
 end
diff --git a/spec/models/discourse_post_event/event_spec.rb b/spec/models/discourse_post_event/event_spec.rb
index c49332a..dc6beea 100644
--- a/spec/models/discourse_post_event/event_spec.rb
+++ b/spec/models/discourse_post_event/event_spec.rb
@@ -1,7 +1,7 @@
 # frozen_string_literal: true
 
 require 'rails_helper'
-require_relative '../fabricators/event_fabricator'
+require_relative '../../fabricators/event_fabricator'
 
 describe DiscoursePostEvent::Event do
   Event ||= DiscoursePostEvent::Event
diff --git a/spec/requests/invitees_controller_spec.rb b/spec/requests/invitees_controller_spec.rb
index 4003a0c..3ecc169 100644
--- a/spec/requests/invitees_controller_spec.rb
+++ b/spec/requests/invitees_controller_spec.rb
@@ -44,6 +44,41 @@ module DiscoursePostEvent
           expect(invitee.status).to eq(1)
           expect(invitee.post_id).to eq(post_1.id)
         end
+
+        context 'when changing status' do
+          it 'sets tracking of the topic' do
+            invitee = post_event_2.invitees.first
+
+            expect(invitee.status).to eq(0)
+
+            put "/discourse-post-event/invitees/#{invitee.id}.json", params: {
+              invitee: {
+                status: 'interested'
+              }
+            }
+
+            tu = TopicUser.get(invitee.event.post.topic, invitee.user)
+            expect(tu.notification_level).to eq(TopicUser.notification_levels[:tracking])
+
+            put "/discourse-post-event/invitees/#{invitee.id}.json", params: {
+              invitee: {
+                status: 'going'
+              }
+            }
+
+            tu = TopicUser.get(invitee.event.post.topic, invitee.user)
+            expect(tu.notification_level).to eq(TopicUser.notification_levels[:watching])
+
+            put "/discourse-post-event/invitees/#{invitee.id}.json", params: {
+              invitee: {
+                status: 'not_going'
+              }
+            }
+
+            tu = TopicUser.get(invitee.event.post.topic, invitee.user)
+            expect(tu.notification_level).to eq(TopicUser.notification_levels[:regular])
+          end
+        end
       end
 
       context 'when an invitee doesn’t exist' do
@@ -65,6 +100,21 @@ module DiscoursePostEvent
           )
         end
 
+        it 'sets tracking of the topic' do
+          post "/discourse-post-event/invitees.json", params: {
+            invitee: {
+              user_id: user.id,
+              post_id: post_event_2.id,
+              status: 'going',
+            }
+          }
+
+          invitee = Invitee.find_by(user_id: user.id)
+
+          tu = TopicUser.get(invitee.event.post.topic, user)
+          expect(tu.notification_level).to eq(TopicUser.notification_levels[:watching])
+        end
+
         context 'when the invitee is the event owner' do
           let(:post_event_2) { Fabricate(:event, post: post_1) }
 

GitHub sha: a29b7ab2