Exemple #1
0
    def get_chat_notifications(self, page_number):
        """
        Get Chat Notifications with Pagination (next page of results).
        This is for appending to the bottom of the notifications list.
        Chat Notifications are:
            1. UnreadChatRoomMessages
        """
        user = self.scope["user"]
        if user.is_authenticated:
            chatmessage_ct = ContentType.objects.get_for_model(
                UnreadChatRoomMessages)
            notifications = Notification.objects.filter(
                target=user,
                content_type=chatmessage_ct).order_by('-timestamp')
            p = Paginator(notifications, DEFAULT_NOTIFICATION_PAGE_SIZE)

            # sleep 1s for testing
            # sleep(1)
            payload = {}
            if len(notifications) > 0:
                if int(page_number) <= p.num_pages:
                    s = LazyNotificationEncoder()
                    serialized_notifications = s.serialize(
                        p.page(page_number).object_list)
                    payload['notifications'] = serialized_notifications
                    new_page_number = int(page_number) + 1
                    payload['new_page_number'] = new_page_number
                else:
                    return None
        else:
            raise NotificationClientError(
                "User must be authenticated to get notifications.")

        return json.dumps(payload)
Exemple #2
0
    def refresh_general_notifications(self, oldest_timestamp):
        """
        Retrieve the general notifications newer than the older one on the screen.
        This will accomplish 2 things:
        1. Notifications currently visible will be updated
        2. Any new notifications will be appending to the top of the list
        """
        payload = {}
        user = self.scope["user"]
        if user.is_authenticated:
            timestamp = oldest_timestamp[0:oldest_timestamp.find(
                "+")]  # remove timezone because who cares
            timestamp = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')
            friend_request_ct = ContentType.objects.get_for_model(
                FriendRequest)
            friend_list_ct = ContentType.objects.get_for_model(FriendList)
            notifications = Notification.objects.filter(
                target=user,
                content_type__in=[friend_request_ct, friend_list_ct],
                timestamp__gte=timestamp).order_by('-timestamp')

            s = LazyNotificationEncoder()
            payload['notifications'] = s.serialize(notifications)
        else:
            raise NotificationClientError(
                "User must be authenticated to get notifications.")

        return json.dumps(payload)
Exemple #3
0
def refresh_general_notifications(user, oldest_timestamp, newest_timestatmp):
    """
    Retrieve the general notifications newer than the oldest one on the screen and younger than the newest one the screen.
    The result will be: Notifications currently visible will be updated
    """
    payload = {}
    if user.is_authenticated:
        oldest_ts = oldest_timestamp[0:oldest_timestamp.find(
            "+")]  # remove timezone because who cares
        oldest_ts = datetime.strptime(oldest_ts, '%Y-%m-%d %H:%M:%S.%f')
        newest_ts = newest_timestatmp[0:newest_timestatmp.find(
            "+")]  # remove timezone because who cares
        newest_ts = datetime.strptime(newest_ts, '%Y-%m-%d %H:%M:%S.%f')
        friend_request_ct = ContentType.objects.get_for_model(FriendRequest)
        friend_list_ct = ContentType.objects.get_for_model(FriendList)
        notifications = Notification.objects.filter(
            target=user,
            content_type__in=[friend_request_ct, friend_list_ct],
            timestamp__gte=oldest_ts,
            timestamp__lte=newest_ts).order_by('-timestamp')

        s = LazyNotificationEncoder()
        payload['notifications'] = s.serialize(notifications)
    else:
        raise NotificationClientError(
            "User must be authenticated to get notifications.")

    return json.dumps(payload)
Exemple #4
0
    def accept_friend_request(self, notification_id):
        """
        Accept a friend request
        """
        payload = {}
        user = self.scope["user"]
        if user.is_authenticated:
            try:
                notification = Notification.objects.get(pk=notification_id)
                friend_request = notification.content_object
                # confirm this is the correct user
                if friend_request.receiver == user:
                    # accept the request and get the updated notification
                    updated_notification = friend_request.accept()

                    # return the notification associated with this FriendRequest
                    s = LazyNotificationEncoder()
                    payload['notification'] = s.serialize(
                        [updated_notification])[0]
                    return json.dumps(payload)
            except Notification.DoesNotExist:
                raise NotificationClientError(
                    "An error occurred with that notification. Try refreshing the browser."
                )
        return None
Exemple #5
0
def get_unread_chat_notification_count(user):
    payload = {}
    if user.is_authenticated:
        chatmessage_ct = ContentType.objects.get_for_model(
            UnreadChatRoomMessages)
        notifications = Notification.objects.filter(
            target=user, content_type__in=[chatmessage_ct])

        unread_count = 0
        if notifications:
            unread_count = len(notifications.all())
        payload['count'] = unread_count
        return json.dumps(payload)
    else:
        raise NotificationClientError(
            "User must be authenticated to get notifications.")
    return None
Exemple #6
0
def get_unread_general_notification_count(user):
    payload = {}
    if user.is_authenticated:
        friend_request_ct = ContentType.objects.get_for_model(FriendRequest)
        friend_list_ct = ContentType.objects.get_for_model(FriendList)
        notifications = Notification.objects.filter(
            target=user, content_type__in=[friend_request_ct, friend_list_ct])

        unread_count = 0
        if notifications:
            for notification in notifications.all():
                if not notification.read:
                    unread_count = unread_count + 1
        payload['count'] = unread_count
        return json.dumps(payload)
    else:
        raise NotificationClientError(
            "User must be authenticated to get notifications.")
    return None
Exemple #7
0
def get_new_chat_notifications(user, newest_timestatmp):
    """
    Retrieve any notifications newer than the newest_timestatmp on the screen.
    """
    payload = {}
    if user.is_authenticated:
        timestamp = newest_timestatmp[0:newest_timestatmp.find(
            "+")]  # remove timezone because who cares
        timestamp = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')
        chatmessage_ct = ContentType.objects.get_for_model(
            UnreadChatRoomMessages)
        notifications = Notification.objects.filter(
            target=user,
            content_type__in=[chatmessage_ct],
            timestamp__gt=timestamp).order_by('-timestamp')
        s = LazyNotificationEncoder()
        payload['notifications'] = s.serialize(notifications)
    else:
        raise NotificationClientError(
            "User must be authenticated to get notifications.")

    return json.dumps(payload)
Exemple #8
0
def get_general_notifications(user, page_number):
    """
    Get General Notifications with Pagination (next page of results).
    This is for appending to the bottom of the notifications list.
    General Notifications are:
        1. FriendRequest
        2. FriendList
    """
    if user.is_authenticated:
        friend_request_ct = ContentType.objects.get_for_model(FriendRequest)
        friend_list_ct = ContentType.objects.get_for_model(FriendList)
        notifications = Notification.objects.filter(target=user,
                                                    content_type__in=[
                                                        friend_request_ct,
                                                        friend_list_ct
                                                    ]).order_by('-timestamp')
        p = Paginator(notifications, DEFAULT_NOTIFICATION_PAGE_SIZE)

        # sleep 1s for testing
        # sleep(1)
        payload = {}
        if len(notifications) > 0:
            if int(page_number) <= p.num_pages:
                s = LazyNotificationEncoder()
                serialized_notifications = s.serialize(
                    p.page(page_number).object_list)
                payload['notifications'] = serialized_notifications
                new_page_number = int(page_number) + 1
                payload['new_page_number'] = new_page_number
            else:
                return None
    else:
        raise NotificationClientError(
            "User must be authenticated to get notifications.")

    return json.dumps(payload)
Exemple #9
0
    async def receive_json(self, content):
        """
        Called when we get a text frame. Channels will JSON-decode the payload
        for us and pass it as the first argument.
        """
        command = content.get("command", None)
        #print("NotificationConsumer: receive_json. Command: " + command)
        try:
            if command == "get_general_notifications":
                await self.display_progress_bar(True)
                payload = await self.get_general_notifications(
                    content.get("page_number", None))
                if payload == None:
                    await self.general_pagination_exhausted()
                else:
                    payload = json.loads(payload)
                    await self.send_general_notifications_payload(
                        payload['notifications'], payload['new_page_number'])
                await self.display_progress_bar(False)

            elif command == "refresh_general_notifications":
                payload = await self.refresh_general_notifications(
                    content['oldest_timestamp'])
                if payload == None:
                    raise NotificationClientError(
                        "Something went wrong. Try refreshing the browser.")
                else:
                    payload = json.loads(payload)
                    await self.send_general_refreshed_notifications_payload(
                        payload['notifications'])

            elif command == "get_chat_notifications":
                await self.display_progress_bar(True)
                payload = await self.get_chat_notifications(
                    content.get("page_number", None))
                if payload == None:
                    await self.chat_pagination_exhausted()
                else:
                    payload = json.loads(payload)
                    await self.send_chat_notifications_payload(
                        payload['notifications'], payload['new_page_number'])
                await self.display_progress_bar(False)

            elif command == "get_unread_general_notifications_count":
                payload = await self.get_unread_general_notification_count()
                if payload != None:
                    payload = json.loads(payload)
                    await self.send_unread_general_notification_count(
                        payload['count'])

            elif command == "refresh_chat_notifications":
                try:
                    payload = await self.refresh_chat_notifications(
                        content['oldest_timestamp'])
                    if payload == None:
                        raise NotificationClientError(
                            "Something went wrong. Try refreshing the browser."
                        )
                    else:
                        payload = json.loads(payload)
                        await self.send_chat_refreshed_notifications_payload(
                            payload['notifications'])
                except Exception as e:
                    print("EXCEPTION: " + str(e))

            elif command == "mark_notifications_read":
                await self.mark_notifications_read()

            elif command == "accept_friend_request":
                try:
                    notification_id = content['notification_id']
                    payload = await self.accept_friend_request(notification_id)
                    print("ACCEPT: accept_friend_request")
                    if payload == None:
                        raise NotificationClientError(
                            "Something went wrong. Try refreshing the browser."
                        )
                    else:
                        payload = json.loads(payload)
                        await self.send_updated_friend_request_notification(
                            payload['notification'])
                except Exception as e:
                    print("EXCEPTION: " + str(e))

            elif command == "decline_friend_request":
                notification_id = content['notification_id']
                payload = await self.decline_friend_request(notification_id)
                if payload == None:
                    raise NotificationClientError(
                        "Something went wrong. Try refreshing the browser.")
                else:
                    payload = json.loads(payload)
                    await self.send_updated_friend_request_notification(
                        payload['notification'])

        except Exception as e:
            await self.display_progress_bar(False)
            # Catch any errors and send it back
            errorData = {}
            try:
                if e.code:
                    errorData['error'] = e.code
                if e.message:
                    errorData['message'] = e.message
            except:
                errorData['message'] = "An unknown error occurred"
            await self.send_json(errorData)
Exemple #10
0
    async def receive_json(self, content):
        """
        Called when we get a text frame. Channels will JSON-decode the payload
        for us and pass it as the first argument.
        """
        command = content.get("command", None)
        #print("NotificationConsumer: receive_json. Command: " + command)
        try:
            if command == "get_general_notifications":
                payload = await get_general_notifications(
                    self.scope["user"], content.get("page_number", None))
                if payload == None:
                    await self.general_pagination_exhausted()
                else:
                    payload = json.loads(payload)
                    await self.send_general_notifications_payload(
                        payload['notifications'], payload['new_page_number'])

            elif command == "get_new_general_notifications":
                payload = await get_new_general_notifications(
                    self.scope["user"], content.get("newest_timestamp", None))

                if payload != None:
                    payload = json.loads(payload)
                    await self.send_new_general_notifications_payload(
                        payload['notifications'])

            elif command == "refresh_general_notifications":
                payload = await refresh_general_notifications(
                    self.scope["user"], content['oldest_timestamp'],
                    content['newest_timestamp'])
                if payload == None:
                    raise NotificationClientError(
                        "Something went wrong. Try refreshing the browser.")
                else:
                    payload = json.loads(payload)
                    await self.send_general_refreshed_notifications_payload(
                        payload['notifications'])

            elif command == "get_chat_notifications":
                payload = await get_chat_notifications(
                    self.scope["user"], content.get("page_number", None))
                if payload == None:
                    await self.chat_pagination_exhausted()
                else:
                    payload = json.loads(payload)
                    await self.send_chat_notifications_payload(
                        payload['notifications'], payload['new_page_number'])

            elif command == "get_unread_general_notifications_count":
                payload = await get_unread_general_notification_count(
                    self.scope["user"])
                if payload != None:
                    payload = json.loads(payload)
                    await self.send_unread_general_notification_count(
                        payload['count'])

            elif command == "refresh_chat_notifications":
                payload = await refresh_chat_notifications(
                    self.scope["user"], content['oldest_timestamp'],
                    content['newest_timestamp'])
                if payload == None:
                    raise NotificationClientError(
                        "Something went wrong. Try refreshing the browser.")
                else:
                    payload = json.loads(payload)
                    await self.send_chat_refreshed_notifications_payload(
                        payload['notifications'])

            elif command == "get_new_chat_notifications":
                payload = await get_new_chat_notifications(
                    self.scope["user"], content.get("newest_timestamp", None))

                if payload != None:
                    payload = json.loads(payload)
                    await self.send_new_chat_notifications_payload(
                        payload['notifications'])

            elif command == "get_unread_chat_notifications_count":
                try:
                    payload = await get_unread_chat_notification_count(
                        self.scope["user"])
                    if payload != None:
                        payload = json.loads(payload)
                        await self.send_unread_chat_notification_count(
                            payload['count'])
                except Exception as e:
                    print("UNREAD CHAT MESSAGE COUNT EXCEPTION: " + str(e))
                    pass

            elif command == "mark_notifications_read":
                await mark_notifications_read(self.scope["user"])

            elif command == "accept_friend_request":
                try:
                    notification_id = content['notification_id']
                    payload = await accept_friend_request(
                        self.scope['user'], notification_id)
                    if payload == None:
                        raise NotificationClientError(
                            "Something went wrong. Try refreshing the browser."
                        )
                    else:
                        payload = json.loads(payload)
                        await self.send_updated_friend_request_notification(
                            payload['notification'])
                except Exception as e:
                    print("EXCEPTION: " + str(e))
                    pass

            elif command == "decline_friend_request":
                notification_id = content['notification_id']
                payload = await decline_friend_request(self.scope['user'],
                                                       notification_id)
                if payload == None:
                    raise NotificationClientError(
                        "Something went wrong. Try refreshing the browser.")
                else:
                    payload = json.loads(payload)
                    await self.send_updated_friend_request_notification(
                        payload['notification'])

        except Exception as e:
            # Catch any errors and send it back
            errorData = {}
            try:
                if e.code:
                    errorData['error'] = e.code
                if e.message:
                    errorData['message'] = e.message
            except:
                errorData[
                    'message'] = "An error occurred while trying command: " + command + ". " + str(
                        e)
            await self.send_json(errorData)