Fix one test, add another

This commit is contained in:
Netscape Navigator 2019-12-10 22:05:54 -06:00
parent dab113961a
commit 52b902b935
2 changed files with 16 additions and 5 deletions

View File

@ -2,7 +2,7 @@ require "digest"
module Pigeon
class Message
attr_reader :author, :kind, :prev, :body, :depth, :signature
attr_reader :author, :kind, :prev, :body, :signature
def self.create(kind:, prev: nil, body: {})
self.new(author: KeyPair.current.public_key,
@ -16,7 +16,7 @@ module Pigeon
@kind = kind
@prev = prev
@body = body
@depth = calculate_depth
@depth = nil # Set at save time
@prev = previous_message ? previous_message.signature : EMPTY_MESSAGE
end
@ -50,6 +50,7 @@ module Pigeon
def sign
@signature = calculate_signature
@depth = Pigeon::Storage.current.message_count
self.freeze
Pigeon::Storage.current.save_message(self)
Pigeon::Message.reset_current
@ -60,6 +61,10 @@ module Pigeon
Template.new(self).render
end
def depth
calculate_depth
end
private
def calculate_signature
@ -86,8 +91,7 @@ module Pigeon
end
def calculate_depth
# Dir[OUTBOX_PATH].count
0
@depth || Pigeon::Storage.current.message_count
end
def message_id # I need this to calculate `prev`.

View File

@ -6,10 +6,17 @@ module Pigeon
@current ||= self.new
end
def message_count
store.transaction do
store[MESG_NS] ||= {}
store[MESG_NS].count
end
end
def save_message(msg)
store.transaction do
store[MESG_NS] ||= {}
store[MESG_NS][msg.signature] = msg
store[MESG_NS][msg.depth || -100] = msg
end
end