def notify_on_attendee(event_id, user_id): try: event = Event.find(event_id) user = User.find(user_id) if event.owner is None: return except DoesNotExist: return if event.is_expired or not user.is_attending(event): return targets = [5, 10, 20, 30, 50, 75, 100, 150, 200, 250, 300, 350, 400, 450, 500] num_attending = get_num_attending(event_id) target = next(reversed([t for t in targets if t <= num_attending]), None) if target is None: return rl_key = 'notify:event_creators:{}:{}:{}'.format(event.id, event.owner_id, target) with rate_limit(rl_key, event.expires) as limited: if not limited: notification = Notification({ 'user_id': event.owner_id, 'type': 'system', 'navigate': '/events/{}'.format(event_id), 'badge': 1, 'message': '{} people are going to your event {}'.format( num_attending, event.name.encode('utf-8')) }).save() send_notification_push.delay(notification.to_primitive())
def notify_on_friend_attending(event_id, user_id, friend_id): num_attending = get_num_attending(event_id, user_id) if num_attending < 5: return try: event = Event.find(event_id) user = User.find(user_id) except DoesNotExist: return if event.is_expired: return rl_key = 'notify:nofa:{}:{}'.format(event_id, user_id) with rate_limit(rl_key, event.expires) as limited: if not limited: friends = list(islice(EventAttendee.select().event(event).user(user).limit(6), 5)) if user in friends: friends.remove(user) num_attending -= 1 logger.info('notifying user {} of {} friends attending event {}'.format(user_id, num_attending, event_id)) if len(friends) >= 2: notification = Notification({ 'user_id': user.id, 'type': 'system', 'navigate': '/users/me/events/{}'.format(event_id), 'badge': 1, 'message': '{}, {}, and {} others are going to {}'.format( friends[0].full_name.encode('utf-8'), friends[1].full_name.encode('utf-8'), num_attending - 2, event.name.encode('utf-8')) }).save() send_notification_push.delay(notification.to_primitive())
def notify_on_eventmessage_vote(voter_id, message_id): try: voter = User.find(voter_id) message = EventMessage.find(message_id) except DoesNotExist: return user = message.user type = 'video' if message.media_mime_type == 'video/mp4' else 'photo' # don't send to self or if not friend if (voter_id == message.user_id) or (not user.is_friend(voter_id)): return with rate_limit('notify:vote:%s:%s:%s' % (message.user_id, message_id, voter_id), timedelta(hours=2)) as limited: if not limited: message_text = '{name} liked your {type} in {event}'.format( name=voter.full_name.encode('utf-8'), type=type, event=message.event.name.encode('utf-8')) notification = Notification({ 'user_id': message.user_id, 'type': 'eventmessage.vote', 'from_user_id': voter_id, 'navigate': '/users/me/events/{}/messages/{}'.format(message.event_id, message.id), 'message': message_text }).save() send_notification_push.delay(notification.to_primitive())
def notify_unlocked(user_id): with rate_limit('notify:unlock:{}'.format(user_id), timedelta(hours=1)) as limited: if not limited: user = User.find(user_id) notification = Notification({ 'user_id': user.id, 'type': 'unlocked', 'badge': 1, 'message': 'You\'re in! It\'s time to party!' }) __send_notification_push(notification)
def notify_on_tap(user_id, tapped_id): with rate_limit('notify:tap:{}:{}'.format(user_id, tapped_id), timedelta(hours=2)) as limited: if not limited: user = User.find(user_id) message_text = '{} wants to see you out'.format(user.full_name.encode('utf-8')) notification = Notification({ 'user_id': tapped_id, 'type': 'tap', 'from_user_id': user_id, 'navigate': '/users/{}'.format(user_id), 'badge': 1, 'message': message_text }).save() send_notification_push.delay(notification.to_primitive())
def notify_fb_friend_user_joined(user_id, facebook_id): user = User.find(user_id) with rate_limit('notify:friend_joined:{}:{}'.format(user_id, facebook_id), timedelta(hours=12)) as limited: if not limited: try: friend = User.find(facebook_id=facebook_id) notification = Notification({ 'user_id': friend.id, 'type': 'friend.joined', 'navigate': '/find/users/user/{}'.format(user.id), 'badge': 1, 'message': 'Your Facebook friend {} just joined Wigo Summer'.format( user.full_name.encode('utf-8')) }) __send_notification_push(notification) except DoesNotExist: pass
def notify_on_invite(inviter_id, invited_id, event_id): rl_key = 'notify:invite:{}:{}:{}'.format(inviter_id, invited_id, event_id) with rate_limit(rl_key, timedelta(hours=2)) as limited: if not limited: inviter = User.find(inviter_id) invited = User.find(invited_id) event = Event.find(event_id) if event_id else None message_text = '{} invited you out to {}'.format(inviter.full_name.encode('utf-8'), event.name.encode('utf-8')) notification = Notification({ 'user_id': invited_id, 'type': 'invite', 'from_user_id': inviter_id, 'navigate': '/users/me/events/{}'.format(event_id), 'badge': 1, 'message': message_text }).save() send_notification_push.delay(notification.to_primitive())
def notify_on_eventmessage(message_id): try: message = EventMessage.find(message_id) except DoesNotExist: return user = message.user if message.event.is_expired: return type = 'video' if message.media_mime_type == 'video/mp4' else 'photo' for friend in EventAttendee.select().user(message.user).event(message.event): if friend == user: continue with rate_limit('notify:eventmessage:{}:{}:{}'.format(user.id, message.event.id, friend.id), timedelta(hours=2)) as limited: if limited: return message_text = '{name} posted a {type} in {event}'.format( name=user.full_name.encode('utf-8'), type=type, event=message.event.name.encode('utf-8')) notification = Notification({ 'user_id': friend.id, 'type': 'eventmessage.post', 'from_user_id': message.user_id, 'navigate': '/users/me/events/{}/messages/{}'.format(message.event_id, message.id), 'message': message_text }).save() send_notification_push.delay(notification.to_primitive())
def generate_friend_recs(user, num_friends_to_recommend=200, force=False): with rate_limit('gen_f_recs:{}'.format(user.id), timedelta(minutes=10)) as limited: if force or not limited: _do_generate_friend_recs.delay(user.id, num_friends_to_recommend)
def sync_parse(user_id): with rate_limit('parse:sync:{}'.format(user_id), timedelta(hours=1)) as limited: if not limited: __do_sync_parse(user_id)