@@ -66,7 +66,11 @@ def mark_topic_as_question
6666 topic . save!
6767 end
6868
69- def correct_topic_custom_fields_after_removal ( group_name :, new_post : false )
69+ def correct_topic_custom_fields_after_removal (
70+ group_name :,
71+ new_post : false ,
72+ category : topic . category
73+ )
7074 has_accepted_posts_from_same_group =
7175 group_name &&
7276 PostCustomField . where (
@@ -101,10 +105,10 @@ def correct_topic_custom_fields_after_removal(group_name:, new_post: false)
101105
102106 DiscourseEvent . trigger ( :category_experts_unapproved , post ) if post && !new_post
103107
104- remove_auto_tag if should_remove_auto_tag
108+ remove_auto_tag ( category : category ) if should_remove_auto_tag
105109 end
106110
107- def correct_topic_custom_fields_after_addition ( new_post : false )
111+ def correct_topic_custom_fields_after_addition ( new_post : false , category : topic . category )
108112 topic . custom_fields [ CategoryExperts ::TOPIC_EXPERT_POST_GROUP_NAMES ] = (
109113 topic . custom_fields [ CategoryExperts ::TOPIC_EXPERT_POST_GROUP_NAMES ] &.split ( "|" ) || [ ]
110114 ) . push ( users_expert_group . name ) . uniq . join ( "|" )
@@ -119,13 +123,17 @@ def correct_topic_custom_fields_after_addition(new_post: false)
119123
120124 DiscourseEvent . trigger ( :category_experts_approved , post ) unless new_post
121125
122- add_auto_tag
126+ add_auto_tag ( category : category )
123127 end
124128
125129 def handle_topic_category_change ( old_category_id , new_category_id )
126130 old_category = Category . find_by ( id : old_category_id )
127131 new_category = Category . find_by ( id : new_category_id )
128132
133+ # Get auto-tags before processing posts
134+ old_auto_tag = old_category &.custom_fields &.[]( CategoryExperts ::CATEGORY_EXPERT_AUTO_TAG )
135+ new_auto_tag = new_category &.custom_fields &.[]( CategoryExperts ::CATEGORY_EXPERT_AUTO_TAG )
136+
129137 # Get all posts in the topic that have expert status
130138 expert_posts =
131139 Post
@@ -158,7 +166,10 @@ def handle_topic_category_change(old_category_id, new_category_id)
158166 CategoryExperts ::PostHandler . new (
159167 post : expert_post ,
160168 topic : topic ,
161- ) . correct_topic_custom_fields_after_removal ( group_name : old_group_name )
169+ ) . correct_topic_custom_fields_after_removal (
170+ group_name : old_group_name ,
171+ category : old_category ,
172+ )
162173 else
163174 # Author is still an expert in the new category - update the group name
164175 new_expert_group = Group . find_by ( id : author_expert_group_ids . first )
@@ -175,20 +186,20 @@ def handle_topic_category_change(old_category_id, new_category_id)
175186 CategoryExperts ::PostHandler . new (
176187 post : expert_post ,
177188 topic : topic ,
178- ) . correct_topic_custom_fields_after_removal ( group_name : old_group_name )
189+ ) . correct_topic_custom_fields_after_removal (
190+ group_name : old_group_name ,
191+ category : old_category ,
192+ )
179193
180194 # Add new group to topic custom fields
181195 CategoryExperts ::PostHandler . new (
182196 post : expert_post ,
183197 topic : topic ,
184198 user : post_author ,
185- ) . correct_topic_custom_fields_after_addition
199+ ) . correct_topic_custom_fields_after_addition ( category : new_category )
186200 end
187201 end
188202 end
189-
190- # Handle auto-tag changes
191- handle_auto_tag_change ( old_category , new_category )
192203 end
193204
194205 private
@@ -206,63 +217,40 @@ def users_expert_group
206217 @users_expert_group = group_id . nil? ? nil : Group . find_by ( id : group_id )
207218 end
208219
209- def add_auto_tag
220+ def add_auto_tag ( category : )
210221 return if !SiteSetting . tagging_enabled
211- return if auto_tag_for_category . blank?
222+
223+ auto_tag = auto_tag_for_category ( category : category )
224+ return if auto_tag . blank?
212225
213226 existing_tag_names = topic . tags . map ( &:name )
214227 # Return early if the topic already has the automatic tag
215- return if existing_tag_names . include? ( auto_tag_for_category )
228+ return if existing_tag_names . include? ( auto_tag )
216229
217230 PostRevisor . new ( topic . ordered_posts . first ) . revise! (
218231 Discourse . system_user ,
219- { tags : ( existing_tag_names << auto_tag_for_category ) } ,
232+ { tags : ( existing_tag_names << auto_tag ) } ,
220233 )
221234 end
222235
223- def remove_auto_tag
236+ def remove_auto_tag ( category : )
224237 return if !SiteSetting . tagging_enabled
225- return if auto_tag_for_category . blank?
238+
239+ auto_tag = auto_tag_for_category ( category : category )
240+ return if auto_tag . blank?
226241
227242 existing_tag_names = topic . tags . map ( &:name )
228243 # Return early if the topic doesn't have the automatic tag
229- return if !existing_tag_names . include? ( auto_tag_for_category )
244+ return if !existing_tag_names . include? ( auto_tag )
230245
231246 PostRevisor . new ( topic . ordered_posts . first ) . revise! (
232247 Discourse . system_user ,
233- { tags : ( ( existing_tag_names || [ ] ) - [ auto_tag_for_category ] ) } ,
248+ { tags : ( ( existing_tag_names || [ ] ) - [ auto_tag ] ) } ,
234249 )
235250 end
236251
237- def auto_tag_for_category
238- return @auto_tag_for_category if defined? ( @auto_tag_for_category )
239-
240- @auto_tag_for_category =
241- @topic . category . custom_fields [ CategoryExperts ::CATEGORY_EXPERT_AUTO_TAG ]
242- end
243-
244- def handle_auto_tag_change ( old_category , new_category )
245- return if !SiteSetting . tagging_enabled
246-
247- old_auto_tag = old_category &.custom_fields &.[]( CategoryExperts ::CATEGORY_EXPERT_AUTO_TAG )
248- new_auto_tag = new_category &.custom_fields &.[]( CategoryExperts ::CATEGORY_EXPERT_AUTO_TAG )
249-
250- existing_tag_names = topic . tags . map ( &:name )
251-
252- # Determine what tags to add/remove
253- tags_to_remove = old_auto_tag . present? ? [ old_auto_tag ] : [ ]
254- tags_to_add = new_auto_tag . present? ? [ new_auto_tag ] : [ ]
255-
256- # Only revise if there's actually a change needed
257- if ( tags_to_remove . any? && existing_tag_names . include? ( old_auto_tag ) ) ||
258- ( tags_to_add . any? && !existing_tag_names . include? ( new_auto_tag ) )
259- new_tag_names = ( existing_tag_names - tags_to_remove + tags_to_add ) . uniq
260-
261- PostRevisor . new ( topic . ordered_posts . first ) . revise! (
262- Discourse . system_user ,
263- { tags : new_tag_names } ,
264- )
265- end
252+ def auto_tag_for_category ( category :)
253+ category . custom_fields [ CategoryExperts ::CATEGORY_EXPERT_AUTO_TAG ]
266254 end
267255 end
268256end
0 commit comments