Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

feat(notifications): select default chat connection

Marcos Paulo Dinizbcccfa139d9a
diff --git a/backend/app/api/notifications.py b/backend/app/api/notifications.py
index 289b3491..388398ff 100644
--- a/backend/app/api/notifications.py
+++ b/backend/app/api/notifications.py
@@ -450,6 +450,18 @@ def test_chat_connection(conn_id):
return jsonify(result), 200 if result.get('success') else 400
+@notifications_bp.route('/admin/chat-connections/<int:conn_id>/default', methods=['POST'])
+@jwt_required()
+@admin_required
+def set_default_chat_connection(conn_id):
+ """Select the active administrative default for a connection kind."""
+ from app.services.chat_webhook_service import ChatWebhookService
+ conn = ChatWebhookService.set_default(conn_id)
+ if conn is None:
+ return jsonify({'error': 'Connection not found'}), 404
+ return jsonify({'success': True, 'connection': conn.to_dict()}), 200
+
+
@notifications_bp.route('/admin/chat-connections/<int:conn_id>', methods=['DELETE'])
@jwt_required()
@admin_required
diff --git a/backend/app/models/chat_webhook.py b/backend/app/models/chat_webhook.py
index cc152d78..42dffbb9 100644
--- a/backend/app/models/chat_webhook.py
+++ b/backend/app/models/chat_webhook.py
@@ -35,7 +35,8 @@ class ChatWebhookConnection(db.Model):
categories_json = db.Column(db.Text)
is_active = db.Column(db.Boolean, default=True, nullable=False)
- # The default connection for its kind (used when nothing category-matches).
+ # Administrative default for this kind. Category fan-out remains driven by
+ # active connections whose category filters match the notification.
is_default = db.Column(db.Boolean, default=False, index=True)
# True when created by the one-time import of legacy notifications.json config.
imported = db.Column(db.Boolean, default=False)
diff --git a/backend/app/services/chat_webhook_service.py b/backend/app/services/chat_webhook_service.py
index 142149e2..05ae743d 100644
--- a/backend/app/services/chat_webhook_service.py
+++ b/backend/app/services/chat_webhook_service.py
@@ -174,6 +174,19 @@ class ChatWebhookService:
db.session.commit()
return conn
+ @classmethod
+ def set_default(cls, conn_id):
+ """Select and activate the administrative default for one kind."""
+ conn = db.session.get(ChatWebhookConnection, conn_id)
+ if conn is None:
+ return None
+
+ for candidate in ChatWebhookConnection.query.filter_by(kind=conn.kind).all():
+ candidate.is_default = candidate.id == conn.id
+ conn.is_active = True
+ db.session.commit()
+ return conn
+
@classmethod
def delete(cls, conn_id):
"""Delete a connection. If it was its kind's default, promote the oldest
diff --git a/backend/tests/test_notification_chat_connections.py b/backend/tests/test_notification_chat_connections.py
index c50db373..686377b9 100644
--- a/backend/tests/test_notification_chat_connections.py
+++ b/backend/tests/test_notification_chat_connections.py
@@ -171,6 +171,31 @@ class TestCrud:
assert conn.is_active is True
+class TestDefaults:
+ def test_set_default_is_scoped_to_kind_and_activates_selection(self, app):
+ first_discord = ChatWebhookService.add({
+ 'kind': 'discord', 'name': 'Primary', 'url': 'https://discord/primary',
+ })
+ second_discord = ChatWebhookService.add({
+ 'kind': 'discord', 'name': 'Secondary', 'url': 'https://discord/secondary',
+ 'is_active': False,
+ })
+ slack = ChatWebhookService.add({
+ 'kind': 'slack', 'name': 'Slack', 'url': 'https://slack/default',
+ })
+
+ selected = ChatWebhookService.set_default(second_discord.id)
+
+ assert selected.id == second_discord.id
+ assert selected.is_default is True
+ assert selected.is_active is True
+ assert first_discord.is_default is False
+ assert slack.is_default is True
+
+ def test_set_default_returns_none_for_unknown_id(self, app):
+ assert ChatWebhookService.set_default(999999) is None
+
+
class TestCategoryRouting:
def test_catch_all_matches_every_category(self, app):
conn = ChatWebhookService.add({'kind': 'webhook', 'name': 'All', 'url': 'https://x/all'})
@@ -465,3 +490,36 @@ class TestApi:
assert resp.status_code == 404
assert resp.get_json() == {'error': 'Connection not found'}
+
+ def test_set_default_connection(self, app, client, auth_headers):
+ first = ChatWebhookService.add({
+ 'kind': 'discord', 'name': 'First', 'url': 'https://discord/first',
+ })
+ second = ChatWebhookService.add({
+ 'kind': 'discord', 'name': 'Second', 'url': 'https://discord/second',
+ 'is_active': False,
+ })
+
+ resp = client.post(
+ f'/api/v1/notifications/admin/chat-connections/{second.id}/default',
+ headers=auth_headers,
+ )
+
+ assert resp.status_code == 200
+ body = resp.get_json()
+ assert body['success'] is True
+ assert body['connection']['id'] == second.id
+ assert body['connection']['is_default'] is True
+ assert body['connection']['is_active'] is True
+ db.session.refresh(first)
+ assert first.is_default is False
+
+ def test_set_default_connection_returns_404_for_unknown_id(self, app, client,
+ auth_headers):
+ resp = client.post(
+ '/api/v1/notifications/admin/chat-connections/999999/default',
+ headers=auth_headers,
+ )
+
+ assert resp.status_code == 404
+ assert resp.get_json() == {'error': 'Connection not found'}