From f88abf7fd29faa657bb7b446e3876f92a207a138 Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Sun, 8 Nov 2020 13:24:35 -0600 Subject: [PATCH] Bundle imports work :heavy_check_mark: --- project/encoders.go | 29 +++++++++++++++++++++++++---- project/import_bundle.go | 2 +- project/import_bundle_test.go | 3 --- project/message.go | 12 ++++++++++++ 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/project/encoders.go b/project/encoders.go index 7e16eef..fa9505d 100644 --- a/project/encoders.go +++ b/project/encoders.go @@ -2,6 +2,8 @@ package main import ( "encoding/base32" + "fmt" + "strings" ) var alphabet = "0123456789ABCDEFGHJKMNPQRSTVWXYZ" @@ -20,8 +22,27 @@ func encodeBlobMhash(sha256 []byte) string { return BlobSigil + B32Encode(sha256[:]) } -func encodeMessageMhash(b32signature string) string { - sigData := B32Decode(b32signature) - shaData := getSha256(sigData) - return MessageSigil + B32Encode(shaData) +type rawMessage struct { + mhash string + content string +} + +func formatMessage(message pigeonMessage) rawMessage { + var str strings.Builder + str.WriteString(fmt.Sprintf("author %s\n", message.author)) + str.WriteString(fmt.Sprintf("depth %d\n", message.depth)) + str.WriteString(fmt.Sprintf("kind %s\n", message.kind)) + str.WriteString(fmt.Sprintf("lipmaa %s\n", message.lipmaa)) + str.WriteString(fmt.Sprintf("prev %s\n\n", message.prev)) + for _, item := range message.body { + str.WriteString(fmt.Sprintf("%s:%s\n", item.key, item.value)) + } + str.WriteString(fmt.Sprintf("\nsignature %s", message.signature)) + content := str.String() + raw := []byte(content) + b32 := B32Encode(getSha256(raw)) + return rawMessage{ + content: content, + mhash: fmt.Sprintf("%s%s", MessageSigil, b32), + } } diff --git a/project/import_bundle.go b/project/import_bundle.go index 1fc5263..62eaf3d 100644 --- a/project/import_bundle.go +++ b/project/import_bundle.go @@ -14,7 +14,7 @@ func ingestOneMessage(msg pigeonMessage, blobIndex map[string]bool) { if getPeerStatus(msg.author) == following { tx, err1 := getDB().Begin() check(err1, "ingestOneMessage: Can't open DB: %s", err1) - mhash := encodeMessageMhash(msg.signature) + mhash := formatMessage(msg).mhash results, err2 := tx.Exec(insertMessageQuery, msg.author, msg.depth, diff --git a/project/import_bundle_test.go b/project/import_bundle_test.go index 84f7e13..2fa9a5a 100644 --- a/project/import_bundle_test.go +++ b/project/import_bundle_test.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "testing" ) @@ -17,8 +16,6 @@ func TestImportBundle(t *testing.T) { error := importBundle("../fixtures/has_blobs/messages.pgn") check(error, "Error while importing: %s", error) - fmt.Println("NEXT STEP: Assert that we have the following assets:") - for _, mhash := range files { if !blobExists(mhash) { t.Fatalf("### Can't find blob: %s", mhash) diff --git a/project/message.go b/project/message.go index 8960920..040ad22 100644 --- a/project/message.go +++ b/project/message.go @@ -1,5 +1,17 @@ package main +const messageExistsQuery = "SELECT count(*) FROM messages WHERE mhash = $1" + +// const messageExistsQuery = "SELECT count(*) FROM messages" + func messageExists(mhash string) bool { + var count int + db := getDB() + result := db.QueryRow(messageExistsQuery, mhash) + err := result.Scan(&count) + check(err, "Error in messageExists: %s", err) + if count == 1 { + return true + } return false }