aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 8915d2fc9d9b777361ba3da5457c3fa41281d105 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Infodump is a commandline social network that works over IPFS and communicates through PubSub
package main

import (
	"bufio"
	"fmt"
	"os"

	"git.kiefte.eu/lapingvino/infodump/message"
	_ "modernc.org/sqlite"
)

var LocalMessages = message.Messages{}

// Readline reads from a buffered stdin and returns the line
func Readline() string {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')
	return line
}

func main() {
	// Set message IPFS client to use the local IPFS node
	message.IPFSGateway = "http://localhost:5001"

	// Run a loop and present a menu to the user to read messages, write messages or quit the program
	for {
		// Present the user with a menu
		fmt.Println("Welcome to Infodump")
		fmt.Println("1. Read Messages")
		fmt.Println("2. Write Messages")
		fmt.Println("3. Sync messages")
		fmt.Println("4. Quit")
		fmt.Println("Enter your choice: ")
		var choice int
		fmt.Scanln(&choice)
		switch choice {
		case 1:
			// Read the messages from PubSub topic "OLN"
			fmt.Println("Reading messages...")
			// TODO: Read messages from PubSub topic "OLN"
		case 2:
			// Write Messages
			fmt.Println("Writing Messages")
			// Get a message and an urgency from the user.
			// The urgency is used to set the strength of the Proof of Work
			fmt.Println("Enter a message: ")
			m := Readline()
			fmt.Println("Enter an urgency (higher is stronger but takes longer to produce): ")
			var urgency int
			fmt.Scanln(&urgency)
			// Create a Message object
			msg := message.New(m, urgency)
			// Add the message to LocalMessages
			LocalMessages.Add(msg)
		case 3:
			// Sync messages
			fmt.Println("Syncing messages...")
			// Upload the LocalMessages to IPFS
			hash, err := LocalMessages.AddToIPFS()
			if err != nil {
				fmt.Println(err)
			}
			// TODO: Publish the hash to the PubSub topic "OLN"
			fmt.Println("Hash: ", hash)
		case 4:
			// Quit
			fmt.Println("Quitting")
			os.Exit(0)
		default:
			fmt.Println("Invalid Choice")
		}
	}
}