Class: WorkActivity
Overview
rubocop:disable Metrics/ClassLength
Defined Under Namespace
Classes: BaseMessage, EmbargoEvent, FileChanges, Message, MetadataChanges, Migration, OtherLogEvent, ProvenanceNote, Renderer
Constant Summary
collapse
- MESSAGE =
TODO: Migrate existing records to “MESSAGE”; then close #825.
"COMMENT"
- NOTIFICATION =
"NOTIFICATION"
- MESSAGE_ACTIVITY_TYPES =
[MESSAGE, NOTIFICATION].freeze
- CHANGES =
"CHANGES"
- DATACITE_ERROR =
"DATACITE-ERROR"
- FILE_CHANGES =
"FILE-CHANGES"
- MIGRATION_START =
"MIGRATION_START"
- MIGRATION_COMPLETE =
"MIGRATION_COMPLETE"
- PROVENANCE_NOTES =
"PROVENANCE-NOTES"
- SYSTEM =
"SYSTEM"
- EMBARGO =
"EMBARGO"
- CHANGE_LOG_ACTIVITY_TYPES =
[CHANGES, FILE_CHANGES, PROVENANCE_NOTES, SYSTEM, DATACITE_ERROR, MIGRATION_COMPLETE, EMBARGO].freeze
- USER_REFERENCE =
/@[\w]*/
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.activities_for_work(work_id, activity_types) ⇒ Object
46
47
48
|
# File 'app/models/work_activity.rb', line 46
def self.activities_for_work(work_id, activity_types)
where(work_id:, activity_type: activity_types)
end
|
.add_work_activity(work_id, message, user_id, activity_type:, created_at: nil) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'app/models/work_activity.rb', line 28
def self.add_work_activity(work_id, message, user_id, activity_type:, created_at: nil)
activity = WorkActivity.new(
work_id:,
activity_type:,
message:,
created_by_user_id: user_id,
created_at: )
activity.save!
activity.notify_users
if activity_type == MESSAGE
activity.notify_creator
activity.notify_curator
end
activity
end
|
.changes_for_work(work_id) ⇒ Object
54
55
56
|
# File 'app/models/work_activity.rb', line 54
def self.changes_for_work(work_id)
activities_for_work(work_id, CHANGE_LOG_ACTIVITY_TYPES)
end
|
.messages_for_work(work_id) ⇒ Object
50
51
52
|
# File 'app/models/work_activity.rb', line 50
def self.messages_for_work(work_id)
activities_for_work(work_id, MESSAGE_ACTIVITY_TYPES)
end
|
Instance Method Details
#created_by_user ⇒ Object
103
104
105
106
|
# File 'app/models/work_activity.rb', line 103
def created_by_user
return nil unless created_by_user_id
User.find(created_by_user_id)
end
|
#log_event_type? ⇒ Boolean
112
113
114
|
# File 'app/models/work_activity.rb', line 112
def log_event_type?
CHANGE_LOG_ACTIVITY_TYPES.include? activity_type
end
|
#message_event_type? ⇒ Boolean
108
109
110
|
# File 'app/models/work_activity.rb', line 108
def message_event_type?
MESSAGE_ACTIVITY_TYPES.include? activity_type
end
|
#notify_creator ⇒ Object
68
69
70
71
72
73
|
# File 'app/models/work_activity.rb', line 68
def notify_creator
if WorkActivityNotification.where(work_activity_id: id, user_id: work.created_by_user_id).count.zero?
WorkActivityNotification.create(work_activity_id: id, user_id: work.created_by_user_id)
end
end
|
#notify_curator ⇒ Object
58
59
60
61
62
63
64
65
66
|
# File 'app/models/work_activity.rb', line 58
def notify_curator
return if work.curator_user_id.nil?
if WorkActivityNotification.where(work_activity_id: id, user_id: work.curator_user_id).count.zero?
WorkActivityNotification.create(work_activity_id: id, user_id: work.curator_user_id)
end
end
|
#notify_group(groupid) ⇒ Object
87
88
89
90
91
92
93
94
95
96
|
# File 'app/models/work_activity.rb', line 87
def notify_group(groupid)
group = Group.where(code: groupid).first
if group.nil?
Rails.logger.info("Message #{id} for work #{work_id} referenced an non-existing user: #{groupid}")
else
group.administrators.each do |admin|
WorkActivityNotification.create(work_activity_id: id, user_id: admin.id)
end
end
end
|
#notify_users ⇒ Object
Log notifications for each of the users references on the activity
76
77
78
79
80
81
82
83
84
85
|
# File 'app/models/work_activity.rb', line 76
def notify_users
users_referenced.each do |uid|
user_id = User.where(uid:).first&.id
if user_id.nil?
notify_group(uid)
else
WorkActivityNotification.create(work_activity_id: id, user_id:)
end
end
end
|
#users_referenced ⇒ Object
Returns the uid of the users referenced on the activity (without the ‘@` symbol)
99
100
101
|
# File 'app/models/work_activity.rb', line 99
def users_referenced
message.scan(USER_REFERENCE).map { |at_uid| at_uid[1..-1] }
end
|