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



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



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

Returns:

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

Returns:

  • (Boolean)


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

def message_event_type?
  MESSAGE_ACTIVITY_TYPES.include? activity_type
end

#notify_creatorObject



68
69
70
71
72
73
# File 'app/models/work_activity.rb', line 68

def notify_creator
  # only send a notification to the creator if they are not already referenced in the message
  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_curatorObject



58
59
60
61
62
63
64
65
66
# File 'app/models/work_activity.rb', line 58

def notify_curator
  # do not send a notification if no curator is assigned
  return if work.curator_user_id.nil?

  # only send a notification to the curator if they are not already referenced in the message
  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_usersObject

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

#rendererObject



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

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)



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