Update to latest APIs

This commit is contained in:
Paul Frazee 2022-10-26 14:34:47 -05:00
parent 349cfe7177
commit 1983512fef
74 changed files with 2334 additions and 525 deletions

View file

@ -1,5 +1,5 @@
import {autorun} from 'mobx'
import AdxApi from '../third-party/api'
import AtpApi from '../third-party/api'
import {RootStoreModel} from './models/root-store'
import * as libapi from './lib/api'
import * as storage from './lib/storage'
@ -17,7 +17,7 @@ export async function setupState() {
libapi.doPolyfill()
const api = AdxApi.service(DEFAULT_SERVICE)
const api = AtpApi.service(DEFAULT_SERVICE)
rootStore = new RootStoreModel(api)
try {
data = (await storage.load(ROOT_STATE_STORAGE_KEY)) || {}

View file

@ -4,32 +4,37 @@
*/
// import {ReactNativeStore} from './auth'
import AdxApi from '../../third-party/api'
import AtpApi from '../../third-party/api'
import * as Profile from '../../third-party/api/src/types/app/bsky/profile'
import {AdxUri} from '../../third-party/uri'
import * as Post from '../../third-party/api/src/types/app/bsky/post'
import {AtUri} from '../../third-party/uri'
import {RootStoreModel} from '../models/root-store'
import {extractEntities} from '../../view/lib/strings'
export function doPolyfill() {
AdxApi.xrpc.fetch = fetchHandler
AtpApi.xrpc.fetch = fetchHandler
}
export async function post(
store: RootStoreModel,
text: string,
replyToUri?: string,
replyTo?: Post.PostRef,
) {
let reply
if (replyToUri) {
const replyToUrip = new AdxUri(replyToUri)
if (replyTo) {
const replyToUrip = new AtUri(replyTo.uri)
const parentPost = await store.api.app.bsky.post.get({
nameOrDid: replyToUrip.host,
tid: replyToUrip.recordKey,
user: replyToUrip.host,
rkey: replyToUrip.rkey,
})
if (parentPost) {
const parentRef = {
uri: parentPost.uri,
cid: parentPost.cid,
}
reply = {
root: parentPost.value.reply?.root || parentPost.uri,
parent: parentPost.uri,
root: parentPost.value.reply?.root || parentRef,
parent: parentRef,
}
}
}
@ -45,39 +50,39 @@ export async function post(
)
}
export async function like(store: RootStoreModel, uri: string) {
export async function like(store: RootStoreModel, uri: string, cid: string) {
return await store.api.app.bsky.like.create(
{did: store.me.did || ''},
{
subject: uri,
subject: {uri, cid},
createdAt: new Date().toISOString(),
},
)
}
export async function unlike(store: RootStoreModel, likeUri: string) {
const likeUrip = new AdxUri(likeUri)
const likeUrip = new AtUri(likeUri)
return await store.api.app.bsky.like.delete({
did: likeUrip.hostname,
tid: likeUrip.recordKey,
rkey: likeUrip.rkey,
})
}
export async function repost(store: RootStoreModel, uri: string) {
export async function repost(store: RootStoreModel, uri: string, cid: string) {
return await store.api.app.bsky.repost.create(
{did: store.me.did || ''},
{
subject: uri,
subject: {uri, cid},
createdAt: new Date().toISOString(),
},
)
}
export async function unrepost(store: RootStoreModel, repostUri: string) {
const repostUrip = new AdxUri(repostUri)
const repostUrip = new AtUri(repostUri)
return await store.api.app.bsky.repost.delete({
did: repostUrip.hostname,
tid: repostUrip.recordKey,
rkey: repostUrip.rkey,
})
}
@ -92,10 +97,10 @@ export async function follow(store: RootStoreModel, subject: string) {
}
export async function unfollow(store: RootStoreModel, followUri: string) {
const followUrip = new AdxUri(followUri)
const followUrip = new AtUri(followUri)
return await store.api.app.bsky.follow.delete({
did: followUrip.hostname,
tid: followUrip.recordKey,
rkey: followUrip.rkey,
})
}
@ -103,15 +108,16 @@ export async function updateProfile(
store: RootStoreModel,
modifyFn: (existing?: Profile.Record) => Profile.Record,
) {
// TODO: replaceme
const res = await store.api.app.bsky.profile.list({
nameOrDid: store.me.did || '',
user: store.me.did || '',
})
const existing = res.records[0]
if (existing) {
await store.api.app.bsky.profile.put(
{
did: store.me.did || '',
tid: new AdxUri(existing.uri).recordKey,
rkey: new AtUri(existing.uri).rkey,
},
modifyFn(existing.value),
)

View file

@ -18,8 +18,8 @@ export class FeedItemModel implements GetHomeFeed.FeedItem {
_reactKey: string = ''
// data
cursor: string = ''
uri: string = ''
cid: string = ''
author: GetHomeFeed.User = {did: '', name: '', displayName: ''}
repostedBy?: GetHomeFeed.User
record: Record<string, unknown> = {}
@ -44,8 +44,8 @@ export class FeedItemModel implements GetHomeFeed.FeedItem {
}
copy(v: GetHomeFeed.FeedItem | GetAuthorFeed.FeedItem) {
this.cursor = v.cursor
this.uri = v.uri
this.cid = v.cid
this.author = v.author
this.repostedBy = v.repostedBy
this.record = v.record
@ -68,7 +68,7 @@ export class FeedItemModel implements GetHomeFeed.FeedItem {
this.myState.like = undefined
})
} else {
const res = await apilib.like(this.rootStore, this.uri)
const res = await apilib.like(this.rootStore, this.uri, this.cid)
runInAction(() => {
this.likeCount++
this.myState.like = res.uri
@ -84,7 +84,7 @@ export class FeedItemModel implements GetHomeFeed.FeedItem {
this.myState.repost = undefined
})
} else {
const res = await apilib.repost(this.rootStore, this.uri)
const res = await apilib.repost(this.rootStore, this.uri, this.cid)
runInAction(() => {
this.repostCount++
this.myState.repost = res.uri
@ -101,6 +101,7 @@ export class FeedModel {
hasReachedEnd = false
error = ''
params: GetHomeFeed.QueryParams | GetAuthorFeed.QueryParams
loadMoreCursor: string | undefined
_loadPromise: Promise<void> | undefined
_loadMorePromise: Promise<void> | undefined
_loadLatestPromise: Promise<void> | undefined
@ -119,6 +120,7 @@ export class FeedModel {
{
rootStore: false,
params: false,
loadMoreCursor: false,
_loadPromise: false,
_loadMorePromise: false,
_loadLatestPromise: false,
@ -141,13 +143,6 @@ export class FeedModel {
return this.hasLoaded && !this.hasContent
}
get loadMoreCursor() {
if (this.hasContent) {
return this.feed[this.feed.length - 1].cursor
}
return undefined
}
// public api
// =
@ -316,6 +311,7 @@ export class FeedModel {
}
private _appendAll(res: GetHomeFeed.Response | GetAuthorFeed.Response) {
this.loadMoreCursor = res.data.cursor
let counter = this.feed.length
for (const item of res.data.feed) {
this._append(counter++, item)

View file

@ -1,5 +1,5 @@
import {makeAutoObservable, runInAction} from 'mobx'
import {AdxUri} from '../../third-party/uri'
import {AtUri} from '../../third-party/uri'
import * as GetLikedBy from '../../third-party/api/src/types/app/bsky/getLikedBy'
import {RootStoreModel} from './root-store'
@ -101,7 +101,7 @@ export class LikedByViewModel {
// =
private async _resolveUri() {
const urip = new AdxUri(this.params.uri)
const urip = new AtUri(this.params.uri)
if (!urip.host.startsWith('did:')) {
urip.host = await this.rootStore.resolveName(urip.host)
}

View file

@ -13,6 +13,7 @@ export class NotificationsViewItemModel implements GroupedNotification {
// data
uri: string = ''
cid: string = ''
author: {
did: string
name: string
@ -37,6 +38,7 @@ export class NotificationsViewItemModel implements GroupedNotification {
copy(v: GroupedNotification) {
this.uri = v.uri
this.cid = v.cid
this.author = v.author
this.reason = v.reason
this.reasonSubject = v.reasonSubject
@ -92,6 +94,7 @@ export class NotificationsViewModel {
hasLoaded = false
error = ''
params: GetNotifications.QueryParams
loadMoreCursor?: string
_loadPromise: Promise<void> | undefined
_loadMorePromise: Promise<void> | undefined
_updatePromise: Promise<void> | undefined
@ -129,21 +132,6 @@ export class NotificationsViewModel {
return this.hasLoaded && !this.hasContent
}
get loadMoreCursor() {
if (this.hasContent) {
const last = this.notifications[this.notifications.length - 1]
if (last.additional?.length) {
// get the lowest indexedAt from all available
return [last, ...last.additional].reduce(
(acc, v) => (v.indexedAt < acc ? v.indexedAt : acc),
last.indexedAt,
)
}
return last.indexedAt
}
return undefined
}
// public api
// =
@ -283,6 +271,7 @@ export class NotificationsViewModel {
}
private _appendAll(res: GetNotifications.Response) {
this.loadMoreCursor = res.data.cursor
let counter = this.notifications.length
for (const item of groupNotifications(res.data.notifications)) {
this._append(counter++, item)

View file

@ -1,6 +1,6 @@
import {makeAutoObservable, runInAction} from 'mobx'
import * as GetPostThread from '../../third-party/api/src/types/app/bsky/getPostThread'
import {AdxUri} from '../../third-party/uri'
import {AtUri} from '../../third-party/uri'
import _omit from 'lodash.omit'
import {RootStoreModel} from './root-store'
import * as apilib from '../lib/api'
@ -29,6 +29,7 @@ export class PostThreadViewPostModel implements GetPostThread.Post {
// data
uri: string = ''
cid: string = ''
author: GetPostThread.User = {did: '', name: '', displayName: ''}
record: Record<string, unknown> = {}
embed?:
@ -112,7 +113,7 @@ export class PostThreadViewPostModel implements GetPostThread.Post {
this.myState.like = undefined
})
} else {
const res = await apilib.like(this.rootStore, this.uri)
const res = await apilib.like(this.rootStore, this.uri, this.cid)
runInAction(() => {
this.likeCount++
this.myState.like = res.uri
@ -128,7 +129,7 @@ export class PostThreadViewPostModel implements GetPostThread.Post {
this.myState.repost = undefined
})
} else {
const res = await apilib.repost(this.rootStore, this.uri)
const res = await apilib.repost(this.rootStore, this.uri, this.cid)
runInAction(() => {
this.repostCount++
this.myState.repost = res.uri
@ -226,7 +227,7 @@ export class PostThreadViewModel {
// =
private async _resolveUri() {
const urip = new AdxUri(this.params.uri)
const urip = new AtUri(this.params.uri)
if (!urip.host.startsWith('did:')) {
urip.host = await this.rootStore.resolveName(urip.host)
}

View file

@ -1,6 +1,6 @@
import {makeAutoObservable} from 'mobx'
import * as Post from '../../third-party/api/src/types/app/bsky/post'
import {AdxUri} from '../../third-party/uri'
import {AtUri} from '../../third-party/uri'
import {RootStoreModel} from './root-store'
export type PostEntities = Post.Record['entities']
@ -76,10 +76,10 @@ export class PostModel implements RemoveIndex<Post.Record> {
private async _load() {
this._xLoading()
try {
const urip = new AdxUri(this.uri)
const urip = new AtUri(this.uri)
const res = await this.rootStore.api.app.bsky.post.get({
nameOrDid: urip.host,
tid: urip.recordKey,
user: urip.host,
rkey: urip.rkey,
})
// TODO
// if (!res.valid) {

View file

@ -28,7 +28,7 @@ export class ProfileViewModel {
followersCount: number = 0
followsCount: number = 0
postsCount: number = 0
badges: GetProfile.Badge[] = []
pinnedBadges: GetProfile.Badge[] = []
myState = new ProfileViewMyStateModel()
constructor(
@ -134,7 +134,7 @@ export class ProfileViewModel {
this.followersCount = res.data.followersCount
this.followsCount = res.data.followsCount
this.postsCount = res.data.postsCount
this.badges = res.data.badges
this.pinnedBadges = res.data.pinnedBadges
if (res.data.myState) {
Object.assign(this.myState, res.data.myState)
}

View file

@ -1,5 +1,5 @@
import {makeAutoObservable, runInAction} from 'mobx'
import {AdxUri} from '../../third-party/uri'
import {AtUri} from '../../third-party/uri'
import * as GetRepostedBy from '../../third-party/api/src/types/app/bsky/getRepostedBy'
import {RootStoreModel} from './root-store'
@ -101,7 +101,7 @@ export class RepostedByViewModel {
// =
private async _resolveUri() {
const urip = new AdxUri(this.params.uri)
const urip = new AtUri(this.params.uri)
if (!urip.host.startsWith('did:')) {
urip.host = await this.rootStore.resolveName(urip.host)
}

View file

@ -3,7 +3,7 @@
*/
import {makeAutoObservable} from 'mobx'
import AdxApi from '../../third-party/api'
import AtpApi from '../../third-party/api'
import type {ServiceClient} from '../../third-party/api/src/index'
import {createContext, useContext} from 'react'
import {isObj, hasProp} from '../lib/type-guards'
@ -74,7 +74,7 @@ export class RootStoreModel {
}
}
const throwawayInst = new RootStoreModel(AdxApi.service('http://localhost')) // this will be replaced by the loader
const throwawayInst = new RootStoreModel(AtpApi.service('http://localhost')) // this will be replaced by the loader
const RootStoreContext = createContext<RootStoreModel>(throwawayInst)
export const RootStoreProvider = RootStoreContext.Provider
export const useStores = () => useContext(RootStoreContext)

View file

@ -1,5 +1,5 @@
import {makeAutoObservable} from 'mobx'
import AdxApi from '../../third-party/api'
import AtpApi from '../../third-party/api'
import type * as GetAccountsConfig from '../../third-party/api/src/types/com/atproto/getAccountsConfig'
import {isObj, hasProp} from '../lib/type-guards'
import {RootStoreModel} from './root-store'
@ -135,7 +135,7 @@ export class SessionModel {
}
async describeService(service: string): Promise<ServiceDescription> {
const api = AdxApi.service(service)
const api = AtpApi.service(service)
const res = await api.com.atproto.getAccountsConfig({})
return res.data
}
@ -149,7 +149,7 @@ export class SessionModel {
username: string
password: string
}) {
const api = AdxApi.service(service)
const api = AtpApi.service(service)
const res = await api.com.atproto.createSession({}, {username, password})
if (res.data.jwt) {
this.setState({
@ -178,7 +178,7 @@ export class SessionModel {
username: string
inviteCode?: string
}) {
const api = AdxApi.service(service)
const api = AtpApi.service(service)
const res = await api.com.atproto.createAccount(
{},
{username, password, email, inviteCode},

View file

@ -1,5 +1,6 @@
import {makeAutoObservable, runInAction} from 'mobx'
import {makeAutoObservable} from 'mobx'
import {ProfileViewModel} from './profile-view'
import * as Post from '../../third-party/api/src/types/app/bsky/post'
export class TabsSelectorModel {
name = 'tabs-selector'
@ -35,12 +36,12 @@ export class SharePostModel {
}
export interface ComposePostModelOpts {
replyTo?: string
replyTo?: Post.PostRef
onPost?: () => void
}
export class ComposePostModel {
name = 'compose-post'
replyTo?: string
replyTo?: Post.PostRef
onPost?: () => void
constructor(opts?: ComposePostModelOpts) {