From a24486e5833fe21798820733d19d26d3e02d612d Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Sat, 18 Apr 2020 09:13:53 -0500 Subject: [PATCH] Start removing @db instance vars --- dist/pigeon.rb | 19 +++++++++++++++++++ dist/pigeon/database.rb | 13 +++++++++++-- dist/pigeon/draft.rb | 36 ++++-------------------------------- dist/pigeon/message.rb | 1 + pigeon-cli | 6 +++--- spec/pigeon/draft_spec.rb | 11 ++++++----- spec/pigeon/lexer_spec.rb | 4 ++-- spec/pigeon/message_spec.rb | 23 +++++++++++------------ 8 files changed, 57 insertions(+), 56 deletions(-) diff --git a/dist/pigeon.rb b/dist/pigeon.rb index 40e363d..bbcf19d 100644 --- a/dist/pigeon.rb +++ b/dist/pigeon.rb @@ -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, "")) diff --git a/dist/pigeon/database.rb b/dist/pigeon/database.rb index 6fad719..fd6417f 100644 --- a/dist/pigeon/database.rb +++ b/dist/pigeon/database.rb @@ -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 diff --git a/dist/pigeon/draft.rb b/dist/pigeon/draft.rb index 8c24807..5dbe021 100644 --- a/dist/pigeon/draft.rb +++ b/dist/pigeon/draft.rb @@ -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 diff --git a/dist/pigeon/message.rb b/dist/pigeon/message.rb index f2627d4..20e8c21 100644 --- a/dist/pigeon/message.rb +++ b/dist/pigeon/message.rb @@ -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, diff --git a/pigeon-cli b/pigeon-cli index fef9e58..5ee9de1 100755 --- a/pigeon-cli +++ b/pigeon-cli @@ -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 diff --git a/spec/pigeon/draft_spec.rb b/spec/pigeon/draft_spec.rb index eb5bd9a..a442b92 100644 --- a/spec/pigeon/draft_spec.rb +++ b/spec/pigeon/draft_spec.rb @@ -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 diff --git a/spec/pigeon/lexer_spec.rb b/spec/pigeon/lexer_spec.rb index 1a930d8..46f1d1f 100644 --- a/spec/pigeon/lexer_spec.rb +++ b/spec/pigeon/lexer_spec.rb @@ -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 diff --git a/spec/pigeon/message_spec.rb b/spec/pigeon/message_spec.rb index 323214c..0eb0eec 100644 --- a/spec/pigeon/message_spec.rb +++ b/spec/pigeon/message_spec.rb @@ -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