Class: Event

Direct Known Subclasses

PushEvent

Constant Summary collapse

WIKI_ACTIONS =
[:created, :updated, :destroyed].freeze
DESIGN_ACTIONS =
[:created, :updated, :destroyed, :archived].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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.contributionsObject

Update Gitlab::ContributionsCalendar#activity_dates if this changes


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

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

.find_sti_class(action) ⇒ Object


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

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


140
141
142
# File 'app/models/event.rb', line 140

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

.model_nameObject


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

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

.target_typesObject


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

def target_types
  TARGET_TYPES.keys
end

Instance Method Details

#action_nameObject


245
246
247
248
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
# File 'app/models/event.rb', line 245

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
  else
    "opened"
  end
end

#authored_by?(user) ⇒ Boolean

Returns:

  • (Boolean)

358
359
360
# File 'app/models/event.rb', line 358

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

#body?Boolean

Returns:

  • (Boolean)

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

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

#commit_note?Boolean

Returns:

  • (Boolean)

279
280
281
# File 'app/models/event.rb', line 279

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

#created_project_action?Boolean

Returns:

  • (Boolean)

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

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

#created_target?Boolean

Returns:

  • (Boolean)

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

def created_target?
  created_action? && target
end

#created_wiki_page?Boolean

Returns:

  • (Boolean)

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

def created_wiki_page?
  wiki_page? && created_action?
end

#designObject


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

def design
  target if design?
end

#design?Boolean

Returns:

  • (Boolean)

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

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

#design_note?Boolean

Returns:

  • (Boolean)

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

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

#issueObject


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

def issue
  target if issue?
end

#issue?Boolean

Returns:

  • (Boolean)

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

def issue?
  target_type == "Issue"
end

#issue_note?Boolean

Returns:

  • (Boolean)

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

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

#membership_changed?Boolean

Returns:

  • (Boolean)

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

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

#merge_requestObject


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

def merge_request
  target if merge_request?
end

#merge_request?Boolean

Returns:

  • (Boolean)

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

def merge_request?
  target_type == "MergeRequest"
end

#merge_request_note?Boolean

Returns:

  • (Boolean)

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

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

#milestoneObject


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

def milestone
  target if milestone?
end

#milestone?Boolean

Returns:

  • (Boolean)

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

def milestone?
  target_type == "Milestone"
end

#noteObject


241
242
243
# File 'app/models/event.rb', line 241

def note
  target if note?
end

#note?Boolean

Returns:

  • (Boolean)

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

def note?
  target.is_a?(Note)
end

#note_targetObject


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

def note_target
  target.noteable
end

#note_target_idObject


307
308
309
310
311
312
313
# File 'app/models/event.rb', line 307

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

#note_target_referenceObject


315
316
317
318
319
320
321
322
323
324
# File 'app/models/event.rb', line 315

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

#note_target_typeObject


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

def note_target_type
  if target.noteable_type.present?
    target.noteable_type.titleize
  else
    "Wall"
  end.downcase
end

#personal_snippet_note?Boolean

Returns:

  • (Boolean)

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

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

#presentObject


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

def present
  super(presenter_class: ::EventPresenter)
end

#project_snippet_note?Boolean

Returns:

  • (Boolean)

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

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

#push_action?Boolean

Returns:

  • (Boolean)

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

def push_action?
  false
end

#reset_project_activityObject


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

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)
    .update_all(last_activity_at: created_at)
end

#resource_parentObject


161
162
163
# File 'app/models/event.rb', line 161

def resource_parent
  project || group
end

#target_iidObject


275
276
277
# File 'app/models/event.rb', line 275

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

#target_titleObject


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

def target_title
  target.try(:title)
end

#to_partial_pathObject


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

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)

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

def updated_wiki_page?
  wiki_page? && updated_action?
end

#visible_to_user?(user = nil) ⇒ Boolean

Returns:

  • (Boolean)

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

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


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

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)

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

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