Class: WorkActivity

Inherits:
ApplicationRecord show all
Defined in:
app/models/work_activity.rb

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 =

e.g. @xy123

/@[\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: # If nil, will be set by activerecord at save.
  )
  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_userObject



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

Returns:

  • (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

Returns:

  • (Boolean)


109
110
111
# File 'app/models/work_activity.rb', line 109

def message_event_type?
  MESSAGE_ACTIVITY_TYPES.include? activity_type
end

#notify_creatorObject

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
  # Don't notify the creator if they are already referenced in the message
  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 no users are referenced in the message, notify the creator
  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_usersObject

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

#rendererObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/work_activity.rb', line 117

def renderer
  @renderer ||= begin
                  klass = if activity_type == CHANGES
                            MetadataChanges
                          elsif activity_type == FILE_CHANGES
                            FileChanges
                          elsif activity_type == MIGRATION_COMPLETE
                            Migration
                          elsif activity_type == PROVENANCE_NOTES
                            ProvenanceNote
                          elsif activity_type == EMBARGO
                            EmbargoEvent
                          elsif CHANGE_LOG_ACTIVITY_TYPES.include?(activity_type)
                            OtherLogEvent
                          else
                            Message
                          end
                  klass.new(self)

                end
end

#users_referencedObject

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