Skip to content
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

Events: sender_type property to differentiate between multiple event sources #399

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions api/activities/activity.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from typing import Any

from fastapi import BackgroundTasks, Header
from fastapi import BackgroundTasks

from ayon_server.activities import (
ActivityType,
Expand All @@ -16,6 +16,8 @@
PathEntityID,
PathProjectLevelEntityType,
ProjectName,
Sender,
SenderType,
)
from ayon_server.api.responses import EmptyResponse
from ayon_server.exceptions import BadRequestException
Expand Down Expand Up @@ -56,7 +58,8 @@ async def post_project_activity(
user: CurrentUser,
activity: ProjectActivityPostModel,
background_tasks: BackgroundTasks,
x_sender: str | None = Header(default=None),
sender: Sender,
sender_type: SenderType,
) -> CreateActivityResponseModel:
"""Create an activity.

Expand All @@ -82,7 +85,8 @@ async def post_project_activity(
files=activity.files,
user_name=user.name,
timestamp=activity.timestamp,
sender=x_sender,
sender=sender,
sender_type=sender_type,
data=activity.data,
)

Expand All @@ -100,7 +104,8 @@ async def delete_project_activity(
activity_id: ActivityID,
user: CurrentUser,
background_tasks: BackgroundTasks,
x_sender: str | None = Header(default=None),
sender: Sender,
sender_type: SenderType,
) -> EmptyResponse:
"""Delete an activity.

Expand All @@ -117,7 +122,8 @@ async def delete_project_activity(
project_name,
activity_id,
user_name=user_name,
sender=x_sender,
sender=sender,
sender_type=sender_type,
)

background_tasks.add_task(delete_unused_files, project_name)
Expand All @@ -137,7 +143,8 @@ async def patch_project_activity(
user: CurrentUser,
activity: ActivityPatchModel,
background_tasks: BackgroundTasks,
x_sender: str | None = Header(default=None),
sender: Sender,
sender_type: SenderType,
) -> EmptyResponse:
"""Edit an activity.

Expand All @@ -156,7 +163,8 @@ async def patch_project_activity(
body=activity.body,
files=activity.files,
user_name=user_name,
sender=x_sender,
sender=sender,
sender_type=sender_type,
)

background_tasks.add_task(delete_unused_files, project_name)
Expand Down
29 changes: 24 additions & 5 deletions api/activities/reactions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from datetime import datetime
from typing import Literal

from fastapi import Header, Path
from fastapi import Path

from ayon_server.api.dependencies import (
ActivityID,
CurrentUser,
ProjectName,
Sender,
SenderType,
)
from ayon_server.entities import UserEntity
from ayon_server.events.eventstream import EventStream
Expand All @@ -23,7 +25,9 @@ async def modify_reactions(
user: UserEntity,
reaction: str,
action: Literal["add", "remove"],
*,
sender: str | None = None,
sender_type: str | None = None,
):
"""

Expand Down Expand Up @@ -116,6 +120,7 @@ async def modify_reactions(
store=False,
user=user.name,
sender=sender,
sender_type=sender_type,
)


Expand All @@ -134,10 +139,17 @@ async def create_reaction_to_activity(
project_name: ProjectName,
activity_id: ActivityID,
request: CreateReactionModel,
x_sender: str | None = Header(None, description="The sender of the request"),
sender: Sender,
sender_type: SenderType,
):
await modify_reactions(
project_name, activity_id, user, request.reaction, "add", x_sender
project_name,
activity_id,
user,
request.reaction,
"add",
sender=sender,
sender_type=sender_type,
)


Expand All @@ -146,14 +158,21 @@ async def delete_reaction_to_activity(
user: CurrentUser,
project_name: ProjectName,
activity_id: ActivityID,
sender: Sender,
sender_type: SenderType,
reaction: str = Path(
...,
description="The reaction to be deleted",
example="like",
regex=NAME_REGEX,
),
x_sender: str | None = Header(None, description="The sender of the request"),
):
await modify_reactions(
project_name, activity_id, user, reaction, "remove", x_sender
project_name,
activity_id,
user,
reaction,
"remove",
sender=sender,
sender_type=sender_type,
)
15 changes: 11 additions & 4 deletions api/activities/watchers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from fastapi import Header

from ayon_server.activities.watchers.set_watchers import set_watchers
from ayon_server.activities.watchers.watcher_list import get_watcher_list
from ayon_server.api.dependencies import (
CurrentUser,
PathEntityID,
PathProjectLevelEntityType,
ProjectName,
Sender,
SenderType,
)
from ayon_server.api.responses import EmptyResponse
from ayon_server.helpers.get_entity_class import get_entity_class
Expand Down Expand Up @@ -44,12 +44,19 @@ async def set_entity_watchers(
entity_id: PathEntityID,
user: CurrentUser,
watchers: WatchersModel,
x_sender: str | None = Header(default=None),
sender: Sender,
sender_type: SenderType,
) -> EmptyResponse:
entity_class = get_entity_class(entity_type)
entity = await entity_class.load(project_name, entity_id)
await entity.ensure_update_access(user)

await set_watchers(entity, watchers.watchers, user, sender=x_sender)
await set_watchers(
entity,
watchers.watchers,
user,
sender=sender,
sender_type=sender_type,
)

return EmptyResponse()
6 changes: 3 additions & 3 deletions api/addons/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from ayon_server.api.dependencies import CurrentUser
from ayon_server.constraints import Constraints
from ayon_server.events import dispatch_event, update_event
from ayon_server.events import EventStream
from ayon_server.exceptions import ForbiddenException
from ayon_server.helpers.download_addon import download_addon
from ayon_server.installer import background_installer
Expand Down Expand Up @@ -81,15 +81,15 @@ async def upload_addon_zip_file(
res = await Postgres.fetch(query, zip_info.name, zip_info.version)
if res:
event_id = res[0]["id"]
await update_event(
await EventStream.update(
event_id,
description="Reinstalling addon from zip file",
summary=zip_info.dict(exclude_none=True),
status="pending",
)
else:
# If not, dispatch a new event
event_id = await dispatch_event(
event_id = await EventStream.dispatch(
"addon.install",
description=f"Installing addon {zip_info.name} {zip_info.version}",
summary=zip_info.dict(exclude_none=True),
Expand Down
8 changes: 4 additions & 4 deletions api/addons/project_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ayon_server.api.responses import EmptyResponse
from ayon_server.config import ayonconfig
from ayon_server.entities import ProjectEntity
from ayon_server.events import dispatch_event
from ayon_server.events import EventStream
from ayon_server.exceptions import (
BadRequestException,
ForbiddenException,
Expand Down Expand Up @@ -235,7 +235,7 @@ async def set_addon_project_settings(
variant=variant,
)

await dispatch_event(
await EventStream.dispatch(
topic="settings.changed",
description=f"{addon_name} {version} {variant} project overrides changed",
summary={
Expand Down Expand Up @@ -357,7 +357,7 @@ async def delete_addon_project_overrides(
"newValue": {},
}

await dispatch_event(
await EventStream.dispatch(
topic="settings.changed",
description=f"{addon_name} {version} {variant} project overrides removed",
summary={
Expand Down Expand Up @@ -515,7 +515,7 @@ async def modify_project_overrides(
"newValue": new_overrides,
}

await dispatch_event(
await EventStream.dispatch(
topic="settings.changed",
description=f"{addon_name} {version} {variant} project overrides changed",
summary={
Expand Down
8 changes: 4 additions & 4 deletions api/addons/studio_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ayon_server.api.dependencies import CurrentUser
from ayon_server.api.responses import EmptyResponse
from ayon_server.config import ayonconfig
from ayon_server.events import dispatch_event
from ayon_server.events import EventStream
from ayon_server.exceptions import (
BadRequestException,
ForbiddenException,
Expand Down Expand Up @@ -134,7 +134,7 @@ async def set_addon_studio_settings(
old_settings=original, new_settings=new_settings, variant=variant
)

await dispatch_event(
await EventStream.dispatch(
topic="settings.changed",
description=f"{addon_name} {addon_version} {variant} studio overrides changed",
summary={
Expand Down Expand Up @@ -217,7 +217,7 @@ async def delete_addon_studio_overrides(
"newValue": {},
}

await dispatch_event(
await EventStream.dispatch(
topic="settings.changed",
description=f"{addon_name} {addon_version} {variant} studio overrides removed",
summary={
Expand Down Expand Up @@ -272,7 +272,7 @@ async def modify_studio_overrides(
"newValue": new_overrides,
}

await dispatch_event(
await EventStream.dispatch(
topic="settings.changed",
description=f"{addon_name} {addon_version} {variant} studio overrides changed",
summary={
Expand Down
27 changes: 20 additions & 7 deletions api/bundles/bundles.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Any, Literal

from fastapi import Header, Query
from fastapi import Query
from nxtools import logging

from ayon_server.addons import AddonLibrary
from ayon_server.api.dependencies import CurrentUser
from ayon_server.api.dependencies import CurrentUser, Sender, SenderType
from ayon_server.api.responses import EmptyResponse
from ayon_server.entities import UserEntity
from ayon_server.events import EventStream
Expand Down Expand Up @@ -84,8 +84,10 @@ async def list_bundles(
async def _create_new_bundle(
conn: Connection,
bundle: BundleModel,
*,
user: UserEntity | None = None,
sender: str | None = None,
sender_type: str | None = None,
):
# Clear constrained values if they are being updated
if bundle.is_production:
Expand Down Expand Up @@ -132,6 +134,7 @@ async def _create_new_bundle(
await EventStream.dispatch(
"bundle.created",
sender=sender,
sender_type=sender_type,
user=user.name if user else None,
description=f"Bundle {bundle.name} created",
summary={
Expand All @@ -156,7 +159,8 @@ async def check_bundle_compatibility(
async def create_new_bundle(
bundle: BundleModel,
user: CurrentUser,
x_sender: str | None = Header(default=None),
sender: Sender,
sender_type: SenderType,
force: bool = Query(False, description="Force creation of bundle"),
) -> EmptyResponse:
if not user.is_admin:
Expand All @@ -177,7 +181,13 @@ async def create_new_bundle(
bundle.addons[system_addon_name] = addon_definition.latest.version

async with Postgres.acquire() as conn, conn.transaction():
await _create_new_bundle(conn, bundle, user, x_sender)
await _create_new_bundle(
conn,
bundle,
user=user,
sender=sender,
sender_type=sender_type,
)

return EmptyResponse(status_code=201)

Expand All @@ -192,12 +202,13 @@ async def update_bundle(
bundle_name: str,
patch: BundlePatchModel,
user: CurrentUser,
sender: Sender,
sender_type: SenderType,
build: list[Platform] | None = Query(
None,
title="Request build",
description="Build dependency packages for selected platforms",
),
x_sender: str | None = Header(default=None),
force: bool = Query(False, description="Force creation of bundle"),
) -> EmptyResponse:
if not user.is_admin:
Expand Down Expand Up @@ -352,7 +363,8 @@ async def update_bundle(

await EventStream.dispatch(
"bundle.updated",
sender=x_sender,
sender=sender,
sender_type=sender_type,
user=user.name,
description=f"Bundle {bundle_name} updated",
summary={
Expand All @@ -368,7 +380,8 @@ async def update_bundle(
if status_changed_to:
await EventStream.dispatch(
"bundle.status_changed",
sender=x_sender,
sender=sender,
sender_type=sender_type,
user=user.name,
description=f"Bundle {bundle_name} changed to {status_changed_to}",
summary={
Expand Down
Loading