Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FakeTwitter {
- constructor(props) {
- this.redisDatabase = {};
- }
- addFollower(user_id, follower_id) {
- // When someone follows the user, store his/her data
- this.redisDatabase[user_id].followers.push(follower_id);
- }
- createUser(id) {
- // When you create the user,
- // create an empty container in redis to hold their timeline
- this.redisDatabase[id] = { followers: [], timeline: [], tweets: [] };
- }
- recordTweet(id, tweetText) {
- // when someone tweets, record the tweet
- this.redisDatabase[id].tweets.push(tweetText);
- // then find all his followers
- // and push the tweet into their timeline array.
- // like the way post offices send mails to your box.
- let followers = this.redisDatabase[id].followers;
- for (const follower of followers) {
- // Ideally you should be pushing id's not text
- // but this is just a demo for you to understand what's going on
- this.redisDatabase[follower].timeline.push({
- id,
- tweet: tweetText
- });
- }
- }
- retweet(retweeted_by, tweetText) {
- // when you retweet, we record the tweet
- this.recordTweet(retweeted_by, tweetText);
- }
- refreshTimeline(id) {
- // When someone refreshes their timeline
- // Just pull the timeline from the database
- return this.redisDatabase[id].timeline;
- }
- preview(id) {
- // preview of our datastore
- console.log(this.redisDatabase[id]);
- }
- }
- // Sample Usage
- // setup instance
- const Twitter = new FakeTwitter();
- // create an account for me
- Twitter.createUser("griffith");
- // create an account for you
- Twitter.createUser("eustace");
- // add a new follower
- Twitter.addFollower("griffith", "eustace");
- // Record my tweet
- Twitter.recordTweet("griffith", "Eustace is a hoe?");
- // Inspect the whole object to see what's going on
- console.log(Twitter);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement