Skip to content

Commit a7c2b4a

Browse files
Matrix89dholms
andauthored
Add block record duplicate detection (bluesky-social#1027)
* Add block record duplicate detection * Update packages/pds/src/services/record/index.ts --------- Co-authored-by: Daniel Holmgren <[email protected]>
1 parent 5f0db0c commit a7c2b4a

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

packages/pds/src/api/com/atproto/repo/createRecord.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ async function getBacklinkDeletions(
9696
prepareDelete({ did, collection, rkey })
9797

9898
if (
99-
collection === ids.AppBskyGraphFollow &&
99+
(collection === ids.AppBskyGraphFollow ||
100+
collection === ids.AppBskyGraphBlock) &&
100101
typeof record['subject'] === 'string'
101102
) {
102103
const backlinks = await recordTxn.getRecordBacklinks({

packages/pds/src/services/record/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,13 @@ export class RecordService {
278278
}
279279

280280
// @NOTE in the future this can be replaced with a more generic routine that pulls backlinks based on lex docs.
281-
// For now we just want to ensure we're tracking links from follows, likes, and reposts.
281+
// For now we just want to ensure we're tracking links from follows, blocks, likes, and reposts.
282282

283283
function getBacklinks(uri: AtUri, record: unknown): Backlink[] {
284-
if (record?.['$type'] === ids.AppBskyGraphFollow) {
284+
if (
285+
record?.['$type'] === ids.AppBskyGraphFollow ||
286+
record?.['$type'] === ids.AppBskyGraphBlock
287+
) {
285288
const subject = record['subject']
286289
if (typeof subject !== 'string') {
287290
return []

packages/pds/tests/crud.test.ts

+62
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,68 @@ describe('crud operations', () => {
979979
await expect(getRepost3).resolves.toBeDefined()
980980
})
981981

982+
it('prevents duplicate blocks', async () => {
983+
const now = new Date().toISOString()
984+
985+
const { data: block1 } = await aliceAgent.api.com.atproto.repo.createRecord(
986+
{
987+
repo: alice.did,
988+
collection: 'app.bsky.graph.block',
989+
record: {
990+
$type: 'app.bsky.graph.block',
991+
subject: bob.did,
992+
createdAt: now,
993+
},
994+
},
995+
)
996+
997+
const { data: block2 } = await bobAgent.api.com.atproto.repo.createRecord({
998+
repo: bob.did,
999+
collection: 'app.bsky.graph.block',
1000+
record: {
1001+
$type: 'app.bsky.graph.block',
1002+
subject: alice.did,
1003+
createdAt: now,
1004+
},
1005+
})
1006+
1007+
const { data: block3 } = await aliceAgent.api.com.atproto.repo.createRecord(
1008+
{
1009+
repo: alice.did,
1010+
collection: 'app.bsky.graph.block',
1011+
record: {
1012+
$type: 'app.bsky.graph.block',
1013+
subject: bob.did,
1014+
createdAt: now,
1015+
},
1016+
},
1017+
)
1018+
1019+
const getBlock1 = aliceAgent.api.com.atproto.repo.getRecord({
1020+
repo: alice.did,
1021+
collection: 'app.bsky.graph.block',
1022+
rkey: new AtUri(block1.uri).rkey,
1023+
})
1024+
1025+
await expect(getBlock1).rejects.toThrow('Could not locate record:')
1026+
1027+
const getBlock2 = aliceAgent.api.com.atproto.repo.getRecord({
1028+
repo: bob.did,
1029+
collection: 'app.bsky.graph.block',
1030+
rkey: new AtUri(block2.uri).rkey,
1031+
})
1032+
1033+
await expect(getBlock2).resolves.toBeDefined()
1034+
1035+
const getBlock3 = aliceAgent.api.com.atproto.repo.getRecord({
1036+
repo: alice.did,
1037+
collection: 'app.bsky.graph.block',
1038+
rkey: new AtUri(block3.uri).rkey,
1039+
})
1040+
1041+
await expect(getBlock3).resolves.toBeDefined()
1042+
})
1043+
9821044
it('prevents duplicate follows', async () => {
9831045
const now = new Date().toISOString()
9841046

0 commit comments

Comments
 (0)