-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathfeed.ts
More file actions
421 lines (388 loc) · 10.1 KB
/
Copy pathfeed.ts
File metadata and controls
421 lines (388 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// @ts-nocheck - Pre-existing type issues: missing await calls on async operations
import { Elysia, t } from 'elysia'
import type { Address, Hex } from 'viem'
import { CreateCastBodySchema, expectValid } from '../schemas'
import * as farcasterService from '../services/farcaster'
import { requireAuth } from '../validation/access-control'
const CastReactionBodySchema = t.Object({
castHash: t.String({ minLength: 3 }),
castFid: t.Number({ minimum: 1 }),
})
const FeedTypeQuerySchema = t.Object({
channel: t.Optional(t.String()),
feedType: t.Optional(
t.Union([t.Literal('channel'), t.Literal('trending'), t.Literal('user')]),
),
fid: t.Optional(t.String()),
cursor: t.Optional(t.String()),
limit: t.Optional(t.String()),
})
const PaginationQuerySchema = t.Object({
cursor: t.Optional(t.String()),
limit: t.Optional(t.String()),
})
async function getViewerFid(
headers: Record<string, string | undefined>,
): Promise<number | undefined> {
const authHeader = headers.authorization
if (!authHeader?.startsWith('Bearer ')) return undefined
const address = authHeader.slice(7) as Address
const link = await farcasterService.getLinkedFid(address)
return link?.fid
}
function getPagination(query: { cursor?: string; limit?: string }) {
return {
limit: query.limit ? parseInt(query.limit, 10) : 20,
cursor: query.cursor,
}
}
async function requireWalletAddress(
headers: Record<string, string | undefined>,
): Promise<Address> {
const authResult = await requireAuth(headers)
if (!authResult.success) {
throw new Error('UNAUTHORIZED')
}
return authResult.address
}
export const feedRoutes = new Elysia({ prefix: '/api/feed' })
// Get feed (channel, trending, or user)
.get(
'/',
async ({ query, headers }) => {
const channel = query.channel ?? farcasterService.getFactoryChannelId()
const feedType = query.feedType ?? 'channel'
const { limit, cursor } = getPagination(query)
const viewerFid = await getViewerFid(headers)
if (feedType === 'trending') {
if (!farcasterService.isNeynarConfigured()) {
return { casts: [], cursor: undefined }
}
return farcasterService.getTrendingFeed({ limit, cursor, viewerFid })
}
if (feedType === 'user' && query.fid) {
return farcasterService.getUserFeed(parseInt(query.fid, 10), {
limit,
cursor,
viewerFid,
})
}
return farcasterService.getChannelFeed(channel, {
limit,
cursor,
viewerFid,
})
},
{
query: FeedTypeQuerySchema,
detail: {
tags: ['feed'],
summary: 'Get feed',
description: 'Get Farcaster feed (channel, trending, or user)',
},
},
)
// Get channel feed
.get(
'/channel/:channelId',
async ({ params, query, headers }) => {
const { limit, cursor } = getPagination(query)
return farcasterService.getChannelFeed(params.channelId, {
limit,
cursor,
viewerFid: await getViewerFid(headers),
})
},
{
query: PaginationQuerySchema,
detail: {
tags: ['feed'],
summary: 'Get channel feed',
description: 'Get casts from a specific Farcaster channel',
},
},
)
// Get user feed
.get(
'/user/:fid',
async ({ params, query, headers }) => {
const { limit, cursor } = getPagination(query)
return farcasterService.getUserFeed(parseInt(params.fid, 10), {
limit,
cursor,
viewerFid: await getViewerFid(headers),
})
},
{
query: PaginationQuerySchema,
detail: {
tags: ['feed'],
summary: 'Get user feed',
description: 'Get casts from a specific user',
},
},
)
// Publish a cast
.post(
'/',
async ({ body, headers, set }) => {
let address: Address
try {
address = await requireWalletAddress(headers)
} catch {
set.status = 401
return {
error: { code: 'UNAUTHORIZED', message: 'Wallet address required' },
}
}
if (!(await farcasterService.isFarcasterConnected(address))) {
set.status = 401
return {
error: {
code: 'NOT_CONNECTED',
message: 'Please connect your Farcaster account first',
},
}
}
const validated = expectValid(CreateCastBodySchema, body, 'request body')
const cast = await farcasterService.publishCast(address, validated.text, {
channelId: validated.channelId,
parentHash: validated.parentHash as Hex | undefined,
embeds: validated.embeds?.map((e) => e.url),
})
set.status = 201
return {
success: true,
cast: {
hash: cast.hash,
fid: cast.fid,
text: cast.text,
timestamp: cast.timestamp,
},
}
},
{
detail: {
tags: ['feed'],
summary: 'Publish cast',
description: 'Publish a new cast to Farcaster',
},
},
)
// Delete a cast
.delete(
'/:castHash',
async ({ params, headers, set }) => {
let address: Address
try {
address = await requireWalletAddress(headers)
} catch {
set.status = 401
return {
error: { code: 'UNAUTHORIZED', message: 'Wallet address required' },
}
}
await farcasterService.deleteCast(address, params.castHash as Hex)
return { success: true }
},
{
detail: {
tags: ['feed'],
summary: 'Delete cast',
description: 'Delete a cast you authored',
},
},
)
// Like a cast
.post(
'/like',
async ({ body, headers, set }) => {
let address: Address
try {
address = await requireWalletAddress(headers)
} catch {
set.status = 401
return {
error: { code: 'UNAUTHORIZED', message: 'Wallet address required' },
}
}
await farcasterService.likeCast(address, {
fid: body.castFid,
hash: body.castHash as Hex,
})
return { success: true }
},
{
body: CastReactionBodySchema,
detail: {
tags: ['feed'],
summary: 'Like cast',
description: 'Like a cast',
},
},
)
// Unlike a cast
.delete(
'/like',
async ({ body, headers, set }) => {
let address: Address
try {
address = await requireWalletAddress(headers)
} catch {
set.status = 401
return {
error: { code: 'UNAUTHORIZED', message: 'Wallet address required' },
}
}
await farcasterService.unlikeCast(address, {
fid: body.castFid,
hash: body.castHash as Hex,
})
return { success: true }
},
{
body: CastReactionBodySchema,
detail: {
tags: ['feed'],
summary: 'Unlike cast',
description: 'Remove like from a cast',
},
},
)
// Recast
.post(
'/recast',
async ({ body, headers, set }) => {
let address: Address
try {
address = await requireWalletAddress(headers)
} catch {
set.status = 401
return {
error: { code: 'UNAUTHORIZED', message: 'Wallet address required' },
}
}
await farcasterService.recastCast(address, {
fid: body.castFid,
hash: body.castHash as Hex,
})
return { success: true }
},
{
body: CastReactionBodySchema,
detail: {
tags: ['feed'],
summary: 'Recast',
description: 'Recast a cast',
},
},
)
// Remove recast
.delete(
'/recast',
async ({ body, headers, set }) => {
let address: Address
try {
address = await requireWalletAddress(headers)
} catch {
set.status = 401
return {
error: { code: 'UNAUTHORIZED', message: 'Wallet address required' },
}
}
await farcasterService.unrecastCast(address, {
fid: body.castFid,
hash: body.castHash as Hex,
})
return { success: true }
},
{
body: CastReactionBodySchema,
detail: {
tags: ['feed'],
summary: 'Remove recast',
description: 'Remove recast from a cast',
},
},
)
// Get user profile
.get(
'/user/:fid/profile',
async ({ params }) => {
const user = await farcasterService.getUser(parseInt(params.fid, 10))
if (!user) {
return { error: { code: 'NOT_FOUND', message: 'User not found' } }
}
return { user }
},
{
detail: {
tags: ['feed'],
summary: 'Get user profile',
description: 'Get Farcaster user profile by FID',
},
},
)
// Follow a user
.post(
'/follow/:fid',
async ({ params, headers, set }) => {
let address: Address
try {
address = await requireWalletAddress(headers)
} catch {
set.status = 401
return {
error: { code: 'UNAUTHORIZED', message: 'Wallet address required' },
}
}
await farcasterService.followUser(address, parseInt(params.fid, 10))
return { success: true }
},
{
detail: {
tags: ['feed'],
summary: 'Follow user',
description: 'Follow a Farcaster user',
},
},
)
// Unfollow a user
.delete(
'/follow/:fid',
async ({ params, headers, set }) => {
let address: Address
try {
address = await requireWalletAddress(headers)
} catch {
set.status = 401
return {
error: { code: 'UNAUTHORIZED', message: 'Wallet address required' },
}
}
await farcasterService.unfollowUser(address, parseInt(params.fid, 10))
return { success: true }
},
{
detail: {
tags: ['feed'],
summary: 'Unfollow user',
description: 'Unfollow a Farcaster user',
},
},
)
// Check if Neynar is configured
.get(
'/status',
() => ({
neynarConfigured: farcasterService.isNeynarConfigured(),
factoryChannelId: farcasterService.getFactoryChannelId(),
}),
{
detail: {
tags: ['feed'],
summary: 'Feed status',
description: 'Get feed service status',
},
},
)