2020-06-22 22:57:01 +02:00
|
|
|
from typing import List, Optional
|
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2020-06-22 22:57:01 +02:00
|
|
|
|
|
|
|
from zerver.lib.actions import (
|
|
|
|
do_mark_all_as_read,
|
|
|
|
do_mark_stream_messages_as_read,
|
|
|
|
do_update_message_flags,
|
|
|
|
)
|
|
|
|
from zerver.lib.exceptions import JsonableError
|
2021-08-21 19:24:20 +02:00
|
|
|
from zerver.lib.request import REQ, RequestNotes, has_request_variables
|
2020-06-22 22:57:01 +02:00
|
|
|
from zerver.lib.response import json_success
|
|
|
|
from zerver.lib.streams import access_stream_by_id
|
|
|
|
from zerver.lib.topic import user_message_exists_for_topic
|
|
|
|
from zerver.lib.validator import check_int, check_list
|
|
|
|
from zerver.models import UserActivity, UserProfile
|
|
|
|
|
|
|
|
|
|
|
|
def get_latest_update_message_flag_activity(user_profile: UserProfile) -> Optional[UserActivity]:
|
2021-02-12 08:19:30 +01:00
|
|
|
return (
|
2021-02-12 08:20:45 +01:00
|
|
|
UserActivity.objects.filter(user_profile=user_profile, query="update_message_flags")
|
2021-02-12 08:19:30 +01:00
|
|
|
.order_by("last_visit")
|
|
|
|
.last()
|
|
|
|
)
|
|
|
|
|
2020-06-22 22:57:01 +02:00
|
|
|
|
|
|
|
# NOTE: If this function name is changed, add the new name to the
|
|
|
|
# query in get_latest_update_message_flag_activity
|
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def update_message_flags(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-04-07 22:00:44 +02:00
|
|
|
messages: List[int] = REQ(json_validator=check_list(check_int)),
|
2021-02-12 08:20:45 +01:00
|
|
|
operation: str = REQ("op"),
|
2021-02-12 08:19:30 +01:00
|
|
|
flag: str = REQ(),
|
|
|
|
) -> HttpResponse:
|
2021-08-21 19:24:20 +02:00
|
|
|
request_notes = RequestNotes.get_notes(request)
|
2021-07-09 18:10:51 +02:00
|
|
|
assert request_notes.client is not None
|
2021-07-09 10:06:04 +02:00
|
|
|
assert request_notes.log_data is not None
|
2020-06-22 22:57:01 +02:00
|
|
|
|
2021-07-09 18:10:51 +02:00
|
|
|
count = do_update_message_flags(user_profile, request_notes.client, operation, flag, messages)
|
2020-06-22 22:57:01 +02:00
|
|
|
|
|
|
|
target_count_str = str(len(messages))
|
|
|
|
log_data_str = f"[{operation} {flag}/{target_count_str}] actually {count}"
|
2021-07-09 10:06:04 +02:00
|
|
|
request_notes.log_data["extra"] = log_data_str
|
2020-06-22 22:57:01 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
return json_success({"result": "success", "messages": messages, "msg": ""})
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-06-22 22:57:01 +02:00
|
|
|
|
|
|
|
@has_request_variables
|
|
|
|
def mark_all_as_read(request: HttpRequest, user_profile: UserProfile) -> HttpResponse:
|
2021-08-21 19:24:20 +02:00
|
|
|
request_notes = RequestNotes.get_notes(request)
|
2021-07-09 18:10:51 +02:00
|
|
|
assert request_notes.client is not None
|
|
|
|
count = do_mark_all_as_read(user_profile, request_notes.client)
|
2020-06-22 22:57:01 +02:00
|
|
|
|
|
|
|
log_data_str = f"[{count} updated]"
|
2021-07-09 10:06:04 +02:00
|
|
|
assert request_notes.log_data is not None
|
|
|
|
request_notes.log_data["extra"] = log_data_str
|
2020-06-22 22:57:01 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
return json_success({"result": "success", "msg": ""})
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-06-22 22:57:01 +02:00
|
|
|
|
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def mark_stream_as_read(
|
2021-04-07 22:00:44 +02:00
|
|
|
request: HttpRequest, user_profile: UserProfile, stream_id: int = REQ(json_validator=check_int)
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2020-10-16 17:25:48 +02:00
|
|
|
stream, sub = access_stream_by_id(user_profile, stream_id)
|
|
|
|
count = do_mark_stream_messages_as_read(user_profile, stream.recipient_id)
|
2020-06-22 22:57:01 +02:00
|
|
|
|
|
|
|
log_data_str = f"[{count} updated]"
|
2021-08-21 19:24:20 +02:00
|
|
|
log_data = RequestNotes.get_notes(request).log_data
|
2021-07-09 10:06:04 +02:00
|
|
|
assert log_data is not None
|
|
|
|
log_data["extra"] = log_data_str
|
2020-06-22 22:57:01 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
return json_success({"result": "success", "msg": ""})
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-06-22 22:57:01 +02:00
|
|
|
|
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def mark_topic_as_read(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2021-04-07 22:00:44 +02:00
|
|
|
stream_id: int = REQ(json_validator=check_int),
|
2021-02-12 08:19:30 +01:00
|
|
|
topic_name: str = REQ(),
|
|
|
|
) -> HttpResponse:
|
2020-10-16 17:25:48 +02:00
|
|
|
stream, sub = access_stream_by_id(user_profile, stream_id)
|
2020-06-22 22:57:01 +02:00
|
|
|
|
|
|
|
if topic_name:
|
|
|
|
topic_exists = user_message_exists_for_topic(
|
|
|
|
user_profile=user_profile,
|
2020-10-16 17:02:33 +02:00
|
|
|
recipient_id=stream.recipient_id,
|
2020-06-22 22:57:01 +02:00
|
|
|
topic_name=topic_name,
|
|
|
|
)
|
|
|
|
|
|
|
|
if not topic_exists:
|
2021-02-12 08:20:45 +01:00
|
|
|
raise JsonableError(_("No such topic '{}'").format(topic_name))
|
2020-06-22 22:57:01 +02:00
|
|
|
|
2020-10-16 16:25:32 +02:00
|
|
|
count = do_mark_stream_messages_as_read(user_profile, stream.recipient_id, topic_name)
|
2020-06-22 22:57:01 +02:00
|
|
|
|
|
|
|
log_data_str = f"[{count} updated]"
|
2021-08-21 19:24:20 +02:00
|
|
|
log_data = RequestNotes.get_notes(request).log_data
|
2021-07-09 10:06:04 +02:00
|
|
|
assert log_data is not None
|
|
|
|
log_data["extra"] = log_data_str
|
2020-06-22 22:57:01 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
return json_success({"result": "success", "msg": ""})
|