-
Notifications
You must be signed in to change notification settings - Fork 2
/
route.tsx
320 lines (292 loc) · 7.34 KB
/
route.tsx
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
/* eslint-disable @next/next/no-img-element */
import type {
DetailedHTMLProps,
HTMLAttributes,
PropsWithChildren,
} from "react";
import type { NextRequest } from "next/server";
import { ImageResponse } from "next/og";
import { getPostThread } from "@/app/bot/services/get-post-thread";
import BlueskyLogo from "@/app/components/BlueskyLogo";
import { format } from "date-fns";
import { validateCronSecret } from "@/app/utils/validate-cron-secret";
export const runtime = "edge";
const fontSizes = {
xs: 20,
sm: 24,
md: 28,
lg: 32,
} as const;
const sizes = {
CONTAINER_GAP: 25,
CONTAINER_PADDING_VERTICAL: 50,
CONTAINER_PADDING_HORIZONTAL: 100,
AVATAR_SIZE: 100,
LINE_HEIGHT: fontSizes.lg * 1.2,
IMAGE: 400,
} as const;
const AVERAGE_CHARACTERS_PER_LINE = 35;
const Flex: React.FC<
PropsWithChildren<
DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement> & {
direction?: "row" | "column";
align?: "start" | "center" | "end";
justify?: "start" | "center" | "end" | "space-between";
gap?: number;
}
>
> = ({ children, direction, gap, align, justify, style, ...rest }) => (
<div
style={{
display: "flex",
flexDirection: direction ?? "column",
gap: gap ?? 20,
alignItems: align ?? "flex-start",
justifyContent: justify ?? "flex-start",
...style,
}}
{...rest}
>
{children}
</div>
);
const Container: React.FC<PropsWithChildren> = ({ children }) => (
<Flex
justify="center"
style={{
color: "white",
background: "#161e26",
width: "100%",
height: "100%",
padding: `${sizes.CONTAINER_PADDING_VERTICAL}px ${sizes.CONTAINER_PADDING_HORIZONTAL}px`,
gap: sizes.CONTAINER_GAP,
fontFamily: "Noto Sans Regular",
}}
>
{children}
</Flex>
);
const Avatar: React.FC<{ src: string }> = ({ src }) => (
<img
alt="avatar"
src={src}
style={{
width: `${sizes.AVATAR_SIZE}px`,
height: `${sizes.AVATAR_SIZE}px`,
borderRadius: "50%",
}}
/>
);
const AuthorName: React.FC<PropsWithChildren> = ({ children }) => (
<div
style={{
fontSize: fontSizes.md,
fontWeight: "bolder",
fontFamily: "Noto Sans Bold",
}}
>
{children}
</div>
);
const AuthorHandle: React.FC<PropsWithChildren> = ({ children }) => (
<div style={{ fontSize: fontSizes.sm, color: "#aeb6bf" }}>{children}</div>
);
const Author: React.FC<{ image: string; name: string; handle: string }> = ({
image,
name,
handle,
}) => (
<Flex direction="row" align="center">
<Avatar src={image} />
<Flex gap={0} justify="center">
<AuthorName>{name || handle}</AuthorName>
<AuthorHandle>{`@${handle}`}</AuthorHandle>
</Flex>
</Flex>
);
const Thumbnail: React.FC<{ src: string }> = ({ src }) => (
<img
alt="thumbnail"
src={src}
style={{
width: "100%",
height: "100%",
objectFit: "cover",
borderRadius: "10px",
}}
/>
);
const Text: React.FC<
PropsWithChildren<
DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
>
> = ({ children, ...rest }) => (
<div style={{ fontSize: fontSizes.lg, color: "white" }} {...rest}>
{children}
</div>
);
const Content: React.FC<{ text: string }> = ({ text }) => {
const lines = text.split("\n").map((line, index) => {
return (
<Text
key={index}
style={{
fontSize: fontSizes.lg,
minHeight: sizes.LINE_HEIGHT,
lineHeight: `${sizes.LINE_HEIGHT}px`,
color: "white",
display: "flex",
flexWrap: "wrap",
columnGap: 8,
width: "100%",
maxWidth: "100%",
}}
>
{line.split(/\s+/).map((word, index) => {
return (
<span
key={index}
style={{
...(/^(@|#).*/.test(word) && {
color: "#208bfe",
}),
}}
>
{word}
</span>
);
})}
</Text>
);
});
return (
<Flex
style={{
maxHeight: "100%",
width: "100%",
maxWidth: "100%",
gap: 0,
}}
>
{lines}
</Flex>
);
};
export const GET = async (request: NextRequest) => {
try {
validateCronSecret(request);
} catch (error) {
return Response.json({ error: (error as Error).message }, { status: 401 });
}
const uri = request.nextUrl.searchParams.get("uri");
const debug = request.nextUrl.searchParams.get("debug");
if (!uri) {
return Response.json({ error: "Invalid URI" }, { status: 400 });
}
const notoSansRegular = await fetch(
new URL(
"../../assets/fonts/NotoSans-Regular.ttf",
import.meta.url
).toString()
).then((res) => res.arrayBuffer());
const notoSansBold = await fetch(
new URL("../../assets/fonts/NotoSans-Bold.ttf", import.meta.url).toString()
).then((res) => res.arrayBuffer());
const notoSansSymbols2 = await fetch(
new URL(
"../../assets/fonts/NotoSans-Symbols2.ttf",
import.meta.url
).toString()
).then((res) => res.arrayBuffer());
const thread = await getPostThread(uri);
const parent = thread.parent as any;
const thumbnail =
parent.post?.embed?.$type === "app.bsky.embed.images#view"
? parent.post.embed?.images?.[0]?.thumb
: undefined;
const formattedDate = format(
new Date(parent.post.record.createdAt),
"dd MMM '•' HH:mm '('OOOO')'"
);
const text = parent.post.record.text as string;
const sanitizedTextLength = text.replace(/\n/g, "").length;
const minimumLines = text.split("\n").length;
const textHeight =
Math.ceil(
minimumLines + sanitizedTextLength / AVERAGE_CHARACTERS_PER_LINE
) * sizes.LINE_HEIGHT;
const totalHeight =
textHeight +
sizes.CONTAINER_PADDING_VERTICAL * 2 +
sizes.AVATAR_SIZE +
sizes.CONTAINER_GAP * (!!thumbnail ? 3 : 2) +
sizes.LINE_HEIGHT +
(!!thumbnail ? sizes.IMAGE : 0);
return new ImageResponse(
(
<Container>
<Flex
direction="row"
align="center"
justify="space-between"
gap={10}
style={{
width: "100%",
}}
>
<Author
image={parent.post.author.avatar}
name={parent.post.author.displayName}
handle={parent.post.author.handle}
/>
<BlueskyLogo width={80} />
</Flex>
<Flex>
<Content text={text} />
</Flex>
{thumbnail && (
<div
style={{
display: "flex",
overflow: "hidden",
flex: "1 1 auto",
width: "100%",
}}
>
<Thumbnail src={thumbnail} />
</div>
)}
<Text
style={{
fontSize: fontSizes.xs,
color: "#aeb6bf",
}}
>
{formattedDate}
</Text>
</Container>
),
{
width: 800,
height: totalHeight,
fonts: [
{
name: "Noto Sans Regular",
data: notoSansRegular,
style: "normal",
},
{
name: "Noto Sans Bold",
data: notoSansBold,
style: "normal",
},
{
name: "Noto Sans Symbols2",
data: notoSansSymbols2,
style: "normal",
},
],
debug: debug === "true",
}
);
};