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
45
46
47
|
# File 'app/models/work_activity.rb', line 45
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
|
# 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
end
activity
end
|
.changes_for_work(work_id) ⇒ Object
53
54
55
|
# File 'app/models/work_activity.rb', line 53
def self.changes_for_work(work_id)
activities_for_work(work_id, CHANGE_LOG_ACTIVITY_TYPES)
end
|
.messages_for_work(work_id) ⇒ Object
49
50
51
|
# File 'app/models/work_activity.rb', line 49
def self.messages_for_work(work_id)
activities_for_work(work_id, MESSAGE_ACTIVITY_TYPES)
end
|
Instance Method Details
#created_by_user ⇒ Object
104
105
106
107
|
# File 'app/models/work_activity.rb', line 104
def created_by_user
return nil unless created_by_user_id
User.find(created_by_user_id)
end
|
#log_event_type? ⇒ Boolean
113
114
115
|
# File 'app/models/work_activity.rb', line 113
def log_event_type?
CHANGE_LOG_ACTIVITY_TYPES.include? activity_type
end
|
#message_event_type? ⇒ Boolean
109
110
111
|
# File 'app/models/work_activity.rb', line 109
def message_event_type?
MESSAGE_ACTIVITY_TYPES.include? activity_type
end
|
#notify_creator ⇒ Object
notify the creator of the work whenever a message activity type is created
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'app/models/work_activity.rb', line 58
def notify_creator
users_referenced.each do |uid|
user_id = User.where(uid:).first&.id
if user_id.nil?
Rails.logger.info("Message #{id} for work #{work_id} referenced an non-existing user: #{uid}")
elsif user_id == work.created_by_user_id
Rails.logger.info("Skipping notification for creator #{work.created_by_user_id} of work #{work_id} because they are already referenced in the message")
else
WorkActivityNotification.create(work_activity_id: id, user_id: work.created_by_user_id)
end
end
if users_referenced.empty?
WorkActivityNotification.create(work_activity_id: id, user_id: work.created_by_user_id)
end
end
|
#notify_group(groupid) ⇒ Object
88
89
90
91
92
93
94
95
96
97
|
# File 'app/models/work_activity.rb', line 88
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
77
78
79
80
81
82
83
84
85
86
|
# File 'app/models/work_activity.rb', line 77
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)
100
101
102
|
# File 'app/models/work_activity.rb', line 100
def users_referenced
message.scan(USER_REFERENCE).map { |at_uid| at_uid[1..-1] }
end
|