forked from bluesky-social/atproto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add protected tag setting (bluesky-social#3050)
* ✨ Add protected tag setting * ✅ Add tests for protected tag options * ✨ Validate mod and role list * 🧹 Replace usage of objects with Map * 🐛 Fix setting validator getter
- Loading branch information
Showing
7 changed files
with
428 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const ProtectedTagSettingKey = 'tools.ozone.setting.protectedTags' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type ProtectedTagSetting = { | ||
[key: string]: { roles?: string[]; moderators?: string[] } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Selectable } from 'kysely' | ||
import { Setting } from '../db/schema/setting' | ||
import { ProtectedTagSettingKey } from './constants' | ||
import { InvalidRequestError } from '@atproto/xrpc-server' | ||
|
||
export const settingValidators = new Map< | ||
string, | ||
(setting: Partial<Selectable<Setting>>) => Promise<void> | ||
>([ | ||
[ | ||
ProtectedTagSettingKey, | ||
async (setting: Partial<Selectable<Setting>>) => { | ||
if (setting.managerRole !== 'tools.ozone.team.defs#roleAdmin') { | ||
throw new InvalidRequestError( | ||
'Only admins should be able to configure protected tags', | ||
) | ||
} | ||
|
||
if (typeof setting.value !== 'object') { | ||
throw new InvalidRequestError('Invalid value') | ||
} | ||
for (const [key, val] of Object.entries(setting.value)) { | ||
if (!val || typeof val !== 'object') { | ||
throw new InvalidRequestError(`Invalid configuration for tag ${key}`) | ||
} | ||
|
||
if (!val['roles'] && !val['moderators']) { | ||
throw new InvalidRequestError( | ||
`Must define who a list of moderators or a role who can action subjects with ${key} tag`, | ||
) | ||
} | ||
|
||
if (val['roles']) { | ||
if (!Array.isArray(val['roles'])) { | ||
throw new InvalidRequestError( | ||
`Roles must be an array of moderator roles for tag ${key}`, | ||
) | ||
} | ||
if (!val['roles']?.length) { | ||
throw new InvalidRequestError( | ||
`Must define at least one role for tag ${key}`, | ||
) | ||
} | ||
} | ||
|
||
if (val['moderators']) { | ||
if (!Array.isArray(val['moderators'])) { | ||
throw new InvalidRequestError( | ||
`Moderators must be an array of moderator DIDs for tag ${key}`, | ||
) | ||
} | ||
if (!val['moderators']?.length) { | ||
throw new InvalidRequestError( | ||
`Must define at least one moderator DID for tag ${key}`, | ||
) | ||
} | ||
} | ||
} | ||
}, | ||
], | ||
]) |
Oops, something went wrong.