2022-12-29 17:31:32 +00:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
2023-01-03 08:01:20 +00:00
|
|
|
from activities.models import TimelineEvent
|
2022-12-21 19:47:48 +00:00
|
|
|
from activities.services import TimelineService
|
2022-12-11 19:37:28 +00:00
|
|
|
from api import schemas
|
|
|
|
from api.decorators import identity_required
|
2022-12-12 07:38:02 +00:00
|
|
|
from api.pagination import MastodonPaginator
|
2022-12-11 19:37:28 +00:00
|
|
|
from api.views.base import api_router
|
2022-12-11 18:22:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@api_router.get("/v1/notifications", response=list[schemas.Notification])
|
|
|
|
@identity_required
|
|
|
|
def notifications(
|
2022-12-29 17:31:32 +00:00
|
|
|
request: HttpRequest,
|
|
|
|
response: HttpResponse,
|
2022-12-11 18:22:06 +00:00
|
|
|
max_id: str | None = None,
|
|
|
|
since_id: str | None = None,
|
|
|
|
min_id: str | None = None,
|
|
|
|
limit: int = 20,
|
|
|
|
account_id: str | None = None,
|
|
|
|
):
|
|
|
|
# Types/exclude_types use weird syntax so we have to handle them manually
|
|
|
|
base_types = {
|
|
|
|
"favourite": TimelineEvent.Types.liked,
|
|
|
|
"reblog": TimelineEvent.Types.boosted,
|
|
|
|
"mention": TimelineEvent.Types.mentioned,
|
|
|
|
"follow": TimelineEvent.Types.followed,
|
2023-01-15 21:48:17 +00:00
|
|
|
"admin.sign_up": TimelineEvent.Types.identity_created,
|
2022-12-11 18:22:06 +00:00
|
|
|
}
|
|
|
|
requested_types = set(request.GET.getlist("types[]"))
|
|
|
|
excluded_types = set(request.GET.getlist("exclude_types[]"))
|
|
|
|
if not requested_types:
|
|
|
|
requested_types = set(base_types.keys())
|
|
|
|
requested_types.difference_update(excluded_types)
|
|
|
|
# Use that to pull relevant events
|
2022-12-21 19:47:48 +00:00
|
|
|
queryset = TimelineService(request.identity).notifications(
|
2022-12-31 17:34:51 +00:00
|
|
|
[base_types[r] for r in requested_types if r in base_types]
|
2022-12-11 18:22:06 +00:00
|
|
|
)
|
2023-01-09 06:06:09 +00:00
|
|
|
paginator = MastodonPaginator()
|
2022-12-29 17:31:32 +00:00
|
|
|
pager = paginator.paginate(
|
2022-12-12 07:38:02 +00:00
|
|
|
queryset,
|
|
|
|
min_id=min_id,
|
|
|
|
max_id=max_id,
|
|
|
|
since_id=since_id,
|
|
|
|
limit=limit,
|
|
|
|
)
|
2023-01-03 08:01:20 +00:00
|
|
|
pager.jsonify_notification_events(identity=request.identity)
|
2022-12-29 17:31:32 +00:00
|
|
|
|
|
|
|
if pager.results:
|
2022-12-29 18:46:25 +00:00
|
|
|
response.headers["Link"] = pager.link_header(request, ["limit", "account_id"])
|
2022-12-29 17:31:32 +00:00
|
|
|
|
2023-01-03 08:01:20 +00:00
|
|
|
return pager.json_results
|