Skip to content

Commit

Permalink
Various fixes related to integration with client software (bluesky-so…
Browse files Browse the repository at this point in the history
…cial#188)

* Refactor xrpc to use native fetch and be able to replace it (for react native)

* Factor out uri package

* Fixes to getPostThread

* Manually construct indexedAt and receivedAt timestamps to ensure theyre properly encoded
  • Loading branch information
pfrazee authored Sep 23, 2022
1 parent 1d136eb commit dcba744
Show file tree
Hide file tree
Showing 34 changed files with 679 additions and 93 deletions.
2 changes: 1 addition & 1 deletion packages/dev-env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
},
"dependencies": {
"@adxp/api": "*",
"@adxp/common": "*",
"@adxp/crypto": "*",
"@adxp/did-sdk": "*",
"@adxp/server": "*",
"@adxp/uri": "*",
"chalk": "^5.0.1",
"dotenv": "^16.0.1",
"get-port": "^6.1.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/dev-env/src/mock/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DevEnv } from '../index'
import { ServerType } from '../types'
import { genServerCfg } from '../util'
import { AdxUri } from '@adxp/common'
import { AdxUri } from '@adxp/uri'
import { postTexts, replyTexts } from './data'

// NOTE
Expand Down
2 changes: 1 addition & 1 deletion packages/dev-env/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"include": ["./src","__tests__/**/**.ts"],
"references": [
{ "path": "../api/tsconfig.build.json" },
{ "path": "../common/tsconfig.build.json" },
{ "path": "../crypto/tsconfig.build.json" },
{ "path": "../did-sdk/tsconfig.build.json" },
{ "path": "../server/tsconfig.json" },
{ "path": "../uri/tsconfig.build.json" },
]
}
16 changes: 8 additions & 8 deletions packages/nsid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
## Usage

```typescript
import * as nsid from '@adxp/nsid'
import { NSID } from '@adxp/nsid'

const id1 = nsid.parse('com.example.foo')
const id1 = NSID.parse('com.example.foo')
id1.authority // => 'example.com'
id1.name // => 'foo'
id1.toString() // => 'com.example.foo'

const id2 = nsid.create('example.com', 'foo')
const id2 = NSID.create('example.com', 'foo')
id2.authority // => 'example.com'
id2.name // => 'foo'
id2.toString() // => 'com.example.foo'

const id3 = nsid.create('example.com', '*')
const id3 = NSID.create('example.com', '*')
id3.authority // => 'example.com'
id3.name // => '*'
id3.toString() // => 'com.example.*'

nsid.isValid('com.example.foo') // => true
nsid.isValid('com.example.*') // => true
nsid.isValid('example.com/foo') // => false
nsid.isValid('foo') // => false
NSID.isValid('com.example.foo') // => true
NSID.isValid('com.example.*') // => true
NSID.isValid('example.com/foo') // => false
NSID.isValid('foo') // => false
```

## License
Expand Down
1 change: 1 addition & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@adxp/crypto": "*",
"@adxp/lexicon": "*",
"@adxp/repo": "*",
"@adxp/uri": "*",
"cors": "^2.8.5",
"dotenv": "^16.0.0",
"express": "^4.17.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/api/todo/adx/repo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Server } from '../../../lexicon'
import { InvalidRequestError, AuthRequiredError } from '@adxp/xrpc-server'
import { resolveName, AdxUri, TID } from '@adxp/common'
import { AuthStore } from '@adxp/auth'
import { resolveName, TID } from '@adxp/common'
import { AdxUri } from '@adxp/uri'
import * as didSdk from '@adxp/did-sdk'
import * as repoDiff from '../../../repo-diff'
import * as util from '../../../util'
Expand Down
14 changes: 8 additions & 6 deletions packages/server/src/api/todo/social/getPostThread.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Server } from '../../../lexicon'
import { AuthRequiredError } from '@adxp/xrpc-server'
import { AuthRequiredError, InvalidRequestError } from '@adxp/xrpc-server'
import { DataSource } from 'typeorm'
import * as GetPostThread from '../../../lexicon/types/todo/social/getPostThread'
import { PostIndex } from '../../../db/records/post'
Expand Down Expand Up @@ -27,16 +27,18 @@ export default function (server: Server) {
uri,
})
.getRawOne()
if (!queryRes) {
throw new InvalidRequestError(`Post not found: ${uri}`)
}

let thread = rowToPost(queryRes)
const thread = rowToPost(queryRes)
if (depth > 0) {
thread.replies = await getReplies(db.db, thread, depth - 1, requester)
}
if (queryRes.parent !== null) {
const parentRes = await postInfoBuilder(db.db, requester).where(
'post.uri = :uri',
{ uri: queryRes.parent },
)
const parentRes = await postInfoBuilder(db.db, requester)
.where('post.uri = :uri', { uri: queryRes.parent })
.getRawOne()
thread.parent = rowToPost(parentRes)
}

Expand Down
5 changes: 4 additions & 1 deletion packages/server/src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import profilePlugin, {
ProfileIndex,
} from './records/profile'
import repostPlugin, { RepostIndex } from './records/repost'
import { AdxUri } from '@adxp/common'
import { AdxUri } from '@adxp/uri'
import { CID } from 'multiformats/cid'
import { RepoRoot } from './repo-root'
import { AdxRecord } from './record'
Expand Down Expand Up @@ -161,6 +161,9 @@ export class Database {
}
record.raw = JSON.stringify(obj)

record.indexedAt = new Date().toISOString()
record.receivedAt = record.indexedAt

const recordTable = this.db.getRepository(AdxRecord)
await recordTable.save(record)

Expand Down
8 changes: 4 additions & 4 deletions packages/server/src/db/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export class AdxRecord {
@Column('text')
raw: string

@CreateDateColumn()
receivedAt: Date
@Column('varchar')
receivedAt: string

@UpdateDateColumn()
indexedAt: Date
@Column('varchar')
indexedAt: string
}
8 changes: 4 additions & 4 deletions packages/server/src/db/records/badge.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { AdxUri } from '@adxp/common'
import { AdxUri } from '@adxp/uri'
import * as Badge from '../../lexicon/types/todo/social/badge'
import {
DataSource,
Entity,
Column,
PrimaryColumn,
Repository,
UpdateDateColumn,
ManyToOne,
} from 'typeorm'
import { DbRecordPlugin } from '../types'
Expand Down Expand Up @@ -38,8 +37,8 @@ export class BadgeIndex {
@Column('datetime')
createdAt: string

@UpdateDateColumn()
indexedAt: Date
@Column('varchar')
indexedAt: string
}

const getFn =
Expand Down Expand Up @@ -68,6 +67,7 @@ const setFn =
badge.assertionType = obj.assertion.type
badge.assertionTag = (obj.assertion as Badge.TagAssertion).tag
badge.createdAt = obj.createdAt
badge.indexedAt = new Date().toISOString()
await repo.save(badge)
}

Expand Down
8 changes: 4 additions & 4 deletions packages/server/src/db/records/follow.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { AdxUri } from '@adxp/common'
import { AdxUri } from '@adxp/uri'
import * as Follow from '../../lexicon/types/todo/social/follow'
import {
DataSource,
Entity,
Column,
PrimaryColumn,
Repository,
UpdateDateColumn,
ManyToOne,
} from 'typeorm'
import { DbRecordPlugin } from '../types'
Expand All @@ -32,8 +31,8 @@ export class FollowIndex {
@Column('datetime')
createdAt: string

@UpdateDateColumn()
indexedAt: Date
@Column('varchar')
indexedAt: string
}

const getFn =
Expand All @@ -60,6 +59,7 @@ const setFn =
follow.creator = uri.host
follow.subject = obj.subject
follow.createdAt = obj.createdAt
follow.indexedAt = new Date().toISOString()
await repo.save(follow)
}

Expand Down
9 changes: 4 additions & 5 deletions packages/server/src/db/records/like.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { AdxUri } from '@adxp/common'
import { AdxUri } from '@adxp/uri'
import * as Like from '../../lexicon/types/todo/social/like'
import {
DataSource,
Entity,
Column,
PrimaryColumn,
Repository,
In,
UpdateDateColumn,
ManyToOne,
} from 'typeorm'
import { DbRecordPlugin } from '../types'
Expand All @@ -33,8 +31,8 @@ export class LikeIndex {
@Column('datetime')
createdAt: string

@UpdateDateColumn()
indexedAt: Date
@Column('varchar')
indexedAt: string
}

const getFn =
Expand All @@ -61,6 +59,7 @@ const setFn =
like.creator = uri.host
like.subject = obj.subject
like.createdAt = obj.createdAt
like.indexedAt = new Date().toISOString()
await repo.save(like)
}

Expand Down
16 changes: 5 additions & 11 deletions packages/server/src/db/records/post.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { AdxUri } from '@adxp/common'
import { AdxUri } from '@adxp/uri'
import * as Post from '../../lexicon/types/todo/social/post'
import {
DataSource,
Entity,
Column,
PrimaryColumn,
UpdateDateColumn,
ManyToOne,
} from 'typeorm'
import { DataSource, Entity, Column, PrimaryColumn, ManyToOne } from 'typeorm'
import { DbRecordPlugin } from '../types'
import { UserDid } from '../user-dids'
import schemas from '../schemas'
Expand Down Expand Up @@ -37,8 +30,8 @@ export class PostIndex {
@Column('datetime')
createdAt: string

@UpdateDateColumn()
indexedAt: Date
@Column('varchar')
indexedAt: string
}

@Entity({ name: `${tableName}_entities` })
Expand Down Expand Up @@ -109,6 +102,7 @@ const setFn =
post.createdAt = obj.createdAt
post.replyRoot = obj.reply?.root
post.replyParent = obj.reply?.parent
post.indexedAt = new Date().toISOString()

await db.getRepository(PostIndex).save(post)
}
Expand Down
15 changes: 5 additions & 10 deletions packages/server/src/db/records/profile.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { AdxUri } from '@adxp/common'
import { AdxUri } from '@adxp/uri'
import * as Profile from '../../lexicon/types/todo/social/profile'
import {
DataSource,
Entity,
Column,
PrimaryColumn,
UpdateDateColumn,
} from 'typeorm'
import { DataSource, Entity, Column, PrimaryColumn } from 'typeorm'
import { DbRecordPlugin } from '../types'
import schemas from '../schemas'
import { collectionToTableName } from '../util'
Expand All @@ -28,8 +22,8 @@ export class ProfileIndex {
@Column({ type: 'text', nullable: true })
description?: string

@UpdateDateColumn()
indexedAt: Date
@Column('varchar')
indexedAt: string
}

@Entity({ name: `${tableName}_badges` })
Expand Down Expand Up @@ -84,6 +78,7 @@ const setFn =
profile.creator = uri.host
profile.displayName = obj.displayName
profile.description = obj.description
profile.indexedAt = new Date().toISOString()
await db.getRepository(ProfileIndex).save(profile)
}

Expand Down
9 changes: 4 additions & 5 deletions packages/server/src/db/records/repost.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { AdxUri } from '@adxp/common'
import { AdxUri } from '@adxp/uri'
import * as Repost from '../../lexicon/types/todo/social/repost'
import {
DataSource,
Entity,
Column,
PrimaryColumn,
Repository,
In,
UpdateDateColumn,
ManyToOne,
} from 'typeorm'
import { DbRecordPlugin } from '../types'
Expand Down Expand Up @@ -35,8 +33,8 @@ export class RepostIndex {
@Column('datetime')
createdAt: string

@UpdateDateColumn()
indexedAt: Date
@Column('varchar')
indexedAt: string
}

const getFn =
Expand All @@ -63,6 +61,7 @@ const setFn =
repost.creator = uri.host
repost.subject = obj.subject
repost.createdAt = obj.createdAt
repost.indexedAt = new Date().toISOString()

await repo.save(repost)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/db/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdxUri } from '@adxp/common'
import { AdxUri } from '@adxp/uri'
import { ValidationResult } from '@adxp/lexicon'

export type DbRecordPlugin<T, S> = {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/repo-diff.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdxUri } from '@adxp/common'
import { AdxUri } from '@adxp/uri'
import { DataDiff, Repo } from '@adxp/repo'
import Database from './db'

Expand Down
2 changes: 1 addition & 1 deletion packages/server/tests/crud.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AdxApi, { ServiceClient as AdxServiceClient } from '@adxp/api'
import * as Post from '@adxp/api/src/types/todo/social/post'
import { AdxUri } from '@adxp/common'
import { AdxUri } from '@adxp/uri'
import * as util from './_util'

const alice = {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/tests/views.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AdxApi, { ServiceClient as AdxServiceClient } from '@adxp/api'
import { AdxUri } from '@adxp/common'
import { AdxUri } from '@adxp/uri'
import { users, posts, replies } from './test-data'
import * as util from './_util'

Expand Down
3 changes: 2 additions & 1 deletion packages/server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
{ "path": "../common/tsconfig.build.json" },
{ "path": "../crypto/tsconfig.build.json" },
{ "path": "../lexicon/tsconfig.build.json" },
{ "path": "../repo/tsconfig.build.json" },
{ "path": "../lex-cli/tsconfig.build.json" },
{ "path": "../repo/tsconfig.build.json" },
{ "path": "../uri/tsconfig.build.json" },
{ "path": "../xrpc-server/tsconfig.build.json" }
]
}
2 changes: 2 additions & 0 deletions packages/uri/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
Loading

0 comments on commit dcba744

Please sign in to comment.