mumuki-domain 9.3.0 → 9.8.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: b78b493d9f7e6314a746e22d17fe33c140dada6e77a9af84da850098180a1677
4
- data.tar.gz: 9ed9017804d844c8a24540dcdf5c32d4ad78d8a28b186e3df1dbd83b845ff5cd
3
+ metadata.gz: 801f11c820f31c39323edff06d235e0c48804b1454f5dbd118363a34f153808f
4
+ data.tar.gz: daaa6a61637d5d52412fee38276de7c2ebad10cb696f28b912cbe4ef3ca49247
5
5
  SHA512:
6
- metadata.gz: 84faddd371cc851bbb08155cddb140dd6a17eaa3920f73cf235950d3321f07be4dd4f44325708b582904ee4d1eac2b5c426be3afb7b82e566834d87686eec3b2
7
- data.tar.gz: 93cf6080fba52bffbe3d7fafd4a3ed0121f55accaa8b1c7950e9512a4f409b9031288b51998ab7caff22ac161fd4c0d4c9fb663f369cafb00a42afcfe6ad5732
6
+ metadata.gz: 4b0370b955ddf9ae29c02dde36613b1bde7fcd3c3711e55f20744bcdcd283e711f461ad6ab2766b0b560fa7e845a401a2cf0172d4a04d7223eb93671ea85e95f
7
+ data.tar.gz: 1ea134ed3fe1ac847c524eff4479463f7a75c6d96ef2ecdcb6ef2c62162c38ab0effaae3e76da02c57666418747fd01753c3b369f5a0dae1058b794dac22c067
@@ -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'
@@ -15,21 +15,24 @@ class Discussion < ApplicationRecord
15
15
 
16
16
  scope :by_language, -> (language) { includes(:exercise).joins(exercise: :language).where(languages: {name: language}) }
17
17
  scope :order_by_responses_count, -> (direction) { reorder(validated_messages_count: direction, messages_count: opposite(direction)) }
18
- scope :by_requires_moderator_response, -> (boolean) { where(requires_moderator_response: boolean.to_boolean) }
18
+ scope :by_requires_attention, -> (boolean) { opened_and_requires_moderator_response(boolean).or(pending_review).no_responsible_moderator }
19
+ scope :opened_and_requires_moderator_response, -> (boolean) { where(requires_moderator_response: boolean.to_boolean)
20
+ .where(status: :opened) }
21
+ scope :no_responsible_moderator, -> { where('responsible_moderator_at < ?', Time.now - MODERATOR_MAX_RESPONSIBLE_TIME)
22
+ .or(where(responsible_moderator_at: nil)) }
23
+ scope :pending_review, -> { where(status: :pending_review) }
19
24
 
20
25
  after_create :subscribe_initiator!
21
26
 
22
27
  markdown_on :description
23
28
 
24
29
  sortable :responses_count, :upvotes_count, :created_at, default: :created_at_desc
25
- filterable :status, :language, :requires_moderator_response
30
+ filterable :status, :language, :requires_attention
26
31
  pageable
27
32
 
28
33
  delegate :language, to: :item
29
34
  delegate :to_discussion_status, to: :status
30
35
 
31
- MODERATOR_REVIEW_AVERAGE_TIME = 17.minutes
32
-
33
36
  scope :for_user, -> (user) do
34
37
  if user.try(:moderator_here?)
35
38
  all
@@ -81,7 +84,7 @@ class Discussion < ApplicationRecord
81
84
  end
82
85
 
83
86
  def friendly
84
- initiator.name
87
+ initiator.abbreviated_name
85
88
  end
86
89
 
87
90
  def subscription_for(user)
@@ -101,6 +104,7 @@ class Discussion < ApplicationRecord
101
104
  messages.create(message)
102
105
  user.subscribe_to! self
103
106
  unread_subscriptions(user)
107
+ no_responsible! if responsible?(user)
104
108
  end
105
109
 
106
110
  def authorized?(user)
@@ -125,6 +129,8 @@ class Discussion < ApplicationRecord
125
129
  update! status: status,
126
130
  status_updated_by: user,
127
131
  status_updated_at: Time.now
132
+
133
+ no_responsible! if responsible?(user)
128
134
  end
129
135
  end
130
136
 
@@ -144,6 +150,14 @@ class Discussion < ApplicationRecord
144
150
  validated_messages_count > 0
145
151
  end
146
152
 
153
+ def requires_attention_for?(user)
154
+ user&.moderator_here? && requires_attention?
155
+ end
156
+
157
+ def requires_attention?
158
+ no_current_responsible? && status.requires_attention_for?(self)
159
+ end
160
+
147
161
  def subscribe_initiator!
148
162
  initiator.subscribe_to! self
149
163
  end
@@ -163,25 +177,6 @@ class Discussion < ApplicationRecord
163
177
  requires_moderator_response: !has_moderator_response
164
178
  end
165
179
 
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
180
  def self.debatable_for(klazz, params)
186
181
  debatable_id = params[:"#{klazz.underscore}_id"]
187
182
  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)
@@ -11,7 +11,7 @@ class UserStats < ApplicationRecord
11
11
  end
12
12
 
13
13
  def activity(date_range = nil)
14
- date_filter = { submitted_at: date_range }.compact
14
+ date_filter = { updated_at: date_range }.compact
15
15
  {
16
16
  exercises: {
17
17
  solved_count: organization_exercises
@@ -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
@@ -12,4 +12,8 @@ module Mumuki::Domain::Status::Discussion::Closed
12
12
  def self.iconize
13
13
  {class: :danger, type: 'times-circle'}
14
14
  end
15
+
16
+ def self.requires_attention_for?(_)
17
+ false
18
+ end
15
19
  end
@@ -28,4 +28,8 @@ module Mumuki::Domain::Status::Discussion::Opened
28
28
  def self.should_be_shown?(*)
29
29
  true
30
30
  end
31
+
32
+ def self.requires_attention_for?(discussion)
33
+ discussion.requires_moderator_response?
34
+ end
31
35
  end
@@ -12,4 +12,8 @@ module Mumuki::Domain::Status::Discussion::PendingReview
12
12
  def self.iconize
13
13
  {class: :info, type: 'hourglass'}
14
14
  end
15
+
16
+ def self.requires_attention_for?(_)
17
+ true
18
+ end
15
19
  end
@@ -16,4 +16,8 @@ module Mumuki::Domain::Status::Discussion::Solved
16
16
  def self.should_be_shown?(*)
17
17
  true
18
18
  end
19
+
20
+ def self.requires_attention_for?(_)
21
+ false
22
+ end
19
23
  end
@@ -1,5 +1,5 @@
1
1
  module Mumuki
2
2
  module Domain
3
- VERSION = '9.3.0'
3
+ VERSION = '9.8.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.3.0
4
+ version: 9.8.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-13 00:00:00.000000000 Z
11
+ date: 2021-07-01 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