|
| 1 | +const { StreamClient } = require('@stream-io/node-sdk'); |
| 2 | + |
| 3 | +const templateNames = [ |
| 4 | + 'moderation_template_activity', |
| 5 | + 'moderation_template_reaction', |
| 6 | +]; |
| 7 | +const rule = { name: 'profanity_en', action: 'remove' }; |
| 8 | + |
| 9 | +function withRequiredRule(blockListConfig = {}) { |
| 10 | + const rules = [...(blockListConfig.rules || [])]; |
| 11 | + const index = rules.findIndex((candidate) => candidate.name === rule.name); |
| 12 | + |
| 13 | + if (index === -1) { |
| 14 | + rules.push(rule); |
| 15 | + } else { |
| 16 | + rules[index] = { ...rules[index], action: rule.action }; |
| 17 | + } |
| 18 | + |
| 19 | + return { ...blockListConfig, rules }; |
| 20 | +} |
| 21 | + |
| 22 | +function hasRequiredRule(blockListConfig) { |
| 23 | + return blockListConfig?.rules?.some( |
| 24 | + (candidate) => |
| 25 | + candidate.name === rule.name && candidate.action === rule.action, |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +async function repairPolicy(client, key) { |
| 30 | + const response = await client.moderation.getConfig({ key }); |
| 31 | + const config = response.config; |
| 32 | + if (!config) { |
| 33 | + throw new Error(`Moderation policy ${key} was not found`); |
| 34 | + } |
| 35 | + if (hasRequiredRule(config.block_list_config)) { |
| 36 | + console.log(`Moderation policy ${key} already contains the required rule`); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const writableFields = [ |
| 41 | + 'async', |
| 42 | + 'team', |
| 43 | + 'ai_audio_config', |
| 44 | + 'ai_image_config', |
| 45 | + 'ai_text_config', |
| 46 | + 'ai_video_config', |
| 47 | + 'automod_platform_circumvention_config', |
| 48 | + 'automod_semantic_filters_config', |
| 49 | + 'automod_toxicity_config', |
| 50 | + 'aws_rekognition_config', |
| 51 | + 'block_list_config', |
| 52 | + 'bodyguard_config', |
| 53 | + 'flood_config', |
| 54 | + 'google_vision_config', |
| 55 | + 'llm_config', |
| 56 | + 'rule_builder_config', |
| 57 | + 'velocity_filter_config', |
| 58 | + 'video_call_rule_config', |
| 59 | + ]; |
| 60 | + const payload = { key }; |
| 61 | + for (const field of writableFields) { |
| 62 | + if (config[field] !== undefined) { |
| 63 | + payload[field] = config[field]; |
| 64 | + } |
| 65 | + } |
| 66 | + payload.block_list_config = withRequiredRule(config.block_list_config); |
| 67 | + |
| 68 | + await client.moderation.upsertConfig(payload); |
| 69 | + console.log(`Repaired moderation policy ${key}`); |
| 70 | +} |
| 71 | + |
| 72 | +async function main() { |
| 73 | + if (!process.env.STREAM_KEY || !process.env.STREAM_SECRET) { |
| 74 | + throw new Error('STREAM_KEY and STREAM_SECRET are required'); |
| 75 | + } |
| 76 | + |
| 77 | + const client = new StreamClient( |
| 78 | + process.env.STREAM_KEY, |
| 79 | + process.env.STREAM_SECRET, |
| 80 | + ); |
| 81 | + const response = await client.moderation.v2QueryTemplates(); |
| 82 | + const templates = new Map( |
| 83 | + response.templates.map((template) => [template.name, template]), |
| 84 | + ); |
| 85 | + const repairedPolicies = new Set(); |
| 86 | + |
| 87 | + for (const name of templateNames) { |
| 88 | + const template = templates.get(name); |
| 89 | + if (!template?.config) { |
| 90 | + throw new Error(`Moderation template ${name} was not found`); |
| 91 | + } |
| 92 | + |
| 93 | + if (template.config.config_key) { |
| 94 | + if (!repairedPolicies.has(template.config.config_key)) { |
| 95 | + await repairPolicy(client, template.config.config_key); |
| 96 | + repairedPolicies.add(template.config.config_key); |
| 97 | + } |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + if (hasRequiredRule(template.config.block_list_config)) { |
| 102 | + console.log(`Moderation template ${name} already contains the required rule`); |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + await client.moderation.v2UpsertTemplate({ |
| 107 | + name, |
| 108 | + config: { |
| 109 | + ...template.config, |
| 110 | + block_list_config: withRequiredRule(template.config.block_list_config), |
| 111 | + }, |
| 112 | + }); |
| 113 | + console.log(`Repaired moderation template ${name}`); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +main().catch((error) => { |
| 118 | + console.error(error instanceof Error ? error.message : error); |
| 119 | + process.exitCode = 1; |
| 120 | +}); |
0 commit comments