mumuki-domain 9.2.1 → 9.7.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: 1f51de9222a3fb2a4f3a4e1f8498d90baf49b4b47b87662193510a601e5c7905
4
- data.tar.gz: b12cf9c00c8b1a2e77e5f758205c89874de9c94e2e5ee8ac3f30f39f6210f272
3
+ metadata.gz: 5f0a85995ceefa3f9400d60e29b11c3907b1c42853c2d1517138bc7fa4532239
4
+ data.tar.gz: d468a495d0c21ee1139b4087c7ce5653936d7b9289a5a06d2e7c338f443fe128
5
5
  SHA512:
6
- metadata.gz: d7ec4c49f78dde3d0422309cf38db8049909eba137b0d4c0ea6b0a27a5d7b475139a351e9c2e272771f9e2b8b59c42c00395511050e3c424369b118b3b23e3d6
7
- data.tar.gz: 3bc5e9b62d860e7ec0092352be7ae556ae93c19a2e4173667e3ed8ff3576de8a2bd6bf07128808c64a3ad791c06d8bb358ddfe8d248766b08a3eb1ab54ad51b0
6
+ metadata.gz: f5ddf066cff9cf215e1ad83041b418cf615dcf3f5674b97cc569725d53f2107f2fc4c33b5d75fc3ae36e9a16694eeac90318062a65b3b1f1c003f97924359119
7
+ data.tar.gz: 0200b8bf90e1e7c08797fd2f3c417b744be4c455ff40cff1063fd7f8bf9a28f143f7d272a85e765ef02b663f10957b1ee5fa647b32331ea953bb6b7d8cbf4e0c
@@ -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)
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
@@ -6,6 +6,7 @@ FactoryBot.define do
6
6
  locale { 'en' }
7
7
  settings {}
8
8
  name { 'an-organization' }
9
+ time_zone { 'UTC' }
9
10
  book
10
11
  end
11
12
 
@@ -13,7 +13,8 @@ class Mumuki::Domain::Organization::Profile < Mumukit::Platform::Model
13
13
  :community_link,
14
14
  :errors_explanations,
15
15
  :welcome_email_template,
16
- :welcome_email_sender
16
+ :welcome_email_sender,
17
+ :time_zone
17
18
 
18
19
  def locale_json
19
20
  locale_h.to_json
@@ -38,4 +39,9 @@ class Mumuki::Domain::Organization::Profile < Mumukit::Platform::Model
38
39
  def open_graph_image_url
39
40
  @open_graph_image_url ||= Mumukit::Platform.laboratory.url_for("logo-alt.png") # Best image size: 256x256
40
41
  end
42
+
43
+ def time_zone
44
+ @time_zone || 'Buenos Aires'
45
+ end
46
+
41
47
  end
@@ -46,4 +46,5 @@ class Mumuki::Domain::Organization::Settings < Mumukit::Platform::Model
46
46
  def in_preparation?
47
47
  in_preparation_until.present? && in_preparation_until.future?
48
48
  end
49
+
49
50
  end
@@ -1,5 +1,5 @@
1
1
  module Mumuki
2
2
  module Domain
3
- VERSION = '9.2.1'
3
+ VERSION = '9.7.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.1
4
+ version: 9.7.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-11 00:00:00.000000000 Z
11
+ date: 2021-06-17 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