|
Author |
hbghlyj
Posted at 2025-3-25 10:01:04
存储 discourse 库中的帖子修订的代码。
负责将帖子修订保存到 discourse/discourse 存储库中的 post_revisions 表中的代码位于以下位置:
Post Revisor类 (lib/post_revisor.rb):
- def create_revision
- modifications = post_changes.merge(@topic_changes.diff)
- modifications["raw"][0] = cached_original_raw || modifications["raw"][0] if modifications["raw"]
- if modifications["cooked"]
- modifications["cooked"][0] = cached_original_cooked || modifications["cooked"][0]
- end
- @post_revision = PostRevision.create!(
- user_id: @post.last_editor_id,
- post_id: @post.id,
- number: @post.version,
- modifications: modifications,
- hidden: only_hidden_tags_changed?
- )
- end
- def update_revision
- return unless revision = PostRevision.find_by(post_id: @post.id, number: @post.version)
- revision.user_id = @post.last_editor_id
- modifications = post_changes.merge(@topic_changes.diff)
- modifications.each_key do |field|
- if revision.modifications.has_key?(field)
- old_value = revision.modifications[field][0]
- new_value = modifications[field][1]
- if old_value.to_s != new_value.to_s
- revision.modifications[field] = [old_value, new_value]
- else
- revision.modifications.delete(field)
- end
- else
- revision.modifications[field] = modifications[field]
- end
- end
- if revision.modifications.empty?
- revision.destroy
- @post.last_editor_id = PostRevision.where(post_id: @post.id).order(number: :desc).pick(:user_id) || @post.user_id
- @post.version -= 1
- @post.public_version -= 1
- @post.save(validate: @validate_post)
- else
- revision.save
- end
- end
Copy the Code
此文件包含创建和更新帖子修订的逻辑。具体来说,请查看 create_revision 和 update_revision 方法。
Post Revision模型 (app/models/post_revision.rb):
- class PostRevision < ActiveRecord::Base
- belongs_to :post
- belongs_to :user
- serialize :modifications, type: Hash, coder: YAML
- after_create :create_notification
- def create_notification
- PostActionNotifier.after_create_post_revision(self)
- end
- end
Copy the Code
此模型定义 PostRevision 类并包含 after_create 回调以创建通知。
posts 控制器 (app/controllers/posts_controller.rb):
- def revisions
- post = find_post_from_params
- raise Discourse::NotFound if post.hidden && !guardian.can_view_hidden_post_revisions?
- post_revision = find_post_revision_from_params
- post_revision_serializer = PostRevisionSerializer.new(post_revision, scope: guardian, root: false)
- render_json_dump(post_revision_serializer)
- end
- def hide_revision
- post_revision = find_post_revision_from_params
- guardian.ensure_can_hide_post_revision!(post_revision)
- post_revision.hide!
- post = find_post_from_params
- post.public_version -= 1
- post.save
- post.publish_change_to_clients!(:revised)
- render body: nil
- end
- def permanently_delete_revisions
- guardian.ensure_can_permanently_delete_post_revisions!
- post = find_post_from_params
- raise Discourse::InvalidParameters.new(:post) if post.blank?
- raise Discourse::NotFound if post.revisions.blank?
- RateLimiter.new(current_user, "admin_permanently_delete_post_revisions", 20, 1.minute, apply_limit_to_staff: true).performed!
- ActiveRecord::Base.transaction do
- updated_at = Time.zone.now
- post.revisions.destroy_all
- post.update(version: 1, public_version: 1, last_version_at: updated_at)
- StaffActionLogger.new(current_user).log_permanently_delete_post_revisions(post)
- end
- post.rebake!
- render body: nil
- end
Copy the Code
此控制器包含与帖子修订相关的操作,例如创建、更新、显示和隐藏修订。 |
|