예제 #1
0
파일: db.py 프로젝트: jk128/autopush
    def fetch_messages(
            self,
            uaid,  # type: uuid.UUID
            limit=10,  # type: int
    ):
        # type: (...) -> Tuple[Optional[int], List[WebPushNotification]]
        """Fetches messages for a uaid

        :returns: A tuple of the last timestamp to read for timestamped
                  messages and the list of non-timestamped messages.

        """
        # Eagerly fetches all results in the result set.
        response = self.table.query(
            KeyConditionExpression=(Key("uaid").eq(hasher(uaid.hex))
                                    & Key('chidmessageid').lt('02')),
            ConsistentRead=True,
            Limit=limit,
        )
        results = list(response['Items'])
        # First extract the position if applicable, slightly higher than 01:
        # to ensure we don't load any 01 remainders that didn't get deleted
        # yet
        last_position = None
        if results:
            # Ensure we return an int, as boto2 can return Decimals
            if results[0].get("current_timestamp"):
                last_position = int(results[0]["current_timestamp"])

        return last_position, [
            WebPushNotification.from_message_table(uaid, x)
            for x in results[1:]
        ]
예제 #2
0
    def fetch_timestamp_messages(
            self,
            uaid,  # type: uuid.UUID
            timestamp=None,  # type: Optional[Union[int, str]]
            limit=10,  # type: int
            ):
        # type: (...) -> Tuple[Optional[int], List[WebPushNotification]]
        """Fetches timestamped messages for a uaid

        Note that legacy messages start with a hex UUID, so they may be mixed
        in with timestamp messages beginning with 02. As such we only move our
        last_position forward to the last timestamped message.

        :returns: A tuple of the last timestamp to read and the list of
                  timestamped messages.

        """
        # Turn the timestamp into a proper sort key
        if timestamp:
            sortkey = "02:{timestamp}:z".format(timestamp=timestamp)
        else:
            sortkey = "01;"

        response = self.table.query(
            KeyConditionExpression=(Key('uaid').eq(hasher(uaid.hex))
                                    & Key('chidmessageid').gt(sortkey)),
            ConsistentRead=True,
            Limit=limit
        )
        notifs = [
            WebPushNotification.from_message_table(uaid, x) for x in
            response.get("Items")
        ]
        ts_notifs = [x for x in notifs if x.sortkey_timestamp]
        last_position = None
        if ts_notifs:
            last_position = ts_notifs[-1].sortkey_timestamp
        return last_position, notifs
예제 #3
0
    def fetch_timestamp_messages(
            self,
            uaid,  # type: uuid.UUID
            timestamp=None,  # type: Optional[int]
            limit=10,  # type: int
    ):
        # type: (...) -> Tuple[Optional[int], List[WebPushNotification]]
        """Fetches timestamped messages for a uaid

        Note that legacy messages start with a hex UUID, so they may be mixed
        in with timestamp messages beginning with 02. As such we only move our
        last_position forward to the last timestamped message.

        :returns: A tuple of the last timestamp to read and the list of
                  timestamped messages.

        """
        # Turn the timestamp into a proper sort key
        if timestamp:
            sortkey = "02:{timestamp}:z".format(timestamp=timestamp)
        else:
            sortkey = "01;"

        results = list(
            self.table.query_2(uaid__eq=hasher(uaid.hex),
                               chidmessageid__gt=sortkey,
                               consistent=True,
                               limit=limit))
        notifs = [
            WebPushNotification.from_message_table(uaid, x) for x in results
        ]
        ts_notifs = [x for x in notifs if x.sortkey_timestamp]
        last_position = None
        if ts_notifs:
            last_position = ts_notifs[-1].sortkey_timestamp
        return last_position, notifs