forked from bluesky-social/feed-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrp-next-post.ts
88 lines (76 loc) · 2.86 KB
/
rp-next-post.ts
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
import { InvalidRequestError } from '@atproto/xrpc-server'
import { QueryParams } from '../lexicon/types/app/bsky/feed/getFeedSkeleton'
import { AppContext } from '../config'
// max 15 chars
export const shortname = 'rp-next-post'
export const handler = async (ctx: AppContext, params: QueryParams, requester: string) => {
// 購読者のRepostを今後記録するために登録
const subscriberExist = await ctx.db
.selectFrom('subscriber')
.select(qb => qb.fn.count<number>('did').as('count'))
.where('did', '==', requester)
.execute()
const isNewSubscriber = subscriberExist.flatMap((row) => row.count)[0] === 0
if (isNewSubscriber) {
await ctx.db
.insertInto('subscriber')
.values({ did: requester })
.onConflict((oc) => oc.doNothing())
.execute()
}
let builder = ctx.db
.selectFrom('post')
.selectAll()
.where('post.prevRepostDid', '=', requester) // 購読者がRepostされたものだけ返す
.orderBy('indexedAt', 'desc')
.orderBy('cid', 'desc')
.limit(params.limit)
if (params.cursor) {
const [indexedAt, cid] = params.cursor.split('::')
if (!indexedAt || !cid) {
throw new InvalidRequestError('malformed cursor')
}
const timeStr = new Date(parseInt(indexedAt, 10)).toISOString()
builder = builder
.where('post.indexedAt', '<', timeStr)
.orWhere((qb) => qb.where('post.indexedAt', '=', timeStr))
.where('post.cid', '<', cid)
}
const res = await builder.execute()
const feed = res.map((row) => ({
post: row.uri,
}))
console.log('getFeedSkeleton : subscription by', requester, isNewSubscriber ? '(new!)' : '', 'cursor:', params.cursor ?? 'none', 'limit:', params.limit, 'count:', feed.length)
/*
if (requester !== 'did:plc:6zpjzzdzet62go7lnaoq4xog') {
return {
// メンテナンス(´・ω・`)
feed: [{ post: 'at://did:plc:xt2h3ltab6sagq4lbpbd37m2/app.bsky.feed.post/3kktmo5xrhq22' }]
}
}
*/
if (!params.cursor && feed.length <= 0) {
// 0件のときは待っててねPostを返す
let initialFeed : any[] = new Array()
initialFeed.push({ post: 'at://did:plc:xt2h3ltab6sagq4lbpbd37m2/app.bsky.feed.post/3kmibwwyksx2p' })
initialFeed.push({ post: 'at://did:plc:xt2h3ltab6sagq4lbpbd37m2/app.bsky.feed.post/3kmicro3yhg2q' })
if (isNewSubscriber) {
initialFeed.push({ post: 'at://did:plc:xt2h3ltab6sagq4lbpbd37m2/app.bsky.feed.post/3kmida2r5bn2e' })
} else {
initialFeed.push({ post: 'at://did:plc:xt2h3ltab6sagq4lbpbd37m2/app.bsky.feed.post/3kmiddqhbu52w' })
}
initialFeed.push({ post: 'at://did:plc:xt2h3ltab6sagq4lbpbd37m2/app.bsky.feed.post/3kmidjoypzd2v' })
return {
feed: initialFeed
}
}
let cursor: string | undefined
const last = res.at(-1)
if (last) {
cursor = `${new Date(last.indexedAt).getTime()}::${last.cid}`
}
return {
cursor,
feed,
}
}