Start removing @db instance vars

This commit is contained in:
Netscape Navigator 2020-04-18 09:13:53 -05:00
parent d3ddc21de3
commit a24486e583
8 changed files with 57 additions and 56 deletions

19
dist/pigeon.rb vendored
View File

@ -145,6 +145,25 @@ module Pigeon
verify_key.verify(binary_signature, string)
end
def self.publish_draft(db, draft)
author = db.local_identity
mhash = author.multihash
template = MessageSerializer.new(draft)
depth = db.get_message_count_for(mhash)
draft.author = author
draft.depth = depth
draft.prev = db.get_message_by_depth(mhash, depth - 1)
draft.lipmaa = Helpers.lipmaa(depth)
unsigned = template.render_without_signature
draft.signature = author.sign(unsigned)
tokens = Lexer.tokenize_unsigned(unsigned, draft.signature)
message = Parser.parse(draft, tokens)[0]
db.discard_draft
message
end
def self.decode_multihash(string)
if string[SIG_RANGE] == SIG_FOOTER
return b32_decode(string.gsub(SIG_FOOTER, ""))

View File

@ -34,8 +34,8 @@ module Pigeon
def create_message(kind, params)
draft = Pigeon::Draft.new(kind: kind, db: self)
params.map { |(k, v)| draft[k] = v }
draft.publish
params.map { |(k, v)| draft.put(self, k, v) }
publish_draft(draft)
end
def create_bundle(file_path = DEFAULT_BUNDLE_PATH)
@ -68,6 +68,15 @@ module Pigeon
store.get_config(CURRENT_DRAFT)
end
def discard_draft
set_config(CURRENT_DRAFT, nil)
end
# Author a new message.
def publish_draft(draft)
Helpers.publish_draft(self, draft)
end
private
attr_reader :store

36
dist/pigeon/draft.rb vendored
View File

@ -2,17 +2,10 @@ require "digest"
module Pigeon
class Draft
attr_reader :signature, :prev, :lipmaa, :kind, :internal_id,
:depth, :body, :author
def discard
if @db.current_draft&.internal_id == @internal_id
@db.reset_current_draft
end
end
attr_accessor :signature, :prev, :lipmaa, :kind, :depth,
:body, :author
def initialize(kind:, body: {}, db:)
@db = db
@signature = Pigeon::NOTHING
@prev = Pigeon::NOTHING
@kind = kind
@ -20,14 +13,13 @@ module Pigeon
@body = body
@author = Pigeon::NOTHING
@lipmaa = Pigeon::NOTHING
@internal_id = SecureRandom.uuid
end
def [](key)
self.body[key]
end
def []=(key, value)
def put(db, key, value)
raise STRING_KEYS_ONLY unless key.is_a?(String)
case value[0]
@ -39,30 +31,10 @@ module Pigeon
# This might be a bad or good idea. Not sure yet.
self.body[key] = value.inspect
end
# TODO: You can't store a PStore in a PStore.
# This is terrible and should be fixed:
old_db = @db
@db = nil
old_db.save_draft(self)
@db = old_db
db.save_draft(self)
return self.body[key]
end
# Author a new message.
def publish
template = MessageSerializer.new(self)
@author = @db.local_identity
@depth = @db.get_message_count_for(author.multihash)
@prev = @db.get_message_by_depth(author.multihash, @depth - 1)
@lipmaa = Helpers.lipmaa(@depth)
unsigned = template.render_without_signature
@signature = author.sign(unsigned)
tokens = Lexer.tokenize_unsigned(unsigned, signature)
message = Parser.parse(@db, tokens)[0]
self.discard
message
end
def render_as_draft
DraftSerializer.new(self).render
end

View File

@ -19,6 +19,7 @@ module Pigeon
prev:,
signature:,
db:)
raise "Type mismatch: #{@db.class}" unless @db.is_a?(Pigeon::Database)
params = { author: RemoteIdentity.new(author),
kind: kind,
body: body,

View File

@ -94,7 +94,7 @@ module Pigeon
def append(key, raw_value = "")
v = (raw_value != "") ? raw_value : STDIN.read
draft = Pigeon::Draft.current
draft = db.current_draft
if draft
puts draft[key] = v
else
@ -106,7 +106,7 @@ module Pigeon
def show(message_id = "")
if message_id == ""
puts Pigeon::Draft.current.render_as_draft
puts db.current_draft.render_as_draft
else
bail("You must create a draft first")
end
@ -115,7 +115,7 @@ module Pigeon
desc "sign", "Commit current DRAFT to local feed."
def sign
puts Pigeon::Draft.current.publish.render
puts db.current_draft.publish.render
end
end

View File

@ -10,8 +10,8 @@ RSpec.describe Pigeon::Draft do
let(:message) do
message = db.create_draft(kind: "unit_test")
hash = db.put_blob(File.read("./logo.png"))
message["a"] = "bar"
message["b"] = hash
message.put(db, "a", "bar")
message.put(db, "b", hash)
message
end
@ -45,14 +45,15 @@ RSpec.describe Pigeon::Draft do
"b" => hash,
},
}
message["a"] = "bar"
message["b"] = hash
message.put(db, "a", "bar")
message.put(db, "b", hash)
expect(message["a"]).to eq(expectations.dig(:body, "a"))
expect(message["b"]).to eq(expectations.dig(:body, "b"))
expect(message.kind).to eq("unit_test")
expect(message.body).to eq(expectations.fetch(:body))
expectations.map do |k, v|
expect(Pigeon::Draft.current.send(k)).to eq(v)
left = db.current_draft.send(k)
expect(left).to eq(v)
end
end
end

View File

@ -123,8 +123,8 @@ RSpec.describe Pigeon::Lexer do
let(:message) do
draft = db.create_draft(kind: "unit_test")
draft["foo"] = "bar"
draft.publish
draft.put(db, "foo", "bar")
db.publish_draft(draft)
end
it "tokenizes a bundle" do

View File

@ -3,13 +3,13 @@ require "spec_helper"
RSpec.describe Pigeon::Message do
def create_draft(params)
draft = db.create_draft(kind: "unit_test")
params.each { |(k, v)| draft[k] = v }
params.each { |(k, v)| draft.put(db, k, v) }
draft
end
def create_message(params)
draft = create_draft(params)
draft.publish
db.publish_draft(draft)
end
let(:db) do
@ -30,13 +30,12 @@ RSpec.describe Pigeon::Message do
end
it "discards a draft after signing" do
expect(draft.internal_id).to eq(Pigeon::Draft.current.internal_id)
draft.publish
expect { Pigeon::Draft.current }.to raise_error("NO DRAFT FOUND")
db.publish_draft(draft)
expect { db.current_draft }.to raise_error("NO DRAFT FOUND")
end
it "creates a single message" do
message = draft.publish
message = db.publish_draft(draft)
expect(message.author.multihash).to eq(Pigeon::LocalIdentity.current.multihash)
expect(message.body).to eq(draft.body)
expect(message.depth).to eq(0)
@ -66,8 +65,8 @@ RSpec.describe Pigeon::Message do
all = []
0.upto(4) do |expected_depth|
draft1 = db.create_draft(kind: "unit_test")
draft1["description"] = "Message number #{expected_depth}"
message = draft1.publish
draft1.put(db, "description", "Message number #{expected_depth}")
message = db.publish_draft(draft1)
all.push(message)
expect(message.depth).to eq(expected_depth)
if expected_depth == 0
@ -153,8 +152,8 @@ RSpec.describe Pigeon::Message do
kind = SecureRandom.alphanumeric(8)
kind[rand(0...8)] = n
draft = db.create_draft(kind: kind)
draft["body"] = "empty"
boom = ->() { Pigeon::Lexer.tokenize(draft.publish.render) }
draft.put(db, "body", "empty")
boom = ->() { Pigeon::Lexer.tokenize(db.publish_draft(draft).render) }
expect(boom).to raise_error(Pigeon::Lexer::LexError)
end
end
@ -164,8 +163,8 @@ RSpec.describe Pigeon::Message do
draft = db.create_draft(kind: "unit_test")
key = SecureRandom.alphanumeric(8)
key[rand(0...8)] = n
draft[key] = "should crash"
boom = ->() { Pigeon::Lexer.tokenize(draft.publish.render) }
draft.put(db, key, "should crash")
boom = ->() { Pigeon::Lexer.tokenize(db.publish_draft(draft).render) }
expect(boom).to raise_error(Pigeon::Lexer::LexError)
end
end