message-flags: Migrate message flag endpoints to typed_endpoint.

This commit is contained in:
Kenneth Rodrigues
2024-04-16 18:11:09 +05:30
committed by Tim Abbott
parent 07699eb1d2
commit def2d402f4

View File

@@ -2,6 +2,8 @@ from typing import List, Optional
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from pydantic import Json, NonNegativeInt
from typing_extensions import Annotated
from zerver.actions.message_flags import ( from zerver.actions.message_flags import (
do_mark_all_as_read, do_mark_all_as_read,
@@ -9,17 +11,16 @@ from zerver.actions.message_flags import (
do_update_message_flags, do_update_message_flags,
) )
from zerver.lib.exceptions import JsonableError from zerver.lib.exceptions import JsonableError
from zerver.lib.narrow import ( from zerver.lib.narrow import OptionalNarrowListT, fetch_messages, parse_anchor_value
OptionalNarrowListT, from zerver.lib.request import RequestNotes
fetch_messages,
narrow_parameter,
parse_anchor_value,
)
from zerver.lib.request import REQ, RequestNotes, has_request_variables
from zerver.lib.response import json_success from zerver.lib.response import json_success
from zerver.lib.streams import access_stream_by_id from zerver.lib.streams import access_stream_by_id
from zerver.lib.topic import user_message_exists_for_topic from zerver.lib.topic import user_message_exists_for_topic
from zerver.lib.validator import check_bool, check_int, check_list, to_non_negative_int from zerver.lib.typed_endpoint import (
ApiParamConfig,
typed_endpoint,
typed_endpoint_without_parameters,
)
from zerver.models import UserActivity, UserProfile from zerver.models import UserActivity, UserProfile
@@ -36,13 +37,14 @@ def get_latest_update_message_flag_activity(user_profile: UserProfile) -> Option
# NOTE: If this function name is changed, add the new name to the # NOTE: If this function name is changed, add the new name to the
# query in get_latest_update_message_flag_activity # query in get_latest_update_message_flag_activity
@has_request_variables @typed_endpoint
def update_message_flags( def update_message_flags(
request: HttpRequest, request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
messages: List[int] = REQ(json_validator=check_list(check_int)), *,
operation: str = REQ("op"), messages: Json[List[int]],
flag: str = REQ(), operation: Annotated[str, ApiParamConfig("op")],
flag: str,
) -> HttpResponse: ) -> HttpResponse:
request_notes = RequestNotes.get_notes(request) request_notes = RequestNotes.get_notes(request)
assert request_notes.log_data is not None assert request_notes.log_data is not None
@@ -66,17 +68,18 @@ MAX_MESSAGES_PER_UPDATE = 5000
# NOTE: If this function name is changed, add the new name to the # NOTE: If this function name is changed, add the new name to the
# query in get_latest_update_message_flag_activity # query in get_latest_update_message_flag_activity
@has_request_variables @typed_endpoint
def update_message_flags_for_narrow( def update_message_flags_for_narrow(
request: HttpRequest, request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
anchor_val: str = REQ("anchor"), *,
include_anchor: bool = REQ(json_validator=check_bool, default=True), anchor_val: Annotated[str, ApiParamConfig("anchor")],
num_before: int = REQ(converter=to_non_negative_int), include_anchor: Json[bool] = True,
num_after: int = REQ(converter=to_non_negative_int), num_before: Json[NonNegativeInt],
narrow: OptionalNarrowListT = REQ("narrow", converter=narrow_parameter), num_after: Json[NonNegativeInt],
operation: str = REQ("op"), narrow: Json[OptionalNarrowListT],
flag: str = REQ(), operation: Annotated[str, ApiParamConfig("op")],
flag: str,
) -> HttpResponse: ) -> HttpResponse:
anchor = parse_anchor_value(anchor_val, use_first_unread_anchor=False) anchor = parse_anchor_value(anchor_val, use_first_unread_anchor=False)
@@ -116,7 +119,7 @@ def update_message_flags_for_narrow(
) )
@has_request_variables @typed_endpoint_without_parameters
def mark_all_as_read(request: HttpRequest, user_profile: UserProfile) -> HttpResponse: def mark_all_as_read(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
request_notes = RequestNotes.get_notes(request) request_notes = RequestNotes.get_notes(request)
count = do_mark_all_as_read(user_profile, timeout=50) count = do_mark_all_as_read(user_profile, timeout=50)
@@ -130,9 +133,9 @@ def mark_all_as_read(request: HttpRequest, user_profile: UserProfile) -> HttpRes
return json_success(request, data={"complete": True}) return json_success(request, data={"complete": True})
@has_request_variables @typed_endpoint
def mark_stream_as_read( def mark_stream_as_read(
request: HttpRequest, user_profile: UserProfile, stream_id: int = REQ(json_validator=check_int) request: HttpRequest, user_profile: UserProfile, *, stream_id: Json[int]
) -> HttpResponse: ) -> HttpResponse:
stream, sub = access_stream_by_id(user_profile, stream_id) stream, sub = access_stream_by_id(user_profile, stream_id)
assert stream.recipient_id is not None assert stream.recipient_id is not None
@@ -146,12 +149,13 @@ def mark_stream_as_read(
return json_success(request) return json_success(request)
@has_request_variables @typed_endpoint
def mark_topic_as_read( def mark_topic_as_read(
request: HttpRequest, request: HttpRequest,
user_profile: UserProfile, user_profile: UserProfile,
stream_id: int = REQ(json_validator=check_int), *,
topic_name: str = REQ(), stream_id: Json[int],
topic_name: str,
) -> HttpResponse: ) -> HttpResponse:
stream, sub = access_stream_by_id(user_profile, stream_id) stream, sub = access_stream_by_id(user_profile, stream_id)
assert stream.recipient_id is not None assert stream.recipient_id is not None