Esempio n. 1
0
def _can_moderate_event(session, event: Event, user_id):
    # if the event is owned by a cluster, then any moderator of that cluster can moderate this event
    if event.owner_cluster is not None and can_moderate_node(
            session, user_id, event.owner_cluster.parent_node_id):
        return True

    # finally check if the user can moderate the parent node of the cluster
    return can_moderate_node(session, user_id, event.parent_node_id)
Esempio n. 2
0
def _can_moderate_page(page: Page, user_id):
    """
    Checks if the user is allowed to moderate this page
    """
    # checks if either the page is in the exclusive moderation area of a node
    with session_scope() as session:
        latest_version = page.versions[-1]

        # if the page has a location, we firstly check if we are the moderator of any node that contains this page
        if latest_version.geom is not None and can_moderate_at(session, user_id, latest_version.geom):
            return True

        # if the page is owned by a cluster, then any moderator of that cluster can moderate this page
        if page.owner_cluster is not None and can_moderate_node(session, user_id, page.owner_cluster.parent_node_id):
            return True

        # finally check if the user can moderate the parent node of the cluster
        return can_moderate_node(session, user_id, page.parent_node_id)
Esempio n. 3
0
def group_to_pb(cluster: Cluster, context):
    with session_scope() as session:
        can_moderate = can_moderate_node(session, context.user_id, cluster.parent_node_id)

        member_count = session.execute(
            select(func.count())
            .select_from(ClusterSubscription)
            .where_users_column_visible(context, ClusterSubscription.user_id)
            .where(ClusterSubscription.cluster_id == cluster.id)
        ).scalar_one()
        is_member = (
            session.execute(
                select(ClusterSubscription)
                .where(ClusterSubscription.user_id == context.user_id)
                .where(ClusterSubscription.cluster_id == cluster.id)
            ).scalar_one_or_none()
            is not None
        )

        admin_count = session.execute(
            select(func.count())
            .select_from(ClusterSubscription)
            .where_users_column_visible(context, ClusterSubscription.user_id)
            .where(ClusterSubscription.cluster_id == cluster.id)
            .where(ClusterSubscription.role == ClusterRole.admin)
        ).scalar_one()
        is_admin = (
            session.execute(
                select(ClusterSubscription)
                .where(ClusterSubscription.user_id == context.user_id)
                .where(ClusterSubscription.cluster_id == cluster.id)
                .where(ClusterSubscription.role == ClusterRole.admin)
            ).scalar_one_or_none()
            is not None
        )

    return groups_pb2.Group(
        group_id=cluster.id,
        name=cluster.name,
        slug=cluster.slug,
        description=cluster.description,
        created=Timestamp_from_datetime(cluster.created),
        parents=_parents_to_pb(cluster),
        main_page=page_to_pb(cluster.main_page, context),
        member=is_member,
        admin=is_admin,
        member_count=member_count,
        admin_count=admin_count,
        can_moderate=can_moderate,
    )
Esempio n. 4
0
def community_to_pb(node: Node, context):
    with session_scope() as session:
        can_moderate = can_moderate_node(session, context.user_id, node.id)

        member_count = session.execute(
            select(func.count())
            .select_from(ClusterSubscription)
            .where_users_column_visible(context, ClusterSubscription.user_id)
            .where(ClusterSubscription.cluster_id == node.official_cluster.id)
        ).scalar_one()
        is_member = (
            session.execute(
                select(ClusterSubscription)
                .where(ClusterSubscription.user_id == context.user_id)
                .where(ClusterSubscription.cluster_id == node.official_cluster.id)
            ).scalar_one_or_none()
            is not None
        )

        admin_count = session.execute(
            select(func.count())
            .select_from(ClusterSubscription)
            .where_users_column_visible(context, ClusterSubscription.user_id)
            .where(ClusterSubscription.cluster_id == node.official_cluster.id)
            .where(ClusterSubscription.role == ClusterRole.admin)
        ).scalar_one()
        is_admin = (
            session.execute(
                select(ClusterSubscription)
                .where(ClusterSubscription.user_id == context.user_id)
                .where(ClusterSubscription.cluster_id == node.official_cluster.id)
                .where(ClusterSubscription.role == ClusterRole.admin)
            ).scalar_one_or_none()
            is not None
        )

    return communities_pb2.Community(
        community_id=node.id,
        name=node.official_cluster.name,
        slug=node.official_cluster.slug,
        description=node.official_cluster.description,
        created=Timestamp_from_datetime(node.created),
        parents=_parents_to_pb(node.id),
        member=is_member,
        admin=is_admin,
        member_count=member_count,
        admin_count=admin_count,
        main_page=page_to_pb(node.official_cluster.main_page, context),
        can_moderate=can_moderate,
    )
Esempio n. 5
0
def community_to_pb(node: Node, user_id):
    with session_scope() as session:
        can_moderate = can_moderate_node(session, user_id, node.id)

    return communities_pb2.Community(
        community_id=node.id,
        name=node.official_cluster.name,
        slug=node.official_cluster.slug,
        description=node.official_cluster.description,
        created=Timestamp_from_datetime(node.created),
        parents=_parents_to_pb(node.id, user_id),
        main_page=page_to_pb(node.official_cluster.main_page, user_id),
        member=node.official_cluster.members.filter(
            User.id == user_id).one_or_none() is not None,
        admin=node.official_cluster.admins.filter(
            User.id == user_id).one_or_none() is not None,
        member_count=node.official_cluster.members.count(),
        admin_count=node.official_cluster.admins.count(),
        can_moderate=can_moderate,
    )
Esempio n. 6
0
def group_to_pb(cluster: Cluster, user_id):
    with session_scope() as session:
        can_moderate = can_moderate_node(session, user_id,
                                         cluster.parent_node_id)

    return groups_pb2.Group(
        group_id=cluster.id,
        name=cluster.name,
        slug=cluster.slug,
        description=cluster.description,
        created=Timestamp_from_datetime(cluster.created),
        parents=_parents_to_pb(cluster, user_id),
        main_page=page_to_pb(cluster.main_page, user_id),
        member=cluster.members.filter(User.id == user_id).one_or_none()
        is not None,
        admin=cluster.admins.filter(User.id == user_id).one_or_none()
        is not None,
        member_count=cluster.members.count(),
        admin_count=cluster.admins.count(),
        can_moderate=can_moderate,
    )
Esempio n. 7
0
def discussion_to_pb(discussion: Discussion, user_id):
    owner_community_id = None
    owner_group_id = None
    if discussion.owner_cluster.is_official_cluster:
        owner_community_id = discussion.owner_cluster.parent_node_id
    else:
        owner_group_id = discussion.owner_cluster.id

    with session_scope() as session:
        can_moderate = can_moderate_node(session, user_id, discussion.owner_cluster.parent_node_id)

    return discussions_pb2.Discussion(
        discussion_id=discussion.id,
        slug=discussion.slug,
        created=Timestamp_from_datetime(discussion.created),
        creator_user_id=discussion.creator_user_id,
        owner_community_id=owner_community_id,
        owner_group_id=owner_group_id,
        title=discussion.title,
        content=discussion.content,
        thread_id=pack_thread_id(discussion.thread_id, 0),
        can_moderate=can_moderate,
    )