Example #1
0
def get_topics_backend(
    request: HttpRequest,
    maybe_user_profile: Union[UserProfile, AnonymousUser],
    stream_id: int = REQ(converter=to_non_negative_int, path_only=True),
) -> HttpResponse:

    if not maybe_user_profile.is_authenticated:
        is_web_public_query = True
        user_profile: Optional[UserProfile] = None
    else:
        is_web_public_query = False
        assert isinstance(maybe_user_profile, UserProfile)
        user_profile = maybe_user_profile
        assert user_profile is not None

    if is_web_public_query:
        realm = get_valid_realm_from_request(request)
        stream = access_web_public_stream(stream_id, realm)
        result = get_topic_history_for_public_stream(
            recipient_id=stream.recipient_id)

    else:
        assert user_profile is not None

        (stream, sub) = access_stream_by_id(user_profile, stream_id)

        result = get_topic_history_for_stream(
            user_profile=user_profile,
            recipient_id=stream.recipient_id,
            public_history=stream.is_history_public_to_subscribers(),
        )

    return json_success(dict(topics=result))
Example #2
0
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:
        return render(
            request,
            "zerver/archive/index.html",
            context={
                "is_web_public": is_web_public,
                "message_list": rendered_message_list,
                "stream": stream_name,
                "topic": topic_name,
            },
        )

    try:
        stream = access_web_public_stream(stream_id, request.realm)
    except JsonableError:
        return get_response([], False, "")

    all_messages = list(
        messages_for_topic(
            stream_recipient_id=stream.recipient_id,
            topic_name=topic_name,
        )
        .select_related("sender")
        .order_by("date_sent"),
    )

    if not all_messages:
        return get_response([], True, stream.name)

    rendered_message_list = []
    prev_sender: Optional[UserProfile] = None
    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 else msg.date_sent
            ),
            "message_content": msg.rendered_content,
            "avatar_url": get_gravatar_url(msg.sender.delivery_email, 1),
            "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)
    return get_response(rendered_message_list, True, stream.name)
Example #3
0
def get_web_public_topics_backend(request: HttpRequest, stream_id: int) -> HttpResponse:
    try:
        stream = access_web_public_stream(stream_id, request.realm)
    except JsonableError:
        return json_success(dict(topics=[]))

    result = get_topic_history_for_public_stream(recipient_id=stream.recipient_id)

    return json_success(dict(topics=result))