def send_interest_nudge(interest):
    users = User.get_many_by_field('interests', interest, ['device_gcm_token', 'device_pns_token'])
    for user in users:
        friend_keys = list(FriendStore.get_friends_raw(user['_id']).iterkeys())
        friends_with_interest = User.get_by_ids_interest(friend_keys, interest)
        num = len(friends_with_interest) - 3
        message = ["Your mates"]
        for friend in friends_with_interest[:3]:
            message.append('@' + friend['first_name'] + ' ' + friend['last_name'])
            message.append(', ')
        message.pop()
        if num == 1:
            message.append(" and 1 other")
        elif num >= 2:
            message.append(" and " + str(num) + " others")
        message.append(" are keen for #" + interest + " with you. Why don't you suggest it?")
        pic_url =  'http://keen.to/static/android-72.png' 
        store_data = {
            'uid': user['_id'],
            'firstName' : 'Keen',
            'lastName'  : '',
            'network'   : '',
            'pic'    : pic_url,
        }
        badge_num = add_notification(user['_id'], settings.NUDGE, message, pic_url, store_data)
        data =  {
            'type'     : settings.NUDGE,
            'ticker'   : 'Keen',
            'msg'      : 'Your friends are keen for #' + interest,
          }
        send(data, [user.get('device_gcm_token')], [user.get('device_pns_token')], [badge_num])
    return True
Exemple #2
0
def ensure_content(user_id):
    """
    Checks to see whether the user has any content and if not, adds some to make sure they have >= 10
    """
    activity_collection = get_activity_collection()
    timeline_col = get_timeline_collection()
    activities = Timeline.get_visible_activities(user_id)
    size = len(activities)
    user = User.get_by_id(user_id, extra_fields = ['gender'])
    interests = user.get('interests', [])
    if user.get('gender') == 'male':
        default_interests = ['touch', 'cycling', 'gymsession', 'bbq', 'indoorfootball', 'squash', 'poker', 'surf', 'rockclimbing', 'drinksatmine', 'beachcricket']
    else:
        #Female
        default_interests = ['beachtime', 'champagnebreakfast', 'freetradecoffee', 'byodinner', 'crossfit', 'easyjog', 'movieday', 'tennis', 'drinksatmine', 'missionbaymission']
    shuffle(default_interests)
    prefixes = ['Whos keen for some #%s tomorrow?', 'Whos keen.to #%s tomorrow?', 'Im feeling like #%s today, whos keen?', 'Whos up for #%s today?', 'keen.to #%s today?']
    raw_friends = FriendStore.get_friends_raw(user_id)
    suggesters = User.get_by_ids_type(ids = list(raw_friends.iterkeys()), user_type=User.ACTIVE)
    friend_interests = []
    for friend in suggesters:
        friend_interests.extend(friend.get('interests'))
    if len(suggesters) + size < 10:
        raw_fof_ids = FriendOfFriendStore.get_fof_ids_raw(user_id)
        more_suggesters =  User.get_by_ids_type(ids = list(raw_fof_ids), user_type=User.ACTIVE)
        suggesters.extend(more_suggesters)
    
    while size < 10:
        
        if len(suggesters) == 0:
            break
        
        shuffle(prefixes)
        #We want to add some more
        if len(interests) >= 1:
            interest = interests.pop()
        elif len(friend_interests) >= 1:
            interest = friend_interests.pop()
        elif len(default_interests) >= 1:
            interest = default_interests.pop()
        prefix = prefixes[0]
        text = prefix % interest
        raw_suggester = suggesters.pop()
        
        #Add the activity into activities
        activity = {
            "suggester_id" : raw_suggester.get('_id'),
            "text" : text,
            "keyword" : interest,
            "keens" : [{
                 "user_id"     : raw_suggester.get('_id'),
                 "fb_username" : '',
                 "first_name"  : raw_suggester.get('first_name'),
                 "last_name"   : raw_suggester.get('last_name'),
                 "network"     : raw_suggester.get('network'),
                 "confirmed"   : True,
                 "user_type"   : User.ACTIVE
            }],
            'invited' : [],
            "status" : Activity.EXPIRED,
            "visibility" : '',
            "created_on" : datetime.datetime.now() - datetime.timedelta(days = randint(2,7)),
        }
        activity_id = str(activity_collection.insert(activity))
        #Add it to the users timeline
        activity_timeline_dict = {
            "suggester_id" : raw_suggester.get('_id'),   
            "keyword" : interest,
            "activity_id" : activity_id, #This has to be string to consistency
            "activity_hash" : '',
            "confirmed" : False,
            "visibility" : '',
            "keen" : False,
            "degree" :  None
        }
        
        timeline_col.update({"user_id" : user_id}, #Match criteria
                            {"$push": { "visible_activities" :  activity_timeline_dict } })
        size += 1
    return True