-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
621 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.exe | ||
/secret.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/andyleap/go-ssb" | ||
"github.com/andyleap/go-ssb/cmd/sbot/rpc" | ||
|
||
r "net/rpc" | ||
|
||
"github.com/andyleap/boltinspect" | ||
) | ||
|
||
var datastore *ssb.DataStore | ||
|
||
func main() { | ||
datastore, _ = ssb.OpenDataStore("feeds.db", "secret.json") | ||
|
||
bi := boltinspect.New(datastore.DB()) | ||
|
||
http.HandleFunc("/bolt", bi.InspectEndpoint) | ||
|
||
http.HandleFunc("/", Index) | ||
|
||
go http.ListenAndServe(":9823", nil) | ||
|
||
r.Register(&Gossip{datastore}) | ||
r.Register(&Feed{datastore}) | ||
|
||
l, _ := net.Listen("tcp", ":9822") | ||
|
||
r.Accept(l) | ||
|
||
for { | ||
} | ||
} | ||
|
||
type Gossip struct { | ||
ds *ssb.DataStore | ||
} | ||
|
||
func (g *Gossip) AddPub(req rpc.AddPubReq, res *rpc.AddPubRes) error { | ||
g.ds.AddPub(ssb.PubData{ | ||
Host: req.Host, | ||
Port: req.Port, | ||
Link: ssb.Ref(req.PubKey), | ||
}) | ||
return nil | ||
} | ||
|
||
type Feed struct { | ||
ds *ssb.DataStore | ||
} | ||
|
||
func (f *Feed) Post(req rpc.PostReq, res *rpc.PostRes) error { | ||
if req.Feed == "" { | ||
req.Feed = string(f.ds.PrimaryRef) | ||
} | ||
feed := f.ds.GetFeed(ssb.Ref(req.Feed)) | ||
|
||
post := &ssb.Post{} | ||
|
||
post.Text = req.Text | ||
post.Channel = req.Channel | ||
post.Branch = ssb.Ref(req.Branch) | ||
post.Root = ssb.Ref(req.Root) | ||
post.Type = "post" | ||
|
||
content, _ := json.Marshal(post) | ||
|
||
m := &ssb.Message{ | ||
Author: feed.ID, | ||
Timestamp: float64(time.Now().UnixNano() / int64(time.Millisecond)), | ||
Hash: "sha256", | ||
Content: content, | ||
Sequence: 1, | ||
} | ||
|
||
if l := feed.Latest(); l != nil { | ||
key := l.Key() | ||
m.Previous = &key | ||
m.Sequence = l.Sequence + 1 | ||
for m.Timestamp <= l.Timestamp { | ||
m.Timestamp += 0.01 | ||
} | ||
} | ||
|
||
signer := f.ds.Keys[feed.ID] | ||
if signer == nil { | ||
return nil | ||
} | ||
sm := m.Sign(signer) | ||
|
||
err := feed.AddMessage(sm) | ||
if err != nil { | ||
log.Println(err) | ||
} else { | ||
log.Println("Message ", sm, " posted to feed ", feed.ID) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (f *Feed) Follow(req rpc.FollowReq, res *rpc.FollowRes) error { | ||
if req.Feed == "" { | ||
req.Feed = string(f.ds.PrimaryRef) | ||
} | ||
feed := f.ds.GetFeed(ssb.Ref(req.Feed)) | ||
|
||
follow := &ssb.Contact{} | ||
|
||
following := true | ||
follow.Following = &following | ||
follow.Contact = ssb.Ref(req.Contact) | ||
follow.Type = "contact" | ||
|
||
content, _ := json.Marshal(follow) | ||
|
||
m := &ssb.Message{ | ||
Author: feed.ID, | ||
Timestamp: float64(time.Now().UnixNano() / int64(time.Millisecond)), | ||
Hash: "sha256", | ||
Content: content, | ||
Sequence: 1, | ||
} | ||
|
||
if l := feed.Latest(); l != nil { | ||
key := l.Key() | ||
m.Previous = &key | ||
m.Sequence = l.Sequence + 1 | ||
for m.Timestamp <= l.Timestamp { | ||
m.Timestamp += 0.01 | ||
} | ||
} | ||
|
||
signer := f.ds.Keys[feed.ID] | ||
if signer == nil { | ||
return nil | ||
} | ||
sm := m.Sign(signer) | ||
|
||
err := feed.AddMessage(sm) | ||
if err != nil { | ||
log.Println(err) | ||
} else { | ||
log.Println("Message ", sm, " posted to feed ", feed.ID) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package rpc | ||
|
||
type PostReq struct { | ||
Feed string | ||
Text string | ||
Root string | ||
Branch string | ||
Channel string | ||
} | ||
|
||
type PostRes struct { | ||
Err error | ||
} | ||
|
||
type FollowReq struct { | ||
Feed string | ||
Contact string | ||
} | ||
|
||
type FollowRes struct { | ||
Err error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package rpc | ||
|
||
type AddPubReq struct { | ||
Host string | ||
Port int | ||
PubKey string | ||
} | ||
|
||
type AddPubRes struct { | ||
Err error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{.Content.Contact}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{{.Message.Author}}<br/> | ||
{{.Content.Text}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"html/template" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/andyleap/go-ssb" | ||
) | ||
|
||
var ContentTemplates = template.Must(template.ParseGlob("templates/content/*.tpl")) | ||
|
||
var IndexTemplate = template.Must(template.New("index").Funcs(template.FuncMap{ | ||
"RenderContent": func(m *ssb.SignedMessage) template.HTML { | ||
t, md := m.DecodeMessage() | ||
buf := &bytes.Buffer{} | ||
err := ContentTemplates.ExecuteTemplate(buf, t+".tpl", struct { | ||
Message *ssb.SignedMessage | ||
Content interface{} | ||
}{m, md}) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
return template.HTML(buf.String()) | ||
}, | ||
}).Parse(` | ||
<html> | ||
<head> | ||
</head> | ||
<body> | ||
{{range .Messages}} | ||
{{RenderContent .}}<hr> | ||
{{end}} | ||
</body> | ||
</html>`)) | ||
|
||
func init() { | ||
log.Println(ContentTemplates.DefinedTemplates()) | ||
} | ||
|
||
func Index(rw http.ResponseWriter, req *http.Request) { | ||
mchan := datastore.GetFeed(datastore.PrimaryRef).Log(0, false) | ||
messages := []*ssb.SignedMessage{} | ||
for m := range mchan { | ||
log.Println(m) | ||
messages = append(messages, m) | ||
} | ||
log.Println(messages) | ||
err := IndexTemplate.Execute(rw, struct { | ||
Messages []*ssb.SignedMessage | ||
}{ | ||
messages, | ||
}) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/urfave/cli" | ||
|
||
r "net/rpc" | ||
|
||
"github.com/andyleap/go-ssb/cmd/sbot/rpc" | ||
) | ||
|
||
func main() { | ||
app := cli.NewApp() | ||
|
||
client, _ := r.Dial("tcp", "127.0.0.1:9822") | ||
|
||
app.Commands = []cli.Command{ | ||
{ | ||
Name: "gossip.add", | ||
Aliases: []string{"g.a"}, | ||
Usage: "add a peer to the gossip list", | ||
Action: func(c *cli.Context) error { | ||
if c.NArg() != 3 { | ||
return fmt.Errorf("Expected 3 arguments") | ||
} | ||
port, err := strconv.Atoi(c.Args().Get(1)) | ||
if err != nil { | ||
return err | ||
} | ||
req := rpc.AddPubReq{ | ||
Host: c.Args().Get(0), | ||
Port: port, | ||
PubKey: c.Args().Get(2), | ||
} | ||
res := rpc.AddPubRes{} | ||
return client.Call("Gossip.AddPub", req, &res) | ||
}, | ||
}, | ||
{ | ||
Name: "feed.post", | ||
Aliases: []string{"f.p"}, | ||
Usage: "publish a new post to a feed", | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "feed", | ||
Usage: "Feed to publish to", | ||
}, | ||
cli.StringFlag{ | ||
Name: "root", | ||
Usage: "Root post to respond to", | ||
}, | ||
cli.StringFlag{ | ||
Name: "branch", | ||
Usage: "Branch post to respond to", | ||
}, | ||
cli.StringFlag{ | ||
Name: "channel", | ||
Usage: "Channel to publish to", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
if c.NArg() != 1 { | ||
return fmt.Errorf("Expected 1 argument") | ||
} | ||
req := rpc.PostReq{ | ||
Feed: c.String("feed"), | ||
Text: c.Args().Get(0), | ||
Root: c.String("root"), | ||
Branch: c.String("branch"), | ||
Channel: c.String("channel"), | ||
} | ||
res := rpc.PostRes{} | ||
return client.Call("Feed.Post", req, &res) | ||
}, | ||
}, | ||
{ | ||
Name: "feed.follow", | ||
Aliases: []string{"f.f"}, | ||
Usage: "follow a feed", | ||
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "feed", | ||
Usage: "Feed to publish to", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
if c.NArg() != 1 { | ||
return fmt.Errorf("Expected 1 argument") | ||
} | ||
req := rpc.FollowReq{ | ||
Feed: c.String("feed"), | ||
Contact: c.Args().Get(0), | ||
} | ||
res := rpc.FollowRes{} | ||
return client.Call("Feed.Follow", req, &res) | ||
}, | ||
}, | ||
} | ||
app.Run(os.Args) | ||
} |
Oops, something went wrong.