Applications model
#開発 #ATP #atproto
ATプロトコル | Guides | Applications
TL;DR:
アプリはユーザーの PDS にサインインして、アカウントにアクセスします
アプリはリポジトリ レコードを直接読み書きできます
ほとんどの相互作用は、より高いレベルのLexiconを通じて発生します
アプリケーション モデル
AT プロトコル上のアプリケーションは、ユーザーの個人データ サーバー (PDS) に接続して、ユーザーのアカウントにアクセスします。 セッションが確立されると、アプリは PDS によって実装されたlexiconを使用して動作を駆動できます。
このガイドでは、いくつかの一般的なパターンを (簡単なコード例と共に) 順を追って説明し、これについて直感的に理解できるようにします。 以下に示すすべての API は、Lexicon のコード ジェネレーター CLI を使用して生成されます。
サインイン
サインインと認証は、単純なセッション指向のプロセスです。 com.atproto.server lexiconには、これらのセッションを作成および管理するための API が含まれています。
code:js
// 自分のPDSのAPIインスタンスを作成する
const api = AtpApi.service('my-pds.com')
// 自分のハンドルとパスワードを使ってサインインする
const res = await api.com.atproto.server.createSession({
identifier: 'alice.host.com',
password: 'hunter2'
})
// Authorization ヘッダにトークンを含むように呼び出し(future call)を設定する
api.setHeader('Authorization', Bearer ${res.data.accessJwt})
Repo CRUD
すべてのユーザーは公開データ リポジトリを持っています。 アプリケーションは、API を使用してレコードに対して基本的な CRUD を実行できます。
code:js
await api.com.atproto.repo.listRecords({
repo: 'alice.com',
collection: 'app.bsky.post'
})
await api.com.atproto.repo.getRecord({
repo: 'alice.com',
collection: 'app.bsky.post',
reky: '3jyfrk3olgd2h'
})
await api.com.atproto.repo.createRecord({
repo: 'alice.com',
collection: 'app.bsky.post'
}, {
text: 'Second post!',
createdAt: (new Date()).toISOString()
})
await api.com.atproto.repo.putRecord({
repo: 'alice.com',
collection: 'app.bsky.post',
rkey: '3jyfrk3olgd2h'
}, {
text: 'Hello universe!',
createdAt: originalPost.data.createdAt
})
await api.com.atproto.repo.deleteRecord({
repo: 'alice.com',
collection: 'app.bsky.post',
rkey: '3jyfrk3olgd2h'
})
上記のrepoは、ドメイン名 alice.com で識別されていることに気付くかもしれません。 詳細については、Identity guideを参照してください。
Record types
"type" フィールドに気付き、それがどのように機能するのか疑問に思っている場合は、Intro to Lexicon ガイドを参照してください。 以下は、現在 ATP ソフトウェアで使用されているタイプの短いリストです。
一部のスキーマで「cid」に気付くでしょう。 「cid」は「コンテンツ ID」であり、参照されるコンテンツの sha256 ハッシュです。 これらは整合性を確保するために使用されます。 たとえば、いいね! には、いいね! されている投稿の cid が含まれているため、今後の編集を検出して UI に記録できます。
app.bsky.graph.follow
ソーシャルフォロー。 例:
code:json
{
$type: 'app.bsky.graph.follow',
subject: 'did:plc:bv6ggog3tya2z3vxsub7hnal',
createdAt: '2022-10-10T00:39:08.609Z'
}
app.bsky.feed.like
コンテンツの「いいね!」 例:
code:json
{
$type: 'app.bsky.feed.like',
subject: {
uri: 'at://did:plc:bv6ggog3tya2z3vxsub7hnal/app.bsky.post/1',
cid: 'bafyreif5lqnk3tgbhi5vgqd6wy5dtovfgndhwta6bwla4iqaohuf2yd764'
}
createdAt: '2022-10-10T00:39:08.609Z'
}
app.bsky.feed.post
マイクロブログ投稿。 例:
code:json
{
$type: 'app.bsky.feed.post',
text: 'Hello, world!',
createdAt: '2022-10-10T00:39:08.609Z'
}
app.bsky.actor.profile
ユーザー プロファイル。 例:
code:json
{
$type: 'app.bsky.actor.profile',
displayName: 'Alice',
description: 'A cool hacker'
}
app.bsky.feed.repost
既存のマイクロブログ投稿の再投稿 (リツイートに似ています)。 例:
code:json
{
$type: 'app.bsky.feed.repost',
subject: {
uri: 'at://did:plc:bv6ggog3tya2z3vxsub7hnal/app.bsky.post/1',
cid: 'bafyreif5lqnk3tgbhi5vgqd6wy5dtovfgndhwta6bwla4iqaohuf2yd764'
}
createdAt: '2022-10-10T00:39:08.609Z'
}
ソーシャル API
repo CRUD やその他の低レベルの com.atproto.* API で実行できることはたくさんありますが、app.bsky.* レキシコンはソーシャル アプリケーション向けのより強力で使いやすい API を提供します。
code:js
await api.app.bsky.feed.getTimeline()
await api.app.bsky.feed.getAuthorFeed({author: 'alice.com'})
await api.app.bsky.feed.getPostThread({uri: 'at://alice.com/app.bsky.post/1'})
await api.app.bsky.feed.getLikes({uri: 'at://alice.com/app.bsky.post/1'})
await api.app.bsky.feed.getRepostedBy({uri: 'at://alice.com/app.bsky.post/1'})
await api.app.bsky.actor.getProfile({user: 'alice.com'})
await api.app.bsky.graph.getFollowers({user: 'alice.com'})
await api.app.bsky.graph.getFollows({user: 'alice.com'})
await api.app.bsky.notification.listNotifications()
await api.app.bsky.notification.getUnreadCount()
await api.app.bsky.notification.updateSeen()