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.streams import get_stream_by_id
|
|
|
|
|
2020-02-18 17:25:43 +01:00
|
|
|
from zerver.models import Message, UserProfile
|
2018-04-27 15:48:55 +02:00
|
|
|
from zerver.lib.avatar import get_gravatar_url
|
2018-06-14 00:44:22 +02:00
|
|
|
from zerver.lib.response import json_success
|
2018-04-27 15:48:55 +02:00
|
|
|
from zerver.lib.timestamp import datetime_to_timestamp
|
2018-11-09 19:02:54 +01:00
|
|
|
from zerver.lib.topic import (
|
|
|
|
get_topic_history_for_web_public_stream,
|
|
|
|
messages_for_topic,
|
|
|
|
)
|
2018-05-02 20:08:10 +02:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2018-04-27 15:48:55 +02:00
|
|
|
|
|
|
|
def archive(request: HttpRequest,
|
|
|
|
stream_id: int,
|
|
|
|
topic_name: str) -> HttpResponse:
|
|
|
|
|
2018-05-02 20:08:10 +02:00
|
|
|
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,
|
|
|
|
'zerver/archive/index.html',
|
|
|
|
context={
|
|
|
|
'is_web_public': is_web_public,
|
|
|
|
'message_list': rendered_message_list,
|
2018-05-02 20:08:10 +02:00
|
|
|
'stream': stream_name,
|
2018-04-27 15:48:55 +02:00
|
|
|
'topic': topic_name,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-05-02 20:08:10 +02:00
|
|
|
try:
|
|
|
|
stream = get_stream_by_id(stream_id)
|
|
|
|
except JsonableError:
|
|
|
|
return get_response([], False, '')
|
|
|
|
|
2018-04-27 15:48:55 +02:00
|
|
|
if not stream.is_web_public:
|
2018-05-02 20:08:10 +02:00
|
|
|
return get_response([], False, '')
|
2018-04-27 15:48:55 +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,
|
2019-08-28 02:43:19 +02: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 = []
|
2019-07-28 22:55:33 +02:00
|
|
|
prev_sender = None # type: Optional[UserProfile]
|
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:
|
|
|
|
status_message = msg.rendered_content[4+3: -4]
|
|
|
|
context = {
|
|
|
|
'sender_full_name': msg.sender.full_name,
|
|
|
|
'timestampstr': datetime_to_timestamp(msg.last_edit_time
|
|
|
|
if msg.last_edit_time
|
2019-08-28 02:43:19 +02:00
|
|
|
else msg.date_sent),
|
2018-04-27 15:48:55 +02:00
|
|
|
'message_content': msg.rendered_content,
|
2019-11-16 02:14:44 +01:00
|
|
|
'avatar_url': get_gravatar_url(msg.sender.delivery_email, 1),
|
2018-04-27 15:48:55 +02:00
|
|
|
'include_sender': include_sender,
|
|
|
|
'status_message': status_message,
|
|
|
|
}
|
|
|
|
rendered_msg = loader.render_to_string('zerver/archive/single_message.html', context)
|
|
|
|
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
|
|
|
|
|
|
|
def get_web_public_topics_backend(request: HttpRequest, stream_id: int) -> HttpResponse:
|
|
|
|
try:
|
|
|
|
stream = get_stream_by_id(stream_id)
|
|
|
|
except JsonableError:
|
|
|
|
return json_success(dict(topics=[]))
|
|
|
|
|
|
|
|
if not stream.is_web_public:
|
|
|
|
return json_success(dict(topics=[]))
|
|
|
|
|
2020-02-18 17:25:43 +01:00
|
|
|
result = get_topic_history_for_web_public_stream(recipient=stream.recipient)
|
2018-06-14 00:44:22 +02:00
|
|
|
|
|
|
|
return json_success(dict(topics=result))
|