2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-06 17:17:11 -04:00
|
|
|
# This module takes care of updating cache columns for Markdown-containing
|
|
|
|
# fields. Use like this in the body of your class:
|
|
|
|
#
|
|
|
|
# include CacheMarkdownField
|
|
|
|
# cache_markdown_field :foo
|
|
|
|
# cache_markdown_field :bar
|
|
|
|
# cache_markdown_field :baz, pipeline: :single_line
|
2019-03-20 16:01:27 -04:00
|
|
|
# cache_markdown_field :baz, whitelisted: true
|
2016-10-06 17:17:11 -04:00
|
|
|
#
|
|
|
|
# Corresponding foo_html, bar_html and baz_html fields should exist.
|
|
|
|
module CacheMarkdownField
|
2017-04-10 14:36:30 -04:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
# changes to these attributes cause the cache to be invalidates
|
|
|
|
INVALIDATED_BY = %w[author project].freeze
|
|
|
|
|
2017-01-20 05:28:40 -05:00
|
|
|
def skip_project_check?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2020-02-28 16:09:15 -05:00
|
|
|
def can_cache_field?(field)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-04-10 14:36:30 -04:00
|
|
|
# Returns the default Banzai render context for the cached markdown field.
|
|
|
|
def banzai_render_context(field)
|
2021-05-04 11:10:36 -04:00
|
|
|
raise ArgumentError, "Unknown field: #{field.inspect}" unless
|
2021-06-07 11:09:56 -04:00
|
|
|
cached_markdown_fields.key?(field)
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2017-04-10 14:36:30 -04:00
|
|
|
# Always include a project key, or Banzai complains
|
|
|
|
project = self.project if self.respond_to?(:project)
|
2018-04-17 11:42:19 -04:00
|
|
|
group = self.group if self.respond_to?(:group)
|
2017-11-01 13:35:14 -04:00
|
|
|
context = cached_markdown_fields[field].merge(project: project, group: group)
|
2017-04-10 14:36:30 -04:00
|
|
|
|
|
|
|
# Banzai is less strict about authors, so don't always have an author key
|
|
|
|
context[:author] = self.author if self.respond_to?(:author)
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2019-01-31 03:58:23 -05:00
|
|
|
context[:markdown_engine] = :common_mark
|
2018-04-17 11:42:19 -04:00
|
|
|
|
2020-08-14 11:10:05 -04:00
|
|
|
if Feature.enabled?(:personal_snippet_reference_filters, context[:author])
|
|
|
|
context[:user] = self.parent_user
|
|
|
|
end
|
|
|
|
|
2017-04-10 14:36:30 -04:00
|
|
|
context
|
|
|
|
end
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2020-02-28 16:09:15 -05:00
|
|
|
def rendered_field_content(markdown_field)
|
|
|
|
return unless can_cache_field?(markdown_field)
|
|
|
|
|
2017-04-10 14:36:30 -04:00
|
|
|
options = { skip_project_check: skip_project_check? }
|
2020-02-28 16:09:15 -05:00
|
|
|
Banzai::Renderer.cacheless_render_field(self, markdown_field, options)
|
|
|
|
end
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2020-02-28 16:09:15 -05:00
|
|
|
# Update every applicable column in a row if any one is invalidated, as we only store
|
|
|
|
# one version per row
|
|
|
|
def refresh_markdown_cache
|
2021-03-28 11:09:30 -04:00
|
|
|
updates = cached_markdown_fields.markdown_fields.to_h do |markdown_field|
|
2017-04-10 14:36:30 -04:00
|
|
|
[
|
|
|
|
cached_markdown_fields.html_field(markdown_field),
|
2020-02-28 16:09:15 -05:00
|
|
|
rendered_field_content(markdown_field)
|
2017-04-10 14:36:30 -04:00
|
|
|
]
|
2021-03-28 11:09:30 -04:00
|
|
|
end
|
2020-02-28 16:09:15 -05:00
|
|
|
|
2018-04-17 11:42:19 -04:00
|
|
|
updates['cached_markdown_version'] = latest_cached_markdown_version
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2019-06-05 00:59:48 -04:00
|
|
|
updates.each { |field, data| write_markdown_field(field, data) }
|
2017-09-19 03:44:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def refresh_markdown_cache!
|
|
|
|
updates = refresh_markdown_cache
|
2020-12-04 07:09:39 -05:00
|
|
|
if updates.present? && save_markdown(updates)
|
|
|
|
# save_markdown updates DB columns directly, so compute and save mentions
|
|
|
|
# by calling store_mentions! or we end-up with missing mentions although those
|
|
|
|
# would appear in the notes, descriptions, etc in the UI
|
|
|
|
store_mentions! if mentionable_attributes_changed?(updates)
|
|
|
|
end
|
2017-04-10 14:36:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cached_html_up_to_date?(markdown_field)
|
2019-06-05 00:59:48 -04:00
|
|
|
return false if cached_html_for(markdown_field).nil? && __send__(markdown_field).present? # rubocop:disable GitlabSecurity/PublicSend
|
2017-04-10 14:36:30 -04:00
|
|
|
|
2019-06-05 00:59:48 -04:00
|
|
|
html_field = cached_markdown_fields.html_field(markdown_field)
|
2017-05-02 09:24:41 -04:00
|
|
|
|
2019-06-05 00:59:48 -04:00
|
|
|
markdown_changed = markdown_field_changed?(markdown_field)
|
|
|
|
html_changed = markdown_field_changed?(html_field)
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2018-04-17 11:42:19 -04:00
|
|
|
latest_cached_markdown_version == cached_markdown_version &&
|
2017-04-10 14:36:30 -04:00
|
|
|
(html_changed || markdown_changed == html_changed)
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalidated_markdown_cache?
|
|
|
|
cached_markdown_fields.html_fields.any? {|html_field| attribute_invalidated?(html_field) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def attribute_invalidated?(attr)
|
2017-08-10 12:39:26 -04:00
|
|
|
__send__("#{attr}_invalidated?") # rubocop:disable GitlabSecurity/PublicSend
|
2017-04-10 14:36:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cached_html_for(markdown_field)
|
2021-05-04 11:10:36 -04:00
|
|
|
raise ArgumentError, "Unknown field: #{markdown_field}" unless
|
2021-06-07 11:09:56 -04:00
|
|
|
cached_markdown_fields.key?(markdown_field)
|
2017-04-10 14:36:30 -04:00
|
|
|
|
2017-08-10 12:39:26 -04:00
|
|
|
__send__(cached_markdown_fields.html_field(markdown_field)) # rubocop:disable GitlabSecurity/PublicSend
|
2017-04-10 14:36:30 -04:00
|
|
|
end
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2019-07-03 19:12:02 -04:00
|
|
|
# Updates the markdown cache if necessary, then returns the field
|
|
|
|
# Unlike `cached_html_for` it returns `nil` if the field does not exist
|
|
|
|
def updated_cached_html_for(markdown_field)
|
2021-06-07 11:09:56 -04:00
|
|
|
return unless cached_markdown_fields.key?(markdown_field)
|
2019-07-03 19:12:02 -04:00
|
|
|
|
2020-12-04 07:09:39 -05:00
|
|
|
if attribute_invalidated?(cached_markdown_fields.html_field(markdown_field))
|
|
|
|
# Invalidated due to Markdown content change
|
|
|
|
# We should not persist the updated HTML here since this will depend on whether the
|
|
|
|
# Markdown content change will be persisted. Both will be persisted together when the model is saved.
|
|
|
|
if changed_attributes.key?(markdown_field)
|
|
|
|
refresh_markdown_cache
|
|
|
|
else
|
|
|
|
# Invalidated due to stale HTML cache
|
|
|
|
# This could happen when the Markdown cache version is bumped or when a model is imported and the HTML is empty.
|
|
|
|
# We persist the updated HTML here so that subsequent calls to this method do not have to regenerate the HTML again.
|
|
|
|
refresh_markdown_cache!
|
|
|
|
end
|
|
|
|
end
|
2019-07-03 19:12:02 -04:00
|
|
|
|
|
|
|
cached_html_for(markdown_field)
|
|
|
|
end
|
|
|
|
|
2018-04-17 11:42:19 -04:00
|
|
|
def latest_cached_markdown_version
|
2019-06-05 00:59:48 -04:00
|
|
|
@latest_cached_markdown_version ||= (Gitlab::MarkdownCache::CACHE_COMMONMARK_VERSION << 16) | local_version
|
2019-01-31 06:28:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def local_version
|
|
|
|
# because local_markdown_version is stored in application_settings which
|
|
|
|
# uses cached_markdown_version too, we check explicitly to avoid
|
|
|
|
# endless loop
|
2019-06-05 00:59:48 -04:00
|
|
|
return local_markdown_version if respond_to?(:has_attribute?) && has_attribute?(:local_markdown_version)
|
2019-01-31 06:28:31 -05:00
|
|
|
|
|
|
|
settings = Gitlab::CurrentSettings.current_application_settings
|
|
|
|
|
|
|
|
# Following migrations are not properly isolated and
|
|
|
|
# use real models (by calling .ghost method), in these migrations
|
|
|
|
# local_markdown_version attribute doesn't exist yet, so we
|
|
|
|
# use a default value:
|
|
|
|
# db/migrate/20170825104051_migrate_issues_to_ghost_user.rb
|
|
|
|
# db/migrate/20171114150259_merge_requests_author_id_foreign_key.rb
|
|
|
|
if settings.respond_to?(:local_markdown_version)
|
|
|
|
settings.local_markdown_version
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
2019-01-29 11:46:04 -05:00
|
|
|
end
|
|
|
|
|
2020-08-14 11:10:05 -04:00
|
|
|
def parent_user
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2020-12-04 07:09:39 -05:00
|
|
|
def store_mentions!
|
2021-06-10 14:10:05 -04:00
|
|
|
# We can only store mentions if the mentionable is a database object
|
|
|
|
return unless self.is_a?(ApplicationRecord)
|
|
|
|
|
2020-12-04 07:09:39 -05:00
|
|
|
refs = all_references(self.author)
|
|
|
|
|
|
|
|
references = {}
|
2021-06-28 17:10:13 -04:00
|
|
|
references[:mentioned_users_ids] = refs.mentioned_user_ids.presence
|
|
|
|
references[:mentioned_groups_ids] = refs.mentioned_group_ids.presence
|
|
|
|
references[:mentioned_projects_ids] = refs.mentioned_project_ids.presence
|
2020-12-04 07:09:39 -05:00
|
|
|
|
|
|
|
# One retry is enough as next time `model_user_mention` should return the existing mention record,
|
|
|
|
# that threw the `ActiveRecord::RecordNotUnique` exception in first place.
|
|
|
|
self.class.safe_ensure_unique(retries: 1) do
|
|
|
|
user_mention = model_user_mention
|
|
|
|
|
|
|
|
# this may happen due to notes polymorphism, so noteable_id may point to a record
|
|
|
|
# that no longer exists as we cannot have FK on noteable_id
|
|
|
|
break if user_mention.blank?
|
|
|
|
|
|
|
|
user_mention.mentioned_users_ids = references[:mentioned_users_ids]
|
|
|
|
user_mention.mentioned_groups_ids = references[:mentioned_groups_ids]
|
|
|
|
user_mention.mentioned_projects_ids = references[:mentioned_projects_ids]
|
|
|
|
|
|
|
|
if user_mention.has_mentions?
|
|
|
|
user_mention.save!
|
|
|
|
else
|
|
|
|
user_mention.destroy!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def mentionable_attributes_changed?(changes = saved_changes)
|
|
|
|
return false unless is_a?(Mentionable)
|
|
|
|
|
|
|
|
self.class.mentionable_attrs.any? do |attr|
|
|
|
|
changes.key?(cached_markdown_fields.html_field(attr.first)) &&
|
|
|
|
changes.fetch(cached_markdown_fields.html_field(attr.first)).last.present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-10 14:36:30 -04:00
|
|
|
included do
|
|
|
|
cattr_reader :cached_markdown_fields do
|
2019-06-05 00:59:48 -04:00
|
|
|
Gitlab::MarkdownCache::FieldData.new
|
2016-10-06 17:17:11 -04:00
|
|
|
end
|
|
|
|
|
2019-06-05 00:59:48 -04:00
|
|
|
if self < ActiveRecord::Base
|
|
|
|
include Gitlab::MarkdownCache::ActiveRecord::Extension
|
|
|
|
else
|
|
|
|
prepend Gitlab::MarkdownCache::Redis::Extension
|
2016-10-06 17:17:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
private
|
|
|
|
|
|
|
|
# Specify that a field is markdown. Its rendered output will be cached in
|
|
|
|
# a corresponding _html field. Any custom rendering options may be provided
|
|
|
|
# as a context.
|
|
|
|
def cache_markdown_field(markdown_field, context = {})
|
|
|
|
cached_markdown_fields[markdown_field] = context
|
|
|
|
|
|
|
|
html_field = cached_markdown_fields.html_field(markdown_field)
|
|
|
|
invalidation_method = "#{html_field}_invalidated?".to_sym
|
|
|
|
|
|
|
|
# The HTML becomes invalid if any dependent fields change. For now, assume
|
|
|
|
# author and project invalidate the cache in all circumstances.
|
|
|
|
define_method(invalidation_method) do
|
2019-07-03 19:12:02 -04:00
|
|
|
changed_fields = changed_attributes.keys
|
|
|
|
invalidations = changed_fields & [markdown_field.to_s, *INVALIDATED_BY]
|
2017-04-10 14:36:30 -04:00
|
|
|
!invalidations.empty? || !cached_html_up_to_date?(markdown_field)
|
2016-10-06 17:17:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|