refac
This commit is contained in:
+89
-15
@@ -116,12 +116,20 @@ from open_webui.env import (
|
||||
WEBUI_SESSION_COOKIE_SAME_SITE,
|
||||
WEBUI_SESSION_COOKIE_SECURE,
|
||||
)
|
||||
from open_webui.events import (
|
||||
EVENT_CATALOG,
|
||||
EVENTS,
|
||||
delete_event_webhook,
|
||||
get_event_webhooks,
|
||||
migrate_legacy_webhook_config,
|
||||
publish_event,
|
||||
upsert_event_webhook,
|
||||
)
|
||||
from open_webui.internal.db import engine, get_async_session
|
||||
from open_webui.models.access_grants import AccessGrants
|
||||
from open_webui.models.channels import Channels
|
||||
from open_webui.models.chats import ChatForm, Chats
|
||||
from open_webui.models.config import Config
|
||||
from open_webui.events import EVENT_CATALOG, EVENT_VERSION, EVENTS, publish_event
|
||||
from open_webui.models.functions import Functions
|
||||
from open_webui.models.messages import Messages
|
||||
from open_webui.models.models import Models
|
||||
@@ -298,6 +306,7 @@ async def lifespan(app: FastAPI):
|
||||
await import_legacy_config_json()
|
||||
await seed_registered_defaults()
|
||||
await initialize_runtime_config(app)
|
||||
await migrate_legacy_webhook_config()
|
||||
await publish_event(app, EVENTS.SYSTEM_STARTUP_STARTED, source='system')
|
||||
|
||||
if LICENSE_KEY:
|
||||
@@ -2005,37 +2014,102 @@ async def get_app_config(request: Request):
|
||||
}
|
||||
|
||||
|
||||
class UrlForm(BaseModel):
|
||||
class EventWebhookForm(BaseModel):
|
||||
name: str | None = None
|
||||
url: str
|
||||
enabled: bool = True
|
||||
events: list[str] | None = None
|
||||
|
||||
|
||||
@app.get('/api/webhook')
|
||||
async def get_webhook_url(user=Depends(get_admin_user)):
|
||||
return {
|
||||
'url': await Config.get('webhook_url'),
|
||||
}
|
||||
class EventWebhookUpdateForm(BaseModel):
|
||||
name: str | None = None
|
||||
url: str | None = None
|
||||
enabled: bool | None = None
|
||||
events: list[str] | None = None
|
||||
|
||||
|
||||
@app.get('/api/events')
|
||||
async def get_event_catalog(user=Depends(get_admin_user)):
|
||||
return {
|
||||
'schema': EVENT_VERSION,
|
||||
'schema': VERSION,
|
||||
'events': list(EVENT_CATALOG),
|
||||
}
|
||||
|
||||
|
||||
@app.post('/api/webhook')
|
||||
async def update_webhook_url(form_data: UrlForm, user=Depends(get_admin_user)):
|
||||
await Config.upsert({'webhook_url': form_data.url})
|
||||
app.state.WEBHOOK_URL = form_data.url
|
||||
@app.get('/api/events/webhooks')
|
||||
async def get_event_webhooks_api(user=Depends(get_admin_user)):
|
||||
return await get_event_webhooks()
|
||||
|
||||
|
||||
@app.post('/api/events/webhooks')
|
||||
async def create_event_webhook(form_data: EventWebhookForm, user=Depends(get_admin_user)):
|
||||
try:
|
||||
webhook = await upsert_event_webhook(
|
||||
{
|
||||
'name': form_data.name,
|
||||
'url': form_data.url,
|
||||
'enabled': form_data.enabled,
|
||||
'events': form_data.events,
|
||||
}
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
||||
|
||||
await publish_event(
|
||||
app,
|
||||
EVENTS.CONFIG_WEBHOOK_UPDATED,
|
||||
actor=user,
|
||||
subject_id='webhook_url', subject_type='config',
|
||||
data={'enabled': bool(form_data.url)},
|
||||
subject_id=webhook['id'],
|
||||
subject_type='config',
|
||||
data={'action': 'created', 'enabled': webhook.get('enabled'), 'events': webhook.get('events')},
|
||||
)
|
||||
return {'url': form_data.url}
|
||||
return webhook
|
||||
|
||||
|
||||
@app.put('/api/events/webhooks/{webhook_id}')
|
||||
async def update_event_webhook(webhook_id: str, form_data: EventWebhookUpdateForm, user=Depends(get_admin_user)):
|
||||
webhooks = await get_event_webhooks()
|
||||
existing = next((webhook for webhook in webhooks if webhook.get('id') == webhook_id), None)
|
||||
if not existing:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='Webhook not found')
|
||||
|
||||
try:
|
||||
webhook = await upsert_event_webhook(
|
||||
{
|
||||
**existing,
|
||||
**form_data.model_dump(exclude_unset=True),
|
||||
'id': webhook_id,
|
||||
}
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
||||
|
||||
await publish_event(
|
||||
app,
|
||||
EVENTS.CONFIG_WEBHOOK_UPDATED,
|
||||
actor=user,
|
||||
subject_id=webhook_id,
|
||||
subject_type='config',
|
||||
data={'action': 'updated', 'enabled': webhook.get('enabled'), 'events': webhook.get('events')},
|
||||
)
|
||||
return webhook
|
||||
|
||||
|
||||
@app.delete('/api/events/webhooks/{webhook_id}')
|
||||
async def delete_event_webhook_api(webhook_id: str, user=Depends(get_admin_user)):
|
||||
deleted = await delete_event_webhook(webhook_id)
|
||||
if not deleted:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='Webhook not found')
|
||||
|
||||
await publish_event(
|
||||
app,
|
||||
EVENTS.CONFIG_WEBHOOK_UPDATED,
|
||||
actor=user,
|
||||
subject_id=webhook_id,
|
||||
subject_type='config',
|
||||
data={'action': 'deleted'},
|
||||
)
|
||||
return {'status': True}
|
||||
|
||||
|
||||
@app.get('/api/version')
|
||||
|
||||
Reference in New Issue
Block a user