Skip to content

Commit

Permalink
repo parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Nov 8, 2022
1 parent 3cc12fb commit 4bad54c
Show file tree
Hide file tree
Showing 14 changed files with 1,935 additions and 126 deletions.
52 changes: 25 additions & 27 deletions api/atproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package api
import (
"bytes"
"context"
"encoding/json"
"fmt"

"github.com/whyrusleeping/gosky/xrpc"
)
Expand All @@ -15,41 +13,43 @@ type ATProto struct {

type TID string

type CreateAccountResp struct {
type CreateSessionResp struct {
Jwt string `json:"jwt"`
Username string `json:"username"`
Username string `json:"name"` // TODO: probably will change to username
Did string `json:"did"`
}

func (atp *ATProto) CreateAccount(ctx context.Context, username, email, password string) (*CreateAccountResp, error) {
func (atp *ATProto) CreateSession(ctx context.Context, username, password string) (*CreateSessionResp, error) {
body := map[string]string{
"username": username,
"email": email,
"password": password,
}

var resp CreateAccountResp
if err := atp.C.Do(ctx, xrpc.Procedure, "com.atproto.createAccount", nil, body, &resp); err != nil {
var resp CreateSessionResp
if err := atp.C.Do(ctx, xrpc.Procedure, "com.atproto.session.create", nil, body, &resp); err != nil {
return nil, err
}

return &resp, nil
}

type CreateSessionResp struct {
Jwt string `json:"jwt"`
Username string `json:"name"` // TODO: probably will change to username
Did string `json:"did"`
type CreateAccountResp struct {
AccessJwt string `json:"accessJwt"`
RefreshJwt string `json:"refreshJwt"`
Handle string `json:"handle"`
Did string `json:"did"`
DeclarationCid string `json:"declarationCid"`
}

func (atp *ATProto) CreateSession(ctx context.Context, username, password string) (*CreateSessionResp, error) {
func (atp *ATProto) CreateAccount(ctx context.Context, email, handle, password string) (*CreateAccountResp, error) {
body := map[string]string{
"username": username,
"email": email,
"handle": handle,
"password": password,
}

var resp CreateSessionResp
if err := atp.C.Do(ctx, xrpc.Procedure, "com.atproto.session.create", nil, body, &resp); err != nil {
var resp CreateAccountResp
if err := atp.C.Do(ctx, xrpc.Procedure, "com.atproto.account.create", nil, body, &resp); err != nil {
return nil, err
}

Expand All @@ -62,21 +62,19 @@ type CreateRecordResponse struct {
}

func (atp *ATProto) RepoCreateRecord(ctx context.Context, did, collection string, validate bool, rec JsonLD) (*CreateRecordResponse, error) {
params := map[string]interface{}{
"did": did,
"collection": collection,
"validate": validate,
}

rw := &RecordWrapper{
Sub: rec,
}

b, _ := json.Marshal(rw)
fmt.Println(string(b))
body := map[string]interface{}{
"did": did,
"collection": collection,
"validate": validate,
"record": rw,
}

var out CreateRecordResponse
if err := atp.C.Do(ctx, xrpc.Procedure, "com.atproto.repoCreateRecord", params, rw, &out); err != nil {
if err := atp.C.Do(ctx, xrpc.Procedure, "com.atproto.repo.createRecord", nil, body, &out); err != nil {
return nil, err
}

Expand All @@ -92,7 +90,7 @@ func (atp *ATProto) SyncGetRepo(ctx context.Context, did string, from *string) (
}

out := new(bytes.Buffer)
if err := atp.C.Do(ctx, xrpc.Query, "com.atproto.syncGetRepo", params, nil, &out); err != nil {
if err := atp.C.Do(ctx, xrpc.Query, "com.atproto.sync.getRepo", params, nil, out); err != nil {
return nil, err
}

Expand All @@ -107,7 +105,7 @@ func (atp *ATProto) SyncGetRoot(ctx context.Context, did string) (string, error)
var out struct {
Root string `json:"root"`
}
if err := atp.C.Do(ctx, xrpc.Query, "com.atproto.syncGetRoot", params, nil, &out); err != nil {
if err := atp.C.Do(ctx, xrpc.Query, "com.atproto.sync.getRoot", params, nil, &out); err != nil {
return "", err
}

Expand Down
2 changes: 1 addition & 1 deletion api/bsky.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type PostRecord struct {
}

func (pr PostRecord) Type() string {
return "app.bsky.post"
return "app.bsky.feed.post"
}

type JsonLD interface {
Expand Down
94 changes: 52 additions & 42 deletions cmd/gosky/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (

cli "github.com/urfave/cli/v2"
api "github.com/whyrusleeping/gosky/api"
"github.com/whyrusleeping/gosky/xrpc"
cliutil "github.com/whyrusleeping/gosky/cmd/gosky/util"
)

func main() {
app := cli.NewApp()

app.Commands = []*cli.Command{
createSessionCmd,
newAccountCmd,
postCmd,
didCmd,
syncCmd,
Expand All @@ -26,21 +27,24 @@ func main() {
app.RunAndExitOnError()
}

var createSessionCmd = &cli.Command{
Name: "createSession",
var newAccountCmd = &cli.Command{
Name: "newAccount",
Action: func(cctx *cli.Context) error {
atp := &api.ATProto{
C: &xrpc.Client{
Host: "https://pds.staging.bsky.dev",
},
atp, err := cliutil.GetATPClient(cctx, false)
if err != nil {
return err
}

ses, err := atp.CreateSession(context.TODO(), cctx.Args().Get(0), cctx.Args().Get(1))
email := cctx.Args().Get(0)
handle := cctx.Args().Get(1)
password := cctx.Args().Get(2)

acc, err := atp.CreateAccount(context.TODO(), email, handle, password)
if err != nil {
return err
}

b, err := json.MarshalIndent(ses, "", " ")
b, err := json.MarshalIndent(acc, "", " ")
if err != nil {
return err
}
Expand All @@ -49,43 +53,44 @@ var createSessionCmd = &cli.Command{
return nil
},
}

func loadAuthFromEnv(req bool) (*xrpc.AuthInfo, error) {
val := os.Getenv("BSKY_AUTH")
if val == "" {
if req {
return nil, fmt.Errorf("no auth env present, BSKY_AUTH not set")
var createSessionCmd = &cli.Command{
Name: "createSession",
Action: func(cctx *cli.Context) error {
atp, err := cliutil.GetATPClient(cctx, false)
if err != nil {
return err
}
handle := cctx.Args().Get(0)
password := cctx.Args().Get(1)

return nil, nil
}
ses, err := atp.CreateSession(context.TODO(), handle, password)
if err != nil {
return err
}

var auth xrpc.AuthInfo
if err := json.Unmarshal([]byte(val), &auth); err != nil {
return nil, err
}
b, err := json.MarshalIndent(ses, "", " ")
if err != nil {
return err
}

return &auth, nil
fmt.Println(string(b))
return nil
},
}

var postCmd = &cli.Command{
Name: "post",
Action: func(cctx *cli.Context) error {
auth, err := loadAuthFromEnv(true)
atp, err := cliutil.GetATPClient(cctx, true)
if err != nil {
return err
}

atp := &api.ATProto{
C: &xrpc.Client{
Host: "https://pds.staging.bsky.dev",
Auth: auth,
},
}
auth := atp.C.Auth

text := strings.Join(cctx.Args().Slice(), " ")

resp, err := atp.RepoCreateRecord(context.TODO(), auth.Did, "app.bsky.post", true, &api.PostRecord{
resp, err := atp.RepoCreateRecord(context.TODO(), auth.Did, "app.bsky.feed.post", true, &api.PostRecord{
Text: text,
CreatedAt: time.Now().Format(time.RFC3339),
})
Expand All @@ -110,9 +115,7 @@ var didCmd = &cli.Command{
var didGetCmd = &cli.Command{
Name: "get",
Action: func(cctx *cli.Context) error {
s := &api.PLCServer{
Host: "https://plc.staging.bsky.dev",
}
s := cliutil.GetPLCClient(cctx)

doc, err := s.GetDocument(cctx.Args().First())
if err != nil {
Expand All @@ -139,11 +142,15 @@ var syncCmd = &cli.Command{

var syncGetRepoCmd = &cli.Command{
Name: "getRepo",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "raw",
},
},
Action: func(cctx *cli.Context) error {
atp := &api.ATProto{
C: &xrpc.Client{
Host: "https://pds.staging.bsky.dev",
},
atp, err := cliutil.GetATPClient(cctx, false)
if err != nil {
return err
}

ctx := context.TODO()
Expand All @@ -153,7 +160,11 @@ var syncGetRepoCmd = &cli.Command{
return err
}

fmt.Printf("%x", repobytes)
if cctx.Bool("raw") {
os.Stdout.Write(repobytes)
} else {
fmt.Printf("%x", repobytes)
}

return nil
},
Expand All @@ -162,10 +173,9 @@ var syncGetRepoCmd = &cli.Command{
var syncGetRootCmd = &cli.Command{
Name: "getRoot",
Action: func(cctx *cli.Context) error {
atp := &api.ATProto{
C: &xrpc.Client{
Host: "https://pds.staging.bsky.dev",
},
atp, err := cliutil.GetATPClient(cctx, false)
if err != nil {
return err
}

ctx := context.TODO()
Expand Down
60 changes: 60 additions & 0 deletions cmd/gosky/util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cliutil

import (
"encoding/json"
"fmt"
"os"

"github.com/urfave/cli/v2"
"github.com/whyrusleeping/gosky/api"
"github.com/whyrusleeping/gosky/xrpc"
)

func GetPLCClient(cctx *cli.Context) *api.PLCServer {
h := "https://plc.staging.bsky.dev"

if envh := os.Getenv("BSK_PLC_URL"); envh != "" {
h = envh
}

return &api.PLCServer{
Host: h,
}
}

func GetATPClient(cctx *cli.Context, authreq bool) (*api.ATProto, error) {
h := "https://pds.staging.bsky.dev"
if envh := os.Getenv("BSKY_PDS_URL"); envh != "" {
h = envh
}

auth, err := loadAuthFromEnv(authreq)
if err != nil {
return nil, err
}

return &api.ATProto{
C: &xrpc.Client{
Host: h,
Auth: auth,
},
}, nil
}

func loadAuthFromEnv(req bool) (*xrpc.AuthInfo, error) {
val := os.Getenv("BSKY_AUTH")
if val == "" {
if req {
return nil, fmt.Errorf("no auth env present, BSKY_AUTH not set")
}

return nil, nil
}

var auth xrpc.AuthInfo
if err := json.Unmarshal([]byte(val), &auth); err != nil {
return nil, err
}

return &auth, nil
}
Loading

0 comments on commit 4bad54c

Please sign in to comment.