2023-02-07 11:07:15 -08:00
|
|
|
from django.http import HttpRequest
|
2023-07-04 07:06:31 -07:00
|
|
|
from django.shortcuts import get_object_or_404
|
2023-02-13 20:17:26 -08:00
|
|
|
from hatchway import ApiResponse, api_view
|
2022-12-29 09:31:32 -08:00
|
|
|
|
2023-07-05 06:58:54 -07:00
|
|
|
from activities.models import PostInteraction, TimelineEvent
|
2022-12-21 11:47:48 -08:00
|
|
|
from activities.services import TimelineService
|
2022-12-11 11:37:28 -08:00
|
|
|
from api import schemas
|
2023-02-19 10:37:02 -08:00
|
|
|
from api.decorators import scope_required
|
2023-02-07 11:07:15 -08:00
|
|
|
from api.pagination import MastodonPaginator, PaginatingApiResponse, PaginationResult
|
2022-12-11 10:22:06 -08:00
|
|
|
|
2023-07-04 07:06:31 -07:00
|
|
|
# Types/exclude_types use weird syntax so we have to handle them manually
|
|
|
|
NOTIFICATION_TYPES = {
|
|
|
|
"favourite": TimelineEvent.Types.liked,
|
|
|
|
"reblog": TimelineEvent.Types.boosted,
|
|
|
|
"mention": TimelineEvent.Types.mentioned,
|
|
|
|
"follow": TimelineEvent.Types.followed,
|
|
|
|
"admin.sign_up": TimelineEvent.Types.identity_created,
|
|
|
|
}
|
|
|
|
|
2022-12-11 10:22:06 -08:00
|
|
|
|
2023-02-19 10:37:02 -08:00
|
|
|
@scope_required("read:notifications")
|
2023-02-07 11:07:15 -08:00
|
|
|
@api_view.get
|
2022-12-11 10:22:06 -08:00
|
|
|
def notifications(
|
2022-12-29 09:31:32 -08:00
|
|
|
request: HttpRequest,
|
2022-12-11 10:22:06 -08:00
|
|
|
max_id: str | None = None,
|
|
|
|
since_id: str | None = None,
|
|
|
|
min_id: str | None = None,
|
|
|
|
limit: int = 20,
|
|
|
|
account_id: str | None = None,
|
2023-02-07 11:07:15 -08:00
|
|
|
) -> ApiResponse[list[schemas.Notification]]:
|
2022-12-11 10:22:06 -08:00
|
|
|
requested_types = set(request.GET.getlist("types[]"))
|
|
|
|
excluded_types = set(request.GET.getlist("exclude_types[]"))
|
|
|
|
if not requested_types:
|
2023-07-04 07:06:31 -07:00
|
|
|
requested_types = set(NOTIFICATION_TYPES.keys())
|
2022-12-11 10:22:06 -08:00
|
|
|
requested_types.difference_update(excluded_types)
|
|
|
|
# Use that to pull relevant events
|
2022-12-21 11:47:48 -08:00
|
|
|
queryset = TimelineService(request.identity).notifications(
|
2023-07-04 07:06:31 -07:00
|
|
|
[NOTIFICATION_TYPES[r] for r in requested_types if r in NOTIFICATION_TYPES]
|
2022-12-11 10:22:06 -08:00
|
|
|
)
|
2023-01-08 22:06:09 -08:00
|
|
|
paginator = MastodonPaginator()
|
2023-02-07 11:07:15 -08:00
|
|
|
pager: PaginationResult[TimelineEvent] = paginator.paginate(
|
2022-12-11 23:38:02 -08:00
|
|
|
queryset,
|
|
|
|
min_id=min_id,
|
|
|
|
max_id=max_id,
|
|
|
|
since_id=since_id,
|
|
|
|
limit=limit,
|
|
|
|
)
|
2023-07-05 06:58:54 -07:00
|
|
|
interactions = PostInteraction.get_event_interactions(
|
|
|
|
pager.results,
|
|
|
|
request.identity,
|
|
|
|
)
|
2023-02-07 11:07:15 -08:00
|
|
|
return PaginatingApiResponse(
|
2023-07-05 06:58:54 -07:00
|
|
|
[
|
|
|
|
schemas.Notification.from_timeline_event(event, interactions=interactions)
|
|
|
|
for event in pager.results
|
|
|
|
],
|
2023-02-07 11:07:15 -08:00
|
|
|
request=request,
|
|
|
|
include_params=["limit", "account_id"],
|
|
|
|
)
|
2023-07-04 07:06:31 -07:00
|
|
|
|
|
|
|
|
|
|
|
@scope_required("read:notifications")
|
|
|
|
@api_view.get
|
|
|
|
def get_notification(
|
|
|
|
request: HttpRequest,
|
|
|
|
id: str,
|
|
|
|
) -> schemas.Notification:
|
|
|
|
notification = get_object_or_404(
|
|
|
|
TimelineService(request.identity).notifications(
|
|
|
|
list(NOTIFICATION_TYPES.values())
|
|
|
|
),
|
|
|
|
id=id,
|
|
|
|
)
|
|
|
|
return schemas.Notification.from_timeline_event(notification)
|