0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-04 10:39:43 +08:00
discourse/app/services/group_mentions_updater.rb
Régis Hanol 73dd5b4da8
FIX: Sync category description when post content changes outside PostRevisor (#39184)
When a group or user is renamed, `GroupMentionsUpdater` and
`Jobs::UpdateUsername` update the post's raw/cooked content but skip
`PostRevisor`, so `categories.description` (a denormalized copy of the
first paragraph) is never refreshed. The Edit Category page and category
banner keep showing the old name, while the actual topic shows the new
one. Same issue with `Post#rebake!` (Rebuild HTML) and `ProcessPost`.

Extract `Post#sync_category_description` as the single source of truth
for deriving `categories.description` from a post's cooked HTML — both
`PostRevisor` and the new `Post#sync_first_post_caches` now delegate to
it. `sync_first_post_caches` also updates `topics.excerpt`, centralizing
all first-post denormalized field updates.

Call `sync_first_post_caches` from every code path that modifies post
content outside PostRevisor: `GroupMentionsUpdater`, `UpdateUsername`,
`ChangeDisplayName`, `ProcessPost`, and `Post#rebake!`.

Skip the update when the description hasn't changed to avoid unnecessary
writes. When it has changed, call `publish_category` and
`Site.clear_cache` so connected clients see the update immediately
(fixes a pre-existing flicker where the old description would briefly
appear then get overwritten by stale cached site data).

Also add a targeted query in `UpdateUsername` for category description
posts authored by the system user, which were missed by the existing
`user_actions`-based and self-mention queries.

Ref - t/181445
2026-05-11 16:13:23 +02:00

18 lines
516 B
Ruby
Vendored

# frozen_string_literal: true
class GroupMentionsUpdater
def self.update(current_name, previous_name)
Post
.where(
"cooked LIKE '%class=\"mention-group%' AND raw LIKE :previous_name",
previous_name: "%@#{previous_name}%",
)
.find_in_batches do |posts|
posts.each do |post|
post.raw.gsub!(/(^|\s)(@#{previous_name})(\s|$)/, "\\1@#{current_name}\\3")
post.save!(validate: false)
post.sync_first_post_caches
end
end
end
end