8.4 Posting or deleting a status update
One of the most fundamental operations on a service like Twitter is posting status messages.
People post to share their ideas, and people read because they’re interested in
what’s going on with others. Section 8.1.2 showed how to create a status message as a prerequisite
for knowing the types of data that we’ll be storing, but didn’t show how to get
that status message into a profile timeline or the home timeline of the user’s followers.
In this section, we’ll discuss what happens to a status message when it’s posted so it
can find its way into the home timelines of that user’s followers. We’ll also talk about
how to delete a status message.
You already know how to create the status message itself, but we now need to get
the status message ID into the home timeline of all of our followers. How we should
perform this operation will depend on the number of followers that the posting user
happens to have. If the user has a relatively small number of followers (say, up to 1,000
or so), we can update their home timelines immediately. But for users with larger
number of followers (like 1 million, or even the 25 million that some users have on
Twitter), attempting to perform those insertions directly will take longer than is reasonable
for a user to wait.
To allow for our call to return quickly, we’ll do two things. First, we’ll add the status
ID to the home timelines of the first 1,000 followers as part of the call that posts
the status message. Based on statistics from a site like Twitter, that should handle at
least 99.9% of all users who post (Twitter-wide analytics suggest that there are
roughly 100,000–250,000 users with more than 1,000 followers, which amounts to
roughly .1% of the active user base). This means that only the top .1% of users will
need another step.
Second, for those users with more than 1,000 followers, we’ll start a deferred task
using a system similar to what we built back in section 6.4. The next listing shows the
code for pushing status updates to followers.
Notice that we broke our status updating into two parts. The first part calls the
create_status() function from listing 8.2 to actually create the status message, and
then adds it to the poster’s profile timeline. The second part actually adds the status
message to the timelines of the user’s followers, which can be seen next.
This second function is what actually handles pushing status messages to the first 1,000
followers’ home timelines, and starts a delayed task using the API we defined in section
6.4 for followers past the first 1,000. With those new functions, we’ve now completed
the tools necessary to actually post a status update and send it to all of a user’s followers.
Exercise: Updating lists
In the last section, I suggested an exercise to build named lists of users. Can you
extend the syndicate_message() function to also support updating the list timelines
Let’s imagine that we posted a status message that we weren’t proud of; what would we
need to do to delete it?
It turns out that deleting a status message is pretty easy. Before returning the
fetched status messages from a user’s home or profile timeline in get_messages(),
we’re already filtering “empty” status messages with the Python filter() function. So
to delete a status message, we only need to delete the status message HASH and update
the number of status messages posted for the user. The function that deletes a status
message is shown in the following listing.
While deleting the status message and updating the status count, we also went ahead
and removed the message from the user’s home timeline and profile timeline.
Though this isn’t technically necessary, it does allow us to keep both of those timelines
a little cleaner without much effort.
Exercise: Cleaning out deleted IDs
As status messages are deleted, “zombie” status message IDs will still be in the
home timelines of all followers. Can you clean out these status IDs? Hint: Think about
how we sent the messages out in the first place. Bonus points: also handle lists.
Being able to post or delete status messages more or less completes the primary functionality
of a Twitter-like social network from a typical user’s perspective. But to complete
the experience, you may want to consider adding a few other features:
- Private users, along with the ability to request to follow someone
- Favorites (keeping in mind the privacy of a tweet)
- Direct messaging between users
- Replying to messages resulting in conversation flow
- Reposting/retweeting of messages
- The ability to @mention users or #tag ideas
- Keeping a record of who @mentions someone
- Spam and abuse reporting and controls
These additional features would help to round out the functionality of a site like Twitter,
but may not be necessary in every situation. Expanding beyond those features that
Twitter provides, some social networks have chosen to offer additional functionality
that you may want to consider:
- Liking/+1 voting status messages
- Moving status messages around the timeline depending on “importance”
- Direct messaging between a prespecified group of people (like in section 6.5.2)
- Groups where users can post to and/or follow a group timeline (public groups, private groups, or even announcement-style groups)
Now that we’ve built the last piece of the standard functional API for actually servicing
a site like Twitter, let’s see what it’d take to build a system for processing streaming API