Class: WorkActivity

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

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: BaseMessage, 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"
CHANGE_LOG_ACTIVITY_TYPES =
[CHANGES, FILE_CHANGES, PROVENANCE_NOTES, SYSTEM, DATACITE_ERROR, MIGRATION_COMPLETE].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



40
41
42
# File 'app/models/work_activity.rb', line 40

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



27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/work_activity.rb', line 27

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
  activity
end

.changes_for_work(work_id) ⇒ Object



48
49
50
# File 'app/models/work_activity.rb', line 48

def self.changes_for_work(work_id)
  activities_for_work(work_id, CHANGE_LOG_ACTIVITY_TYPES)
end

.messages_for_work(work_id) ⇒ Object



44
45
46
# File 'app/models/work_activity.rb', line 44

def self.messages_for_work(work_id)
  activities_for_work(work_id, MESSAGE_ACTIVITY_TYPES)
end

Instance Method Details

#created_by_userObject



80
81
82
83
# File 'app/models/work_activity.rb', line 80

def created_by_user
  return nil unless created_by_user_id
  User.find(created_by_user_id)
end

#log_event_type?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/models/work_activity.rb', line 89

def log_event_type?
  CHANGE_LOG_ACTIVITY_TYPES.include? activity_type
end

#message_event_type?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'app/models/work_activity.rb', line 85

def message_event_type?
  MESSAGE_ACTIVITY_TYPES.include? activity_type
end

#notify_group(groupid) ⇒ Object



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

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



53
54
55
56
57
58
59
60
61
62
# File 'app/models/work_activity.rb', line 53

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/work_activity.rb', line 93

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 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)



76
77
78
# File 'app/models/work_activity.rb', line 76

def users_referenced
  message.scan(USER_REFERENCE).map { |at_uid| at_uid[1..-1] }
end