Example #1
0
 def get_group_chat_messages(self, community_id, group_chat_id, limit,
                             **kwargs):
     """Return a collection of group chat messages."""
     cursor = {}
     if kwargs.get("cursor"):
         cursor = decode_cursor(kwargs["cursor"])
     group_chat = self.get_group_chat(community_id, group_chat_id)
     if not group_chat:
         raise NotFoundException("Group chat not found")
     primary_key = self._group_chat_mapper.key(community_id, group_chat_id)
     query_results = self._dynamodb_client.query(
         limit,
         cursor, {
             "pk_name": "PK",
             "pk_value": primary_key["SK"],
             "sk_name": "SK",
             "sk_value": {
                 "S": PrimaryKeyPrefix.GROUP_CHAT_MESSAGE
             },
         },
         scan_forward=False)
     if not query_results["Items"]:
         response = {"models": [], "total": 0}
     else:
         response = self._process_query_or_scan_results(
             query_results, self._group_chat_message_mapper,
             ItemType.GROUP_CHAT_MESSAGE.name)
     response["has_next"] = query_results["LastEvaluatedKey"] is not None
     response["next"] = encode_cursor(query_results["LastEvaluatedKey"]
                                      or {})
     return response
Example #2
0
    def get_user_notifications(self, user_id, limit, **kwargs):
        """Return a collection of the user's notifications."""
        user = self.get_user(user_id)
        if not user:
            raise NotFoundException("User not found")
        cursor = {}
        if kwargs.get("cursor"):
            cursor = decode_cursor(kwargs["cursor"])
        user_primary_key = self._user_mapper.key(user_id)
        query_results = self._dynamodb_client.query(
            limit,
            cursor, {
                "pk_name": "PK",
                "pk_value": user_primary_key["PK"],
                "sk_name": "SK",
                "sk_value": {
                    "S": PrimaryKeyPrefix.NOTIFICATION
                },
            },
            scan_forward=False)

        if not query_results["Items"]:
            response = {"models": [], "total": 0}
        else:
            response = self._process_query_or_scan_results(
                query_results, self._notification_mapper,
                ItemType.NOTIFICATION.name)
        response["has_next"] = query_results["LastEvaluatedKey"] is not None
        response["next"] = encode_cursor(query_results["LastEvaluatedKey"]
                                         or {})
        return response
Example #3
0
 def get_community_members(self, community_id, limit, **kwargs):
     """Return a collection of users who are in the given community."""
     cursor = {}
     if kwargs.get("cursor"):
         cursor = decode_cursor(kwargs["cursor"])
     community = self.get_community(community_id)
     if not community:
         raise NotFoundException("Community not found")
     primary_key = self._community_mapper.key(community_id)
     query_results = self._dynamodb_client.query(
         limit, cursor, {
             "pk_name": "PK",
             "pk_value": primary_key["PK"],
             "sk_name": "SK",
             "sk_value": {
                 "S": PrimaryKeyPrefix.USER
             },
         })
     user_keys = [
         self._user_mapper.key(item["user_id"]["S"], item["user_id"]["S"])
         for item in query_results["Items"]
     ]
     batch_results = self._dynamodb_client.batch_get_items(user_keys)
     response = self._process_batch_results(batch_results,
                                            self._user_mapper,
                                            ItemType.USER.name)
     response["has_next"] = query_results["LastEvaluatedKey"] is not None
     response["next"] = encode_cursor(query_results["LastEvaluatedKey"]
                                      or {})
     return response
Example #4
0
 def _process_query_or_scan_results(self, results, mapper, item_type):
     next_cursor = encode_cursor(results["LastEvaluatedKey"] or {})
     models = [
         mapper.deserialize_to_model(item) for item in results["Items"]
         if item["type"]["S"] == item_type
     ]
     response = {
         "models": models,
         "next": next_cursor,
         "has_next": results["LastEvaluatedKey"] is not None,
         "total": len(models),
     }
     return response
Example #5
0
    def get_user_communities(self, user_id, limit, **kwargs):
        """Return a collection of the user's communities."""
        cursor = {}
        if kwargs.get("cursor"):
            cursor = decode_cursor(kwargs["cursor"])
        user = self.get_user(user_id)
        if not user:
            raise NotFoundException("User not found")
        primary_key = self._user_mapper.key(user_id)
        query_results = self._dynamodb_client.query(
            limit,
            cursor, {
                "pk_name": "INVERTED_GSI_PK",
                "pk_value": primary_key["PK"],
                "sk_name": "INVERTED_GSI_SK",
                "sk_value": {
                    "S": PrimaryKeyPrefix.COMMUNITY
                },
            },
            index="InvertedIndex")

        if not query_results["Items"]:
            response = {"models": [], "total": 0}
        else:
            community_keys = [
                self._community_mapper.key(item["community_id"]["S"],
                                           item["community_id"]["S"])
                for item in query_results["Items"]
            ]

            batch_results = self._dynamodb_client.batch_get_items(
                community_keys)

            response = self._process_batch_results(batch_results,
                                                   self._community_mapper,
                                                   ItemType.COMMUNITY.name)
        response["has_next"] = query_results["LastEvaluatedKey"] is not None
        response["next"] = encode_cursor(query_results["LastEvaluatedKey"]
                                         or {})
        return response
Example #6
0
    def get_user_private_chats(self, user_id, limit, **kwargs):
        """Return a collection of users that the given user has DMs with."""
        user = self.get_user(user_id)
        if not user:
            raise NotFoundException("User not found")
        cursor = {}
        if kwargs.get("cursor"):
            cursor = decode_cursor(kwargs["cursor"])
        primary_key = self._user_mapper.key(user_id)
        query_results = self._dynamodb_client.query(
            limit, cursor, {
                "pk_name": "PK",
                "pk_value": primary_key["PK"],
                "sk_name": "SK",
                "sk_value": {
                    "S": PrimaryKeyPrefix.PRIVATE_CHAT
                },
            })

        if not query_results["Items"]:
            response = {"models": [], "total": 0}
        else:
            user_keys = []
            for item in query_results["Items"]:
                user_key = self._user_mapper.key(item["other_user_id"]["S"],
                                                 item["other_user_id"]["S"])
                user_keys.append(user_key)
            batch_results = self._dynamodb_client.batch_get_items(user_keys)
            response = self._process_batch_results(batch_results,
                                                   self._user_mapper,
                                                   ItemType.USER.name)
            response["models"] = self._create_private_chats(
                user, response["models"], query_results["Items"])
        response["has_next"] = query_results["LastEvaluatedKey"] is not None
        response["next"] = encode_cursor(query_results["LastEvaluatedKey"]
                                         or {})
        return response