Beispiel #1
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
Beispiel #2
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
Beispiel #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
Beispiel #4
0
 def get_users(self, limit, encoded_start_key=None):
     """Return a collection of user models."""
     decoded_start_key = {}
     if encoded_start_key:
         decoded_start_key = decode_cursor(encoded_start_key)
     results = self._dynamodb_client.get_items(limit, decoded_start_key,
                                               "UsersIndex")
     return self._process_query_or_scan_results(results, self._user_mapper,
                                                ItemType.USER.name)
Beispiel #5
0
 def get_private_chat_messages(self, private_chat_id, limit, **kwargs):
     """Return a collection of messages from a private chat."""
     chat_primary_key = self._private_chat_mapper.key(
         private_chat_id, private_chat_id)
     private_chat = self.get_private_chat(private_chat_id)
     if not private_chat:
         raise NotFoundException("Private chat not found")
     chat_primary_key = {
         "PK": {
             "S": PrimaryKeyPrefix.PRIVATE_CHAT + private_chat_id
         }
     }
     chat_query_results = self._dynamodb_client.query(
         1, {}, {
             "pk_name": "INVERTED_GSI_PK",
             "pk_value": chat_primary_key["PK"],
             "sk_name": "INVERTED_GSI_SK",
             "sk_value": {
                 "S": PrimaryKeyPrefix.USER
             },
         },
         index="InvertedIndex")
     if not chat_query_results["Items"]:
         raise NotFoundException("Private chat not found")
     cursor = {}
     if kwargs.get("cursor"):
         cursor = decode_cursor(kwargs["cursor"])
     message_primary_key = {
         "PK": {
             "S": PrimaryKeyPrefix.PRIVATE_CHAT + private_chat_id
         }
     }
     query_results = self._dynamodb_client.query(
         limit,
         cursor, {
             "pk_name": "PK",
             "pk_value": message_primary_key["PK"],
             "sk_name": "SK",
             "sk_value": {
                 "S": PrimaryKeyPrefix.PRIVATE_CHAT_MESSAGE
             },
         },
         scan_forward=False)
     return self._process_query_or_scan_results(
         query_results, self._private_chat_message_mapper,
         ItemType.PRIVATE_CHAT_MESSAGE.name)
Beispiel #6
0
 def get_communities(self, limit, **kwargs):
     """Return a collection of community models."""
     cursor = {}
     if kwargs.get("cursor"):
         cursor = decode_cursor(kwargs["cursor"])
     if "topic" in kwargs:
         return self.get_communities_by_topic(limit,
                                              kwargs["topic"],
                                              cursor=cursor)
     elif "country" in kwargs:
         return self.get_communities_by_location(limit,
                                                 kwargs,
                                                 cursor=cursor)
     else:
         results = self._dynamodb_client.get_items(limit, cursor,
                                                   "CommunitiesByLocation")
         return self._process_query_or_scan_results(results,
                                                    self._community_mapper,
                                                    ItemType.COMMUNITY.name)
Beispiel #7
0
 def get_community_group_chats(self, community_id, limit, **kwargs):
     """Return a collection of the community's group chats."""
     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.GROUP_CHAT
             },
         })
     response = self._process_query_or_scan_results(
         query_results, self._group_chat_mapper, ItemType.GROUP_CHAT.name)
     return response
Beispiel #8
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
Beispiel #9
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