• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_describe / 31a62f9e-3a5b-4033-a3e3-effd2d651737

pending completion
31a62f9e-3a5b-4033-a3e3-effd2d651737

Pull #962

circleci

Hector Correa
Fixes tests for S3 query service (and a couple of code duplications from a merge?)
Pull Request #962: Fetch pre-curation files from directly from AWS

35 of 35 new or added lines in 4 files covered. (100.0%)

1821 of 1864 relevant lines covered (97.69%)

149.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.66
/app/models/work_activity.rb
1
# frozen_string_literal: true
2

3
require_relative "../lib/diff_tools"
1✔
4

5
# rubocop:disable Metrics/ClassLength
6
class WorkActivity < ApplicationRecord
1✔
7
  MESSAGE = "COMMENT" # TODO: Migrate existing records to "MESSAGE"; then close #825.
1✔
8
  NOTIFICATION = "NOTIFICATION"
1✔
9
  MESSAGE_ACTIVITY_TYPES = [MESSAGE, NOTIFICATION].freeze
1✔
10

11
  CHANGES = "CHANGES"
1✔
12
  FILE_CHANGES = "FILE-CHANGES"
1✔
13
  PROVENANCE_NOTES = "PROVENANCE-NOTES"
1✔
14
  SYSTEM = "SYSTEM"
1✔
15
  DATACITE_ERROR = "DATACITE-ERROR"
1✔
16
  CHANGE_LOG_ACTIVITY_TYPES = [CHANGES, FILE_CHANGES, PROVENANCE_NOTES, SYSTEM, DATACITE_ERROR].freeze
1✔
17

18
  USER_REFERENCE = /@[\w]*/.freeze # e.g. @xy123
1✔
19

20
  include Rails.application.routes.url_helpers
1✔
21

22
  belongs_to :work
1✔
23
  has_many :work_activity_notifications, dependent: :destroy
1✔
24

25
  def self.add_work_activity(work_id, message, user_id, activity_type:, created_at: nil)
1✔
26
    activity = WorkActivity.new(
304✔
27
      work_id: work_id,
28
      activity_type: activity_type,
29
      message: message,
30
      created_by_user_id: user_id,
31
      created_at: created_at # If nil, will be set by activerecord at save.
32
    )
33
    activity.save!
304✔
34
    activity.notify_users
304✔
35
    activity
304✔
36
  end
37

38
  def self.activities_for_work(work_id, activity_types)
1✔
39
    where(work_id: work_id, activity_type: activity_types)
197✔
40
  end
41

42
  def self.messages_for_work(work_id)
1✔
43
    activities_for_work(work_id, MESSAGE_ACTIVITY_TYPES)
66✔
44
  end
45

46
  def self.changes_for_work(work_id)
1✔
47
    activities_for_work(work_id, CHANGE_LOG_ACTIVITY_TYPES)
67✔
48
  end
49

50
  # Log notifications for each of the users references on the activity
51
  def notify_users
1✔
52
    users_referenced.each do |uid|
308✔
53
      user_id = User.where(uid: uid).first&.id
163✔
54
      if user_id.nil?
163✔
55
        Rails.logger.info("Message #{id} for work #{work_id} referenced an non-existing user: #{uid}")
3✔
56
      else
57
        WorkActivityNotification.create(work_activity_id: id, user_id: user_id)
160✔
58
      end
59
    end
60
  end
61

62
  # Returns the `uid` of the users referenced on the activity (without the `@` symbol)
63
  def users_referenced
1✔
64
    message.scan(USER_REFERENCE).map { |at_uid| at_uid[1..-1] }
471✔
65
  end
66

67
  def created_by_user
1✔
68
    return nil unless created_by_user_id
137✔
69

70
    User.find(created_by_user_id)
136✔
71
  end
72

73
  def message_event_type?
1✔
74
    MESSAGE_ACTIVITY_TYPES.include? activity_type
×
75
  end
76

77
  def log_event_type?
1✔
78
    CHANGE_LOG_ACTIVITY_TYPES.include? activity_type
×
79
  end
80

81
  def to_html
1✔
82
    klass = if activity_type == CHANGES
66✔
83
              MetadataChanges
10✔
84
            elsif activity_type == FILE_CHANGES
56✔
85
              FileChanges
1✔
86
            elsif CHANGE_LOG_ACTIVITY_TYPES.include?(activity_type)
55✔
87
              OtherLogEvent
27✔
88
            else
89
              Message
28✔
90
            end
91
    renderer = klass.new(self)
66✔
92
    renderer.to_html
66✔
93
  end
94

95
  class Renderer
1✔
96
    def initialize(work_activity)
1✔
97
      @work_activity = work_activity
66✔
98
    end
99

100
    UNKNOWN_USER = "Unknown user outside the system"
1✔
101
    DATE_TIME_FORMAT = "%B %d, %Y %H:%M"
1✔
102

103
    def to_html
1✔
104
      title_html + "<span class='message-html'>#{body_html.chomp}</span>"
66✔
105
    end
106

107
    def created_by_user_html
1✔
108
      return UNKNOWN_USER unless @work_activity.created_by_user
38✔
109

110
      @work_activity.created_by_user.display_name_safe
38✔
111
    end
112

113
    def created_updated_html
1✔
114
      created = @work_activity.created_at.time.strftime(DATE_TIME_FORMAT)
66✔
115
      updated = @work_activity.updated_at.time.strftime(DATE_TIME_FORMAT)
66✔
116
      if created == updated
66✔
117
        created
56✔
118
      else
119
        "#{created} (backdated event created #{updated})"
10✔
120
      end
121
    end
122

123
    def title_html
1✔
124
      "<span class='activity-history-title'>#{created_updated_html} by #{created_by_user_html}</span>"
38✔
125
    end
126
  end
127

128
  class MetadataChanges < Renderer
1✔
129
    # Returns the message formatted to display _metadata_ changes that were logged as an activity
130
    def body_html
1✔
131
      changes = JSON.parse(@work_activity.message)
10✔
132

133
      changes.keys.map do |field|
10✔
134
        mapped = changes[field].map { |value| change_value_html(value) }
48✔
135
        "<details class='message-html'><summary class='show-changes'>#{field}</summary>#{mapped.join}</details>"
24✔
136
      end.join
137
    end
138

139
    def change_value_html(value)
1✔
140
      if value["action"] == "changed"
24✔
141
        DiffTools::SimpleDiff.new(value["from"], value["to"]).to_html
24✔
142
      else
143
        "old change"
×
144
      end
145
    end
146
  end
147

148
  class FileChanges < Renderer
1✔
149
    # Returns the message formatted to display _file_ changes that were logged as an activity
150
    def body_html
1✔
151
      changes = JSON.parse(@work_activity.message)
1✔
152
      changes_html = changes.map do |change|
1✔
153
        icon = if change["action"] == "deleted"
×
154
                 '<i class="bi bi-file-earmark-minus-fill file-deleted-icon"></i>'
×
155
               else
156
                 '<i class="bi bi-file-earmark-plus-fill file-added-icon"></i>'
×
157
               end
158
        "<tr><td>#{icon}</td><td>#{change['action']}</td> <td>#{change['filename']}</td>"
×
159
      end
160

161
      "<p><b>Files updated:</b></p><table>#{changes_html.join}</table>"
1✔
162
    end
163
  end
164

165
  class BaseMessage < Renderer
1✔
166
    # rubocop:disable Metrics/MethodLength
167
    def body_html
1✔
168
      # convert user references to user links
169
      text = @work_activity.message.gsub(USER_REFERENCE) do |at_uid|
55✔
170
        uid = at_uid[1..-1]
25✔
171
        user_info = UNKNOWN_USER
25✔
172

173
        if uid
25✔
174
          user = User.find_by(uid: uid)
25✔
175
          user_info = if user
25✔
176
                        user.display_name_safe
25✔
177
                      else
178
                        uid
×
179
                      end
180
        end
181

182
        "<a class='message-user-link' title='#{user_info}' href='#{@work_activity.users_path}/#{uid}'>#{at_uid}</a>"
25✔
183
      end
184

185
      # allow ``` for code blocks (Kramdown only supports ~~~)
186
      text = text.gsub("```", "~~~")
55✔
187
      Kramdown::Document.new(text).to_html
55✔
188
    end
189
    # rubocop:enable Metrics/MethodLength
190
  end
191

192
  class OtherLogEvent < BaseMessage
1✔
193
  end
194

195
  class Message < BaseMessage
1✔
196
    # Override the default:
197
    def created_by_user_html
1✔
198
      return UNKNOWN_USER unless @work_activity.created_by_user
28✔
199

200
      user = @work_activity.created_by_user
27✔
201
      "#{user.display_name_safe} (@#{user.uid})"
27✔
202
    end
203

204
    def title_html
1✔
205
      "<span class='activity-history-title'>#{created_by_user_html} at #{created_updated_html}</span>"
28✔
206
    end
207
  end
208
end
209
# rubocop:enable Metrics/ClassLength
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc