Skip to content

Commit

Permalink
Program -> Namespace (bluesky-social#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms authored Apr 7, 2022
1 parent 28d476e commit 608752f
Show file tree
Hide file tree
Showing 21 changed files with 260 additions and 269 deletions.
20 changes: 10 additions & 10 deletions common/src/auth/bluesky-capability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Resource name: 'bluesky'
- Full permission for account:
did:bsky:userDid|*
- Permission to write to particular program namespace:
- Permission to write to particular namespace:
did:bsky:userDid|did:bsky:microblog|*
- Permission to make only interactions in a given namespace:
did:bsky:userDid|did:bsky:microblog|interactions|*
Expand Down Expand Up @@ -75,20 +75,20 @@ export const blueskySemantics: CapabilitySemantics<BlueskyCapability> = {
}
}

const [childDid, childProgram, childCollection, childTid] =
const [childDid, childNamespace, childCollection, childTid] =
childCap.bluesky.split('|')
const [parentDid, parentProgram, parentCollection, parentTid] =
const [parentDid, parentNamespace, parentCollection, parentTid] =
parentCap.bluesky.split('|')

if (childDid !== parentDid) {
return null
}

if (parentProgram === '*') {
if (parentNamespace === '*') {
return childCap
} else if (childProgram === '*') {
} else if (childNamespace === '*') {
return namespaceEscalation(childCap)
} else if (childProgram !== parentProgram) {
} else if (childNamespace !== parentNamespace) {
return null
}

Expand Down Expand Up @@ -122,7 +122,7 @@ export const hasPermission = (

export const namespaceEscalation = (cap: BlueskyCapability) => {
return {
escalation: 'Bluesky program namespace esclation',
escalation: 'Bluesky namespace esclation',
capability: cap,
}
}
Expand All @@ -143,13 +143,13 @@ export const tidEscalation = (cap: BlueskyCapability) => {

export function writeCap(
did: string,
program?: string,
namespace?: string,
collection?: Collection,
tid?: TID,
): BlueskyCapability {
let resource = did
if (program) {
resource += '|' + program
if (namespace) {
resource += '|' + namespace
}
if (collection) {
resource += '|' + collection
Expand Down
4 changes: 2 additions & 2 deletions common/src/auth/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const MONTH_IN_SECONDS = 60 * 60 * 24 * 30
export const delegateForPost = async (
serverDid: string,
did: string,
program: string,
namespace: string,
collection: Collection,
tid: TID,
keypair: ucan.Keypair,
Expand All @@ -24,7 +24,7 @@ export const delegateForPost = async (
.withLifetimeInSeconds(30)
.delegateCapability(
blueskySemantics,
writeCap(did, program, collection, tid),
writeCap(did, namespace, collection, tid),
ucanStore,
)
.build()
Expand Down
4 changes: 2 additions & 2 deletions common/src/auth/ucan-checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ export const hasRelationshipsPermission =
}

export const hasPostingPermission =
(did: string, program: string, collection: Collection, tid: TID) =>
(did: string, namespace: string, collection: Collection, tid: TID) =>
(token: Chained): Error | null => {
// the capability we need for the given post
const needed = writeCap(did, program, collection, tid)
const needed = writeCap(did, namespace, collection, tid)
return hasValidCapability(did, needed)(token)
}
26 changes: 13 additions & 13 deletions common/src/microblog/delegator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Keypair } from '../common/types.js'
import * as auth from '../auth/index.js'

export class MicroblogDelegator {
programName = 'did:bsky:microblog'
namespace = 'did:bsky:microblog'
keypair: Keypair | null
ucanStore: ucan.Store | null
serverDid: string | null
Expand Down Expand Up @@ -79,7 +79,7 @@ export class MicroblogDelegator {
return auth.delegateForPost(
serverDid,
this.did,
this.programName,
this.namespace,
collection,
tid,
this.keypair,
Expand Down Expand Up @@ -151,7 +151,7 @@ export class MicroblogDelegator {
const params = {
tid: tid.toString(),
did: did,
program: this.programName,
namespace: this.namespace,
}
let res: AxiosResponse
try {
Expand All @@ -173,7 +173,7 @@ export class MicroblogDelegator {
const tid = TID.next()
const post: Post = {
tid: tid.toString(),
program: this.programName,
namespace: this.namespace,
author: this.did,
text,
time: new Date().toISOString(),
Expand All @@ -191,7 +191,7 @@ export class MicroblogDelegator {
async editPost(tid: TID, text: string): Promise<void> {
const updated: Post = {
tid: tid.toString(),
program: this.programName,
namespace: this.namespace,
author: this.did,
text,
time: new Date().toISOString(),
Expand All @@ -209,7 +209,7 @@ export class MicroblogDelegator {
const data = {
tid: tid.toString(),
did: this.did,
program: this.programName,
namespace: this.namespace,
}
const token = await this.postToken('posts', tid)
try {
Expand All @@ -235,7 +235,7 @@ export class MicroblogDelegator {
const did = await this.resolveDid(nameOrDid)
const params = {
did,
program: this.programName,
namespace: this.namespace,
count,
from: from?.toString(),
}
Expand Down Expand Up @@ -318,12 +318,12 @@ export class MicroblogDelegator {
const tid = TID.next()
const like: Like = {
tid: tid.toString(),
program: this.programName,
namespace: this.namespace,
author: this.did,
time: new Date().toISOString(),
post_tid: postTid.toString(),
post_author: postAuthorDid,
post_program: this.programName,
post_namespace: this.namespace,
}
const token = await this.postToken('interactions', tid)
try {
Expand All @@ -339,7 +339,7 @@ export class MicroblogDelegator {
const data = {
tid: likeTid.toString(),
did: this.did,
program: this.programName,
namespace: this.namespace,
}
const token = await this.postToken('interactions', likeTid)
try {
Expand Down Expand Up @@ -369,7 +369,7 @@ export class MicroblogDelegator {
const params = {
did: this.did,
postAuthor: authorDid,
postProgram: this.programName,
postNamespace: this.namespace,
postTid: postTid.toString(),
}
try {
Expand All @@ -396,7 +396,7 @@ export class MicroblogDelegator {
const did = await this.resolveDid(nameOrDid)
const params = {
did,
program: this.programName,
namespace: this.namespace,
count,
from: from?.toString(),
}
Expand All @@ -414,7 +414,7 @@ export class MicroblogDelegator {
async likeCount(author: string, tid: TID): Promise<number> {
const params = {
author,
program: this.programName,
namespace: this.namespace,
tid: tid.toString(),
}
try {
Expand Down
44 changes: 22 additions & 22 deletions common/src/microblog/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import Repo from '../repo/index.js'
import Program from '../repo/program.js'
import NamespaceImpl from '../repo/namespace-impl.js'

import { Post, Like, schema } from './types.js'
import TID from '../repo/tid.js'

export class Microblog extends Program {
export class Microblog extends NamespaceImpl {
constructor(repo: Repo) {
super('did:bsky:microblog', repo)
}

async getPost(id: TID): Promise<Post | null> {
const postCid = await this.runOnProgram(async (program) => {
return program.posts.getEntry(id)
const postCid = await this.runOnNamespace(async (namespace) => {
return namespace.posts.getEntry(id)
})
if (postCid === null) return null
const post = await this.repo.get(postCid, schema.post)
Expand All @@ -22,41 +22,41 @@ export class Microblog extends Program {
const tid = TID.next()
const post: Post = {
tid: tid.toString(),
program: this.name,
namespace: this.name,
text,
author: this.repo.did,
time: new Date().toISOString(),
}
const postCid = await this.repo.put(post)
await this.runOnProgram(async (program) => {
await program.posts.addEntry(tid, postCid)
await this.runOnNamespace(async (namespace) => {
await namespace.posts.addEntry(tid, postCid)
})
return post
}

async editPost(tid: TID, text: string): Promise<void> {
const post: Post = {
tid: tid.toString(),
program: this.name,
namespace: this.name,
text,
author: this.repo.did,
time: new Date().toISOString(),
}
const postCid = await this.repo.put(post)
await this.runOnProgram(async (program) => {
await program.posts.editEntry(tid, postCid)
await this.runOnNamespace(async (namespace) => {
await namespace.posts.editEntry(tid, postCid)
})
}

async deletePost(tid: TID): Promise<void> {
await this.runOnProgram(async (program) => {
await program.posts.deleteEntry(tid)
await this.runOnNamespace(async (namespace) => {
await namespace.posts.deleteEntry(tid)
})
}

async listPosts(count: number, from?: TID): Promise<Post[]> {
const entries = await this.runOnProgram(async (program) => {
return program.posts.getEntries(count, from)
const entries = await this.runOnNamespace(async (namespace) => {
return namespace.posts.getEntries(count, from)
})
const posts = await Promise.all(
entries.map((entry) => this.repo.get(entry.cid, schema.post)),
Expand All @@ -68,29 +68,29 @@ export class Microblog extends Program {
const tid = TID.next()
const like: Like = {
tid: tid.toString(),
program: this.name,
namespace: this.name,
author: this.repo.did,
time: new Date().toISOString(),
post_tid: postTid.toString(),
post_author: postAuthor,
post_program: this.name,
post_namespace: this.name,
}
const likeCid = await this.repo.put(like)
await this.runOnProgram(async (program) => {
await program.interactions.addEntry(tid, likeCid)
await this.runOnNamespace(async (namespace) => {
await namespace.interactions.addEntry(tid, likeCid)
})
return tid
}

async deleteLike(tid: TID): Promise<void> {
await this.runOnProgram(async (program) => {
await program.interactions.deleteEntry(tid)
await this.runOnNamespace(async (namespace) => {
await namespace.interactions.deleteEntry(tid)
})
}

async listLikes(count: number, from?: TID): Promise<Like[]> {
const entries = await this.runOnProgram(async (program) => {
return program.interactions.getEntries(count, from)
const entries = await this.runOnNamespace(async (namespace) => {
return namespace.interactions.getEntries(count, from)
})
const likes = await Promise.all(
entries.map((entry) => this.repo.get(entry.cid, schema.like)),
Expand Down
6 changes: 3 additions & 3 deletions common/src/microblog/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { z } from 'zod'
const post = z.object({
tid: z.string(),
author: z.string(),
program: z.string(),
namespace: z.string(),
text: z.string(),
time: z.string(),
})
Expand All @@ -12,11 +12,11 @@ export type Post = z.infer<typeof post>
const like = z.object({
tid: z.string(),
author: z.string(),
program: z.string(),
namespace: z.string(),
time: z.string(),
post_tid: z.string(),
post_author: z.string(),
post_program: z.string(),
post_namespace: z.string(),
})
export type Like = z.infer<typeof like>

Expand Down
Loading

0 comments on commit 608752f

Please sign in to comment.