Esempio n. 1
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)
Esempio n. 2
0
def new_user(user):
    """
    A new user has been created

    This is a new user.
    Create a notification for this user's friends
    """
    new_notifications = []

    my_byn_friends = user.get_my_facebook_friends_on_banyan()
    for friend in my_byn_friends:
        description = "{from_user} joined Banyan".format(from_user=user.get_full_name())
        notif = BanyanUserNotifications(
            from_user=user, user=friend, description=description, type=BanyanUserNotifications.JOIN_NOTIF
        )
        new_notifications.extend([notif])
        pass
    BanyanUserNotifications.bulk_save_all(new_notifications)
Esempio n. 3
0
def new_piece(piece):
    """
    Send notifications and update story when a new piece is added
    """

    story = piece.story
    """
    Update the timeStamp of the story to get the last updated stamp
    """
    if piece.timeStamp > story.timeStamp:
        story.timeStamp = piece.timeStamp
        Story.objects.filter(pk=story.pk).update(
            timeStamp=piece.timeStamp)  # So as not to call pre-save/post-save

    # Create notification for followers
    followActivities = story.activities.filter(type=Activity.FOLLOW_STORY)
    """
    No need to dedupe here since follow activities are guaranteed to be unique
    because of the unique constraint in the model
    """
    new_notifications = []
    for followActivity in followActivities:
        user = followActivity.user
        """
        If this is the author of the piece, don't do anything
        """
        if user == piece.author:
            continue

        # Create a notification entries for this follower
        description = "{from_user} has added a new piece \"{piece}\" to the story \"{story}\"".format(
            from_user=piece.author.get_full_name(),
            piece=piece.piece_description(40),
            story=story.title)
        notif = BanyanUserNotifications(
            content_object=piece,
            from_user=piece.author,
            user=user,
            type=BanyanUserNotifications.PIECE_ADDED_NOTIF,
            description=description)
        new_notifications.extend([notif])

    BanyanUserNotifications.bulk_save_all(new_notifications)
Esempio n. 4
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)
Esempio n. 5
0
def new_piece(piece):
    """
    Send notifications and update story when a new piece is added
    """

    story = piece.story
    
    """
    Update the timeStamp of the story to get the last updated stamp
    """
    if piece.timeStamp > story.timeStamp:
        story.timeStamp = piece.timeStamp
        Story.objects.filter(pk=story.pk).update(timeStamp=piece.timeStamp) # So as not to call pre-save/post-save

    # Create notification for followers
    followActivities = story.activities.filter(type=Activity.FOLLOW_STORY)
    
    """
    No need to dedupe here since follow activities are guaranteed to be unique
    because of the unique constraint in the model
    """
    new_notifications = []
    for followActivity in followActivities:
        user = followActivity.user
        """
        If this is the author of the piece, don't do anything
        """
        if user == piece.author:
            continue

        # Create a notification entries for this follower
        description = "{from_user} has added a new piece \"{piece}\" to the story \"{story}\"".format(from_user = piece.author.get_full_name(),
                                                                                                      piece = piece.piece_description(40),
                                                                                                      story = story.title)
        notif = BanyanUserNotifications(content_object = piece, 
                                          from_user = piece.author, 
                                          user = user, 
                                          type = BanyanUserNotifications.PIECE_ADDED_NOTIF,
                                          description = description)
        new_notifications.extend([notif])
    
    BanyanUserNotifications.bulk_save_all(new_notifications)
Esempio n. 6
0
def new_user(user):
    """
    A new user has been created

    This is a new user.
    Create a notification for this user's friends
    """
    new_notifications = []

    my_byn_friends = user.get_my_facebook_friends_on_banyan()
    for friend in my_byn_friends:
        description = "{from_user} joined Banyan".format(
            from_user=user.get_full_name())
        notif = BanyanUserNotifications(
            from_user=user,
            user=friend,
            description=description,
            type=BanyanUserNotifications.JOIN_NOTIF,
        )
        new_notifications.extend([notif])
        pass
    BanyanUserNotifications.bulk_save_all(new_notifications)
Esempio n. 7
0
def story_permission_notifications(story=None):
    """
    Check the permissions of the story and send notifications accordingly
    """

    # To bulk create new notifications for "createStory"
    new_notifications = []

    contributors_fb = story.writeAccess.get('inviteeList').get(
        'facebookFriends', [])
    contributors_fb_ids = [
        contributors['id'] for contributors in contributors_fb
        if 'id' in contributors
    ]

    viewers_fb = story.readAccess.get('inviteeList').get('facebookFriends', [])
    viewers_fb = [x for x in viewers_fb if x not in contributors_fb]
    viewers_fb_ids = [
        viewers['id'] for viewers in viewers_fb if 'id' in viewers
    ]

    # Contributors
    for contributor_fb_id in set(contributors_fb_ids):
        user = BanyanUser.user_with_facebook_id(contributor_fb_id)
        if user:
            description = "{from_user} invited you to contribute to story \"{story}\"".format(
                from_user=story.author.get_full_name(), story=story.title)
            notif = BanyanUserNotifications(
                content_object=story,
                from_user=story.author,
                user=user,
                type=BanyanUserNotifications.CONTRIB_INVITE_NOTIF,
                description=description)
            new_notifications.extend([notif])

    # Viewers
    for viewer_fb_id in set(viewers_fb_ids):
        user = BanyanUser.user_with_facebook_id(viewer_fb_id)
        if user:
            description = "{from_user} invited you to read the story \"{story}\"".format(
                from_user=story.author.get_full_name(), story=story.title)
            notif = BanyanUserNotifications(
                content_object=story,
                from_user=story.author,
                user=user,
                type=BanyanUserNotifications.VIEW_INVITE_NOTIF,
                description=description)
            new_notifications.extend([notif])

    # Send notifications to all facebook friends who are already on Banyan
    all_fb_friends = story.readAccess.get('inviteeList').get(
        'allFacebookFriendsOf', [])
    all_fb_friends_ids = [
        viewers['id'] for viewers in all_fb_friends if 'id' in viewers
    ]
    for viewer_fb_id in all_fb_friends_ids:
        user = BanyanUser.user_with_facebook_id(viewer_fb_id)
        for fb_friend in user.get_my_facebook_friends_on_banyan():
            fb_friend_social_auth = fb_friend.social_auth.filter(
                provider="facebook").first()
            if not fb_friend_social_auth:
                # User hadn't logged in through facebook
                continue
            if fb_friend_social_auth.uid in viewers_fb_ids or fb_friend_social_auth.uid in contributors_fb_ids:
                # Already sending notifications to this friend.
                continue
            description = "{from_user} has started a new story \"{story}\"".format(
                from_user=story.author.get_full_name(), story=story.title)
            notif = BanyanUserNotifications(
                content_object=story,
                from_user=story.author,
                user=fb_friend,
                type=BanyanUserNotifications.STORY_STARTED_NOTIF,
                description=description)
            new_notifications.extend([notif])

    BanyanUserNotifications.bulk_save_all(new_notifications)
Esempio n. 8
0
def story_permission_notifications(story = None):
    """
    Check the permissions of the story and send notifications accordingly
    """
    
    # To bulk create new notifications for "createStory"
    new_notifications = []
    
    contributors_fb = story.writeAccess.get('inviteeList').get('facebookFriends', [])
    contributors_fb_ids = [contributors['id'] for contributors in contributors_fb if 'id' in contributors]
    
    viewers_fb = story.readAccess.get('inviteeList').get('facebookFriends', [])
    viewers_fb = [x for x in viewers_fb if x not in contributors_fb]
    viewers_fb_ids = [viewers['id'] for viewers in viewers_fb if 'id' in viewers]

    # Contributors
    for contributor_fb_id in set(contributors_fb_ids):
        user = BanyanUser.user_with_facebook_id(contributor_fb_id)
        if user:
            description = "{from_user} invited you to contribute to story \"{story}\"".format(from_user = story.author.get_full_name(),
                                                                                              story=story.title)
            notif = BanyanUserNotifications(content_object = story, 
                                              from_user = story.author, 
                                              user = user, 
                                              type = BanyanUserNotifications.CONTRIB_INVITE_NOTIF,
                                              description = description)
            new_notifications.extend([notif])
    
    # Viewers
    for viewer_fb_id in set(viewers_fb_ids):
        user = BanyanUser.user_with_facebook_id(viewer_fb_id)
        if user:
            description = "{from_user} invited you to read the story \"{story}\"".format(from_user = story.author.get_full_name(),
                                                                                         story=story.title)
            notif = BanyanUserNotifications(content_object = story, 
                                              from_user = story.author, 
                                              user = user, 
                                              type = BanyanUserNotifications.VIEW_INVITE_NOTIF,
                                              description = description)
            new_notifications.extend([notif])

    # Send notifications to all facebook friends who are already on Banyan
    all_fb_friends = story.readAccess.get('inviteeList').get('allFacebookFriendsOf', [])
    all_fb_friends_ids = [viewers['id'] for viewers in all_fb_friends if 'id' in viewers]
    for viewer_fb_id in all_fb_friends_ids:
        user = BanyanUser.user_with_facebook_id(viewer_fb_id)
        for fb_friend in user.get_my_facebook_friends_on_banyan():
            fb_friend_social_auth = fb_friend.social_auth.filter(provider="facebook").first()
            if not fb_friend_social_auth:
                # User hadn't logged in through facebook
                continue
            if fb_friend_social_auth.uid in viewers_fb_ids or fb_friend_social_auth.uid in contributors_fb_ids:
                # Already sending notifications to this friend.
                continue
            description = "{from_user} has started a new story \"{story}\"".format(from_user = story.author.get_full_name(),
                                                                                    story=story.title)
            notif = BanyanUserNotifications(content_object = story,
                                              from_user = story.author,
                                              user = fb_friend,
                                              type = BanyanUserNotifications.STORY_STARTED_NOTIF,
                                              description = description)
            new_notifications.extend([notif])

    BanyanUserNotifications.bulk_save_all(new_notifications)