Bundle imports work ✔️
This commit is contained in:
parent
f3580b8c8c
commit
f88abf7fd2
|
@ -2,6 +2,8 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base32"
|
"encoding/base32"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var alphabet = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
|
var alphabet = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
|
||||||
|
@ -20,8 +22,27 @@ func encodeBlobMhash(sha256 []byte) string {
|
||||||
return BlobSigil + B32Encode(sha256[:])
|
return BlobSigil + B32Encode(sha256[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
func encodeMessageMhash(b32signature string) string {
|
type rawMessage struct {
|
||||||
sigData := B32Decode(b32signature)
|
mhash string
|
||||||
shaData := getSha256(sigData)
|
content string
|
||||||
return MessageSigil + B32Encode(shaData)
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ func ingestOneMessage(msg pigeonMessage, blobIndex map[string]bool) {
|
||||||
if getPeerStatus(msg.author) == following {
|
if getPeerStatus(msg.author) == following {
|
||||||
tx, err1 := getDB().Begin()
|
tx, err1 := getDB().Begin()
|
||||||
check(err1, "ingestOneMessage: Can't open DB: %s", err1)
|
check(err1, "ingestOneMessage: Can't open DB: %s", err1)
|
||||||
mhash := encodeMessageMhash(msg.signature)
|
mhash := formatMessage(msg).mhash
|
||||||
results, err2 := tx.Exec(insertMessageQuery,
|
results, err2 := tx.Exec(insertMessageQuery,
|
||||||
msg.author,
|
msg.author,
|
||||||
msg.depth,
|
msg.depth,
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,8 +16,6 @@ func TestImportBundle(t *testing.T) {
|
||||||
error := importBundle("../fixtures/has_blobs/messages.pgn")
|
error := importBundle("../fixtures/has_blobs/messages.pgn")
|
||||||
check(error, "Error while importing: %s", error)
|
check(error, "Error while importing: %s", error)
|
||||||
|
|
||||||
fmt.Println("NEXT STEP: Assert that we have the following assets:")
|
|
||||||
|
|
||||||
for _, mhash := range files {
|
for _, mhash := range files {
|
||||||
if !blobExists(mhash) {
|
if !blobExists(mhash) {
|
||||||
t.Fatalf("### Can't find blob: %s", mhash)
|
t.Fatalf("### Can't find blob: %s", mhash)
|
||||||
|
|
|
@ -1,5 +1,17 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
const messageExistsQuery = "SELECT count(*) FROM messages WHERE mhash = $1"
|
||||||
|
|
||||||
|
// const messageExistsQuery = "SELECT count(*) FROM messages"
|
||||||
|
|
||||||
func messageExists(mhash string) bool {
|
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
|
return false
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue