-
-
Notifications
You must be signed in to change notification settings - Fork 661
add slack reaction based moderation alerts #5034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a87ade4
Move Slack moderation logic into a seperate service+ update test +rea…
Mr-Rahul-Paul 93f0df1
Fix Slack workspace token placeholder
Mr-Rahul-Paul 9e941af
Add Slack moderation model help text
Mr-Rahul-Paul 4d68f86
Use admin-managed Slack reaction rules
Mr-Rahul-Paul cdc506d
Generalize Slack moderation models as reactions
Mr-Rahul-Paul 1f86707
Remove unnecessary local ngrok origin setting
Mr-Rahul-Paul 02acae1
check test fixes
Mr-Rahul-Paul 71aae42
Merge branch 'main' into pr/Mr-Rahul-Paul/5034
arkid15r baaa11b
Update code
arkid15r 2500f93
Update code
arkid15r 7e8146a
Address comments
arkid15r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,39 @@ | ||
| """Django admin screen for emitted Slack reaction alerts.""" | ||
|
|
||
| from django.contrib import admin | ||
|
|
||
| from apps.slack.models.reaction_alert import ReactionAlert | ||
|
|
||
|
|
||
| @admin.register(ReactionAlert) | ||
| class ReactionAlertAdmin(admin.ModelAdmin): | ||
| """Admin list/search controls for emitted reaction alerts.""" | ||
|
|
||
| list_display = ( | ||
| "conversation", | ||
| "message_ts", | ||
| "report_type", | ||
| "reaction_count", | ||
| "nest_created_at", | ||
| ) | ||
| readonly_fields = ( | ||
| "conversation", | ||
| "message_ts", | ||
| "report_type", | ||
| "reaction_count", | ||
| "reporter_user_ids", | ||
| "alert_message_ts", | ||
| ) | ||
| search_fields = ( | ||
| "conversation__name", | ||
| "message_ts", | ||
| "report_type", | ||
| ) | ||
|
|
||
| def has_add_permission(self, request): | ||
| """Disable manual alert creation in Django admin.""" | ||
| return False | ||
|
|
||
| def has_delete_permission(self, request, obj=None): | ||
| """Disable manual alert deletion in Django admin.""" | ||
| return False |
This file contains hidden or 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,27 @@ | ||
| """Django admin screen for Slack reaction rules.""" | ||
|
|
||
| from django.contrib import admin | ||
|
|
||
| from apps.slack.models.reaction_rule import ReactionRule | ||
|
|
||
|
|
||
| @admin.register(ReactionRule) | ||
| class ReactionRuleAdmin(admin.ModelAdmin): | ||
| """Admin list/search controls for reaction rules.""" | ||
|
|
||
| autocomplete_fields = ("conversation",) | ||
| list_display = ( | ||
| "conversation", | ||
| "emojis", | ||
| "report_type", | ||
| "threshold", | ||
| "is_active", | ||
| ) | ||
| list_filter = ( | ||
| "is_active", | ||
| "report_type", | ||
| ) | ||
| search_fields = ( | ||
| "conversation__name", | ||
| "alert_channel_id", | ||
| ) | ||
This file contains hidden or 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 hidden or 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,130 @@ | ||
| """Handle Slack reaction_added events.""" | ||
|
|
||
| import logging | ||
|
|
||
| from slack_sdk.errors import SlackApiError, SlackClientError | ||
|
|
||
| from apps.slack.blocks import markdown | ||
| from apps.slack.events.event import EventBase | ||
| from apps.slack.models.reaction_alert import ReactionAlert | ||
| from apps.slack.models.reaction_rule import ReactionRule | ||
| from apps.slack.utils.reaction import ( | ||
| format_emojis, | ||
| mention_users, | ||
| parse_message_reaction, | ||
| reaction_from_payload, | ||
| ) | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def fetch_permalink(client, channel_id: str, message_ts: str) -> str: | ||
| """Return a Slack permalink for the message, or an empty string.""" | ||
| try: | ||
| return ( | ||
| client.chat_getPermalink( | ||
| channel=channel_id, | ||
| message_ts=message_ts, | ||
| ).get("permalink") | ||
| or "" | ||
| ) | ||
| except SlackApiError as e: | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| logger.warning( | ||
| "Could not fetch Slack permalink for moderation alert: %s", | ||
| e.response.get("error", "unknown_error"), | ||
| ) | ||
| return "" | ||
| except SlackClientError as e: | ||
| logger.warning("Could not fetch Slack permalink for moderation alert: %s", e) | ||
| return "" | ||
|
|
||
|
|
||
| def fetch_reaction(client, channel_id: str, message_ts: str, emojis: list[str]): | ||
| """Return Slack's current unique-reporter snapshot for the rule emojis, or None.""" | ||
| try: | ||
| payload = client.reactions_get( | ||
| channel=channel_id, | ||
| full=True, | ||
| timestamp=message_ts, | ||
| ) | ||
| except SlackApiError as e: | ||
| logger.warning( | ||
| "Could not fetch Slack reactions for moderation alert: %s", | ||
| e.response.get("error", "unknown_error"), | ||
| ) | ||
| return None | ||
|
|
||
| return reaction_from_payload(payload, emojis) | ||
|
|
||
|
|
||
| class ReactionAdded(EventBase): | ||
| """Handle reaction_added events for moderation alerts.""" | ||
|
|
||
| event_type = "reaction_added" | ||
|
|
||
| def handle_event(self, event, client): | ||
| """Post an alert when Slack shows the rule threshold is reached.""" | ||
| if (details := parse_message_reaction(event)) is None: | ||
| return | ||
|
|
||
| channel_id, message_ts, emoji_name = details | ||
| if (rule := ReactionRule.for_emoji(channel_id, emoji_name)) is None: | ||
| return | ||
|
|
||
| if ReactionAlert.exists_for(rule.conversation, message_ts, rule.report_type): | ||
| return | ||
|
|
||
| if (snapshot := fetch_reaction(client, channel_id, message_ts, rule.emojis)) is None: | ||
| return | ||
|
|
||
| reaction_count, reporter_user_ids, permalink, matched_emojis = snapshot | ||
| if reaction_count < rule.threshold: | ||
| return | ||
|
|
||
| # Lock in-flight posts; the DB row is written only after Slack succeeds. | ||
| if ( | ||
| owner := ReactionAlert.acquire(rule.conversation, message_ts, rule.report_type) | ||
| ) is None: | ||
| return | ||
|
|
||
| try: | ||
| if not permalink: | ||
| permalink = fetch_permalink(client, channel_id, message_ts) | ||
|
|
||
| if ReactionAlert.renew(rule.conversation, message_ts, rule.report_type, owner): | ||
| alert_users = mention_users(rule.alert_user_ids) | ||
| reporters = mention_users(reporter_user_ids) | ||
| emojis = format_emojis(matched_emojis) | ||
| text = ( | ||
| f"{alert_users}\n" | ||
| f"A message in <#{channel_id}> reached the " | ||
| f"{rule.report_type} report threshold." | ||
| ) | ||
| if reporters: | ||
| text = f"{text}\nReported by: {reporters} using the following emojis: {emojis}" | ||
| if permalink: | ||
| text = f"{text}\n{permalink}" | ||
| text = text.strip() | ||
|
|
||
| try: | ||
| alert = client.chat_postMessage( | ||
| blocks=[markdown(text)], | ||
| channel=rule.alert_channel_id, | ||
| text=text, | ||
| ) | ||
| except SlackApiError as e: | ||
| logger.warning( | ||
| "Could not post Slack moderation alert: %s", | ||
| e.response.get("error", "unknown_error"), | ||
| ) | ||
| else: | ||
| ReactionAlert.record( | ||
| rule.conversation, | ||
| message_ts, | ||
| rule.report_type, | ||
| reaction_count, | ||
| alert.get("ts", ""), | ||
| reporter_user_ids=reporter_user_ids, | ||
| ) | ||
| finally: | ||
| ReactionAlert.release(rule.conversation, message_ts, rule.report_type, owner) | ||
This file contains hidden or 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
113 changes: 113 additions & 0 deletions
113
backend/src/apps/slack/migrations/0023_reactionalert_reactionrule.py
This file contains hidden or 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,113 @@ | ||
| # Generated by Django 6.0.6 on 2026-06-22 20:33 | ||
|
|
||
| import django.core.validators | ||
| import django.db.models.deletion | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("slack", "0022_workspace_invite_link_last_alert_message_ts_and_more"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="ReactionAlert", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.BigAutoField( | ||
| auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
| ), | ||
| ), | ||
| ("nest_created_at", models.DateTimeField(auto_now_add=True)), | ||
| ("nest_updated_at", models.DateTimeField(auto_now=True)), | ||
| ( | ||
| "message_ts", | ||
| models.CharField( | ||
| help_text="Slack timestamp of the message that triggered the alert.", | ||
| max_length=32, | ||
| ), | ||
| ), | ||
| ( | ||
| "report_type", | ||
| models.CharField( | ||
| help_text="Report category for the emitted reaction alert.", | ||
| max_length=64, | ||
| ), | ||
| ), | ||
| ("reaction_count", models.PositiveSmallIntegerField(default=0)), | ||
| ( | ||
| "alert_message_ts", | ||
| models.CharField( | ||
| blank=True, | ||
| default="", | ||
| help_text="Slack timestamp of the posted reaction alert message.", | ||
| max_length=32, | ||
| ), | ||
| ), | ||
| ( | ||
| "conversation", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, to="slack.conversation" | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "db_table": "slack_reaction_alerts", | ||
| "unique_together": {("conversation", "message_ts", "report_type")}, | ||
| }, | ||
| ), | ||
| migrations.CreateModel( | ||
| name="ReactionRule", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.BigAutoField( | ||
| auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
| ), | ||
| ), | ||
| ("nest_created_at", models.DateTimeField(auto_now_add=True)), | ||
| ("nest_updated_at", models.DateTimeField(auto_now=True)), | ||
| ( | ||
| "emoji_name", | ||
| models.CharField( | ||
| help_text="Slack emoji name that triggers this reaction rule.", | ||
| max_length=64, | ||
| ), | ||
| ), | ||
| ( | ||
| "report_type", | ||
| models.CharField( | ||
| help_text="Report category recorded when this reaction rule triggers.", | ||
| max_length=64, | ||
| ), | ||
| ), | ||
| ( | ||
| "threshold", | ||
| models.PositiveSmallIntegerField( | ||
| default=3, validators=[django.core.validators.MinValueValidator(1)] | ||
| ), | ||
| ), | ||
| ( | ||
| "alert_channel_id", | ||
| models.CharField( | ||
| help_text="Slack channel ID where reaction alerts are posted.", | ||
| max_length=50, | ||
| ), | ||
| ), | ||
| ("alert_user_ids", models.JSONField(blank=True, default=list)), | ||
| ("is_enabled", models.BooleanField(default=True)), | ||
| ( | ||
| "conversation", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, to="slack.conversation" | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "db_table": "slack_reaction_rules", | ||
| "unique_together": {("conversation", "emoji_name")}, | ||
| }, | ||
| ), | ||
| ] |
23 changes: 23 additions & 0 deletions
23
backend/src/apps/slack/migrations/0024_reactionalert_reporter_user_ids.py
This file contains hidden or 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,23 @@ | ||
| # Generated by Django 6.0.8 on 2026-08-17 00:17 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("slack", "0023_reactionalert_reactionrule"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="reactionalert", | ||
| name="reporter_user_ids", | ||
| field=models.JSONField( | ||
| blank=True, | ||
| default=list, | ||
| help_text=( | ||
| "Slack user IDs that had the triggering reaction when the alert was posted." | ||
| ), | ||
| ), | ||
| ), | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.