Module: API::Helpers::NotesHelpers
- Includes:
- RendersNotes
- Defined in:
- lib/api/helpers/notes_helpers.rb
Class Method Summary
collapse
Instance Method Summary
collapse
-
#ability_name(noteable) ⇒ Object
-
#add_parent_to_finder_params(finder_params, noteable_type) ⇒ Object
-
#create_note(noteable, opts) ⇒ Object
-
#delete_note(noteable, note_id) ⇒ Object
-
#find_noteable(noteable_type, noteable_id) ⇒ Object
-
#finder_params_by_noteable_type_and_id(type, id) ⇒ Object
-
#get_note(noteable, note_id) ⇒ Object
-
#noteable_parent(noteable) ⇒ Object
-
#noteable_read_ability_name(noteable) ⇒ Object
-
#resolve_discussion(noteable, discussion_id, resolved) ⇒ Object
-
#resolve_note(noteable, note_id, resolved) ⇒ Object
-
#update_note(noteable, note_id) ⇒ Object
-
#whitelist_query_limiting ⇒ Object
#prepare_notes_for_rendering
Class Method Details
.noteable_types ⇒ Object
8
9
10
11
12
|
# File 'lib/api/helpers/notes_helpers.rb', line 8
def self.noteable_types
[Issue, MergeRequest, Snippet]
end
|
Instance Method Details
#ability_name(noteable) ⇒ Object
79
80
81
82
83
84
85
|
# File 'lib/api/helpers/notes_helpers.rb', line 79
def ability_name(noteable)
if noteable.respond_to?(:to_ability_name)
noteable.to_ability_name
else
noteable.class.to_s.underscore
end
end
|
#add_parent_to_finder_params(finder_params, noteable_type) ⇒ Object
108
109
110
|
# File 'lib/api/helpers/notes_helpers.rb', line 108
def add_parent_to_finder_params(finder_params, noteable_type)
finder_params[:project] = user_project
end
|
#create_note(noteable, opts) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/api/helpers/notes_helpers.rb', line 116
def create_note(noteable, opts)
whitelist_query_limiting
authorize!(:create_note, noteable)
parent = noteable_parent(noteable)
opts.delete(:created_at) unless current_user.can?(:set_note_created_at, noteable)
opts[:updated_at] = opts[:created_at] if opts[:created_at]
project = parent if parent.is_a?(Project)
::Notes::CreateService.new(project, current_user, opts).execute
end
|
#delete_note(noteable, note_id) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/api/helpers/notes_helpers.rb', line 52
def delete_note(noteable, note_id)
note = noteable.notes.find(note_id)
authorize! :admin_note, note
parent = noteable_parent(noteable)
project = parent if parent.is_a?(Project)
destroy_conditionally!(note) do |note|
::Notes::DestroyService.new(project, current_user).execute(note)
end
end
|
#find_noteable(noteable_type, noteable_id) ⇒ Object
87
88
89
90
91
92
93
|
# File 'lib/api/helpers/notes_helpers.rb', line 87
def find_noteable(noteable_type, noteable_id)
params = finder_params_by_noteable_type_and_id(noteable_type, noteable_id)
noteable = NotesFinder.new(current_user, params).target
noteable = nil unless can?(current_user, noteable_read_ability_name(noteable), noteable)
noteable || not_found!(noteable_type)
end
|
#finder_params_by_noteable_type_and_id(type, id) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/api/helpers/notes_helpers.rb', line 95
def finder_params_by_noteable_type_and_id(type, id)
target_type = type.name.underscore
{ target_type: target_type }.tap do |h|
if %w(issue merge_request).include?(target_type)
h[:target_iid] = id
else
h[:target_id] = id
end
add_parent_to_finder_params(h, type)
end
end
|
#get_note(noteable, note_id) ⇒ Object
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/api/helpers/notes_helpers.rb', line 64
def get_note(noteable, note_id)
note = noteable.notes.with_metadata.find(note_id)
can_read_note = note.readable_by?(current_user)
if can_read_note
present note, with: Entities::Note
else
not_found!("Note")
end
end
|
#noteable_parent(noteable) ⇒ Object
112
113
114
|
# File 'lib/api/helpers/notes_helpers.rb', line 112
def noteable_parent(noteable)
public_send("user_#{noteable.class.parent_class.to_s.underscore}") end
|
#noteable_read_ability_name(noteable) ⇒ Object
75
76
77
|
# File 'lib/api/helpers/notes_helpers.rb', line 75
def noteable_read_ability_name(noteable)
"read_#{ability_name(noteable)}".to_sym
end
|
#resolve_discussion(noteable, discussion_id, resolved) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/api/helpers/notes_helpers.rb', line 130
def resolve_discussion(noteable, discussion_id, resolved)
discussion = noteable.find_discussion(discussion_id)
forbidden! unless discussion.can_resolve?(current_user)
if resolved
parent = noteable_parent(noteable)
::Discussions::ResolveService.new(parent, current_user, one_or_more_discussions: discussion).execute
else
discussion.unresolve!
end
present discussion, with: Entities::Discussion
end
|
#resolve_note(noteable, note_id, resolved) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/api/helpers/notes_helpers.rb', line 35
def resolve_note(noteable, note_id, resolved)
note = noteable.notes.find(note_id)
authorize! :resolve_note, note
bad_request!("Note is not resolvable") unless note.resolvable?
if resolved
parent = noteable_parent(noteable)
::Notes::ResolveService.new(parent, current_user).execute(note)
else
note.unresolve!
end
present note, with: Entities::Note
end
|
#update_note(noteable, note_id) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/api/helpers/notes_helpers.rb', line 14
def update_note(noteable, note_id)
note = noteable.notes.find(note_id)
authorize! :admin_note, note
opts = {
note: params[:body],
confidential: params[:confidential]
}.compact
parent = noteable_parent(noteable)
project = parent if parent.is_a?(Project)
note = ::Notes::UpdateService.new(project, current_user, opts).execute(note)
if note.valid?
present note, with: Entities::Note
else
bad_request!("Failed to save note #{note.errors.messages}")
end
end
|
#whitelist_query_limiting ⇒ Object
145
146
147
|
# File 'lib/api/helpers/notes_helpers.rb', line 145
def whitelist_query_limiting
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab/-/issues/211538')
end
|