Class: Event

Direct Known Subclasses

PushEvent

Constant Summary collapse

WIKI_ACTIONS =
[:created, :updated, :destroyed].freeze
DESIGN_ACTIONS =
[:created, :updated, :destroyed].freeze
TEAM_ACTIONS =
[:joined, :left, :expired].freeze
ISSUE_ACTIONS =
[:created, :updated, :closed, :reopened].freeze
TARGET_TYPES =
HashWithIndifferentAccess.new(
  issue:          Issue,
  milestone:      Milestone,
  merge_request:  MergeRequest,
  note:           Note,
  project:        Project,
  snippet:        Snippet,
  user:           User,
  wiki:           WikiPage::Meta,
  design:         DesignManagement::Design
).freeze
RESET_PROJECT_ACTIVITY_INTERVAL =
1.hour
REPOSITORY_UPDATED_AT_INTERVAL =
5.minutes

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.contributionsObject

Update Gitlab::ContributionsCalendar#activity_dates if this changes


134
135
136
137
138
139
140
# File 'app/models/event.rb', line 134

def contributions
  where(
    'action IN (?) OR (target_type IN (?) AND action IN (?))',
    [actions[:pushed], actions[:commented]],
    %w(MergeRequest Issue), [actions[:created], actions[:closed], actions[:merged]]
  )
end

.find_sti_class(action) ⇒ Object


125
126
127
128
129
130
131
# File 'app/models/event.rb', line 125

def find_sti_class(action)
  if actions.fetch(action, action) == actions[:pushed] # action can be integer or symbol
    PushEvent
  else
    Event
  end
end

.limit_recent(limit = 20, offset = nil) ⇒ Object


142
143
144
# File 'app/models/event.rb', line 142

def limit_recent(limit = 20, offset = nil)
  recent.limit(limit).offset(offset)
end

.model_nameObject


121
122
123
# File 'app/models/event.rb', line 121

def model_name
  ActiveModel::Name.new(self, nil, 'event')
end

.target_typesObject


146
147
148
# File 'app/models/event.rb', line 146

def target_types
  TARGET_TYPES.keys
end

Instance Method Details

#action_nameObject

rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/PerceivedComplexity


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'app/models/event.rb', line 249

def action_name
  if push_action?
    push_action_name
  elsif design?
    design_action_names[action.to_sym]
  elsif closed_action?
    "closed"
  elsif merged_action?
    "accepted"
  elsif joined_action?
    'joined'
  elsif left_action?
    'left'
  elsif expired_action?
    'removed due to membership expiration from'
  elsif destroyed_action?
    'destroyed'
  elsif commented_action?
    "commented on"
  elsif created_wiki_page?
    'created'
  elsif updated_wiki_page?
    'updated'
  elsif created_project_action?
    created_project_action_name
  elsif approved_action?
    'approved'
  else
    "opened"
  end
end

#authored_by?(user) ⇒ Boolean

Returns:

  • (Boolean)

362
363
364
# File 'app/models/event.rb', line 362

def authored_by?(user)
  user ? author_id == user.id : false
end

#body?Boolean

Returns:

  • (Boolean)

338
339
340
341
342
343
344
345
346
# File 'app/models/event.rb', line 338

def body?
  if push_action?
    push_with_commits?
  elsif note?
    true
  else
    target.respond_to? :title
  end
end

#commit_note?Boolean

Returns:

  • (Boolean)

287
288
289
# File 'app/models/event.rb', line 287

def commit_note?
  note? && target && target.for_commit?
end

#created_project_action?Boolean

Returns:

  • (Boolean)

179
180
181
# File 'app/models/event.rb', line 179

def created_project_action?
  created_action? && !target && target_type.nil?
end

#created_target?Boolean

Returns:

  • (Boolean)

191
192
193
# File 'app/models/event.rb', line 191

def created_target?
  created_action? && target
end

#created_wiki_page?Boolean

Returns:

  • (Boolean)

183
184
185
# File 'app/models/event.rb', line 183

def created_wiki_page?
  wiki_page? && created_action?
end

#designObject


227
228
229
# File 'app/models/event.rb', line 227

def design
  target if design?
end

#design?Boolean

Returns:

  • (Boolean)

215
216
217
# File 'app/models/event.rb', line 215

def design?
  target_type == 'DesignManagement::Design'
end

#design_note?Boolean

Returns:

  • (Boolean)

311
312
313
# File 'app/models/event.rb', line 311

def design_note?
  note? && note.for_design?
end

#issueObject


223
224
225
# File 'app/models/event.rb', line 223

def issue
  target if issue?
end

#issue?Boolean

Returns:

  • (Boolean)

203
204
205
# File 'app/models/event.rb', line 203

def issue?
  target_type == "Issue"
end

#issue_note?Boolean

Returns:

  • (Boolean)

291
292
293
# File 'app/models/event.rb', line 291

def issue_note?
  note? && target && target.for_issue?
end

#membership_changed?Boolean

Returns:

  • (Boolean)

175
176
177
# File 'app/models/event.rb', line 175

def membership_changed?
  joined_action? || left_action? || expired_action?
end

#merge_requestObject


231
232
233
# File 'app/models/event.rb', line 231

def merge_request
  target if merge_request?
end

#merge_request?Boolean

Returns:

  • (Boolean)

207
208
209
# File 'app/models/event.rb', line 207

def merge_request?
  target_type == "MergeRequest"
end

#merge_request_note?Boolean

Returns:

  • (Boolean)

295
296
297
# File 'app/models/event.rb', line 295

def merge_request_note?
  note? && target && target.for_merge_request?
end

#milestoneObject


219
220
221
# File 'app/models/event.rb', line 219

def milestone
  target if milestone?
end

#milestone?Boolean

Returns:

  • (Boolean)

195
196
197
# File 'app/models/event.rb', line 195

def milestone?
  target_type == "Milestone"
end

#noteObject


243
244
245
# File 'app/models/event.rb', line 243

def note
  target if note?
end

#note?Boolean

Returns:

  • (Boolean)

199
200
201
# File 'app/models/event.rb', line 199

def note?
  target.is_a?(Note)
end

#note_targetObject


315
316
317
# File 'app/models/event.rb', line 315

def note_target
  target.noteable
end

#note_target_idObject


319
320
321
322
323
324
325
# File 'app/models/event.rb', line 319

def note_target_id
  if commit_note?
    target.commit_id
  else
    target.noteable_id.to_s
  end
end

#note_target_referenceObject


327
328
329
330
331
332
333
334
335
336
# File 'app/models/event.rb', line 327

def note_target_reference
  return unless note_target

  # Commit#to_reference returns the full SHA, but we want the short one here
  if commit_note?
    note_target.short_id
  else
    note_target.to_reference
  end
end

#personal_snippet_note?Boolean

Returns:

  • (Boolean)

307
308
309
# File 'app/models/event.rb', line 307

def personal_snippet_note?
  note? && target && target.for_personal_snippet?
end

#presentObject


151
152
153
# File 'app/models/event.rb', line 151

def present
  super(presenter_class: ::EventPresenter)
end

#project_snippet_note?Boolean

Returns:

  • (Boolean)

303
304
305
# File 'app/models/event.rb', line 303

def project_snippet_note?
  note? && target && target.for_project_snippet?
end

#push_action?Boolean

Returns:

  • (Boolean)

171
172
173
# File 'app/models/event.rb', line 171

def push_action?
  false
end

#reset_project_activityObject


348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'app/models/event.rb', line 348

def reset_project_activity
  return unless project

  # Don't bother updating if we know the project was updated recently.
  return if recent_update?

  # At this point it's possible for multiple threads/processes to try to
  # update the project. Only one query should actually perform the update,
  # hence we add the extra WHERE clause for last_activity_at.
  Project.unscoped.where(id: project_id)
    .where('last_activity_at <= ?', RESET_PROJECT_ACTIVITY_INTERVAL.ago)
    .touch_all(:last_activity_at, time: created_at) # rubocop: disable Rails/SkipsModelValidations
end

#resource_parentObject


163
164
165
# File 'app/models/event.rb', line 163

def resource_parent
  project || group
end

#snippet_note?Boolean

Returns:

  • (Boolean)

299
300
301
# File 'app/models/event.rb', line 299

def snippet_note?
  note? && target && target.for_snippet?
end

#target_iidObject

rubocop: enable Metrics/CyclomaticComplexity rubocop: enable Metrics/PerceivedComplexity


283
284
285
# File 'app/models/event.rb', line 283

def target_iid
  target.respond_to?(:iid) ? target.iid : target_id
end

#target_titleObject


167
168
169
# File 'app/models/event.rb', line 167

def target_title
  target.try(:title)
end

#to_partial_pathObject


366
367
368
369
370
# File 'app/models/event.rb', line 366

def to_partial_path
  # We are intentionally using `Event` rather than `self.class` so that
  # subclasses also use the `Event` implementation.
  Event._to_partial_path
end

#updated_wiki_page?Boolean

Returns:

  • (Boolean)

187
188
189
# File 'app/models/event.rb', line 187

def updated_wiki_page?
  wiki_page? && updated_action?
end

#visible_to_user?(user = nil) ⇒ Boolean

Returns:

  • (Boolean)

155
156
157
158
159
160
161
# File 'app/models/event.rb', line 155

def visible_to_user?(user = nil)
  return false unless capability.present?

  capability.all? do |rule|
    Ability.allowed?(user, rule, permission_object)
  end
end

#wiki_pageObject


235
236
237
238
239
240
241
# File 'app/models/event.rb', line 235

def wiki_page
  strong_memoize(:wiki_page) do
    next unless wiki_page?

    ProjectWiki.new(project, author).find_page(target.canonical_slug)
  end
end

#wiki_page?Boolean

Returns:

  • (Boolean)

211
212
213
# File 'app/models/event.rb', line 211

def wiki_page?
  target_type == 'WikiPage::Meta'
end