diff options
author | Joop Kiefte <ikojba@gmail.com> | 2021-12-14 22:36:39 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-14 22:36:39 +0000 |
commit | d3b4f55a5cf59da007b2332fb2a5f15fd64d2726 (patch) | |
tree | adf780ec2b0a96a15fe4fe6d1f4b86cee28ad311 | |
parent | 4bde5cf47840d65b8e523b0f8acb8472af12f998 (diff) |
Add pubsub publishing functionality. It should be possible to test now, but it's crazy arcane atm
-rw-r--r-- | sync.go | 53 |
1 files changed, 51 insertions, 2 deletions
@@ -2,8 +2,10 @@ package main import ( "fmt" + "strings" "git.kiefte.eu/lapingvino/infodump/message" + shell "github.com/ipfs/go-ipfs-api" ) // A menu for the several parts of Sync: @@ -50,8 +52,16 @@ func ReadMessagesFromDatabase() { // ReadMessagesFromNetwork reads the messages from the IPFS network and adds them to LocalMessages func ReadMessagesFromNetwork() { - // Get the messages from the IPFS network - // TODO: Implement this + // Ask for a CID from the user and get the messages from the IPFS network + // Add the messages to LocalMessages + fmt.Println("Enter the CID of the messages to read: ") + cid := Readline() + messages, err := message.MessagesFromIPFS(cid) + if err != nil { + fmt.Println(err) + } else { + LocalMessages.AddMany(messages) + } } // WriteMessagesToNetwork writes the messages in LocalMessages to the IPFS network @@ -63,4 +73,43 @@ func WriteMessagesToNetwork() { } else { fmt.Println("Messages synced to IPFS: ", cid) } + // Split each message on spaces and map the resulting strings + // to the message stamp + messages := make(map[string][]*message.Message) + LocalMessages.Each(func(m *message.Message) { + tags := strings.Split(m.Message, " ") + for _, tag := range tags { + if messages[tag] == nil { + messages[tag] = []*message.Message{} + } + messages[tag] = append(messages[tag], m) + } + }) + // Create a new set of messages per tag and send them over PubSub + // Create an IPFS shell to later publish the messages to via PubSub + // Per message, invoke Add on the tags map entry + ipfs := shell.NewShell(message.IPFSGateway) + tags := make(map[string]*message.Messages) + for tag, messages := range messages { + if tags[tag] == nil { + tags[tag] = &message.Messages{} + } + for _, m := range messages { + tags[tag].Add(m) + } + // Publish the messages to the IPFS network and get a CID + cid, err := tags[tag].AddToIPFS() + if err != nil { + fmt.Println(err) + } else { + fmt.Println("Messages synced to IPFS: ", cid) + } + // Publish the messages via PubSub on the IPFS shell + err = ipfs.PubSubPublish(tag, cid) + if err != nil { + fmt.Println(err) + } else { + fmt.Println("Messages published to the rest of the network for tag: ", tag) + } + } } |