def is_following(who_id, whom_id): """Check to see if who is following whom. """ if r.zrank(k.USER_FOLLOWING.format(who_id), whom_id) is not None: return True return False
def unapprove_user(who_uid, whom_uid): """Allow a user to un-approve a follower""" # Check the follower is actually approved if r.zrank(k.USER_APPROVED.format(who_uid), whom_uid) is None: return False # No alert for un-approved r.zrem(k.USER_APPROVED.format(who_uid), whom_uid) return True
def unfollow_user(who_uid, whom_uid): """Remove whom from who's following zset and who to whom's followers zset. """ # Check that we are actually following the users if r.zrank(k.USER_FOLLOWING.format(who_uid), whom_uid) is None: return False # Delete uid from who following and whom followers r.zrem(k.USER_FOLLOWING.format(who_uid), whom_uid) r.zrem(k.USER_FOLLOWERS.format(whom_uid), who_uid) return True
def approve_user(who_uid, whom_uid): """Allow a user to approve a follower""" # Check that the user is actually following. # Fail if not if r.zrank(k.USER_FOLLOWERS.format(who_uid), whom_uid) is None: return False # Add the user to the approved list # No alert is generated r.zadd(k.USER_APPROVED.format(who_uid), timestamp(), whom_uid) return True
def unfollow_user(who_uid, whom_uid): """Remove whom from who's following zset and who to whom's followers zset. """ # Check that we are actually following the users if r.zrank(k.USER_FOLLOWING.format(who_uid), whom_uid) is None: return False # Delete uid from who following and whom followers r.zrem(k.USER_FOLLOWING.format(who_uid), whom_uid) r.zrem(k.USER_FOLLOWERS.format(whom_uid), who_uid) # Delete the user from the approved list unapprove_user(whom_uid, who_uid) return True
def follow_user(who_uid, whom_uid): """Add whom to who's following zset and who to whom's followers zset. Generate an alert for this action. """ # Check that we are not already following the user if r.zrank(k.USER_FOLLOWING.format(who_uid), whom_uid) is not None: return False # Follow user # Score is based on UTC epoch time r.zadd(k.USER_FOLLOWING.format(who_uid), timestamp(), whom_uid) r.zadd(k.USER_FOLLOWERS.format(whom_uid), timestamp(), who_uid) # Create an alert and inform whom that who is now following them alert = FollowAlert(who_uid) AlertManager().alert(alert, [whom_uid]) # Back fill the who's feed with some posts from whom back_feed(who_uid, whom_uid) return True
def has_flagged(user_id, post_id): """""" return r.zrank(k.POST_FLAGS.format(post_id), user_id) is not None
def is_subscribed(user_id, post_id): """Returns a boolean to denote if a user is subscribed or not """ return r.zrank(k.POST_SUBSCRIBERS.format(post_id), user_id) is not None
def test_subscriptions(self): """ Test the backend subscription system. This includes subscribe(), unsubscribe() and is_subscribed(). This will also test the correct subscriptions after a post, comment or tagging. """ # Create a couple of test accounts user1 = create_account('user1', '*****@*****.**', 'Password') user2 = create_account('user2', '*****@*****.**', 'Password') user3 = create_account('user3', '*****@*****.**', 'Password') # Post as user 1 and ensure user 1 exists in Redis post1 = create_post(user1, 'user1', 'Test post 1') self.assertIsNotNone(r.zrank(K.POST_SUBSCRIBERS.format(post1), user1)) # Even though it is exactly the same as the above ensure that # is_subscribed() returns True self.assertTrue(is_subscribed(user1, post1)) # Lets ensure this actually fails! And see if user2 is a subscriber self.assertFalse(is_subscribed(user2, post1)) # Ensure that the REASON (zset score) is set to the correct value self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user1), SubscriptionReasons.POSTER) # Post a comment as user 1 and ensure the reason does NOT changes create_post(user1, 'user1', 'Test comment', post1) # Ensure our reason did not change # If we were not subscribed we would be given reason 2 (COMMENTER) self.assertNotEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user1), SubscriptionReasons.COMMENTER) # Test unsubscribe self.assertTrue(unsubscribe(user1, post1)) # Ensure that is_subscribed shows correct self.assertFalse(is_subscribed(user1, post1)) # Test that if we unsubscribe again we get a False result self.assertFalse(unsubscribe(user1, post1)) # Check that unsubscribing some that was never subscribed returns false self.assertFalse(unsubscribe(user2, post1)) # Let's test that commenting subscribes us to the post with the correct # reason. Try tag yourself at the same time create_post(user1, 'user1', 'Test comment @user1', post1) # Ensure we are subscribed self.assertTrue(is_subscribed(user1, post1)) # Check that our reason HAS changed self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user1), SubscriptionReasons.COMMENTER) # Create a comment as test2 and ensure this user becomes subscribed for # the same reason create_post(user2, 'user2', 'Test comment', post1) self.assertTrue(is_subscribed(user2, post1)) self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user2), SubscriptionReasons.COMMENTER) # Create a new post as test1 and tag test2 and test3 in it # ensure all 3 are subscribed and test2 and test3 have the correct # reason. # Also try and tag ourselves this should have no affect # Try tagging someone that does not even exist post2 = create_post(user1, 'user1', 'Test post @user1 @user2 @user3 @user4') self.assertTrue(is_subscribed(user2, post2)) self.assertTrue(is_subscribed(user3, post2)) self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post2), user2), SubscriptionReasons.TAGEE) self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post2), user3), SubscriptionReasons.TAGEE) # Test tagging user 3 in a comment on post1. This ensures that tags # in comments do work. create_post(user2, 'user2', 'Test comment @user3', post1) self.assertTrue(is_subscribed(user3, post1)) self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user3), SubscriptionReasons.TAGEE) # Unsubscribe user2 and user3 self.assertTrue(unsubscribe(user2, post2)) self.assertFalse(is_subscribed(user2, post2)) self.assertTrue(unsubscribe(user3, post2)) self.assertFalse(is_subscribed(user3, post2)) # Ensure that subscribe doe not happen when it is not a valid post self.assertFalse(subscribe(user1, K.NIL_VALUE, SubscriptionReasons.POSTER))
def is_approved(who_id, whom_id): """Is the current user approved by the user with who_id""" if r.zrank(k.USER_APPROVED.format(who_id), whom_id) is not None: return True return False
def test_subscriptions(self): """ Test the backend subscription system. This includes subscribe(), unsubscribe() and is_subscribed(). This will also test the correct subscriptions after a post, comment or tagging. """ # Create a couple of test accounts user1 = create_account('user1', '*****@*****.**', 'Password') user2 = create_account('user2', '*****@*****.**', 'Password') user3 = create_account('user3', '*****@*****.**', 'Password') activate(user1) activate(user2) activate(user3) # Post as user 1 and ensure user 1 exists in Redis post1 = create_post(user1, 'user1', 'Test post 1') self.assertIsNotNone(r.zrank(K.POST_SUBSCRIBERS.format(post1), user1)) # Even though it is exactly the same as the above ensure that # is_subscribed() returns True self.assertTrue(is_subscribed(user1, post1)) # Lets ensure this actually fails! And see if user2 is a subscriber self.assertFalse(is_subscribed(user2, post1)) # Ensure that the REASON (zset score) is set to the correct value self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user1), SubscriptionReasons.POSTER) # Post a comment as user 1 and ensure the reason does NOT changes create_post(user1, 'user1', 'Test comment', post1) # Ensure our reason did not change # If we were not subscribed we would be given reason 2 (COMMENTER) self.assertNotEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user1), SubscriptionReasons.COMMENTER) # Test unsubscribe self.assertTrue(unsubscribe(user1, post1)) # Ensure that is_subscribed shows correct self.assertFalse(is_subscribed(user1, post1)) # Test that if we unsubscribe again we get a False result self.assertFalse(unsubscribe(user1, post1)) # Check that unsubscribing some that was never subscribed returns false self.assertFalse(unsubscribe(user2, post1)) # Let's test that commenting subscribes us to the post with the correct # reason. Try tag yourself at the same time create_post(user1, 'user1', 'Test comment @user1', post1) # Ensure we are subscribed self.assertTrue(is_subscribed(user1, post1)) # Check that our reason HAS changed self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user1), SubscriptionReasons.COMMENTER) # Create a comment as test2 and ensure this user becomes subscribed for # the same reason create_post(user2, 'user2', 'Test comment', post1) self.assertTrue(is_subscribed(user2, post1)) self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user2), SubscriptionReasons.COMMENTER) # Create a new post as test1 and tag test2 and test3 in it # ensure all 3 are subscribed and test2 and test3 have the correct # reason. # Also try and tag ourselves this should have no affect # Try tagging someone that does not even exist post2 = create_post(user1, 'user1', 'Test post @user1 @user2 @user3 @user4') self.assertTrue(is_subscribed(user2, post2)) self.assertTrue(is_subscribed(user3, post2)) self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post2), user2), SubscriptionReasons.TAGEE) self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post2), user3), SubscriptionReasons.TAGEE) # Test tagging user 3 in a comment on post1. This ensures that tags # in comments do work. create_post(user2, 'user2', 'Test comment @user3', post1) self.assertTrue(is_subscribed(user3, post1)) self.assertEqual(r.zscore(K.POST_SUBSCRIBERS.format(post1), user3), SubscriptionReasons.TAGEE) # Unsubscribe user2 and user3 self.assertTrue(unsubscribe(user2, post2)) self.assertFalse(is_subscribed(user2, post2)) self.assertTrue(unsubscribe(user3, post2)) self.assertFalse(is_subscribed(user3, post2)) # Ensure that subscribe doe not happen when it is not a valid post self.assertFalse( subscribe(user1, K.NIL_VALUE, SubscriptionReasons.POSTER))