def test_back_feed(self): """Test the back feed feature, once a user follows another user it places their latest 5 posts on their feed. They will be in chronologic order. I know that ``back_feed()`` is a posts feature but it is triggered on follow. """ user1 = create_account('user1', '*****@*****.**', 'Password') user2 = create_account('user2', '*****@*****.**', 'Password') # Create 6 test posts ('Test 1' shouldn't be back fed) post1 = create_post(user1, 'user1', 'Test 1') post2 = create_post(user1, 'user1', 'Test 2') post3 = create_post(user1, 'user1', 'Test 3') post4 = create_post(user1, 'user1', 'Test 4') post5 = create_post(user1, 'user1', 'Test 5') post6 = create_post(user1, 'user1', 'Test 6') follow_user(user2, user1) # Check that the posts are in the feed (we can do this in Redis) feed = r.zrevrange(k.USER_FEED.format(user2), 0, -1) self.assertNotIn(post1, feed) self.assertIn(post2, feed) self.assertIn(post3, feed) self.assertIn(post4, feed) self.assertIn(post5, feed) self.assertIn(post6, feed)
def get_alerts(user_id, page=1, per_page=None): """Return a list of alert objects as a pagination. """ if per_page is None: per_page = app.config.get('ALERT_ITEMS_PER_PAGE') # Get the last time the users checked the alerts # Try and cast the value to an int so we can boolean compare them try: alerts_last_checked = m.db.users.find_one({ '_id': user_id }).get('alerts_last_checked') except (AttributeError, TypeError, ValueError): alerts_last_checked = 0 # Get total number of elements in the sorted set total = r.zcard(k.USER_ALERTS.format(user_id)) aids = r.zrevrange(k.USER_ALERTS.format(user_id), (page - 1) * per_page, (page * per_page) - 1) # Create AlertManager to load the alerts am = AlertManager() alerts = [] for aid in aids: # Load the alert in to the alert manager alert = am.get(aid) if alert: # Check to see if the alert is newer than the time we last checked. # This allows us to highlight in the template # This will assign a new property to the object: `new` if int(alert.timestamp) > alerts_last_checked: alert.new = True # Add the entire alert from the manager on the list alerts.append(alert) else: # Self cleaning zset r.zrem(k.USER_ALERTS.format(user_id), aid) total = r.zcard(k.USER_ALERTS.format(user_id)) # May as well delete the alert if there is one r.delete(k.ALERT.format(aid)) # Update the last time the user checked there alerts # This will allow us to alert a user too new alerts with the /i-has-alerts # url m.db.users.update({'_id': user_id}, {'$set': { 'alerts_last_checked': timestamp() }}) return Pagination(alerts, total, page, per_page)
def get_alerts(user_id, page=1, per_page=None): """Return a list of alert objects as a pagination. """ if per_page is None: per_page = app.config.get('ALERT_ITEMS_PER_PAGE') # Get the last time the users checked the alerts # Try and cast the value to an int so we can boolean compare them try: alerts_last_checked = m.db.users.find_one( {'_id': user_id} ).get('alerts_last_checked') except (AttributeError, TypeError, ValueError): alerts_last_checked = 0 # Get total number of elements in the sorted set total = r.zcard(k.USER_ALERTS.format(user_id)) aids = r.zrevrange(k.USER_ALERTS.format(user_id), (page - 1) * per_page, (page * per_page) - 1) # Create AlertManager to load the alerts am = AlertManager() alerts = [] for aid in aids: # Load the alert in to the alert manager alert = am.get(aid) if alert: # Check to see if the alert is newer than the time we last checked. # This allows us to highlight in the template # This will assign a new property to the object: `new` if int(alert.timestamp) > alerts_last_checked: alert.new = True # Add the entire alert from the manager on the list alerts.append(alert) else: # Self cleaning zset r.zrem(k.USER_ALERTS.format(user_id), aid) total = r.zcard(k.USER_ALERTS.format(user_id)) # May as well delete the alert if there is one r.delete(k.ALERT.format(aid)) # Update the last time the user checked there alerts # This will allow us to alert a user too new alerts with the /i-has-alerts # url m.db.users.update({'_id': user_id}, {'$set': {'alerts_last_checked': timestamp()}}) return Pagination(alerts, total, page, per_page)
def get_feed(user_id, page=1, per_page=None): """Returns all the posts in a users feed as a pagination object. .. note: The feed is stored inside Redis still as this requires fan-out to update all the users who are following you. """ if per_page is None: per_page = app.config.get('FEED_ITEMS_PER_PAGE') # Get the total number of item in the feed and the subset for the current # page. total = r.zcard(k.USER_FEED.format(user_id)) pids = r.zrevrange(k.USER_FEED.format(user_id), (page - 1) * per_page, (page * per_page) - 1) # Get all the posts in one call to MongoDB posts = [] cursor = m.db.posts.find({ '_id': { '$in': pids } }).sort('created', pymongo.DESCENDING) for post in cursor: posts.append(post) # Get a list of unique `user_id`s from all the post. user_ids = list(set([post.get('user_id') for post in posts])) cursor = m.db.users.find({'_id': {'$in': user_ids}}, {'avatar': True}) # Create a lookup dict `{username: email}` user_avatars = \ dict((user.get('_id'), user.get('avatar')) for user in cursor) # Add the e-mails to the posts processed_posts = [] for post in posts: post['user_avatar'] = user_avatars.get(post.get('user_id')) processed_posts.append(post) # Clean up the list in Redis if the if len(processed_posts) < len(pids): diff_pids = list( set(pids) - set([post.get('_id') for post in processed_posts])) r.zrem(k.USER_FEED.format(user_id), *diff_pids) return Pagination(processed_posts, total, page, per_page)
def get_following(uid, page=1): """Returns a list of users uid is following as a pagination object.""" per_page = app.config.get('PROFILE_ITEMS_PER_PAGE') total = r.zcard(k.USER_FOLLOWING.format(uid)) fids = r.zrevrange(k.USER_FOLLOWING.format(uid), (page - 1) * per_page, (page * per_page) - 1) users = [] for fid in fids: user = get_user(fid) if user: users.append(user) else: # Self cleaning sorted sets r.zrem(k.USER_FOLLOWING.format(uid), fid) total = r.zcard(k.USER_FOLLOWING.format(id)) return Pagination(users, total, page, per_page)
def get_feed(user_id, page=1, per_page=None): """Returns all the posts in a users feed as a pagination object. .. note: The feed is stored inside Redis still as this requires fan-out to update all the users who are following you. """ if per_page is None: per_page = app.config.get('FEED_ITEMS_PER_PAGE') # Get the total number of item in the feed and the subset for the current # page. total = r.zcard(k.USER_FEED.format(user_id)) pids = r.zrevrange(k.USER_FEED.format(user_id), (page - 1) * per_page, (page * per_page) - 1) # Get all the posts in one call to MongoDB posts = [] cursor = m.db.posts.find({'_id': {'$in': pids}}).sort( 'created', pymongo.DESCENDING) for post in cursor: posts.append(post) # Get a list of unique `user_id`s from all the post. user_ids = list(set([post.get('user_id') for post in posts])) cursor = m.db.users.find({'_id': {'$in': user_ids}}, {'avatar': True}) # Create a lookup dict `{username: email}` user_avatars = \ dict((user.get('_id'), user.get('avatar')) for user in cursor) # Add the e-mails to the posts processed_posts = [] for post in posts: post['user_avatar'] = user_avatars.get(post.get('user_id')) processed_posts.append(post) # Clean up the list in Redis if the if len(processed_posts) < len(pids): diff_pids = list( set(pids) - set([post.get('_id') for post in processed_posts])) r.zrem(k.USER_FEED.format(user_id), *diff_pids) return Pagination(processed_posts, total, page, per_page)
def get_following(uid, page=1, per_page=None): """Returns a list of users uid is following as a pagination object.""" if per_page is None: per_page = app.config.get('FEED_ITEMS_PER_PAGE') total = r.zcard(k.USER_FOLLOWING.format(uid)) fids = r.zrevrange(k.USER_FOLLOWING.format(uid), (page - 1) * per_page, (page * per_page) - 1) users = [] for fid in fids: user = get_user(fid) if user: users.append(user) else: # Self cleaning sorted sets r.zrem(k.USER_FOLLOWING.format(uid), fid) total = r.zcard(k.USER_FOLLOWING.format(id)) return Pagination(users, total, page, per_page)
def get_followers(uid, page=1, per_page=None): """Returns a list of users who follow user with uid as a pagination object. """ if per_page is None: per_page = app.config.get('FEED_ITEMS_PER_PAGE') total = r.zcard(k.USER_FOLLOWERS.format(uid)) fids = r.zrevrange(k.USER_FOLLOWERS.format(uid), (page - 1) * per_page, (page * per_page) - 1) users = [] for fid in fids: user = get_user(fid) if user: users.append(user) else: # Self cleaning sorted sets r.zrem(k.USER_FOLLOWERS.format(uid), fid) total = r.zcard(k.USER_FOLLOWERS.format(uid)) return Pagination(users, total, page, per_page)
def get_feed(user_id, page=1): """Returns a users feed as a pagination object. .. note: The feed is stored inside Redis still as this requires fan-out to update all the users who are following you. """ per_page = app.config.get('FEED_ITEMS_PER_PAGE') total = r.zcard(k.USER_FEED.format(user_id)) pids = r.zrevrange(k.USER_FEED.format(user_id), (page - 1) * per_page, (page * per_page) - 1) posts = [] for pid in pids: # Get the post post = get_post(pid) if post: posts.append(post) else: # Self cleaning lists r.zrem(k.USER_FEED.format(user_id), pid) total = r.zcard(k.USER_FEED.format(user_id)) return Pagination(posts, total, page, per_page)