Skip to content

Commit

Permalink
[webui] Add shorten payload in notifications
Browse files Browse the repository at this point in the history
- Add shorten_payload_if_necessary method in Notification model to be
sure that a payload is not bigger than the field size in database.
- This should fix openSUSE#4161.

Mobprogrammed with @mdeniz and @DavidKang.
  • Loading branch information
eduardoj committed Jan 4, 2018
1 parent 0f38245 commit dde06b2
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/api/app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,30 @@ class Notification < ApplicationRecord

serialize :event_payload, JSON

def initialize
super
shorten_payload_if_necessary
end

def event
@event ||= event_type.constantize.new(event_payload)
end

private

def shorten_payload_if_necessary
return if event.shortenable_key.nil? # If no shortenable_key is set then we cannot shorten the payload

# NOTE: ActiveSupport::JSON is used for serializing ActiveRecord models attributes
overflow_bytes = ActiveSupport::JSON.encode(event_payload).bytesize - 65535

return if overflow_bytes <= 0

# Shorten the payload so it will fit into the database column
shortenable_content = event_payload[event.shortenable_key.to_s]
new_size = shortenable_content.bytesize - overflow_bytes
event_payload[event.shortenable_key.to_s] = shortenable_content.mb_chars.limit(new_size)
end
end

# == Schema Information
Expand Down

0 comments on commit dde06b2

Please sign in to comment.