def handle_group_filter(
    dataset: fod.Dataset,
    view: foc.SampleCollection,
    filter: GroupElementFilter,
) -> fov.DatasetView:
    """Handle a group filter for App view requests.

    Args:
        dataset: the :class:`fiftyone.core.dataset.Dataset`
        view: the base :class:`fiftyone.core.collections.SampleCollection`
        filter: the :class:`fiftyone.server.aggregations.GroupElementFilter`

    Returns:
        a :class:`fiftyone.core.view.DatasetView` with a group or slice
        selection
    """
    if not filter.id and not filter.slice and not filter.slices:
        # nothing to do
        return view, None

    stages = view._stages
    group_field = dataset.group_field

    selected = False
    for stage in stages:
        if isinstance(stage, fosg.SelectGroupSlices) and stage.flat:
            selected = True

    group_by = any(isinstance(stage, fosg.GroupBy) for stage in stages)

    view = dataset.view()
    if filter.slice:
        view.group_slice = filter.slice

    if not selected and filter.slices:
        # flatten the collection if the view has no slice(s) selected
        view = dataset.select_group_slices(_force_mixed=True)

        if filter.id:
            # use 'match' to select a group by 'id'
            view = view.match(
                {group_field + "._id": {"$in": [ObjectId(filter.id)]}}
            )

        for stage in stages:
            # add stages after flattening and group match
            if group_by and isinstance(stage, fosg.GroupBy) and filter.slices:
                view = view.match(
                    {group_field + ".name": {"$in": filter.slices}}
                )

            if isinstance(
                stage, (fosg.SelectGroupSlices, fosg.ExcludeGroupSlices)
            ):
                continue

            # if selecting a group, filter out select/reorder stages
            if (
                not filter.id
                or type(stage) not in fosg._STAGES_THAT_SELECT_OR_REORDER
            ):
                view = view._add_view_stage(stage, validate=False)

    elif filter.id:
        view = fov.make_optimized_select_view(view, filter.id, groups=True)

    if not group_by and filter.slices:
        # use 'match' to select requested slices, and avoid media type
        # validation
        view = view.match({group_field + ".name": {"$in": filter.slices}})

    media_types = None
    if dataset.group_media_types:
        media_types = (
            set(dataset.group_media_types[s] for s in filter.slices)
            if filter.slices
            else set(dataset.group_media_types.values())
        )
    return view, media_types
Handle a group filter for App view requests.

    Args:
        dataset: the :class:`fiftyone.core.dataset.Dataset`
        view: the base :class:`fiftyone.core.collections.SampleCollection`
        filter: the :class:`fiftyone.server.aggregations.GroupElementFilter`

    Returns:
        a :class:`fiftyone.core.view.DatasetView` with a group or slice
        selection
    
fiftyone/server/view.py