Esempio n. 1
0
    def ListAllEvents(self, request, context):
        with session_scope() as session:
            page_size = min(MAX_PAGINATION_LENGTH, request.page_size
                            or MAX_PAGINATION_LENGTH)
            # the page token is a unix timestamp of where we left off
            page_token = dt_from_millis(int(
                request.page_token)) if request.page_token else now()

            occurrences = select(EventOccurrence).join(
                Event, Event.id == EventOccurrence.event_id)

            if not request.past:
                occurrences = occurrences.where(
                    EventOccurrence.end_time > page_token -
                    timedelta(seconds=1)).order_by(
                        EventOccurrence.start_time.asc())
            else:
                occurrences = occurrences.where(
                    EventOccurrence.end_time < page_token +
                    timedelta(seconds=1)).order_by(
                        EventOccurrence.start_time.desc())

            occurrences = occurrences.limit(page_size + 1)
            occurrences = session.execute(occurrences).scalars().all()

            return events_pb2.ListAllEventsRes(
                events=[
                    event_to_pb(session, occurrence, context)
                    for occurrence in occurrences[:page_size]
                ],
                next_page_token=str(millis_from_dt(occurrences[-1].end_time))
                if len(occurrences) > page_size else None,
            )
Esempio n. 2
0
    def ListEvents(self, request, context):
        with session_scope() as session:
            page_size = min(MAX_PAGINATION_LENGTH, request.page_size or MAX_PAGINATION_LENGTH)
            # the page token is a unix timestamp of where we left off
            page_token = dt_from_millis(int(request.page_token)) if request.page_token else now()

            cluster = session.execute(
                select(Cluster).where(~Cluster.is_official_cluster).where(Cluster.id == request.group_id)
            ).scalar_one_or_none()
            if not cluster:
                context.abort(grpc.StatusCode.NOT_FOUND, errors.GROUP_NOT_FOUND)

            occurrences = (
                select(EventOccurrence)
                .join(Event, Event.id == EventOccurrence.event_id)
                .where(Event.owner_cluster == cluster)
            )

            if not request.past:
                occurrences = occurrences.where(EventOccurrence.end_time > page_token - timedelta(seconds=1)).order_by(
                    EventOccurrence.start_time.asc()
                )
            else:
                occurrences = occurrences.where(EventOccurrence.end_time < page_token + timedelta(seconds=1)).order_by(
                    EventOccurrence.start_time.desc()
                )

            occurrences = occurrences.limit(page_size + 1)
            occurrences = session.execute(occurrences).scalars().all()

            return groups_pb2.ListEventsRes(
                events=[event_to_pb(session, occurrence, context) for occurrence in occurrences[:page_size]],
                next_page_token=str(millis_from_dt(occurrences[-1].end_time)) if len(occurrences) > page_size else None,
            )
Esempio n. 3
0
    def ListEvents(self, request, context):
        with session_scope() as session:
            page_size = min(MAX_PAGINATION_LENGTH, request.page_size or MAX_PAGINATION_LENGTH)
            # the page token is a unix timestamp of where we left off
            page_token = dt_from_millis(int(request.page_token)) if request.page_token else now()

            node = session.execute(select(Node).where(Node.id == request.community_id)).scalar_one_or_none()
            if not node:
                context.abort(grpc.StatusCode.NOT_FOUND, errors.COMMUNITY_NOT_FOUND)

            # for communities, we list events owned by this community or for which this is a parent
            occurrences = (
                select(EventOccurrence)
                .join(Event, Event.id == EventOccurrence.event_id)
                .where(or_(Event.owner_cluster == node.official_cluster, Event.parent_node == node))
            )

            if request.past:
                occurrences = occurrences.where(EventOccurrence.end_time < page_token + timedelta(seconds=1)).order_by(
                    EventOccurrence.start_time.desc()
                )
            else:
                occurrences = occurrences.where(EventOccurrence.end_time > page_token - timedelta(seconds=1)).order_by(
                    EventOccurrence.start_time.asc()
                )

            occurrences = occurrences.limit(page_size + 1)
            occurrences = session.execute(occurrences).scalars().all()

            return communities_pb2.ListEventsRes(
                events=[event_to_pb(session, occurrence, context) for occurrence in occurrences[:page_size]],
                next_page_token=str(millis_from_dt(occurrences[-1].end_time)) if len(occurrences) > page_size else None,
            )
Esempio n. 4
0
    def ListMyEvents(self, request, context):
        with session_scope() as session:
            page_size = min(MAX_PAGINATION_LENGTH, request.page_size
                            or MAX_PAGINATION_LENGTH)
            # the page token is a unix timestamp of where we left off
            page_token = dt_from_millis(int(
                request.page_token)) if request.page_token else now()

            occurrences = select(EventOccurrence).join(
                Event, Event.id == EventOccurrence.event_id)

            include_all = not (request.subscribed or request.attending
                               or request.organizing or request.my_communities)
            include_subscribed = request.subscribed or include_all
            include_organizing = request.organizing or include_all
            include_attending = request.attending or include_all
            include_my_communities = request.my_communities or include_all

            where_ = []

            if include_subscribed:
                occurrences = occurrences.outerjoin(
                    EventSubscription,
                    and_(EventSubscription.event_id == Event.id,
                         EventSubscription.user_id == context.user_id),
                )
                where_.append(EventSubscription.user_id != None)
            if include_organizing:
                occurrences = occurrences.outerjoin(
                    EventOrganizer,
                    and_(EventOrganizer.event_id == Event.id,
                         EventOrganizer.user_id == context.user_id))
                where_.append(EventOrganizer.user_id != None)
            if include_attending:
                occurrences = occurrences.outerjoin(
                    EventOccurrenceAttendee,
                    and_(
                        EventOccurrenceAttendee.occurrence_id ==
                        EventOccurrence.id,
                        EventOccurrenceAttendee.user_id == context.user_id,
                    ),
                )
                where_.append(EventOccurrenceAttendee.user_id != None)
            if include_my_communities:
                my_communities = (session.execute(
                    select(Node.id).join(
                        Cluster, Cluster.parent_node_id == Node.id).join(
                            ClusterSubscription,
                            ClusterSubscription.cluster_id == Cluster.id).
                    where(
                        ClusterSubscription.user_id == context.user_id).where(
                            Cluster.is_official_cluster).order_by(
                                Node.id).limit(100000)).scalars().all())
                where_.append(Event.parent_node_id.in_(my_communities))

            occurrences = occurrences.where(or_(*where_))

            if not request.past:
                occurrences = occurrences.where(
                    EventOccurrence.end_time > page_token -
                    timedelta(seconds=1)).order_by(
                        EventOccurrence.start_time.asc())
            else:
                occurrences = occurrences.where(
                    EventOccurrence.end_time < page_token +
                    timedelta(seconds=1)).order_by(
                        EventOccurrence.start_time.desc())

            occurrences = occurrences.limit(page_size + 1)
            occurrences = session.execute(occurrences).scalars().all()

            return events_pb2.ListMyEventsRes(
                events=[
                    event_to_pb(session, occurrence, context)
                    for occurrence in occurrences[:page_size]
                ],
                next_page_token=str(millis_from_dt(occurrences[-1].end_time))
                if len(occurrences) > page_size else None,
            )