TODO: Create message index

This commit is contained in:
Netscape Navigator 2019-12-29 20:16:42 -06:00
parent 61460a4ae5
commit f68e004e53
5 changed files with 30 additions and 9 deletions

3
dist/pigeon.rb vendored
View File

@ -39,6 +39,9 @@ module Pigeon
IDENTITY_SIGIL = "@"
STRING_SIGIL = "\""
BLOB_FOOTER = ".sha256"
# Error messages
PREV_REQUIRES_SAVE = "Can't fetch `prev` on unsaved messages"
end
require_relative File.join("pigeon", "key_pair.rb")

View File

@ -2,22 +2,19 @@ require "digest"
module Pigeon
class Message
attr_reader :author, :kind, :prev, :body, :signature
attr_reader :author, :kind, :body, :signature
def self.create(kind:, prev: nil, body: {})
def self.create(kind:, body: {})
self.new(author: KeyPair.current.public_key,
kind: kind,
prev: prev,
body: body).save
end
def initialize(author:, kind:, prev: nil, body: {})
def initialize(author:, kind:, body: {})
@author = author
@kind = kind
@prev = prev
@body = body
@depth = nil # Set at save time
@prev = previous_message ? previous_message.signature : EMPTY_MESSAGE
end
def append(key, value)
@ -51,6 +48,7 @@ module Pigeon
def sign
@signature = calculate_signature
@depth = Pigeon::Storage.current.message_count
@saved = true
self.freeze
Pigeon::Storage.current.save_message(self)
Pigeon::Message.reset_current
@ -65,6 +63,18 @@ module Pigeon
calculate_depth
end
def prev
if @saved
previous_message
else
raise Pigeon::PREV_REQUIRES_SAVE
end
end
def saved?
@saved == true
end
private
def calculate_signature
@ -74,6 +84,8 @@ module Pigeon
end
def previous_message
raise "TODO - I need to create a `Pigeon::Index` class or something. " \
"need a way to index messages by: signature, depth"
# if @depth.nil?
# raise "Could not load @depth"
# end
@ -91,6 +103,7 @@ module Pigeon
end
def calculate_depth
raise "Don't do this- read from the index. Also, crash if message is not saved."
@depth || Pigeon::Storage.current.message_count
end

View File

@ -25,7 +25,7 @@ module Pigeon
body = message.body
kind = message.kind
depth = message.depth || "NONE"
prev = message.prev || "NONE"
prev = message.saved? ? message.prev : "NONE"
signature = message.signature || "NONE"
ERB.new(template).result(binding)

View File

@ -20,6 +20,7 @@ RSpec.describe Pigeon::Message do
expect(message2.author).to eq(Pigeon::KeyPair.current.public_key)
expect(message2.kind).to eq("unit_test")
binding.pry
expect(message2.prev).to eq(m1)
expect(message2.body).to eq("expected_sequence" => "1")
expect(message2.depth).to eq(1)
@ -65,6 +66,8 @@ RSpec.describe Pigeon::Message do
expect(message.kind).to eq("unit_test")
expect(message.body).to eq(expectations.fetch(:body))
expect(message.depth).to eq(0)
message.sign
binding.pry
expect(message.prev).to eq(Pigeon::EMPTY_MESSAGE)
expectations.map do |k, v|
expect(Pigeon::Message.current.send(k)).to eq(v)

View File

@ -1,7 +1,8 @@
require "spec_helper"
RSpec.describe Pigeon::Template do
MessageShim = Struct.new(:author, :body, :kind, :depth, :prev, :signature)
SHIM_ATTRS = [:author, :body, :kind, :depth, :prev, :signature, :saved?]
MessageShim = Struct.new(*SHIM_ATTRS)
TOP_HALF = ["author FAKE_AUTHOR",
"\nkind FAKE_KIND",
"\nprev NONE",
@ -16,7 +17,8 @@ RSpec.describe Pigeon::Template do
"FAKE_KIND",
23,
nil,
"XYZ.sig.sha256"]
"XYZ.sig.sha256",
false]
message = MessageShim.new(*args)
template = Pigeon::Template.new(message)
expect(template.render).to eq(EXPECTED_DRAFT)