lib/tobox/plugins/inbox.rb in tobox-0.5.1 vs lib/tobox/plugins/inbox.rb in tobox-0.5.2
- old
+ new
@@ -18,27 +18,50 @@
@inbox_column = configuration[:inbox_column]
end
private
- def prepare_event(event, &blk)
- try_insert_inbox(event) { super }
+ def prepare_events(events)
+ try_insert_inbox(events) do |deduped_events|
+ super(deduped_events)
+ end
end
- def try_insert_inbox(event)
+ def try_insert_inbox(events)
+ inboxed = nil
+
if @inbox_ds.respond_to?(:supports_insert_conflict?) && @inbox_ds.supports_insert_conflict?
- ret = @inbox_ds.insert_conflict.insert(@inbox_column => event[@inbox_column])
+ if @inbox_ds.supports_returning?(:insert)
+ inboxed = @inbox_ds.insert_conflict
+ .returning(@inbox_column)
+ .multi_insert(events.map { |event| { @inbox_column => event[@inbox_column] } })
+ else
+ ret = @inbox_ds.insert_conflict.multi_insert(events.map do |event|
+ { @inbox_column => event[@inbox_column] }
+ end)
- return event unless ret
+ if ret
+ inboxed = @inbox_ds.where(@inbox_column => events.map do |ev|
+ ev[@inbox_column]
+ end).select_map(@inbox_column)
+ end
+ end
else
- begin
+ inboxed = []
+ events.each do |event|
@inbox_ds.insert(@inbox_column => event[@inbox_column])
+ inboxed << event[@inbox_column]
rescue Sequel::UniqueConstraintViolation
- return event
+ # ignore
end
+ return events if inboxed.empty?
end
- yield
+ return events if inboxed && inboxed.empty?
+
+ yield events.select { |ev| inboxed.include?(ev[@inbox_column]) }
+
+ events
end
end
end
register_plugin :inbox, Inbox
end