Skip to content

Commit

Permalink
Full client in CLI (bluesky-social#75)
Browse files Browse the repository at this point in the history
* cleaning up clients

* refactor full client

* full client in cli

* cli option for full v delegator client

* fix a couple tests
  • Loading branch information
dholms authored Apr 15, 2022
1 parent cd9d567 commit 4f5306b
Show file tree
Hide file tree
Showing 33 changed files with 669 additions and 496 deletions.
1 change: 1 addition & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@bluesky/common": "*",
"chalk": "^5.0.0",
"minimist": "^1.2.5",
"multiformats": "^9.6.4",
"prompt": "^1.2.1",
"ucans": "0.9.0-alpha3"
},
Expand Down
22 changes: 10 additions & 12 deletions cli/src/commands/dev/spam.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import cmd from '../../lib/command.js'
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'
import { REPO_PATH } from '../../lib/env.js'
import { TID } from '@bluesky/common'

function makeRandText(l: number) {
var set = ' abcdefghijklmnopqrstuvwxyz ';
var len = set.length;
var out = '';
for ( let i = 0; i < len; i++ ) {
out += set.charAt(Math.floor(Math.random() *
len));
}
return out;
const set = ' abcdefghijklmnopqrstuvwxyz '
const len = set.length
let out = ''
for (let i = 0; i < len; i++) {
out += set.charAt(Math.floor(Math.random() * len))
}
return out
}

export default cmd({
Expand All @@ -20,12 +19,11 @@ export default cmd({
help: 'Create a large number of random posts.',
args: [{ name: 'count' }],
async command(args) {
const count : number = +(args._[0])
const client = await loadDelegate(REPO_PATH)
const count: number = +args._[0]
const client = await loadClient(REPO_PATH)

for (let i = 0; i < count; i++) {
await client.addPost(makeRandText(100))
}

},
})
4 changes: 2 additions & 2 deletions cli/src/commands/interactions/like.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cmd from '../../lib/command.js'
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'
import { REPO_PATH } from '../../lib/env.js'
import { TID } from '@bluesky/common'

Expand All @@ -12,7 +12,7 @@ export default cmd({
async command(args) {
const author = args._[0]
const tid = TID.fromStr(args._[1])
const client = await loadDelegate(REPO_PATH)
const client = await loadClient(REPO_PATH)
const like = await client.likePost(author, tid)
const likeTid = like.tid
console.log(`Created like: `, likeTid.formatted())
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/interactions/unlike.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cmd from '../../lib/command.js'
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'
import { REPO_PATH } from '../../lib/env.js'
import { TID } from '@bluesky/common'

Expand All @@ -12,7 +12,7 @@ export default cmd({
async command(args) {
const authorNameOrDid = args._[0]
const postTid = TID.fromStr(args._[1])
const client = await loadDelegate(REPO_PATH)
const client = await loadClient(REPO_PATH)
await client.unlikePost(authorNameOrDid, postTid)
console.log('Deleted Like')
},
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/posts/delete-post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cmd from '../../lib/command.js'
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'
import { REPO_PATH } from '../../lib/env.js'
import { TID } from '@bluesky/common'

Expand All @@ -10,7 +10,7 @@ export default cmd({
args: [{ name: 'post_tid' }],
opts: [],
async command(args) {
const client = await loadDelegate(REPO_PATH)
const client = await loadClient(REPO_PATH)
const tid = TID.fromStr(args._[0])
await client.deletePost(tid)
console.log('Post deleted')
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/posts/edit-post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cmd from '../../lib/command.js'
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'
import { REPO_PATH } from '../../lib/env.js'
import { TID } from '@bluesky/common'

Expand All @@ -10,7 +10,7 @@ export default cmd({
args: [{ name: 'post_id' }, { name: 'text' }],
opts: [],
async command(args) {
const client = await loadDelegate(REPO_PATH)
const client = await loadClient(REPO_PATH)
const tid = TID.fromStr(args._[0])
const text = args._[1]
await client.editPost(tid, text)
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/posts/feed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cmd from '../../lib/command.js'
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'
import { REPO_PATH } from '../../lib/env.js'
import { TID } from '@bluesky/common'
import chalk from 'chalk'
Expand All @@ -15,7 +15,7 @@ export default cmd({
{ name: 'from', optional: true },
],
async command(args) {
const client = await loadDelegate(REPO_PATH)
const client = await loadClient(REPO_PATH)
const username = args._[0] || client.did
const countParsed = parseInt(args._[1])
const count = isNaN(countParsed) ? 100 : countParsed
Expand Down
5 changes: 2 additions & 3 deletions cli/src/commands/posts/post.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import cmd from '../../lib/command.js'
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'
import { REPO_PATH } from '../../lib/env.js'
import { TID } from '@bluesky/common'

export default cmd({
name: 'post',
Expand All @@ -10,7 +9,7 @@ export default cmd({
args: [{ name: 'text' }],
async command(args) {
const text = args._[0]
const client = await loadDelegate(REPO_PATH)
const client = await loadClient(REPO_PATH)
const post = await client.addPost(text)
const tid = post.tid
console.log(`Created post: `, tid.formatted())
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/posts/timeline.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk'
import cmd from '../../lib/command.js'
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'
import { REPO_PATH } from '../../lib/env.js'
import { TID } from '@bluesky/common'
import { formatDate } from '../../lib/util.js'
Expand All @@ -14,7 +14,7 @@ export default cmd({
{ name: 'from', optional: true },
],
async command(args) {
const client = await loadDelegate(REPO_PATH)
const client = await loadClient(REPO_PATH)
const countParsed = parseInt(args._[0])
const count = isNaN(countParsed) ? 100 : countParsed
const fromStr = args._[1]
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/repo/export.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cmd from '../../lib/command.js'
import path from 'path'
import { promises as fsp } from 'fs'
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'
import { REPO_PATH } from '../../lib/env.js'

export default cmd({
Expand All @@ -11,7 +11,7 @@ export default cmd({
args: [],
opts: [],
async command(args) {
const client = await loadDelegate(REPO_PATH)
const client = await loadClient(REPO_PATH)
const car = await client.export()
const p = path.join(REPO_PATH, 'export.car')
await fsp.writeFile(p, car)
Expand Down
20 changes: 15 additions & 5 deletions cli/src/commands/setup/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import prompt from 'prompt'
import cmd from '../../lib/command.js'
import { REPO_PATH } from '../../lib/env.js'
import * as config from '../../lib/config.js'
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'

prompt.colors = false
prompt.message = ''
Expand All @@ -14,10 +14,11 @@ export default cmd({
opts: [
{ name: 'server', type: 'string', default: '' },
{ name: 'username', type: 'string', default: '' },
{ name: 'register', type: 'boolean', default: false },
{ name: 'register', type: 'boolean', default: true },
{ name: 'delegator', type: 'boolean', default: false },
],
async command(args) {
let { username, server, register } = args
let { username, server, register, delegatorClient } = args

console.log(`Repo path: ${REPO_PATH}`)
if (!username || !server) {
Expand Down Expand Up @@ -50,12 +51,21 @@ export default cmd({
default: true,
})
).question
delegatorClient = (
await prompt.get({
description:
'Run a delegator client (and avoid storing repo locally)',
type: 'boolean',
required: true,
default: false,
})
).question
}

console.log('Generating repo...')

await config.writeCfg(REPO_PATH, username, server)
const client = await loadDelegate(REPO_PATH)
await config.writeCfg(REPO_PATH, username, server, delegatorClient)
const client = await loadClient(REPO_PATH)

if (register) {
console.log('Registering with server...')
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/setup/register.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'
import { loadCfg } from '../../lib/config.js'
import cmd from '../../lib/command.js'
import { REPO_PATH } from '../../lib/env.js'
Expand All @@ -10,7 +10,7 @@ export default cmd({
args: [],
opts: [],
async command(_args) {
const client = await loadDelegate(REPO_PATH)
const client = await loadClient(REPO_PATH)
const cfg = await loadCfg(REPO_PATH)
console.log('Registering with server...')
try {
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/social/follow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cmd from '../../lib/command.js'
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'
import { REPO_PATH } from '../../lib/env.js'

export default cmd({
Expand All @@ -10,7 +10,7 @@ export default cmd({
opts: [],
async command(args) {
const nameOrDid = args._[0]
const client = await loadDelegate(REPO_PATH)
const client = await loadClient(REPO_PATH)
await client.followUser(nameOrDid)
console.log('Follow created')
},
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/social/list-followers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk'
import cmd from '../../lib/command.js'
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'
import { REPO_PATH } from '../../lib/env.js'

export default cmd({
Expand All @@ -10,7 +10,7 @@ export default cmd({
args: [{ name: 'username/did', optional: true }],
opts: [],
async command(args) {
const client = await loadDelegate(REPO_PATH)
const client = await loadClient(REPO_PATH)
const did = args._[0] || client.did
const follows = await client.listFollowersForUser(did)

Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/social/list-follows.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk'
import cmd from '../../lib/command.js'
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'
import { REPO_PATH } from '../../lib/env.js'

export default cmd({
Expand All @@ -10,7 +10,7 @@ export default cmd({
args: [{ name: 'username/did', optional: true }],
opts: [],
async command(args) {
const client = await loadDelegate(REPO_PATH)
const client = await loadClient(REPO_PATH)
const did = args._[0] || client.did
const follows = await client.listFollowsFromUser(did)

Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/social/unfollow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cmd from '../../lib/command.js'
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'
import { REPO_PATH } from '../../lib/env.js'

export default cmd({
Expand All @@ -10,7 +10,7 @@ export default cmd({
opts: [],
async command(args) {
const nameOrDid = args._[0]
const client = await loadDelegate(REPO_PATH)
const client = await loadClient(REPO_PATH)
await client.unfollowUser(nameOrDid)
console.log('Unfollowed user')
},
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/social/whoami.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk'
import cmd from '../../lib/command.js'
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'
import { REPO_PATH } from '../../lib/env.js'

export default cmd({
Expand All @@ -10,7 +10,7 @@ export default cmd({
args: [],
opts: [],
async command(args) {
const client = await loadDelegate(REPO_PATH)
const client = await loadClient(REPO_PATH)
const info = await client.getAccountInfo(client.did)
if (info === null) {
throw new Error(`Could not find user ${client.did}`)
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/social/whois.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk'
import cmd from '../../lib/command.js'
import { loadDelegate } from '../../lib/client.js'
import { loadClient } from '../../lib/client.js'
import { REPO_PATH } from '../../lib/env.js'

export default cmd({
Expand All @@ -11,7 +11,7 @@ export default cmd({
opts: [],
async command(args) {
const nameOrDid = args._[0]
const client = await loadDelegate(REPO_PATH)
const client = await loadClient(REPO_PATH)
const info = await client.getAccountInfo(nameOrDid)
if (info === null) {
throw new Error(`Could not find user ${nameOrDid}`)
Expand Down
45 changes: 42 additions & 3 deletions cli/src/lib/client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
import { MicroblogDelegator } from '@bluesky/common'
import path from 'path'
import {
IpldStore,
MicroblogClient,
MicroblogDelegator,
MicroblogFull,
Repo,
} from '@bluesky/common'
import * as config from '../lib/config.js'

export const loadDelegate = async (
export const loadClient = async (
repoPath: string,
): Promise<MicroblogClient> => {
const cfg = await config.loadCfg(repoPath)
if (cfg.account.delegator) {
return loadDelegate(cfg)
} else {
return loadFull(cfg, repoPath)
}
}

export const loadFull = async (
cfg: config.Config,
repoPath: string,
): Promise<MicroblogFull> => {
const { account, keypair, ucanStore, root } = cfg
const blockstore = IpldStore.createPersistent(
path.join(repoPath, 'blockstore'),
)
let repo: Repo
if (!root) {
repo = await Repo.create(blockstore, keypair.did(), keypair, ucanStore)
} else {
repo = await Repo.load(blockstore, root, keypair, ucanStore)
}
return new MicroblogFull(repo, `http://${account.server}`, {
onPush: async (newRoot) => {
await config.writeRoot(repoPath, newRoot)
},
})
}

export const loadDelegate = async (
cfg: config.Config,
): Promise<MicroblogDelegator> => {
const { account, keypair, ucanStore } = await config.loadCfg(repoPath)
const { account, keypair, ucanStore } = cfg
return new MicroblogDelegator(
`http://${account.server}`,
keypair.did(),
Expand Down
Loading

0 comments on commit 4f5306b

Please sign in to comment.