-
Notifications
You must be signed in to change notification settings - Fork 554
/
unspecced.ts
233 lines (208 loc) · 7.44 KB
/
unspecced.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
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
import { NotEmptyArray } from '@atproto/common'
import { AuthRequiredError, InvalidRequestError } from '@atproto/xrpc-server'
import { Server } from '../../../../lexicon'
import { GenericKeyset, paginate } from '../../../../db/pagination'
import AppContext from '../../../../context'
import { FeedRow } from '../../../services/feed'
import {
isPostView,
GeneratorView,
} from '../../../../lexicon/types/app/bsky/feed/defs'
import { isViewRecord } from '../../../../lexicon/types/app/bsky/embed/record'
import { countAll, valuesList } from '../../../../db/util'
import { FeedKeyset } from './util/feed'
const NO_WHATS_HOT_LABELS: NotEmptyArray<string> = [
'!no-promote',
'corpse',
'self-harm',
]
const NSFW_LABELS = ['porn', 'sexual', 'nudity', 'underwear']
// @NOTE currently relies on the hot-classic feed being configured on the pds
// THIS IS A TEMPORARY UNSPECCED ROUTE
export default function (server: Server, ctx: AppContext) {
server.app.bsky.unspecced.getPopular({
auth: ctx.accessVerifier,
handler: async ({ req, params, auth }) => {
const requester = auth.credentials.did
if (ctx.canProxyRead(req)) {
const hotClassicUri = Object.keys(ctx.algos).find((uri) =>
uri.endsWith('/hot-classic'),
)
if (!hotClassicUri) {
return {
encoding: 'application/json',
body: { feed: [] },
}
}
const { data: feed } =
await ctx.appviewAgent.api.app.bsky.feed.getFeedGenerator(
{ feed: hotClassicUri },
await ctx.serviceAuthHeaders(requester),
)
const res = await ctx.appviewAgent.api.app.bsky.feed.getFeed(
{ ...params, feed: hotClassicUri },
await ctx.serviceAuthHeaders(requester, feed.view.did),
)
return {
encoding: 'application/json',
body: res.data,
}
}
const { limit, cursor, includeNsfw } = params
const db = ctx.db.db
const { ref } = db.dynamic
const accountService = ctx.services.account(ctx.db)
const feedService = ctx.services.appView.feed(ctx.db)
const graphService = ctx.services.appView.graph(ctx.db)
const labelsToFilter = includeNsfw
? NO_WHATS_HOT_LABELS
: [...NO_WHATS_HOT_LABELS, ...NSFW_LABELS]
const postsQb = feedService
.selectPostQb()
.leftJoin('post_agg', 'post_agg.uri', 'post.uri')
.where('post_agg.likeCount', '>=', 12)
.where('post.replyParent', 'is', null)
.whereNotExists((qb) =>
qb
.selectFrom('label')
.selectAll()
.whereRef('val', 'in', valuesList(labelsToFilter))
.where('neg', '=', 0)
.where((clause) =>
clause
.whereRef('label.uri', '=', ref('post.creator'))
.orWhereRef('label.uri', '=', ref('post.uri')),
),
)
.where((qb) =>
accountService.whereNotMuted(qb, requester, [ref('post.creator')]),
)
.whereNotExists(graphService.blockQb(requester, [ref('post.creator')]))
const keyset = new FeedKeyset(ref('sortAt'), ref('cid'))
let feedQb = ctx.db.db.selectFrom(postsQb.as('feed_items')).selectAll()
feedQb = paginate(feedQb, { limit, cursor, keyset })
const feedItems: FeedRow[] = await feedQb.execute()
const feed = await feedService.hydrateFeed(feedItems, requester)
// filter out any quote post where the internal post has a filtered label
const noLabeledQuotePosts = feed.filter((post) => {
const quoteView = post.post.embed?.record
if (!quoteView || !isViewRecord(quoteView)) return true
for (const label of quoteView.labels || []) {
if (labelsToFilter.includes(label.val)) return false
}
return true
})
// remove record embeds in our response
const noRecordEmbeds = noLabeledQuotePosts.map((post) => {
delete post.post.record['embed']
if (post.reply) {
if (isPostView(post.reply.parent)) {
delete post.reply.parent.record['embed']
}
if (isPostView(post.reply.root)) {
delete post.reply.root.record['embed']
}
}
return post
})
return {
encoding: 'application/json',
body: {
feed: noRecordEmbeds,
cursor: keyset.packFromResult(feedItems),
},
}
},
})
server.app.bsky.unspecced.getPopularFeedGenerators({
auth: ctx.accessVerifier,
handler: async ({ auth, params }) => {
const requester = auth.credentials.did
const db = ctx.db.db
const { limit, cursor, query } = params
const { ref } = db.dynamic
const feedService = ctx.services.appView.feed(ctx.db)
let inner = ctx.db.db
.selectFrom('feed_generator')
.select([
'uri',
'cid',
ctx.db.db
.selectFrom('like')
.whereRef('like.subject', '=', ref('feed_generator.uri'))
.select(countAll.as('count'))
.as('likeCount'),
])
if (query) {
// like is case-insensitive is sqlite, and ilike is not supported
const operator = ctx.db.dialect === 'pg' ? 'ilike' : 'like'
inner = inner.where((qb) =>
qb
.where('feed_generator.displayName', operator, `%${query}%`)
.orWhere('feed_generator.description', operator, `%${query}%`),
)
}
let builder = ctx.db.db.selectFrom(inner.as('feed_gens')).selectAll()
const keyset = new LikeCountKeyset(ref('likeCount'), ref('cid'))
builder = paginate(builder, { limit, cursor, keyset, direction: 'desc' })
const res = await builder.execute()
const genInfos = await feedService.getFeedGeneratorInfos(
res.map((feed) => feed.uri),
requester,
)
const creators = Object.values(genInfos).map((gen) => gen.creator)
const profiles = await feedService.getActorInfos(creators, requester)
const genViews: GeneratorView[] = []
for (const row of res) {
const gen = genInfos[row.uri]
if (!gen) continue
const view = feedService.views.formatFeedGeneratorView(gen, profiles)
genViews.push(view)
}
return {
encoding: 'application/json',
body: {
cursor: keyset.packFromResult(res),
feeds: genViews,
},
}
},
})
server.app.bsky.unspecced.applyLabels({
auth: ctx.roleVerifier,
handler: async ({ auth, input }) => {
if (!auth.credentials.admin) {
throw new AuthRequiredError('Insufficient privileges')
}
const { services, db } = ctx
const { labels } = input.body
await services.appView.label(db).createLabels(labels)
},
})
}
type Result = { likeCount: number; cid: string }
type LabeledResult = { primary: number; secondary: string }
export class LikeCountKeyset extends GenericKeyset<Result, LabeledResult> {
labelResult(result: Result) {
return {
primary: result.likeCount,
secondary: result.cid,
}
}
labeledResultToCursor(labeled: LabeledResult) {
return {
primary: labeled.primary.toString(),
secondary: labeled.secondary,
}
}
cursorToLabeledResult(cursor: { primary: string; secondary: string }) {
const likes = parseInt(cursor.primary, 10)
if (isNaN(likes)) {
throw new InvalidRequestError('Malformed cursor')
}
return {
primary: likes,
secondary: cursor.secondary,
}
}
}