Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

goat: schema-flexible record creation, and prefs mgmt #791

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cmd/goat/account_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

comatproto "github.com/bluesky-social/indigo/api/atproto"
appbsky "github.com/bluesky-social/indigo/api/bsky"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/bluesky-social/indigo/xrpc"

Expand Down Expand Up @@ -167,11 +166,11 @@ func runAccountMigrate(cctx *cli.Context) error {

slog.Info("migrating preferences")
// TODO: service proxy header for AppView?
prefResp, err := appbsky.ActorGetPreferences(ctx, oldClient)
prefResp, err := ActorGetPreferences(ctx, oldClient)
if err != nil {
return fmt.Errorf("failed fetching old preferences: %w", err)
}
err = appbsky.ActorPutPreferences(ctx, &newClient, &appbsky.ActorPutPreferences_Input{
err = ActorPutPreferences(ctx, &newClient, &ActorPutPreferences_Input{
Preferences: prefResp.Preferences,
})
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions cmd/goat/actorgetPreferences.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copied from indigo:api/atproto/actorgetPreferences.go

package main

// schema: app.bsky.actor.getPreferences

import (
"context"

"github.com/bluesky-social/indigo/xrpc"
)

// ActorGetPreferences_Output is the output of a app.bsky.actor.getPreferences call.
type ActorGetPreferences_Output struct {
Preferences []map[string]any `json:"preferences" cborgen:"preferences"`
}

// ActorGetPreferences calls the XRPC method "app.bsky.actor.getPreferences".
func ActorGetPreferences(ctx context.Context, c *xrpc.Client) (*ActorGetPreferences_Output, error) {
var out ActorGetPreferences_Output

params := map[string]interface{}{}
if err := c.Do(ctx, xrpc.Query, "", "app.bsky.actor.getPreferences", params, nil, &out); err != nil {
return nil, err
}

return &out, nil
}
25 changes: 25 additions & 0 deletions cmd/goat/actorputPreferences.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copied from indigo:api/atproto/actorputPreferences.go

package main

// schema: app.bsky.actor.putPreferences

import (
"context"

"github.com/bluesky-social/indigo/xrpc"
)

// ActorPutPreferences_Input is the input argument to a app.bsky.actor.putPreferences call.
type ActorPutPreferences_Input struct {
Preferences []map[string]any `json:"preferences" cborgen:"preferences"`
}

// ActorPutPreferences calls the XRPC method "app.bsky.actor.putPreferences".
func ActorPutPreferences(ctx context.Context, c *xrpc.Client, input *ActorPutPreferences_Input) error {
if err := c.Do(ctx, xrpc.Procedure, "application/json", "app.bsky.actor.putPreferences", nil, input, nil); err != nil {
return err
}

return nil
}
12 changes: 4 additions & 8 deletions cmd/goat/bsky_prefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"fmt"
"os"

appbsky "github.com/bluesky-social/indigo/api/bsky"

"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -41,7 +39,7 @@ func runBskyPrefsExport(cctx *cli.Context) error {
}

// TODO: does indigo API code crash with unsupported preference '$type'? Eg "Lexicon decoder" with unsupported type.
resp, err := appbsky.ActorGetPreferences(ctx, xrpcc)
resp, err := ActorGetPreferences(ctx, xrpcc)
if err != nil {
return fmt.Errorf("failed fetching old preferences: %w", err)
}
Expand Down Expand Up @@ -74,14 +72,12 @@ func runBskyPrefsImport(cctx *cli.Context) error {
return err
}

var prefsArray []appbsky.ActorDefs_Preferences_Elem
err = json.Unmarshal(prefsBytes, &prefsArray)
if err != nil {
var prefsArray []map[string]any
if err = json.Unmarshal(prefsBytes, &prefsArray); err != nil {
return err
}

// WARNING: might clobber off-Lexicon or new-Lexicon data fields (which don't round-trip deserialization)
err = appbsky.ActorPutPreferences(ctx, xrpcc, &appbsky.ActorPutPreferences_Input{
err = ActorPutPreferences(ctx, xrpcc, &ActorPutPreferences_Input{
Preferences: prefsArray,
})
if err != nil {
Expand Down
19 changes: 8 additions & 11 deletions cmd/goat/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/bluesky-social/indigo/atproto/data"
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/syntax"
lexutil "github.com/bluesky-social/indigo/lex/util"
"github.com/bluesky-social/indigo/xrpc"

"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -231,9 +230,8 @@ func runRecordCreate(cctx *cli.Context) error {
return err
}

// TODO: replace this with something that allows arbitrary Lexicons, instead of needing registered types
var recordVal lexutil.LexiconTypeDecoder
if err = recordVal.UnmarshalJSON(recordBytes); err != nil {
recordVal, err := data.UnmarshalJSON(recordBytes)
if err != nil {
return err
}

Expand All @@ -248,10 +246,10 @@ func runRecordCreate(cctx *cli.Context) error {
}
validate := !cctx.Bool("no-validate")

resp, err := comatproto.RepoCreateRecord(ctx, xrpcc, &comatproto.RepoCreateRecord_Input{
resp, err := RepoCreateRecord(ctx, xrpcc, &RepoCreateRecord_Input{
Collection: nsid,
Repo: xrpcc.Auth.Did,
Record: &recordVal,
Record: recordVal,
Rkey: rkey,
Validate: &validate,
})
Expand Down Expand Up @@ -300,18 +298,17 @@ func runRecordUpdate(cctx *cli.Context) error {
return err
}

// TODO: replace this with something that allows arbitrary Lexicons, instead of needing registered types
var recordVal lexutil.LexiconTypeDecoder
if err = recordVal.UnmarshalJSON(recordBytes); err != nil {
recordVal, err := data.UnmarshalJSON(recordBytes)
if err != nil {
return err
}

validate := !cctx.Bool("no-validate")

resp, err := comatproto.RepoPutRecord(ctx, xrpcc, &comatproto.RepoPutRecord_Input{
resp, err := RepoPutRecord(ctx, xrpcc, &RepoPutRecord_Input{
Collection: nsid,
Repo: xrpcc.Auth.Did,
Record: &recordVal,
Record: recordVal,
Rkey: rkey,
Validate: &validate,
SwapRecord: existing.Cid,
Expand Down
51 changes: 51 additions & 0 deletions cmd/goat/repocreateRecord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copied from indigo:api/atproto/repocreateRecords.go

package main

// schema: com.atproto.repo.createRecord

import (
"context"

"github.com/bluesky-social/indigo/xrpc"
)

// RepoDefs_CommitMeta is a "commitMeta" in the com.atproto.repo.defs schema.
type RepoDefs_CommitMeta struct {
Cid string `json:"cid" cborgen:"cid"`
Rev string `json:"rev" cborgen:"rev"`
}

// RepoCreateRecord_Input is the input argument to a com.atproto.repo.createRecord call.
type RepoCreateRecord_Input struct {
// collection: The NSID of the record collection.
Collection string `json:"collection" cborgen:"collection"`
// record: The record itself. Must contain a $type field.
Record map[string]any `json:"record" cborgen:"record"`
// repo: The handle or DID of the repo (aka, current account).
Repo string `json:"repo" cborgen:"repo"`
// rkey: The Record Key.
Rkey *string `json:"rkey,omitempty" cborgen:"rkey,omitempty"`
// swapCommit: Compare and swap with the previous commit by CID.
SwapCommit *string `json:"swapCommit,omitempty" cborgen:"swapCommit,omitempty"`
// validate: Can be set to 'false' to skip Lexicon schema validation of record data, 'true' to require it, or leave unset to validate only for known Lexicons.
Validate *bool `json:"validate,omitempty" cborgen:"validate,omitempty"`
}

// RepoCreateRecord_Output is the output of a com.atproto.repo.createRecord call.
type RepoCreateRecord_Output struct {
Cid string `json:"cid" cborgen:"cid"`
Commit *RepoDefs_CommitMeta `json:"commit,omitempty" cborgen:"commit,omitempty"`
Uri string `json:"uri" cborgen:"uri"`
ValidationStatus *string `json:"validationStatus,omitempty" cborgen:"validationStatus,omitempty"`
}

// RepoCreateRecord calls the XRPC method "com.atproto.repo.createRecord".
func RepoCreateRecord(ctx context.Context, c *xrpc.Client, input *RepoCreateRecord_Input) (*RepoCreateRecord_Output, error) {
var out RepoCreateRecord_Output
if err := c.Do(ctx, xrpc.Procedure, "application/json", "com.atproto.repo.createRecord", nil, input, &out); err != nil {
return nil, err
}

return &out, nil
}
47 changes: 47 additions & 0 deletions cmd/goat/repoputRecord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copied from indigo:api/atproto/repoputRecords.go

package main

// schema: com.atproto.repo.putRecord

import (
"context"

"github.com/bluesky-social/indigo/xrpc"
)

// RepoPutRecord_Input is the input argument to a com.atproto.repo.putRecord call.
type RepoPutRecord_Input struct {
// collection: The NSID of the record collection.
Collection string `json:"collection" cborgen:"collection"`
// record: The record to write.
Record map[string]any `json:"record" cborgen:"record"`
// repo: The handle or DID of the repo (aka, current account).
Repo string `json:"repo" cborgen:"repo"`
// rkey: The Record Key.
Rkey string `json:"rkey" cborgen:"rkey"`
// swapCommit: Compare and swap with the previous commit by CID.
SwapCommit *string `json:"swapCommit,omitempty" cborgen:"swapCommit,omitempty"`
// swapRecord: Compare and swap with the previous record by CID. WARNING: nullable and optional field; may cause problems with golang implementation
SwapRecord *string `json:"swapRecord" cborgen:"swapRecord"`
// validate: Can be set to 'false' to skip Lexicon schema validation of record data, 'true' to require it, or leave unset to validate only for known Lexicons.
Validate *bool `json:"validate,omitempty" cborgen:"validate,omitempty"`
}

// RepoPutRecord_Output is the output of a com.atproto.repo.putRecord call.
type RepoPutRecord_Output struct {
Cid string `json:"cid" cborgen:"cid"`
Commit *RepoDefs_CommitMeta `json:"commit,omitempty" cborgen:"commit,omitempty"`
Uri string `json:"uri" cborgen:"uri"`
ValidationStatus *string `json:"validationStatus,omitempty" cborgen:"validationStatus,omitempty"`
}

// RepoPutRecord calls the XRPC method "com.atproto.repo.putRecord".
func RepoPutRecord(ctx context.Context, c *xrpc.Client, input *RepoPutRecord_Input) (*RepoPutRecord_Output, error) {
var out RepoPutRecord_Output
if err := c.Do(ctx, xrpc.Procedure, "application/json", "com.atproto.repo.putRecord", nil, input, &out); err != nil {
return nil, err
}

return &out, nil
}
Loading