Skip to content

Commit

Permalink
Add alias field, codegen with deprecation comment
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey committed Dec 19, 2024
1 parent feebf80 commit 49c45c8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
22 changes: 1 addition & 21 deletions packages/api/definitions/labels.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,27 +147,7 @@
},
{
"identifier": "graphic-media",
"flags": ["adult"],
"configurable": true,
"defaultSetting": "warn",
"severity": "none",
"blurs": "media",
"behaviors": {
"account": {
"avatar": "blur",
"banner": "blur"
},
"profile": {
"avatar": "blur",
"banner": "blur"
},
"content": {
"contentMedia": "blur"
}
}
},
{
"identifier": "gore",
"alias": ["gore"],
"flags": ["adult"],
"configurable": true,
"defaultSetting": "warn",
Expand Down
42 changes: 33 additions & 9 deletions packages/api/scripts/code/labels.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,42 @@ writeFileSync(
)

async function gen() {
const knownValues = new Set()
const flattenedLabelDefs = []

for (const {alias: aliases, ...label} of labelsDef) {
knownValues.add(label.identifier)
flattenedLabelDefs.push([label.identifier, { ...label, locales: [] }])

if (aliases) {
for (const alias of aliases) {
knownValues.add(alias)
flattenedLabelDefs.push([
alias,
{
...label,
identifier: alias,
locales: [],
comment: `@deprecated alias for \`${label.identifier}\``,
},
])
}
}
}

let labelDefsStr = `{`
for (const [key, { comment, ...value }] of flattenedLabelDefs) {
const commentStr = comment ? `\n/** ${comment} */\n` : ''
labelDefsStr += `${commentStr}'${key}': ${JSON.stringify(value, null, 2)},`
}
labelDefsStr += `}`

return prettier.format(
`/** this doc is generated by ./scripts/code/labels.mjs **/
import {InterpretedLabelValueDefinition, LabelPreference} from '../types'
export type KnownLabelValue = ${labelsDef
.map((label) => `"${label.identifier}"`)
export type KnownLabelValue = ${Array.from(knownValues)
.map((value) => `"${value}"`)
.join(' | ')}
export const DEFAULT_LABEL_SETTINGS: Record<string, LabelPreference> = ${JSON.stringify(
Expand All @@ -35,13 +65,7 @@ async function gen() {
),
)}
export const LABELS: Record<KnownLabelValue, InterpretedLabelValueDefinition> = ${JSON.stringify(
Object.fromEntries(
labelsDef.map((label) => [label.identifier, { ...label, locales: [] }]),
),
null,
2,
)}
export const LABELS: Record<KnownLabelValue, InterpretedLabelValueDefinition> = ${labelDefsStr}
`,
{ semi: false, parser: 'typescript', singleQuote: true },
)
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/moderation/const/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const DEFAULT_LABEL_SETTINGS: Record<string, LabelPreference> = {
sexual: 'warn',
nudity: 'ignore',
'graphic-media': 'warn',
gore: 'warn',
}

export const LABELS: Record<KnownLabelValue, InterpretedLabelValueDefinition> =
Expand Down Expand Up @@ -195,6 +194,7 @@ export const LABELS: Record<KnownLabelValue, InterpretedLabelValueDefinition> =
},
locales: [],
},
/** @deprecated alias for `graphic-media` */
gore: {
identifier: 'gore',
flags: ['adult'],
Expand Down

0 comments on commit 49c45c8

Please sign in to comment.