Class: Event

Inherits:
ApplicationRecord show all
Includes:
CreatedAtFilterable, DeleteWithLimit, EachBatch, FromUnion, Gitlab::Utils::StrongMemoize, IgnorableColumns, Presentable, ShaAttribute, Sortable, UsageStatistics
Defined in:
app/models/event.rb

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
ISSUE_TYPES =
[Issue.name, WorkItem.name].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

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

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



137
138
139
140
141
142
143
# File 'app/models/event.rb', line 137

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

.find_sti_class(action) ⇒ Object



128
129
130
131
132
133
134
# File 'app/models/event.rb', line 128

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



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

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

.model_nameObject



124
125
126
# File 'app/models/event.rb', line 124

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

.target_typesObject



149
150
151
# File 'app/models/event.rb', line 149

def target_types
  TARGET_TYPES.keys
end

Instance Method Details

#action_nameObject

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



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'app/models/event.rb', line 256

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)


372
373
374
# File 'app/models/event.rb', line 372

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

#body?Boolean

Returns:

  • (Boolean)


346
347
348
349
350
351
352
353
354
# File 'app/models/event.rb', line 346

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

#commit_note?Boolean

Returns:

  • (Boolean)


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

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

#created_project_action?Boolean

Returns:

  • (Boolean)


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

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

#created_target?Boolean

Returns:

  • (Boolean)


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

def created_target?
  created_action? && target
end

#created_wiki_page?Boolean

Returns:

  • (Boolean)


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

def created_wiki_page?
  wiki_page? && created_action?
end

#designObject



234
235
236
# File 'app/models/event.rb', line 234

def design
  target if design?
end

#design?Boolean

Returns:

  • (Boolean)


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

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

#design_note?Boolean

Returns:

  • (Boolean)


319
320
321
# File 'app/models/event.rb', line 319

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

#has_no_project_and_group?Boolean

Returns:

  • (Boolean)


382
383
384
# File 'app/models/event.rb', line 382

def has_no_project_and_group?
  project_id.nil? && group_id.nil?
end

#issueObject



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

def issue
  target if issue?
end

#issue?Boolean

Returns:

  • (Boolean)


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

def issue?
  target_type == "Issue"
end

#issue_note?Boolean

Returns:

  • (Boolean)


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

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

#membership_changed?Boolean

Returns:

  • (Boolean)


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

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

#merge_requestObject



238
239
240
# File 'app/models/event.rb', line 238

def merge_request
  target if merge_request?
end

#merge_request?Boolean

Returns:

  • (Boolean)


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

def merge_request?
  target_type == "MergeRequest"
end

#merge_request_note?Boolean

Returns:

  • (Boolean)


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

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

#milestoneObject



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

def milestone
  target if milestone?
end

#milestone?Boolean

Returns:

  • (Boolean)


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

def milestone?
  target_type == "Milestone"
end

#noteObject



250
251
252
# File 'app/models/event.rb', line 250

def note
  target if note?
end

#note?Boolean

Returns:

  • (Boolean)


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

def note?
  target.is_a?(Note)
end

#note_targetObject



323
324
325
# File 'app/models/event.rb', line 323

def note_target
  target.noteable
end

#note_target_idObject



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

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

#note_target_referenceObject



335
336
337
338
339
340
341
342
343
344
# File 'app/models/event.rb', line 335

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)


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

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

#presentObject



154
155
156
# File 'app/models/event.rb', line 154

def present
  super(presenter_class: ::EventPresenter)
end

#project_snippet_note?Boolean

Returns:

  • (Boolean)


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

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

#push_action?Boolean

Returns:

  • (Boolean)


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

def push_action?
  false
end

#reset_project_activityObject



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'app/models/event.rb', line 356

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)

  Gitlab::InactiveProjectsDeletionWarningTracker.new(project.id).reset
end

#resource_parentObject



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

def resource_parent
  project || group
end

#snippet_note?Boolean

Returns:

  • (Boolean)


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

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

#target_iidObject

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



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

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

#target_titleObject



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

def target_title
  target.try(:title)
end

#to_partial_pathObject



376
377
378
379
380
# File 'app/models/event.rb', line 376

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)


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

def updated_wiki_page?
  wiki_page? && updated_action?
end

#visible_to_user?(user = nil) ⇒ Boolean

Returns:

  • (Boolean)


158
159
160
161
162
163
164
# File 'app/models/event.rb', line 158

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



242
243
244
245
246
247
248
# File 'app/models/event.rb', line 242

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)


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

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

#work_item?Boolean

Returns:

  • (Boolean)


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

def work_item?
  target_type == 'WorkItem'
end