snapshot of https://github.com/zulip/zulip.git @ 48c69911c7dd4e03b20552eb3bd77ad8151c1357 (2026-09-15)
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
Some checks failed
Code scanning / CodeQL (push) Failing after 15s
Zulip production suite / Ubuntu 22.04 production build (push) Failing after 26s
Zulip production suite / Debian 12 production install with custom db name and user (push) Has been skipped
Zulip production suite / Ubuntu 22.04 production install and PostgreSQL upgrade with pgroonga (push) Has been skipped
Zulip production suite / Ubuntu 24.04 production install (push) Has been skipped
Zulip production suite / Ubuntu 26.04 production install (push) Has been skipped
Zulip production suite / Debian 13 production install (push) Has been skipped
Zulip production suite / 7.0 Version Upgrade (push) Has been skipped
Zulip production suite / 8.0 Version Upgrade (push) Has been skipped
Zulip production suite / 6.0 Version Upgrade (push) Has been skipped
Zulip production suite / 10.0 Version Upgrade (push) Has been skipped
Zulip production suite / 9.0 Version Upgrade (push) Has been skipped
Zulip production suite / 12.0 Version Upgrade (push) Has been skipped
Zulip production suite / 11.0 Version Upgrade (push) Has been skipped
GitHub Actions Security Analysis with zizmor / zizmor (push) Failing after 3s
Zulip CI / Debian 12 (Python 3.11, backend + documentation) (push) Failing after 26s
Zulip CI / Ubuntu 22.04 (Python 3.10, backend + frontend) (push) Failing after 3s
Zulip CI / Ubuntu 24.04 (Python 3.12, backend) (push) Failing after 25s
Zulip CI / Ubuntu 26.04 (Python 3.14, backend) (push) Failing after 25s
Zulip CI / Debian 13 (Python 3.13, backend) (push) Failing after 28s
Zulip production suite / Required jobs (push) Has been cancelled
Zulip CI / Required jobs (push) Has been cancelled
API Documentation Update Check / check-feature-level-updated (push) Has been cancelled
API Documentation Update Check / notify-if-api-docs-changed (push) Has been cancelled
This commit is contained in:
193
zerver/views/message_flags.py
Normal file
193
zerver/views/message_flags.py
Normal file
@@ -0,0 +1,193 @@
|
||||
from typing import Annotated
|
||||
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from django.utils.translation import gettext as _
|
||||
from pydantic import Json, NonNegativeInt
|
||||
|
||||
from zerver.actions.message_flags import (
|
||||
do_mark_all_as_read,
|
||||
do_mark_stream_messages_as_read,
|
||||
do_update_message_flags,
|
||||
)
|
||||
from zerver.lib.exceptions import JsonableError
|
||||
from zerver.lib.narrow import (
|
||||
NarrowParameter,
|
||||
clean_narrow_for_message_fetch,
|
||||
fetch_messages,
|
||||
parse_anchor_value,
|
||||
)
|
||||
from zerver.lib.request import RequestNotes
|
||||
from zerver.lib.response import json_success
|
||||
from zerver.lib.streams import access_stream_by_id
|
||||
from zerver.lib.topic import maybe_rename_general_chat_to_empty_topic, user_message_exists_for_topic
|
||||
from zerver.lib.typed_endpoint import (
|
||||
ApiParamConfig,
|
||||
typed_endpoint,
|
||||
typed_endpoint_without_parameters,
|
||||
)
|
||||
from zerver.models import UserActivity, UserProfile
|
||||
|
||||
|
||||
def get_latest_update_message_flag_activity(user_profile: UserProfile) -> UserActivity | None:
|
||||
return (
|
||||
UserActivity.objects.filter(
|
||||
user_profile=user_profile,
|
||||
query__in=["update_message_flags", "update_message_flags_for_narrow"],
|
||||
)
|
||||
.order_by("last_visit")
|
||||
.last()
|
||||
)
|
||||
|
||||
|
||||
# NOTE: If this function name is changed, add the new name to the
|
||||
# query in get_latest_update_message_flag_activity
|
||||
@typed_endpoint
|
||||
def update_message_flags(
|
||||
request: HttpRequest,
|
||||
user_profile: UserProfile,
|
||||
*,
|
||||
flag: str,
|
||||
messages: Json[list[int]],
|
||||
operation: Annotated[str, ApiParamConfig("op")],
|
||||
) -> HttpResponse:
|
||||
request_notes = RequestNotes.get_notes(request)
|
||||
assert request_notes.log_data is not None
|
||||
|
||||
(count, ignored_because_not_subscribed_channels) = do_update_message_flags(
|
||||
user_profile, operation, flag, messages
|
||||
)
|
||||
|
||||
target_count_str = str(len(messages))
|
||||
log_data_str = f"[{operation} {flag}/{target_count_str}] actually {count}"
|
||||
request_notes.log_data["extra"] = log_data_str
|
||||
|
||||
return json_success(
|
||||
request,
|
||||
data={
|
||||
"messages": messages, # Useless, but included for backwards compatibility.
|
||||
"ignored_because_not_subscribed_channels": ignored_because_not_subscribed_channels,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
MAX_MESSAGES_PER_UPDATE = 5000
|
||||
|
||||
|
||||
# NOTE: If this function name is changed, add the new name to the
|
||||
# query in get_latest_update_message_flag_activity
|
||||
@typed_endpoint
|
||||
def update_message_flags_for_narrow(
|
||||
request: HttpRequest,
|
||||
user_profile: UserProfile,
|
||||
*,
|
||||
anchor_val: Annotated[str, ApiParamConfig("anchor")],
|
||||
flag: str,
|
||||
include_anchor: Json[bool] = True,
|
||||
narrow: Json[list[NarrowParameter] | None],
|
||||
num_after: Json[NonNegativeInt],
|
||||
num_before: Json[NonNegativeInt],
|
||||
operation: Annotated[str, ApiParamConfig("op")],
|
||||
) -> HttpResponse:
|
||||
anchor_info = parse_anchor_value(anchor_val, use_first_unread_anchor=False)
|
||||
|
||||
if num_before > 0 and num_after > 0 and not include_anchor:
|
||||
raise JsonableError(_("The anchor can only be excluded at an end of the range"))
|
||||
|
||||
# Clamp such that num_before + num_after <= MAX_MESSAGES_PER_UPDATE.
|
||||
num_before = min(
|
||||
num_before, max(MAX_MESSAGES_PER_UPDATE - num_after, MAX_MESSAGES_PER_UPDATE // 2)
|
||||
)
|
||||
num_after = min(num_after, MAX_MESSAGES_PER_UPDATE - num_before)
|
||||
|
||||
narrow = clean_narrow_for_message_fetch(narrow, user_profile.realm, user_profile)
|
||||
|
||||
query_info = fetch_messages(
|
||||
narrow=narrow,
|
||||
user_profile=user_profile,
|
||||
realm=user_profile.realm,
|
||||
is_web_public_query=False,
|
||||
anchor_info=anchor_info,
|
||||
include_anchor=include_anchor,
|
||||
num_before=num_before,
|
||||
num_after=num_after,
|
||||
)
|
||||
|
||||
messages = [row[0] for row in query_info.rows]
|
||||
(updated_count, ignored_because_not_subscribed_channels) = do_update_message_flags(
|
||||
user_profile, operation, flag, messages
|
||||
)
|
||||
|
||||
return json_success(
|
||||
request,
|
||||
data={
|
||||
"processed_count": len(messages),
|
||||
"updated_count": updated_count,
|
||||
"first_processed_id": messages[0] if messages else None,
|
||||
"last_processed_id": messages[-1] if messages else None,
|
||||
"found_oldest": query_info.found_oldest,
|
||||
"found_newest": query_info.found_newest,
|
||||
"ignored_because_not_subscribed_channels": ignored_because_not_subscribed_channels,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@typed_endpoint_without_parameters
|
||||
def mark_all_as_read(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
|
||||
request_notes = RequestNotes.get_notes(request)
|
||||
count = do_mark_all_as_read(user_profile, timeout=50)
|
||||
if count is None:
|
||||
return json_success(request, data={"complete": False})
|
||||
|
||||
log_data_str = f"[{count} updated]"
|
||||
assert request_notes.log_data is not None
|
||||
request_notes.log_data["extra"] = log_data_str
|
||||
|
||||
return json_success(request, data={"complete": True})
|
||||
|
||||
|
||||
@typed_endpoint
|
||||
def mark_stream_as_read(
|
||||
request: HttpRequest, user_profile: UserProfile, *, stream_id: Json[int]
|
||||
) -> HttpResponse:
|
||||
stream, _sub = access_stream_by_id(user_profile, stream_id)
|
||||
assert stream.recipient_id is not None
|
||||
count = do_mark_stream_messages_as_read(user_profile, stream.recipient_id)
|
||||
|
||||
log_data_str = f"[{count} updated]"
|
||||
log_data = RequestNotes.get_notes(request).log_data
|
||||
assert log_data is not None
|
||||
log_data["extra"] = log_data_str
|
||||
|
||||
return json_success(request)
|
||||
|
||||
|
||||
@typed_endpoint
|
||||
def mark_topic_as_read(
|
||||
request: HttpRequest,
|
||||
user_profile: UserProfile,
|
||||
*,
|
||||
topic_name: str,
|
||||
stream_id: Json[int],
|
||||
) -> HttpResponse:
|
||||
stream, _sub = access_stream_by_id(user_profile, stream_id)
|
||||
assert stream.recipient_id is not None
|
||||
|
||||
if topic_name:
|
||||
topic_name = maybe_rename_general_chat_to_empty_topic(topic_name)
|
||||
topic_exists = user_message_exists_for_topic(
|
||||
user_profile=user_profile,
|
||||
recipient_id=stream.recipient_id,
|
||||
topic_name=topic_name,
|
||||
)
|
||||
|
||||
if not topic_exists:
|
||||
raise JsonableError(_("No such topic '{topic}'").format(topic=topic_name))
|
||||
|
||||
count = do_mark_stream_messages_as_read(user_profile, stream.recipient_id, topic_name)
|
||||
|
||||
log_data_str = f"[{count} updated]"
|
||||
log_data = RequestNotes.get_notes(request).log_data
|
||||
assert log_data is not None
|
||||
log_data["extra"] = log_data_str
|
||||
|
||||
return json_success(request)
|
||||
Reference in New Issue
Block a user