Design the core of a social network. Users add friends, publish posts, like and comment, and see a feed of friends' posts.
Users send and accept **friend requests**.
Users publish **posts** (text, photo) with a **privacy** setting.
Users **like** and **comment** on posts they can see.
A **feed** shows recent posts from friends, in a rankable order.
Users get **notifications** for likes, comments and requests.
Stage 1: Users, friendships and posts
Model who is connected and what they publish.
Friendship is two-sided, and a post needs an audience. A `Friendship` is one record naming two users, so both see it or neither does. `Post` carries a `Privacy`. One record means no mismatch between two lists. Finding a user's friends needs an index on either side of the record.
Stage 2: Likes, comments and the graph
React to posts and answer 'can I see this?'.
Nothing decides whether a viewer may see a post. `SocialGraph.canSee` is the one place: public posts are visible to all, friends-only to friends, private to the author. Privacy is enforced in one function used by every read path. Friends-of-friends and lists multiply the cases. Keep `canSee` the only place they live.
Stage 3: Posting and the feed: Strategy
Build the feed and make its order swappable.
The feed needs both visibility and an order, and the order will keep changing. `FeedService` collects visible posts from friends, then lets a `FeedRanking` order them. `NewestFirst` is the first rule. A ranked or personalised feed is a new class (**Strategy**), and posting is untouched. Building the feed on read is simple but slow for users with many friends. Pre-compute for them.
Stage 4: Notifications: Observer
Tell people when something happens to them.
Authors are never told about likes and comments. `PostService` hands each like and comment to the `Notifier`, which decides who to tell and how. The post code does not know about push or email. Notify through a queue, so a slow channel never delays a like.