mumuki-domain 9.2.0 → 9.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fc8a965a85ae367436a825f12235e8ce3149baacb9e610d6f2801c97270cf603
4
- data.tar.gz: d1f59395e72b047031ff987e47e8a48a596aefd1c57d566480c4c7e33737d058
3
+ metadata.gz: 6bf110cfb0548ff83999aba2c18fa8e2647237c764f5475bb7f0543b74d2e573
4
+ data.tar.gz: 06fbf9c04aa1d09579315504ad1f9594581b067f925e88d804632a63d6f36e3d
5
5
  SHA512:
6
- metadata.gz: b9b535002f54d54cfd9487122943bd44e31a6563eda77f672fb3b78347b13bf2cecacc880c7999efa04ed3a028d1352f899764a0fd95cbc4f53f617e9f010904
7
- data.tar.gz: dbde9d021212f2fd8f6118df3a9f6585606e317b8b6d14acd0adb1190b834b8fdf63646ba1b2025db204e3424bcefe17c2491aa1c2f63cdeadd4b4b6a7bc798f
6
+ metadata.gz: e398b8225bbc019cf01b72900476a306c32ac4d2f6a4a8522e543c89bf2f641ff9d234efd866b417c59cee8051f2983039379fa32f2f8d45dd1d3152ebabb176
7
+ data.tar.gz: af14215013b827db24006cdff794a5ac0b4aac7229b503abb935d741bf4e10ab6a667a53bb76a36672a88236f1708dd0b4c0d4beb007cdb9b308213d65fb20d4
@@ -103,6 +103,21 @@ class ApplicationRecord < ActiveRecord::Base
103
103
  end
104
104
  end
105
105
 
106
+ def self.with_pg_retry(&block)
107
+ retries ||= 0
108
+ transaction(&block)
109
+ rescue ActiveRecord::StatementInvalid => e
110
+ retries += 1
111
+
112
+ raise e if retries > 2
113
+ if %w(PG::ConnectionBad PG::UnableToSend).any? { |it| e.message.include? it }
114
+ warn "Postgres connection failed. Retrying in 5 seconds..."
115
+ sleep 5
116
+ ActiveRecord::Base.connection.verify!
117
+ retry
118
+ end
119
+ end
120
+
106
121
  def self.numbered(*associations)
107
122
  class_eval do
108
123
  associations.each do |it|
@@ -0,0 +1,19 @@
1
+ module WithPgLock
2
+ ##
3
+ # Lock PG table, reload model and execute callback if
4
+ # criterion is met
5
+ #
6
+ def with_pg_lock(callback, criterion = proc { true })
7
+ # Some notes:
8
+ #
9
+ # * nowait is a postgre specific option and may not work with other databases
10
+ # * nowait will raise an exception if the lock can not be acquired
11
+ # * we are using a double check lock pattern to reduce lock acquisition
12
+ with_lock('for update nowait') do
13
+ reload
14
+ callback.call if criterion.call
15
+ end if criterion.call
16
+ rescue
17
+ nil
18
+ end
19
+ end
@@ -1,5 +1,6 @@
1
1
  module WithReminders
2
2
  extend ActiveSupport::Concern
3
+ include WithPgLock
3
4
 
4
5
  def build_reminder
5
6
  mailer = UserMailer.new
@@ -18,23 +19,13 @@ module WithReminders
18
19
  end
19
20
 
20
21
  # Try to send a reminder, by acquiring a database lock for update
21
- # the aproppriate record. This object can't be updated as long as
22
+ # the appropriate record. This object can't be updated as long as
22
23
  # the reminder is being sent.
23
24
  #
24
25
  # This method is aimed to be sent across multiple servers or processed concurrently
25
26
  # and still not send duplicate mails
26
27
  def try_remind_with_lock!
27
- # Some notes:
28
- #
29
- # * nowait is a postgre specific option and may not work with other databases
30
- # * nowait will raise an exception if the lock can not be acquired
31
- # * we are using a double check lock pattern to reduce lock acquisition
32
- with_lock('for update nowait') do
33
- reload
34
- remind! if should_remind?
35
- end if should_remind?
36
- rescue
37
- nil
28
+ with_pg_lock proc { remind! }, proc { should_remind? }
38
29
  end
39
30
 
40
31
  private
@@ -0,0 +1,51 @@
1
+ module WithResponsibleModerator
2
+ extend ActiveSupport::Concern
3
+
4
+ MODERATOR_MAX_RESPONSIBLE_TIME = 45.minutes
5
+
6
+ def toggle_responsible!(moderator)
7
+ if responsible?(moderator)
8
+ no_responsible!
9
+ elsif no_current_responsible?
10
+ responsible! moderator
11
+ end
12
+ end
13
+
14
+ def any_responsible?
15
+ responsible_moderator_at.present? && (responsible_moderator_at + MODERATOR_MAX_RESPONSIBLE_TIME).future?
16
+ end
17
+
18
+ def no_current_responsible?
19
+ !any_responsible?
20
+ end
21
+
22
+ def responsible?(moderator)
23
+ any_responsible? && responsible_moderator_by == moderator
24
+ end
25
+
26
+ def current_responsible_visible_for?(user)
27
+ user&.moderator_here? && any_responsible?
28
+ end
29
+
30
+ def can_toggle_responsible?(user)
31
+ can_have_responsible? && user_can_be_responsible?(user)
32
+ end
33
+
34
+ private
35
+
36
+ def responsible!(moderator)
37
+ update! responsible_moderator_at: Time.now, responsible_moderator_by: moderator
38
+ end
39
+
40
+ def no_responsible!
41
+ update! responsible_moderator_at: nil, responsible_moderator_by: nil
42
+ end
43
+
44
+ def user_can_be_responsible?(user)
45
+ user&.moderator_here? && (no_current_responsible? || responsible?(user))
46
+ end
47
+
48
+ def can_have_responsible?
49
+ opened? || pending_review?
50
+ end
51
+ end
@@ -1,11 +1,11 @@
1
1
  class Discussion < ApplicationRecord
2
- include WithDiscussionStatus, WithScopedQueries, Contextualization
2
+ include WithDiscussionStatus, WithScopedQueries, Contextualization, WithResponsibleModerator
3
3
 
4
4
  belongs_to :item, polymorphic: true
5
5
  has_many :messages, -> { order(:created_at) }, dependent: :destroy
6
6
  belongs_to :initiator, class_name: 'User'
7
7
 
8
- belongs_to :last_moderator_access_by, class_name: 'User', optional: true
8
+ belongs_to :responsible_moderator_by, class_name: 'User', optional: true
9
9
  belongs_to :status_updated_by, class_name: 'User', optional: true
10
10
 
11
11
  belongs_to :exercise, foreign_type: :exercise, foreign_key: 'item_id'
@@ -28,8 +28,6 @@ class Discussion < ApplicationRecord
28
28
  delegate :language, to: :item
29
29
  delegate :to_discussion_status, to: :status
30
30
 
31
- MODERATOR_REVIEW_AVERAGE_TIME = 17.minutes
32
-
33
31
  scope :for_user, -> (user) do
34
32
  if user.try(:moderator_here?)
35
33
  all
@@ -81,7 +79,7 @@ class Discussion < ApplicationRecord
81
79
  end
82
80
 
83
81
  def friendly
84
- initiator.name
82
+ initiator.abbreviated_name
85
83
  end
86
84
 
87
85
  def subscription_for(user)
@@ -101,6 +99,7 @@ class Discussion < ApplicationRecord
101
99
  messages.create(message)
102
100
  user.subscribe_to! self
103
101
  unread_subscriptions(user)
102
+ no_responsible! if responsible?(user)
104
103
  end
105
104
 
106
105
  def authorized?(user)
@@ -125,6 +124,8 @@ class Discussion < ApplicationRecord
125
124
  update! status: status,
126
125
  status_updated_by: user,
127
126
  status_updated_at: Time.now
127
+
128
+ no_responsible! if responsible?(user)
128
129
  end
129
130
  end
130
131
 
@@ -163,25 +164,6 @@ class Discussion < ApplicationRecord
163
164
  requires_moderator_response: !has_moderator_response
164
165
  end
165
166
 
166
- def update_last_moderator_access!(user)
167
- if user&.moderator_here? && !last_moderator_access_visible_for?(user)
168
- update! last_moderator_access_at: Time.now,
169
- last_moderator_access_by: user
170
- end
171
- end
172
-
173
- def being_accessed_by_moderator?
174
- last_moderator_access_at.present? && (last_moderator_access_at + MODERATOR_REVIEW_AVERAGE_TIME).future?
175
- end
176
-
177
- def last_moderator_access_visible_for?(user)
178
- show_last_moderator_access_for?(user) && being_accessed_by_moderator?
179
- end
180
-
181
- def show_last_moderator_access_for?(user)
182
- user&.moderator_here? && last_moderator_access_by != user
183
- end
184
-
185
167
  def self.debatable_for(klazz, params)
186
168
  debatable_id = params[:"#{klazz.underscore}_id"]
187
169
  klazz.constantize.find(debatable_id)
@@ -21,7 +21,7 @@ class ExamAuthorizationRequest < ApplicationRecord
21
21
  def icon
22
22
  case status.to_sym
23
23
  when :pending
24
- { class: 'info-circle', type: 'info' }
24
+ { class: 'hourglass', type: 'info' }
25
25
  when :approved
26
26
  { class: 'check-circle', type: 'success' }
27
27
  when :rejected
@@ -1,4 +1,5 @@
1
1
  class ExamRegistration < ApplicationRecord
2
+ include WithPgLock
2
3
  include WithTimedEnablement
3
4
  include TerminalNavigation
4
5
 
@@ -16,6 +17,8 @@ class ExamRegistration < ApplicationRecord
16
17
 
17
18
  alias_attribute :name, :description
18
19
 
20
+ scope :should_process, -> { where(processed: false).where(arel_table[:end_time].lt(Time.now)) }
21
+
19
22
  def authorization_criterion
20
23
  @authorization_criterion ||= ExamRegistration::AuthorizationCriterion.parse(authorization_criterion_type, authorization_criterion_value)
21
24
  end
@@ -40,11 +43,21 @@ class ExamRegistration < ApplicationRecord
40
43
  registrees.where.not(id: Notification.notified_users_ids_for(self, self.organization))
41
44
  end
42
45
 
46
+ # Try to process authorization request, by acquiring a database lock for update
47
+ # the appropriate record.
48
+ #
49
+ # This method is aimed to be sent across multiple servers or processed concurrently
50
+ # and still not send duplicate mails
43
51
  def process_requests!
52
+ with_pg_lock proc { process_authorization_requests! }, proc { !processed? }
53
+ end
54
+
55
+ def process_authorization_requests!
44
56
  authorization_requests.each do |it|
45
57
  process_request! it
46
58
  it.try_authorize!
47
59
  end
60
+ update! processed: true
48
61
  end
49
62
 
50
63
  def authorization_request_for(user)
@@ -29,7 +29,7 @@ class Exercise < ApplicationRecord
29
29
  defaults { self.submissions_count = 0 }
30
30
 
31
31
  def self.default_scope
32
- where(manual_evaluation: false) if Organization.safe_current&.prevent_manual_evaluation_content
32
+ where(manual_evaluation: false) if hide_manual_evaluation?
33
33
  end
34
34
 
35
35
  alias_method :progress_for, :assignment_for
@@ -256,6 +256,10 @@ class Exercise < ApplicationRecord
256
256
  layouts.keys[0]
257
257
  end
258
258
 
259
+ def self.hide_manual_evaluation?
260
+ Organization.safe_current&.prevent_manual_evaluation_content
261
+ end
262
+
259
263
  def self.with_pending_assignments_for(user, relation)
260
264
  relation.
261
265
  joins("left join assignments assignments
@@ -266,6 +270,7 @@ class Exercise < ApplicationRecord
266
270
  #{Mumuki::Domain::Status::Submission::Passed.to_i},
267
271
  #{Mumuki::Domain::Status::Submission::ManualEvaluationPending.to_i}
268
272
  )").
269
- where('assignments.id is null')
273
+ where('assignments.id is null').
274
+ where(('exercises.manual_evaluation = false' if hide_manual_evaluation?))
270
275
  end
271
276
  end
data/app/models/user.rb CHANGED
@@ -16,6 +16,8 @@ class User < ApplicationRecord
16
16
 
17
17
  has_many :notifications
18
18
  has_many :assignments, foreign_key: :submitter_id
19
+ has_many :indicators
20
+ has_many :user_stats, class_name: 'UserStats'
19
21
  has_many :messages, -> { order(created_at: :desc) }, through: :assignments
20
22
 
21
23
  has_many :submitted_exercises, through: :assignments, class_name: 'Exercise', source: :exercise
@@ -312,6 +314,18 @@ class User < ApplicationRecord
312
314
  UserMailer.certificate(certificate).deliver_later
313
315
  end
314
316
 
317
+ def clear_progress_for!(organization)
318
+ location = { organization: organization }.compact
319
+
320
+ target_assignments = assignments.where(location)
321
+
322
+ messages.where(assignment: target_assignments).delete_all
323
+
324
+ target_assignments.delete_all
325
+ indicators.where(location).delete_all
326
+ user_stats.where(location).delete_all
327
+ end
328
+
315
329
  private
316
330
 
317
331
  def welcome_to_new_organizations!
@@ -31,9 +31,9 @@ class UserStats < ApplicationRecord
31
31
  private
32
32
 
33
33
  def messages_in_discussions_count(date_range = nil)
34
- date_filter = { date: date_range }.compact
34
+ date_filter = { created_at: date_range }.compact
35
35
  result = Message.joins(:discussion)
36
- .where({sender: user.uid, discussions: { organization: organization }}.merge(date_filter))
36
+ .where({sender: user.uid, deletion_motive: nil, discussions: { organization: organization }}.merge(date_filter))
37
37
  .group(:approved)
38
38
  .count
39
39
  unapproved = result[false] || 0
@@ -0,0 +1,5 @@
1
+ class AddProcessedFlagToExamRegistration < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_column :exam_registrations, :processed, :boolean, default: false
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ class RenameLastModeratorAccess < ActiveRecord::Migration[5.1]
2
+ def change
3
+ rename_column :discussions, :last_moderator_access_by_id, :responsible_moderator_by_id
4
+ rename_column :discussions, :last_moderator_access_at, :responsible_moderator_at
5
+ end
6
+ end
@@ -1,5 +1,5 @@
1
1
  module Mumuki
2
2
  module Domain
3
- VERSION = '9.2.0'
3
+ VERSION = '9.6.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mumuki-domain
3
3
  version: !ruby/object:Gem::Version
4
- version: 9.2.0
4
+ version: 9.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franco Leonardo Bulgarelli
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-06 00:00:00.000000000 Z
11
+ date: 2021-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -291,11 +291,13 @@ files:
291
291
  - app/models/concerns/with_name.rb
292
292
  - app/models/concerns/with_notifications.rb
293
293
  - app/models/concerns/with_number.rb
294
+ - app/models/concerns/with_pg_lock.rb
294
295
  - app/models/concerns/with_preferences.rb
295
296
  - app/models/concerns/with_profile.rb
296
297
  - app/models/concerns/with_progress.rb
297
298
  - app/models/concerns/with_randomizations.rb
298
299
  - app/models/concerns/with_reminders.rb
300
+ - app/models/concerns/with_responsible_moderator.rb
299
301
  - app/models/concerns/with_scoped_queries.rb
300
302
  - app/models/concerns/with_scoped_queries/filter.rb
301
303
  - app/models/concerns/with_scoped_queries/page.rb
@@ -666,6 +668,8 @@ files:
666
668
  - db/migrate/20210318194559_add_dates_to_certificate_program.rb
667
669
  - db/migrate/20210318195238_rename_certificate_dates.rb
668
670
  - db/migrate/20210330175706_create_exam_registration_user_join_table.rb
671
+ - db/migrate/20210512200453_add_processed_flag_to_exam_registration.rb
672
+ - db/migrate/20210518100153_rename_last_moderator_access.rb
669
673
  - lib/mumuki/domain.rb
670
674
  - lib/mumuki/domain/area.rb
671
675
  - lib/mumuki/domain/engine.rb