···119119120120## Step 3. Fetching the user's profile
121121122122-Why don't we learn something about our user? Let's start by getting the [Agent](#todo) object. The [Agent](#todo) is the client to the user's `at://` repo server.
123123-124124-```typescript
125125-/** src/routes.ts **/
126126-async function getSessionAgent(
127127- req: IncomingMessage,
128128- res: ServerResponse<IncomingMessage>,
129129- ctx: AppContext
130130-) {
131131- // Fetch the session from their cookie
132132- const session = await getIronSession(req, res)
133133- if (!session.did) return null
134134-135135- // "Restore" the agent for the user
136136- try {
137137- return await ctx.oauthClient.restore(session.did)
138138- } catch(err) {
139139- ctx.logger.warn({ err }, 'oauth restore failed')
140140- await session.destroy()
141141- return null
142142- }
143143-}
144144-```
145145-146146-Users publish JSON records on their `at://` repos. In [Bluesky](https://bsky.app), they publish a "profile" record which looks like this:
122122+Why don't we learn something about our user? In [Bluesky](https://bsky.app), users publish a "profile" record which looks like this:
147123148124```typescript
149125interface ProfileRecord {
···156132}
157133```
158134159159-We're going to use the [Agent](#todo) to fetch this record to include in our app.
135135+You can examine this record directly using [atproto-browser.vercel.app](https://atproto-browser.vercel.app). For instance, [this is the profile record for @bsky.app](https://atproto-browser.vercel.app/at?u=at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.actor.profile/self).
136136+137137+We're going to use the [Agent](#todo) associated with the user's OAuth session to fetch this record.
160138161139```typescript
162140await agent.getRecord({
···168146169147When asking for a record, we provide three pieces of information.
170148171171-- The [DID](#todo) which identifies the user,
172172-- The collection name, and
173173-- The record key
149149+- **repo** The [DID](#todo) which identifies the user,
150150+- **collection** The collection name, and
151151+- **rkey** The record key
174152175153We'll explain the collection name shortly. Record keys are strings with [some limitations](https://atproto.com/specs/record-key#record-key-syntax) and a couple of common patterns. The `"self"` pattern is used when a collection is expected to only contain one record which describes the user.
176154···194172 const { data: profileRecord } = await agent.getRecord({
195173 repo: agent.accountDid, // our user's repo
196174 collection: 'app.bsky.actor.profile', // the bluesky profile record type
197197- rkey: 'self', // the record's name
175175+ rkey: 'self', // the record's key
198176 })
199177200178 // Serve the logged-in view
···206184```
207185208186With that data, we can give a nice personalized welcome banner for our user:
187187+188188+
209189210190```html
211191<!-- pages/home.ts -->
···228208 </div>`}
229209</div>
230210```
231231-232232-
233233-234234-You can examine this record directly using [atproto-browser.vercel.app](https://atproto-browser.vercel.app). For instance, [this is the profile record for @bsky.app](https://atproto-browser.vercel.app/at?u=at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.actor.profile/self).
235211236212## Step 4. Reading & writing records
237213···325301326302## Step 5. Creating a custom "status" schema
327303328328-The collections are typed, meaning that they have a defined schema. The `app.bsky.actor.profile` type definition [can be found here](https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/actor/profile.json).
304304+Repo collections are typed, meaning that they have a defined schema. The `app.bsky.actor.profile` type definition [can be found here](https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/actor/profile.json).
329305330306Anybody can create a new schema using the [Lexicon](#todo) language, which is very similar to [JSON-Schema](#todo). The schemas use [reverse-DNS IDs](#todo) which indicate ownership, but for this demo app we're going to use `com.example` which is safe for non-production software.
331307···511487512488
513489514514-Why read from the event log? Because there are other apps in the network that will write the records we're interested in. By subscribing to the event log, we ensure that we catch all the data we're interested in -- including data published by other apps.
490490+Applications write to the repo. The write events are then emitted on the firehose where they're caught by the apps and ingested into their databases.
491491+492492+Why sync from the event log like this? Because there are other apps in the network that will write the records we're interested in. By subscribing to the event log, we ensure that we catch all the data we're interested in — including data published by other apps!
515493516494## Step 7. Listing the latest statuses
517495···567545568546## Step 8. Optimistic updates
569547570570-As a final optimization, let's introduce "optimistic updates." Remember the information flow loop with the repo write and the event log? Since we're updating our users' repos locally, we can short-circuit that flow to our own database:
548548+As a final optimization, let's introduce "optimistic updates."
549549+550550+Remember the information flow loop with the repo write and the event log?
551551+552552+
553553+554554+Since we're updating our users' repos locally, we can short-circuit that flow to our own database:
571555572556
573557···633617- Design the [Lexicon](#) schemas for the records you'll publish into the Atmosphere.
634618- Create a database for aggregating the records into useful views.
635619- Build your application to write the records on your users' repos.
636636-- Listen to the firehose to hydrate your aggregated database.
620620+- Listen to the firehose to aggregate data across the network.
637621638622Remember this flow of information throughout:
639623