Skip to content

Commit

Permalink
improve tag validation (#1684)
Browse files Browse the repository at this point in the history
* check faceted tags for slurs

* format

* fix baby bug
  • Loading branch information
estrattonbailey authored Sep 28, 2023
1 parent 173f3bf commit 9b2ba28
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
13 changes: 12 additions & 1 deletion packages/pds/src/repo/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
Record as PostRecord,
isRecord as isPost,
} from '../lexicon/types/app/bsky/feed/post'
import { isTag } from '../lexicon/types/app/bsky/richtext/facet'
import { isRecord as isList } from '../lexicon/types/app/bsky/graph/list'
import { isRecord as isProfile } from '../lexicon/types/app/bsky/actor/profile'
import { hasExplicitSlur } from '../handle/explicit-slurs'
Expand Down Expand Up @@ -300,7 +301,17 @@ function assertNoExplicitSlurs(rkey: string, record: RepoRecord) {
toCheck += ' ' + rkey
toCheck += ' ' + record.displayName
} else if (isPost(record)) {
toCheck += record.tags?.join(' ')
if (record.tags) {
toCheck += record.tags.join(' ')
}

for (const facet of record.facets || []) {
for (const feat of facet.features) {
if (isTag(feat)) {
toCheck += ' ' + feat.tag
}
}
}
}
if (hasExplicitSlur(toCheck)) {
throw new InvalidRecordError('Unacceptable slur in record')
Expand Down
35 changes: 34 additions & 1 deletion packages/pds/tests/create-post.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import AtpAgent, { AppBskyFeedPost, AtUri } from '@atproto/api'
import AtpAgent, {
AppBskyFeedPost,
AtUri,
RichText,
AppBskyRichtextFacet,
} from '@atproto/api'
import { runTestServer, TestServerInfo } from './_util'
import { SeedClient } from './seeds/client'
import basicSeed from './seeds/basic'
Expand Down Expand Up @@ -42,4 +47,32 @@ describe('pds posts record creation', () => {
expect(record).toBeTruthy()
expect(record.tags).toEqual(['javascript', 'hehe'])
})

it('handles RichText tag facets as well', async () => {
const rt = new RichText({ text: 'hello #world' })
await rt.detectFacets(agent)

const post: AppBskyFeedPost.Record = {
text: rt.text,
facets: rt.facets,
createdAt: new Date().toISOString(),
}

const res = await agent.api.app.bsky.feed.post.create(
{ repo: sc.dids.alice },
post,
sc.getHeaders(sc.dids.alice),
)
const { value: record } = await agent.api.app.bsky.feed.post.get({
repo: sc.dids.alice,
rkey: new AtUri(res.uri).rkey,
})

expect(record).toBeTruthy()
expect(
record.facets?.every((f) => {
return AppBskyRichtextFacet.isTag(f.features[0])
}),
).toBeTruthy()
})
})

0 comments on commit 9b2ba28

Please sign in to comment.