2019-07-28 22:55:33 +02:00
|
|
|
from typing import List, Optional
|
2018-04-27 15:48:55 +02:00
|
|
|
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.shortcuts import render
|
|
|
|
from django.template import loader
|
|
|
|
|
|
|
|
from zerver.lib.avatar import get_gravatar_url
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2018-06-14 00:44:22 +02:00
|
|
|
from zerver.lib.response import json_success
|
2021-04-09 21:40:30 +02:00
|
|
|
from zerver.lib.streams import access_web_public_stream
|
2018-04-27 15:48:55 +02:00
|
|
|
from zerver.lib.timestamp import datetime_to_timestamp
|
2020-08-21 17:12:34 +02:00
|
|
|
from zerver.lib.topic import get_topic_history_for_public_stream, messages_for_topic
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import Message, UserProfile
|
|
|
|
|
2018-04-27 15:48:55 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
def archive(request: HttpRequest, stream_id: int, topic_name: str) -> HttpResponse:
|
|
|
|
def get_response(
|
|
|
|
rendered_message_list: List[str], is_web_public: bool, stream_name: str
|
|
|
|
) -> HttpResponse:
|
2018-04-27 15:48:55 +02:00
|
|
|
return render(
|
|
|
|
request,
|
2021-02-12 08:20:45 +01:00
|
|
|
"zerver/archive/index.html",
|
2018-04-27 15:48:55 +02:00
|
|
|
context={
|
2021-02-12 08:20:45 +01:00
|
|
|
"is_web_public": is_web_public,
|
|
|
|
"message_list": rendered_message_list,
|
|
|
|
"stream": stream_name,
|
|
|
|
"topic": topic_name,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
},
|
2018-04-27 15:48:55 +02:00
|
|
|
)
|
|
|
|
|
2018-05-02 20:08:10 +02:00
|
|
|
try:
|
2021-04-09 21:40:30 +02:00
|
|
|
stream = access_web_public_stream(stream_id, request.realm)
|
2018-05-02 20:08:10 +02:00
|
|
|
except JsonableError:
|
2021-02-12 08:20:45 +01:00
|
|
|
return get_response([], False, "")
|
2018-05-02 20:08:10 +02:00
|
|
|
|
2018-11-09 19:02:54 +01:00
|
|
|
all_messages = list(
|
|
|
|
messages_for_topic(
|
2020-02-11 16:04:05 +01:00
|
|
|
stream_recipient_id=stream.recipient_id,
|
2018-11-09 19:02:54 +01:00
|
|
|
topic_name=topic_name,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-02-12 08:20:45 +01:00
|
|
|
.select_related("sender")
|
|
|
|
.order_by("date_sent"),
|
2018-11-09 19:02:54 +01:00
|
|
|
)
|
|
|
|
|
2018-04-27 15:48:55 +02:00
|
|
|
if not all_messages:
|
2018-05-02 20:08:10 +02:00
|
|
|
return get_response([], True, stream.name)
|
2018-04-27 15:48:55 +02:00
|
|
|
|
|
|
|
rendered_message_list = []
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
prev_sender: Optional[UserProfile] = None
|
2018-04-27 15:48:55 +02:00
|
|
|
for msg in all_messages:
|
|
|
|
include_sender = False
|
|
|
|
status_message = Message.is_status_message(msg.content, msg.rendered_content)
|
|
|
|
if not prev_sender or prev_sender != msg.sender or status_message:
|
|
|
|
if status_message:
|
|
|
|
prev_sender = None
|
|
|
|
else:
|
|
|
|
prev_sender = msg.sender
|
|
|
|
include_sender = True
|
|
|
|
if status_message:
|
2021-02-12 08:19:30 +01:00
|
|
|
status_message = msg.rendered_content[4 + 3 : -4]
|
2018-04-27 15:48:55 +02:00
|
|
|
context = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"sender_full_name": msg.sender.full_name,
|
|
|
|
"timestampstr": datetime_to_timestamp(
|
2021-02-12 08:19:30 +01:00
|
|
|
msg.last_edit_time if msg.last_edit_time else msg.date_sent
|
|
|
|
),
|
2021-02-12 08:20:45 +01:00
|
|
|
"message_content": msg.rendered_content,
|
|
|
|
"avatar_url": get_gravatar_url(msg.sender.delivery_email, 1),
|
|
|
|
"include_sender": include_sender,
|
|
|
|
"status_message": status_message,
|
2018-04-27 15:48:55 +02:00
|
|
|
}
|
2021-02-12 08:20:45 +01:00
|
|
|
rendered_msg = loader.render_to_string("zerver/archive/single_message.html", context)
|
2018-04-27 15:48:55 +02:00
|
|
|
rendered_message_list.append(rendered_msg)
|
2018-05-02 20:08:10 +02:00
|
|
|
return get_response(rendered_message_list, True, stream.name)
|
2018-06-14 00:44:22 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-06-14 00:44:22 +02:00
|
|
|
def get_web_public_topics_backend(request: HttpRequest, stream_id: int) -> HttpResponse:
|
|
|
|
try:
|
2021-04-09 21:40:30 +02:00
|
|
|
stream = access_web_public_stream(stream_id, request.realm)
|
2018-06-14 00:44:22 +02:00
|
|
|
except JsonableError:
|
|
|
|
return json_success(dict(topics=[]))
|
|
|
|
|
2020-10-16 17:45:21 +02:00
|
|
|
result = get_topic_history_for_public_stream(recipient_id=stream.recipient_id)
|
2018-06-14 00:44:22 +02:00
|
|
|
|
|
|
|
return json_success(dict(topics=result))
|