Add update/delete/subscribe/unsubscribe to lists

This commit is contained in:
Paul Frazee
2023-05-07 16:52:14 -05:00
parent 00a37ec6d9
commit 2482f41ffb
11 changed files with 441 additions and 46 deletions
+87 -1
View File
@@ -1,5 +1,6 @@
import {makeAutoObservable} from 'mobx'
import {
AtUri,
AppBskyGraphGetList as GetList,
AppBskyGraphDefs as GraphDefs,
AppBskyGraphList,
@@ -49,12 +50,14 @@ export class ListModel {
)
record.avatar = blobRes.data.blob
}
return await rootStore.agent.app.bsky.graph.list.create(
const res = await rootStore.agent.app.bsky.graph.list.create(
{
repo: rootStore.me.did,
},
record,
)
await rootStore.agent.app.bsky.graph.subscribeMuteList({list: res.uri})
return res
}
constructor(public rootStore: RootStoreModel, public uri: string) {
@@ -79,6 +82,10 @@ export class ListModel {
return this.hasLoaded && !this.hasContent
}
get isOwner() {
return this.list?.creator.did === this.rootStore.me.did
}
// public api
// =
@@ -108,6 +115,85 @@ export class ListModel {
}
})
async updateMetadata({
name,
description,
avatar,
}: {
name: string
description: string
avatar: RNImage | null | undefined
}) {
if (!this.isOwner) {
throw new Error('Cannot edit this list')
}
// get the current record
const {rkey} = new AtUri(this.uri)
const {value: record} = await this.rootStore.agent.app.bsky.graph.list.get({
repo: this.rootStore.me.did,
rkey,
})
// update the fields
record.name = name
record.description = description
if (avatar) {
const blobRes = await apilib.uploadBlob(
this.rootStore,
avatar.path,
avatar.mime,
)
record.avatar = blobRes.data.blob
} else if (avatar === null) {
record.avatar = undefined
}
// TODO: server doesn't accept puts for list records yet
throw new Error('TODO')
// return await this.rootStore.agent.app.bsky.graph.list.(
// {
// repo: this.rootStore.me.did,
// },
// record,
// )
}
async delete() {
// fetch all the listitem records that belong to this list
let cursor
let records = []
for (let i = 0; i < 100; i++) {
const res = await this.rootStore.agent.app.bsky.graph.listitem.list({
repo: this.rootStore.me.did,
cursor,
limit: PAGE_SIZE,
})
records = records.concat(
res.records.filter(record => record.value.list === this.uri),
)
cursor = res.cursor
if (!cursor) {
break
}
}
// batch delete the list and listitem records
const createDel = (uri: string) => {
const urip = new AtUri(uri)
return {
$type: 'com.atproto.repo.applyWrites#delete',
collection: urip.collection,
rkey: urip.rkey,
}
}
await this.rootStore.agent.com.atproto.repo.applyWrites({
repo: this.rootStore.me.did,
writes: [createDel(this.uri)].concat(
records.map(record => createDel(record.uri)),
),
})
}
/**
* Attempt to load more again after a failure
*/
+6 -3
View File
@@ -5,6 +5,7 @@ import {ProfileModel} from '../content/profile'
import {isObj, hasProp} from 'lib/type-guards'
import {Image as RNImage} from 'react-native-image-crop-picker'
import {ImageModel} from '../media/image'
import {ListModel} from '../content/list'
export interface ConfirmModal {
name: 'confirm'
@@ -37,15 +38,17 @@ export interface ReportAccountModal {
did: string
}
export interface CreateMuteListModal {
name: 'create-mute-list'
onCreate?: (uri: string) => void
export interface CreateOrEditMuteListModal {
name: 'create-or-edit-mute-list'
list?: ListModel
onSave?: (uri: string) => void
}
export interface ListAddUserModal {
name: 'list-add-user'
subject: string
displayName: string
onUpdate?: () => void
}
export interface CropImageModal {