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 +4 -4
- data/app/models/application_record.rb +15 -0
- data/app/models/concerns/with_pg_lock.rb +19 -0
- data/app/models/concerns/with_reminders.rb +3 -12
- data/app/models/concerns/with_responsible_moderator.rb +51 -0
- data/app/models/discussion.rb +21 -26
- data/app/models/exam_authorization_request.rb +1 -1
- data/app/models/exam_registration.rb +13 -0
- data/app/models/user_stats.rb +1 -1
- data/db/migrate/20210512200453_add_processed_flag_to_exam_registration.rb +5 -0
- data/db/migrate/20210518100153_rename_last_moderator_access.rb +6 -0
- data/lib/mumuki/domain/factories/organization_factory.rb +1 -0
- data/lib/mumuki/domain/organization/profile.rb +7 -1
- data/lib/mumuki/domain/organization/settings.rb +1 -0
- data/lib/mumuki/domain/status/discussion/closed.rb +4 -0
- data/lib/mumuki/domain/status/discussion/opened.rb +4 -0
- data/lib/mumuki/domain/status/discussion/pending_review.rb +4 -0
- data/lib/mumuki/domain/status/discussion/solved.rb +4 -0
- data/lib/mumuki/domain/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 801f11c820f31c39323edff06d235e0c48804b1454f5dbd118363a34f153808f
|
4
|
+
data.tar.gz: daaa6a61637d5d52412fee38276de7c2ebad10cb696f28b912cbe4ef3ca49247
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
-
|
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
|
data/app/models/discussion.rb
CHANGED
@@ -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 :
|
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 :
|
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, :
|
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.
|
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: '
|
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_stats.rb
CHANGED
@@ -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
|
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.
|
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-
|
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
|