# frozen_string_literal: true module Tobox module Plugins module Inbox def self.configure(conf) conf.config[:inbox_table] = :inbox conf.config[:inbox_column] = :unique_id end module FetcherMethods def initialize(_, configuration) super inbox_table = configuration[:inbox_table] @inbox_ds = @db[inbox_table] @inbox_column = configuration[:inbox_column] end private def prepare_events(events) try_insert_inbox(events) do |deduped_events| super(deduped_events) end end def try_insert_inbox(events) inboxed = nil if @inbox_ds.respond_to?(:supports_insert_conflict?) && @inbox_ds.supports_insert_conflict? 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) if ret inboxed = @inbox_ds.where(@inbox_column => events.map do |ev| ev[@inbox_column] end).select_map(@inbox_column) end end else inboxed = [] events.each do |event| @inbox_ds.insert(@inbox_column => event[@inbox_column]) inboxed << event[@inbox_column] rescue Sequel::UniqueConstraintViolation # ignore end return events if inboxed.empty? end return events if inboxed && inboxed.empty? yield events.select { |ev| inboxed.include?(ev[@inbox_column]) } events end end end register_plugin :inbox, Inbox end end