diff options
author | Paul Frazee <pfrazee@gmail.com> | 2022-07-22 11:18:47 -0500 |
---|---|---|
committer | Paul Frazee <pfrazee@gmail.com> | 2022-07-22 11:18:47 -0500 |
commit | 0ec0ba996f05876d78039509e0ea61528c5faeec (patch) | |
tree | 1184d8adc886033c312def5b8326a8eda1d8c921 /src/state/lib/api.ts | |
parent | cc8a1702043cfbd783f4caa61ab9eb8aeb421072 (diff) | |
download | voidsky-0ec0ba996f05876d78039509e0ea61528c5faeec.tar.zst |
Implement like and repost
Diffstat (limited to 'src/state/lib/api.ts')
-rw-r--r-- | src/state/lib/api.ts | 83 |
1 files changed, 82 insertions, 1 deletions
diff --git a/src/state/lib/api.ts b/src/state/lib/api.ts index c2b992777..b3992544b 100644 --- a/src/state/lib/api.ts +++ b/src/state/lib/api.ts @@ -4,7 +4,16 @@ */ // import {ReactNativeStore} from './auth' -import {AdxClient, AdxRepoClient, AdxUri, bsky} from '@adxp/mock-api' +import { + AdxClient, + AdxRepoClient, + AdxRepoCollectionClient, + AdxUri, + bsky, + SchemaOpt, + ListRecordsResponseValidated, + GetRecordResponseValidated, +} from '@adxp/mock-api' import * as storage from './storage' import {postTexts} from './mock-data/post-texts' import {replyTexts} from './mock-data/reply-texts' @@ -19,6 +28,78 @@ export async function setup(adx: AdxClient) { ) } +export async function like(adx: AdxClient, user: string, uri: string) { + await adx.repo(user, true).collection('blueskyweb.xyz:Likes').create('Like', { + $type: 'blueskyweb.xyz:Like', + subject: uri, + createdAt: new Date().toISOString(), + }) +} + +export async function unlike(adx: AdxClient, user: string, uri: string) { + const coll = adx.repo(user, true).collection('blueskyweb.xyz:Likes') + const numDels = await deleteWhere(coll, 'Like', record => { + return record.value.subject === uri + }) + return numDels > 0 +} + +export async function repost(adx: AdxClient, user: string, uri: string) { + await adx + .repo(user, true) + .collection('blueskyweb.xyz:Posts') + .create('Repost', { + $type: 'blueskyweb.xyz:Repost', + subject: uri, + createdAt: new Date().toISOString(), + }) +} + +export async function unrepost(adx: AdxClient, user: string, uri: string) { + const coll = adx.repo(user, true).collection('blueskyweb.xyz:Posts') + const numDels = await deleteWhere(coll, 'Repost', record => { + return record.value.subject === uri + }) + return numDels > 0 +} + +type WherePred = (record: GetRecordResponseValidated) => Boolean +async function deleteWhere( + coll: AdxRepoCollectionClient, + schema: SchemaOpt, + cond: WherePred, +) { + const toDelete: string[] = [] + iterateAll(coll, schema, record => { + if (cond(record)) { + toDelete.push(record.key) + } + }) + for (const key of toDelete) { + await coll.del(key) + } + return toDelete.length +} + +type IterateAllCb = (record: GetRecordResponseValidated) => void +async function iterateAll( + coll: AdxRepoCollectionClient, + schema: SchemaOpt, + cb: IterateAllCb, +) { + let cursor + let res: ListRecordsResponseValidated + do { + res = await coll.list(schema, {after: cursor, limit: 100}) + for (const record of res.records) { + if (record.valid) { + cb(record) + cursor = record.key + } + } + } while (res.records.length === 100) +} + // TEMPORARY // mock api config // ======= |