@@ -52,7 +52,7 @@ def filter_and_prepare_notifications
5252 valid_notifications << prepared_attributes if prepared_attributes
5353 end
5454
55- if skipped_count > 0
55+ if skipped_count . positive?
5656 Rails . logger . warn "Skipped #{ skipped_count } notifications with invalid or missing required data"
5757 end
5858
@@ -64,7 +64,7 @@ def prepare_notification_attributes(notification_data)
6464
6565 attributes [ 'user_id' ] = user . id
6666
67- attributes [ 'created_at' ] = Time . current unless attributes [ 'created_at' ] . present ?
67+ attributes [ 'created_at' ] = Time . current if attributes [ 'created_at' ] . blank ?
6868
6969 attributes [ 'updated_at' ] = Time . current
7070
@@ -78,15 +78,7 @@ def prepare_notification_attributes(notification_data)
7878 def filter_existing_notifications ( notifications )
7979 return notifications if notifications . empty?
8080
81- existing_notifications_lookup = { }
82- user . notifications . select ( :title , :content , :created_at , :kind ) . each do |notification |
83- primary_key = [ notification . title . strip , notification . content . strip ]
84-
85- exact_key = [ notification . title . strip , notification . content . strip , normalize_timestamp ( notification . created_at ) ]
86-
87- existing_notifications_lookup [ primary_key ] = true
88- existing_notifications_lookup [ exact_key ] = true
89- end
81+ lookup = build_existing_notifications_lookup
9082
9183 notifications . reject do |notification |
9284 title = notification [ :title ] &.strip
@@ -95,7 +87,7 @@ def filter_existing_notifications(notifications)
9587 primary_key = [ title , content ]
9688 exact_key = [ title , content , normalize_timestamp ( notification [ :created_at ] ) ]
9789
98- if existing_notifications_lookup [ primary_key ] || existing_notifications_lookup [ exact_key ]
90+ if lookup [ primary_key ] || lookup [ exact_key ]
9991 Rails . logger . debug "Notification already exists: #{ notification [ :title ] } "
10092 true
10193 else
@@ -104,6 +96,18 @@ def filter_existing_notifications(notifications)
10496 end
10597 end
10698
99+ def build_existing_notifications_lookup
100+ lookup = { }
101+ user . notifications . select ( :title , :content , :created_at , :kind ) . each do |notification |
102+ title = notification . title . strip
103+ content = notification . content . strip
104+
105+ lookup [ [ title , content ] ] = true
106+ lookup [ [ title , content , normalize_timestamp ( notification . created_at ) ] ] = true
107+ end
108+ lookup
109+ end
110+
107111 def normalize_timestamp ( timestamp )
108112 case timestamp
109113 when String then Time . parse ( timestamp ) . utc . to_i
@@ -131,7 +135,9 @@ def bulk_import_notifications(notifications)
131135 batch_created = result . count
132136 total_created += batch_created
133137
134- Rails . logger . debug "Processed batch of #{ batch . size } notifications, created #{ batch_created } , total created: #{ total_created } "
138+ Rails . logger . debug (
139+ "Processed batch of #{ batch . size } notifications, created #{ batch_created } , total: #{ total_created } "
140+ )
135141 rescue StandardError => e
136142 Rails . logger . error "Failed to process notification batch: #{ e . message } "
137143 Rails . logger . error "Batch size: #{ batch . size } "
@@ -144,12 +150,12 @@ def bulk_import_notifications(notifications)
144150 def valid_notification_data? ( notification_data )
145151 return false unless notification_data . is_a? ( Hash )
146152
147- unless notification_data [ 'title' ] . present ?
153+ if notification_data [ 'title' ] . blank ?
148154 Rails . logger . error "Failed to create notification: Validation failed: Title can't be blank"
149155 return false
150156 end
151157
152- unless notification_data [ 'content' ] . present ?
158+ if notification_data [ 'content' ] . blank ?
153159 Rails . logger . error "Failed to create notification: Validation failed: Content can't be blank"
154160 return false
155161 end
0 commit comments