Skip to content

Commit

Permalink
Add prev_id tag
Browse files Browse the repository at this point in the history
  • Loading branch information
jasl committed Dec 5, 2024
1 parent 10fb6a6 commit b0ecb78
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GIT
remote: https://github.com/rails/rails.git
revision: 95deab7b439abba23fdc4bd659116dab5dbe2606
revision: 44673c80bde08f17659a709279972827ffa0ae64
branch: main
specs:
actioncable (8.1.0.alpha)
Expand Down Expand Up @@ -292,7 +292,7 @@ GEM
activejob (>= 7.2)
activerecord (>= 7.2)
railties (>= 7.2)
solid_queue (1.0.2)
solid_queue (1.1.0)
activejob (>= 7.1)
activerecord (>= 7.1)
concurrent-ruby (>= 1.3.1)
Expand Down
6 changes: 5 additions & 1 deletion app/models/conversation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class Conversation < ApplicationRecord
dependent: :restrict_with_exception

def latest_event
events.order(id: :desc).first
if latest_eid.blank?
nil
else
Event.find(latest_eid)
end
end
end
20 changes: 16 additions & 4 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ class Event < ApplicationRecord
belongs_to :conversation, foreign_key: %i[pubkey session], required: true, autosave: true
has_one :merkle_node, dependent: :restrict_with_exception

after_create :add_to_merkle_tree

# A publisher must not send events in the same time which makes harder to sort them.
validates :created_at,
presence: true,
presence: true
validates :created_at,
comparison: {
greater_than_or_equal_to: ->(current) {
current.conversation&.latest_event_created_at || 0
}
}
},
if: :new_record?

validates :session,
presence: true,
Expand All @@ -36,6 +36,15 @@ class Event < ApplicationRecord
format: { with: /\A\h+\z/ },
allow_nil: true

validate if: :new_record? do
prev_id = tags.find { |tag| tag[0] == "prev_id" }&.[](1)
next if prev_id.blank?

if self.connection.latest_eid != prev_id
errors.add :prev_id, :invalid
end
end

before_validation on: :create do
self.session = tags.find { |tag| tag[0] == "s" }&.[](1)
self.topic = tags.find { |tag| tag[0] == "t" }&.[](1)
Expand All @@ -48,10 +57,13 @@ class Event < ApplicationRecord
end

after_validation on: :create do
self.conversation.latest_eid = eid
self.conversation.latest_event_created_at = created_at
self.conversation.events_count += 1
end

after_create :add_to_merkle_tree

def merkle_tree_hash
Digest::Keccak256.digest([pubkey, topic, session].join)
end
Expand Down
6 changes: 4 additions & 2 deletions db/migrate/20240325200100_create_conversations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ def change
t.string :pubkey, null: false
t.string :session, null: false

t.integer :lock_version
t.datetime :latest_event_created_at
t.integer :events_count

t.string :latest_eid
t.datetime :latest_event_created_at

t.integer :lock_version
t.timestamps
end
end
Expand Down
5 changes: 3 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b0ecb78

Please sign in to comment.