Exemple #1
0
def get_topic(request: Request, comment_order: CommentTreeSortOption) -> dict:
    """View a single topic."""
    topic = request.context

    # deleted and removed comments need to be included since they're necessary for
    # building the tree if they have replies
    comments = (
        request.query(Comment).include_deleted().include_removed().filter(
            Comment.topic == topic).order_by(Comment.created_time).all())
    tree = CommentTree(comments, comment_order, request.user)

    # check if there are any items in the log to show
    visible_events = (
        LogEventType.TOPIC_LINK_EDIT,
        LogEventType.TOPIC_LOCK,
        LogEventType.TOPIC_MOVE,
        LogEventType.TOPIC_REMOVE,
        LogEventType.TOPIC_TAG,
        LogEventType.TOPIC_TITLE_EDIT,
        LogEventType.TOPIC_UNLOCK,
        LogEventType.TOPIC_UNREMOVE,
    )
    log = (request.query(LogTopic).filter(
        LogTopic.topic == topic,
        LogTopic.event_type.in_(visible_events)).order_by(
            desc(LogTopic.event_time)).all())

    tree.collapse_from_labels()

    # if the user has the "mark new comments" feature enabled
    if request.user and request.user.track_comment_visits:
        # update their last visit time for this topic
        statement = TopicVisit.generate_insert_statement(request.user, topic)
        request.db_session.execute(statement)
        mark_changed(request.db_session)

        # collapse old comments if the user has a previous visit to the topic
        # (and doesn't have that behavior disabled)
        if topic.last_visit_time and request.user.collapse_old_comments:
            tree.uncollapse_new_comments(topic.last_visit_time)
            tree.finalize_collapsing_maximized()

    return {
        "topic": topic,
        "log": log,
        "comments": tree,
        "comment_order": comment_order,
        "comment_order_options": CommentTreeSortOption,
        "comment_label_options": CommentLabelOption,
    }
Exemple #2
0
def get_topic(request: Request, comment_order: CommentSortOption) -> dict:
    """View a single topic."""
    topic = request.context

    # deleted and removed comments need to be included since they're necessary
    # for building the tree if they have replies
    comments = (
        request.query(Comment).include_deleted().include_removed().filter(
            Comment.topic == topic).order_by(Comment.created_time).all())
    tree = CommentTree(comments, comment_order)

    # check if there are any items in the log to show
    visible_events = (
        LogEventType.TOPIC_LOCK,
        LogEventType.TOPIC_MOVE,
        LogEventType.TOPIC_TAG,
        LogEventType.TOPIC_TITLE_EDIT,
        LogEventType.TOPIC_UNLOCK,
    )
    log = (
        request.query(LogTopic).filter(
            LogTopic.topic == topic,
            LogTopic.event_type.in_(visible_events)  # noqa
        ).order_by(desc(LogTopic.event_time)).all())

    # if the feature's enabled, update their last visit to this topic
    if request.user and request.user.track_comment_visits:
        statement = TopicVisit.generate_insert_statement(request.user, topic)

        request.db_session.execute(statement)
        mark_changed(request.db_session)

    return {
        'topic': topic,
        'log': log,
        'comments': tree,
        'comment_order': comment_order,
        'comment_order_options': CommentSortOption,
        'comment_tag_options': CommentTagOption,
    }