Exemplo n.º 1
0
def story_start_notif(notification):
    """
    A notification about a user starting a new story
    """
    if not isinstance(notification.content_object, Story):
        logger.error(
            "accounts.tasks.story_start_notif error Invalid object %s for Invited to view notification"
            % (notification.content_object.__class__.__name__))
        raise ValueError("Invalid object %s for Story start notification" %
                         (notification.content_object.__class__.__name__))

    story = notification.content_object
    user = notification.user
    message = "{} has started a new story {}!".format(
        story.author.get_full_name(), story.title)
    dataInSns = {'story': story.bnObjectId}
    sns_send_push_notification_to_user(
        endpoint=BanyanUserDevices.INVITED_TO_VIEW,
        message=message,
        data=dataInSns,
        user=user)
    try:
        Activity.objects.create(content_object=story,
                                user=user,
                                type=Activity.FOLLOW_STORY)
    except IntegrityError as e:
        logger.info(
            "accounts.tasks.story_start_notif info Integrity error when saving follow activity story: {} user: {}"
            .format(story, user))
    except:
        logger.error("accounts.tasks.story_start_notif error {} {} ".format(
            sys.exc_info()[0],
            sys.exc_info()[1]))
Exemplo n.º 2
0
def inv_contribute_notif(notification):
    """
    A notification about invitation to contribute is created
    """
    if not isinstance(notification.content_object, Story):
        logger.error(
            "accounts.tasks.inv_contribute_notif error Invalid object %s for Invited to view notification"
            % (notification.content_object.__class__.__name__)
        )
        raise ValueError(
            "Invalid object %s for Invited to contribute notification"
            % (notification.content_object.__class__.__name__)
        )

    story = notification.content_object
    user = notification.user
    message = "{} has invited you to contribute to story {}!".format(story.author.get_full_name(), story.title)
    dataInSns = {"story": story.bnObjectId}
    sns_send_push_notification_to_user(
        endpoint=BanyanUserDevices.INVITED_TO_CONTRIBUTE, message=message, data=dataInSns, user=user
    )
    try:
        Activity.objects.create(content_object=story, user=user, type=Activity.FOLLOW_STORY)
    except IntegrityError as e:
        logger.info(
            "accounts.tasks.inv_contribute_notif info Integrity error when saving follow activity story: {} user: {}".format(
                story, user
            )
        )
    except:
        logger.error("accounts.tasks.inv_contribute_notif error {} {} ".format(sys.exc_info()[0], sys.exc_info()[1]))
Exemplo n.º 3
0
def new_activity(activity_id):
    """
    Method called when an activity is saved
    """
    try:
        activity = Activity.objects.get(pk=activity_id)
    except Activity.DoesNotExist:
        return

    # We only send notifications for a like activity
    if activity.type == Activity.LIKE:
        """
        A signal for sending push notification to author whose piece was liked
        """
        object = activity.content_object

        # The likes can only be for a piece
        if not isinstance(object, Piece):
            return

        piece = object

        # Don't send a notification if the author himself/herself likes the piece
        if activity.user == piece.author:
            return

        # Send the notification now!
        message = "{} loves your piece- {}!".format(
            activity.user.get_full_name(), piece.piece_description(20))
        dataInSns = {
            'story': piece.story.bnObjectId,
            'piece': piece.bnObjectId
        }

        sns_send_push_notification_to_user(
            endpoint=BanyanUserDevices.PIECE_ACTION,
            message=message,
            data=dataInSns,
            user=piece.author)

        # Create a notification for the user
        new_notifications = []
        description = "{from_user} loves the piece \"{piece}\"".format(
            from_user=activity.user.get_full_name(),
            piece=piece.piece_description(40))
        notif = BanyanUserNotifications(
            content_object=piece,
            from_user=activity.user,
            user=piece.author,
            type=BanyanUserNotifications.LIKE_NOTIF,
            description=description,
            activity=activity)
        new_notifications.extend([notif])
        BanyanUserNotifications.bulk_save_all(new_notifications)
Exemplo n.º 4
0
def piece_add_notif(notification):
    """
    A notification about a piece being added is created
    """
    piece = notification.content_object
    story = piece.story
    user = notification.user

    message = "{} has added a new piece to the story {}!".format(piece.author.get_full_name(), story.title)
    dataInSns = {"story": story.bnObjectId, "piece": piece.bnObjectId}
    sns_send_push_notification_to_user(
        endpoint=BanyanUserDevices.PIECE_ADDED, message=message, data=dataInSns, user=user
    )
Exemplo n.º 5
0
def piece_add_notif(notification):
    """
    A notification about a piece being added is created
    """
    piece = notification.content_object
    story = piece.story
    user = notification.user

    message = "{} has added a new piece to the story {}!".format(
        piece.author.get_full_name(), story.title)
    dataInSns = {'story': story.bnObjectId, 'piece': piece.bnObjectId}
    sns_send_push_notification_to_user(endpoint=BanyanUserDevices.PIECE_ADDED,
                                       message=message,
                                       data=dataInSns,
                                       user=user)
Exemplo n.º 6
0
def new_activity(activity_id):
    """
    Method called when an activity is saved
    """
    try:
        activity = Activity.objects.get(pk=activity_id)
    except Activity.DoesNotExist:
        return

    # We only send notifications for a like activity
    if activity.type == Activity.LIKE:
        """
        A signal for sending push notification to author whose piece was liked
        """
        object = activity.content_object
        
        # The likes can only be for a piece
        if not isinstance(object, Piece):
            return
        
        piece = object
        
        # Don't send a notification if the author himself/herself likes the piece
        if activity.user == piece.author:
            return
        
        # Send the notification now!    
        message = "{} loves your piece- {}!".format(activity.user.get_full_name(), piece.piece_description(20))
        dataInSns = {'story':piece.story.bnObjectId, 'piece':piece.bnObjectId}
    
        sns_send_push_notification_to_user(endpoint=BanyanUserDevices.PIECE_ACTION, message=message, data=dataInSns, user=piece.author)
        
        # Create a notification for the user
        new_notifications = []
        description = "{from_user} loves the piece \"{piece}\"".format(from_user = activity.user.get_full_name(), piece=piece.piece_description(40))
        notif = BanyanUserNotifications(content_object = piece, 
                                          from_user = activity.user, 
                                          user = piece.author, 
                                          type = BanyanUserNotifications.LIKE_NOTIF,
                                          description = description,
                                          activity = activity)
        new_notifications.extend([notif])
        BanyanUserNotifications.bulk_save_all(new_notifications)