Esempio n. 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,
    }
Esempio n. 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,
    }
Esempio n. 3
0
def get_topic(request: Request) -> 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, CommentTreeSortOption.NEWEST, request.user)

    # check for link information (content metadata) to display
    if topic.is_link_type:
        content_metadata = topic.content_metadata_fields_for_display.copy()

        fields_to_hide = ("Domain", "Description")
        for field in fields_to_hide:
            content_metadata.pop(field, None)

        # don't include the title if it's pretty similar to the topic's title
        if "Title" in content_metadata:
            similarity = SequenceMatcher(a=content_metadata["Title"],
                                         b=topic.title)
            if similarity.ratio() >= 0.6:
                del content_metadata["Title"]
    else:
        content_metadata = None

    # 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_PINNED,
        LogEventType.TOPIC_TAG,
        LogEventType.TOPIC_TITLE_EDIT,
        LogEventType.TOPIC_UNLOCK,
        LogEventType.TOPIC_UNREMOVE,
        LogEventType.TOPIC_UNPINNED,
    )
    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 request.user:
        request.db_session.add(TopicVisit(request.user, topic))

        # 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,
        "content_metadata": content_metadata,
        "log": log,
        "comments": tree,
        "comment_label_options": CommentLabelOption,
    }