FIX: add assigned fields to suggested topic serializer (#199)

FIX: add assigned fields to suggested topic serializer (#199)

With hard refresh, information about assigned user disappeared from suggested topics.

diff --git a/app/models/assignment.rb b/app/models/assignment.rb
index 426c22f..9568402 100644
--- a/app/models/assignment.rb
+++ b/app/models/assignment.rb
@@ -4,4 +4,12 @@ class Assignment < ActiveRecord::Base
   belongs_to :topic
   belongs_to :assigned_to, polymorphic: true
   belongs_to :assigned_by_user, class_name: "User"
+
+  def assigned_to_user?
+    assigned_to_type == 'User'
+  end
+
+  def assigned_to_group?
+    assigned_to_type == 'Group'
+  end
 end
diff --git a/app/serializers/assigned_topic_serializer.rb b/app/serializers/assigned_topic_serializer.rb
index 18dbc08..5e9328d 100644
--- a/app/serializers/assigned_topic_serializer.rb
+++ b/app/serializers/assigned_topic_serializer.rb
@@ -17,7 +17,7 @@ class AssignedTopicSerializer < BasicTopicSerializer
   end
 
   def include_assigned_to_user?
-    object.assigned_to.is_a?(User)
+    object.assignment.assigned_to_user?
   end
 
   def assigned_to_group
@@ -25,6 +25,6 @@ class AssignedTopicSerializer < BasicTopicSerializer
   end
 
   def include_assigned_to_group?
-    object.assigned_to.is_a?(Group)
+    object.assignment.assigned_to_group?
   end
 end
diff --git a/lib/topic_assigner.rb b/lib/topic_assigner.rb
index c0d914b..1403cf9 100644
--- a/lib/topic_assigner.rb
+++ b/lib/topic_assigner.rb
@@ -179,7 +179,7 @@ class ::TopicAssigner
     first_post = @topic.posts.find_by(post_number: 1)
     first_post.publish_change_to_clients!(:revised, reload_topic: true)
 
-    serializer = assign_to.is_a?(User) ? BasicUserSerializer : BasicGroupSerializer
+    serializer = assignment.assigned_to_user? ? BasicUserSerializer : BasicGroupSerializer
 
     Jobs.enqueue(:assign_notification,
                  topic_id: @topic.id,
@@ -199,7 +199,7 @@ class ::TopicAssigner
       user_ids: allowed_user_ids
     )
 
-    if assign_to.is_a?(User)
+    if assignment.assigned_to_user?
       if !TopicUser.exists?(
         user_id: assign_to.id,
         topic_id: @topic.id,
@@ -226,7 +226,7 @@ class ::TopicAssigner
         nil,
         bump: false,
         post_type: SiteSetting.assigns_public ? Post.types[:small_action] : Post.types[:whisper],
-        action_code: assign_to.is_a?(User) ? "assigned" : "assigned_group",
+        action_code: assignment.assigned_to_user? ? "assigned" : "assigned_group",
         custom_fields: { "action_code_who" => assign_to.is_a?(User) ? assign_to.username : assign_to.name }
       )
 
@@ -244,7 +244,7 @@ class ::TopicAssigner
         assigned_by_id: @assigned_by.id,
         assigned_by_username: @assigned_by.username
       }
-      if assign_to.is_a?(User)
+      if assignment.assigned_to_user?
         payload.merge!({
           assigned_to_id: assign_to.id,
           assigned_to_username: assign_to.username,
@@ -275,7 +275,7 @@ class ::TopicAssigner
                    assigned_to_id: assignment.assigned_to.id,
                    assigned_to_type: assignment.assigned_to_type)
 
-      if assignment.assigned_to_type == "User"
+      if assignment.assigned_to_user?
         if TopicUser.exists?(
           user_id: assignment.assigned_to_id,
           topic: @topic,
@@ -301,7 +301,7 @@ class ::TopicAssigner
           bump: false,
           post_type: post_type,
           custom_fields: { "action_code_who" => assigned_to.is_a?(User) ? assigned_to.username : assigned_to.name },
-          action_code: assigned_to.is_a?(User) ? "unassigned" : "unassigned_group",
+          action_code: assignment.assigned_to_user? ? "unassigned" : "unassigned_group",
         )
       end
 
@@ -315,7 +315,7 @@ class ::TopicAssigner
           unassigned_by_id: @assigned_by.id,
           unassigned_by_username: @assigned_by.username
         }
-        if assigned_to.is_a?(User)
+        if assignment.assigned_to_user?
           payload.merge!({
             unassigned_to_id: assigned_to.id,
             unassigned_to_username: assigned_to.username,
diff --git a/plugin.rb b/plugin.rb
index 8c0c027..e8ae20b 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -201,16 +201,16 @@ after_initialize do
         assignments = Assignment.strict_loading.where(topic: topics)
         assignments_map = assignments.index_by(&:topic_id)
 
-        user_ids = assignments.filter { |assignment| assignment.assigned_to_type == "User" }.map(&:assigned_to_id)
+        user_ids = assignments.filter { |assignment| assignment.assigned_to_user? }.map(&:assigned_to_id)
         users_map = User.where(id: user_ids).select(UserLookup.lookup_columns).index_by(&:id)
 
-        group_ids = assignments.filter { |assignment| assignment.assigned_to_type == "Group" }.map(&:assigned_to_id)
+        group_ids = assignments.filter { |assignment| assignment.assigned_to_group? }.map(&:assigned_to_id)
         groups_map = Group.where(id: group_ids).index_by(&:id)
 
         topics.each do |topic|
           assignment = assignments_map[topic.id]
-          assigned_to = users_map[assignment.assigned_to_id] if assignment&.assigned_to_type == "User"
-          assigned_to = groups_map[assignment.assigned_to_id] if assignment&.assigned_to_type == "Group"
+          assigned_to = users_map[assignment.assigned_to_id] if assignment&.assigned_to_user?
+          assigned_to = groups_map[assignment.assigned_to_id] if assignment&.assigned_to_group?
           topic.preload_assigned_to(assigned_to)
         end
       end
@@ -412,7 +412,7 @@ after_initialize do
   end
 
   add_to_serializer(:topic_view, :include_assigned_to_user?) do
-    (SiteSetting.assigns_public || scope.can_assign?) && object.topic.assigned_to&.is_a?(User)
+    (SiteSetting.assigns_public || scope.can_assign?) && object.topic.assignment&.assigned_to_user?
   end
 
   add_to_serializer(:topic_view, :assigned_to_group, false) do
@@ -420,7 +420,24 @@ after_initialize do
   end
 
   add_to_serializer(:topic_view, :include_assigned_to_group?) do
-    (SiteSetting.assigns_public || scope.can_assign?) && object.topic.assigned_to&.is_a?(Group)
+    (SiteSetting.assigns_public || scope.can_assign?) && object.topic.assignment&.assigned_to_group?
+  end
+
+  # SuggestedTopic serializer
+  add_to_serializer(:suggested_topic, :assigned_to_user, false) do
+    DiscourseAssign::Helpers.build_assigned_to_user(object.assigned_to, object)
+  end
+
+  add_to_serializer(:suggested_topic, :include_assigned_to_user?) do
+    (SiteSetting.assigns_public || scope.can_assign?) && object.assignment&.assigned_to_user?
+  end
+
+  add_to_serializer(:suggested_topic, :assigned_to_group, false) do
+    DiscourseAssign::Helpers.build_assigned_to_group(object.assigned_to, object)
+  end
+
+  add_to_serializer(:suggested_topic, :include_assigned_to_group?) do
+    (SiteSetting.assigns_public || scope.can_assign?) && object.assignment&.assigned_to_group?
   end
 
   # TopicListItem serializer
@@ -429,7 +446,7 @@ after_initialize do
   end
 
   add_to_serializer(:topic_list_item, :include_assigned_to_user?) do
-    (SiteSetting.assigns_public || scope.can_assign?) && object.assigned_to&.is_a?(User)
+    (SiteSetting.assigns_public || scope.can_assign?) && object.assignment&.assigned_to_user?
   end
 
   add_to_serializer(:topic_list_item, :assigned_to_group) do
@@ -437,7 +454,7 @@ after_initialize do
   end
 
   add_to_serializer(:topic_list_item, :include_assigned_to_group?) do
-    (SiteSetting.assigns_public || scope.can_assign?) && object.assigned_to&.is_a?(Group)
+    (SiteSetting.assigns_public || scope.can_assign?) && object.assignment&.assigned_to_group?
   end
 
   # SearchTopicListItem serializer
@@ -446,7 +463,7 @@ after_initialize do
   end
 
   add_to_serializer(:search_topic_list_item, 'include_assigned_to_user?') do
-    (SiteSetting.assigns_public || scope.can_assign?) && object.assigned_to&.is_a?(User)
+    (SiteSetting.assigns_public || scope.can_assign?) && object.assignment&.assigned_to_user?
   end
 
   add_to_serializer(:search_topic_list_item, :assigned_to_group, false) do
@@ -454,7 +471,7 @@ after_initialize do
   end
 

[... diff too long, it was truncated ...]

GitHub sha: f58b78e027b4cb2583815c1064ce02fa5a32823c