···9 validates_uniqueness_of :collection, scope: :user_id
1011 scope :unfinished, -> { where('(started_from IS NOT NULL) OR (last_completed IS NULL)') }
0000000000000000000000000012end
···9 validates_uniqueness_of :collection, scope: :user_id
1011 scope :unfinished, -> { where('(started_from IS NOT NULL) OR (last_completed IS NULL)') }
12+13+ IMPORT_END = Time.at(0)
14+15+ def imported_until
16+ return nil if cursor.nil? && last_completed.nil?
17+18+ groups = case collection
19+ when 'likes'
20+ [:likes]
21+ when 'reposts'
22+ [:reposts]
23+ when 'posts'
24+ [:pins, :quotes]
25+ end
26+27+ newest_queued_items = groups.map { |g| user.send(g).where(queue: :import).order(:time).last }
28+ newest_queued = newest_queued_items.compact.sort_by(&:time).last
29+30+ if newest_queued
31+ newest_queued.time
32+ elsif fetched_until
33+ fetched_until
34+ else
35+ IMPORT_END
36+ end
37+ end
38end
+3-16
app/models/user.rb
···50 end
5152 def imported_until
53- return nil unless self.imports.exists?
54-55- oldest_imported_items = []
56- started = false
5758- [:likes, :reposts, :pins, :quotes].each do |group|
59- if self.send(group).where(queue: :import).exists?
60- oldest_imported_items << self.send(group).where(queue: nil).order(:time).first
61- end
62- end
63-64- earliest_oldest = oldest_imported_items.compact.sort_by(&:time).last
65-66- if earliest_oldest
67- earliest_oldest.time
68- elsif self.imports.merge(Import.unfinished).exists?
69 nil
70 else
71- :end
72 end
73 end
74
···50 end
5152 def imported_until
53+ import_positions = self.imports.map(&:imported_until)
0005455+ if import_positions.empty? || import_positions.any? { |x| x.nil? }
000000000056 nil
57 else
58+ import_positions.sort.last
59 end
60 end
61
···10#
11# It's strongly recommended that you check this file into your version control system.
1213-ActiveRecord::Schema[7.2].define(version: 2025_09_23_180153) do
14 # These are extensions that must be enabled in order to support this database
15 enable_extension "plpgsql"
16···25 t.datetime "started_from"
26 t.datetime "last_completed"
27 t.string "collection", limit: 20, null: false
028 t.index ["user_id", "collection"], name: "index_imports_on_user_id_and_collection", unique: true
29 end
30
···10#
11# It's strongly recommended that you check this file into your version control system.
1213+ActiveRecord::Schema[7.2].define(version: 2025_10_27_134657) do
14 # These are extensions that must be enabled in order to support this database
15 enable_extension "plpgsql"
16···25 t.datetime "started_from"
26 t.datetime "last_completed"
27 t.string "collection", limit: 20, null: false
28+ t.datetime "fetched_until"
29 t.index ["user_id", "collection"], name: "index_imports_on_user_id_and_collection", unique: true
30 end
31