snippet.rb 4.9 KB
Newer Older
1 2
# frozen_string_literal: true

G
gitlabhq 已提交
3
class Snippet < ActiveRecord::Base
V
Valery Sizov 已提交
4
  include Gitlab::VisibilityLevel
5
  include CacheMarkdownField
6
  include Noteable
7
  include Participable
8 9
  include Referable
  include Sortable
10
  include Awardable
11
  include Mentionable
S
Sean McGivern 已提交
12
  include Spammable
13
  include Editable
14
  include Gitlab::SQL::Pattern
G
gitlabhq 已提交
15

16
  cache_markdown_field :title, pipeline: :single_line
17
  cache_markdown_field :description
18 19
  cache_markdown_field :content

B
blackst0ne 已提交
20 21 22 23 24
  # Aliases to make application_helper#edited_time_ago_with_tooltip helper work properly with snippets.
  # See https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/10392/diffs#note_28719102
  alias_attribute :last_edited_at, :updated_at
  alias_attribute :last_edited_by, :updated_by

25 26 27 28 29 30
  # If file_name changes, it invalidates content
  alias_method :default_content_html_invalidator, :content_html_invalidated?
  def content_html_invalidated?
    default_content_html_invalidator || file_name_changed?
  end

31
  default_value_for(:visibility_level) { Gitlab::CurrentSettings.default_snippet_visibility }
32

R
Robert Speicher 已提交
33 34
  belongs_to :author, class_name: 'User'
  belongs_to :project
A
Andrew8xx8 已提交
35

36
  has_many :notes, as: :noteable, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
G
gitlabhq 已提交
37

D
Dmitriy Zaporozhets 已提交
38
  delegate :name, :email, to: :author, prefix: true, allow_nil: true
G
gitlabhq 已提交
39

A
Andrey Kumanyaev 已提交
40
  validates :author, presence: true
41
  validates :title, presence: true, length: { maximum: 255 }
42
  validates :file_name,
43
    length: { maximum: 255 }
44

V
Valeriy Sizov 已提交
45
  validates :content, presence: true
V
Valery Sizov 已提交
46
  validates :visibility_level, inclusion: { in: Gitlab::VisibilityLevel.values }
G
gitlabhq 已提交
47

A
Andrey Kumanyaev 已提交
48
  # Scopes
V
Valery Sizov 已提交
49 50 51 52
  scope :are_internal,  -> { where(visibility_level: Snippet::INTERNAL) }
  scope :are_private, -> { where(visibility_level: Snippet::PRIVATE) }
  scope :are_public, -> { where(visibility_level: Snippet::PUBLIC) }
  scope :public_and_internal, -> { where(visibility_level: [Snippet::PUBLIC, Snippet::INTERNAL]) }
A
Andrew8xx8 已提交
53
  scope :fresh,   -> { order("created_at DESC") }
54
  scope :inc_relations_for_view, -> { includes(author: :status) }
N
Nihad Abbasov 已提交
55

Y
Yorick Peterse 已提交
56 57
  participant :author
  participant :notes_with_associations
58

S
Sean McGivern 已提交
59 60 61
  attr_spammable :title, spam_title: true
  attr_spammable :content, spam_description: true

62 63 64 65
  def self.reference_prefix
    '$'
  end

66 67 68 69
  # Pattern used to extract `$123` snippet references from text
  #
  # This pattern supports cross-project references.
  def self.reference_pattern
70
    @reference_pattern ||= %r{
71 72
      (#{Project.reference_pattern})?
      #{Regexp.escape(reference_prefix)}(?<snippet>\d+)
73 74 75
    }x
  end

76
  def self.link_reference_pattern
77
    @link_reference_pattern ||= super("snippets", /(?<snippet>\d+)/)
78 79
  end

D
Douwe Maan 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  # Returns a collection of snippets that are either public or visible to the
  # logged in user.
  #
  # This method does not verify the user actually has the access to the project
  # the snippet is in, so it should be only used on a relation that's already scoped
  # for project access
  def self.public_or_visible_to_user(user = nil)
    if user
      authorized = user
        .project_authorizations
        .select(1)
        .where('project_authorizations.project_id = snippets.project_id')

      levels = Gitlab::VisibilityLevel.levels_for_user(user)

      where('EXISTS (?) OR snippets.visibility_level IN (?) or snippets.author_id = (?)', authorized, levels, user.id)
    else
      public_to_user
    end
  end

J
Jarka Kadlecova 已提交
101
  def to_reference(from = nil, full: false)
102 103
    reference = "#{self.class.reference_prefix}#{id}"

104
    if project.present?
J
Jarka Kadlecova 已提交
105
      "#{project.to_reference(from, full: full)}#{reference}"
106 107
    else
      reference
108 109 110
    end
  end

G
gitlabhq 已提交
111
  def self.content_types
N
Nihad Abbasov 已提交
112
    [
G
gitlabhq 已提交
113 114 115 116 117
      ".rb", ".py", ".pl", ".scala", ".c", ".cpp", ".java",
      ".haml", ".html", ".sass", ".scss", ".xml", ".php", ".erb",
      ".js", ".sh", ".coffee", ".yml", ".md"
    ]
  end
G
gitlabhq 已提交
118

D
Douwe Maan 已提交
119 120
  def blob
    @blob ||= Blob.decorate(SnippetBlob.new(self), nil)
121 122
  end

123 124 125 126
  def hook_attrs
    attributes
  end

127 128 129 130
  def file_name
    super.to_s
  end

131 132 133 134
  def sanitized_file_name
    file_name.gsub(/[^a-zA-Z0-9_\-\.]+/, '')
  end

V
Valery Sizov 已提交
135
  def visibility_level_field
136
    :visibility_level
137
  end
V
Valery Sizov 已提交
138

Y
Yorick Peterse 已提交
139
  def notes_with_associations
140
    notes.includes(:author)
Y
Yorick Peterse 已提交
141 142
  end

S
Sean McGivern 已提交
143
  def check_for_spam?
144 145
    visibility_level_changed?(to: Snippet::PUBLIC) ||
      (public? && (title_changed? || content_changed?))
S
Sean McGivern 已提交
146 147 148 149 150 151
  end

  def spammable_entity_type
    'snippet'
  end

152
  class << self
153 154 155 156 157 158 159
    # Searches for snippets with a matching title or file name.
    #
    # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
    #
    # query - The search query as a String.
    #
    # Returns an ActiveRecord::Relation.
160
    def search(query)
161
      fuzzy_search(query, [:title, :file_name])
162 163
    end

164 165 166 167 168 169 170
    # Searches for snippets with matching content.
    #
    # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
    #
    # query - The search query as a String.
    #
    # Returns an ActiveRecord::Relation.
171
    def search_code(query)
172
      fuzzy_search(query, [:content])
173
    end
J
Jan Provaznik 已提交
174 175 176 177

    def parent_class
      ::Project
    end
178
  end
G
gitlabhq 已提交
179
end