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
|
import type { CollectionEntry } from "astro:content";
import {
Blog,
Entity,
type Entry,
type MicroEntry,
type OriginalEntry,
type TranslationEntry,
} from "./schemas.ts";
import { getEntries, type z } from "astro:content";
import { defined, get, identity } from "../../utils/anonymous.ts";
import { createKeyFromArmor } from "../pgp/create.ts";
import { getUserIDsFromKey } from "../pgp/user.ts";
import type { UserIDPacket } from "openpgp";
import { getCollection } from "astro:content";
import {
GetStaticPaths,
GetStaticPathsItem,
GetStaticPathsResult,
} from "astro";
import { getEntry } from "astro:content";
export function getLastUpdate({ data }: CollectionEntry<"blog">): Date {
return data.dateUpdated ?? data.dateCreated;
}
export const sortLastCreated = (
{ data: a }: CollectionEntry<"blog">,
{ data: b }: CollectionEntry<"blog">,
): number => b.dateCreated - a.dateCreated;
export const sortFirstCreated = (
a: CollectionEntry<"blog">,
b: CollectionEntry<"blog">,
): number => sortLastCreated(b, a);
export const sortLastUpdated = (
{ data: a }: CollectionEntry<"blog">,
{ data: b }: CollectionEntry<"blog">,
): number =>
(b.dateUpdated ?? b.dateCreated) - (a.dateUpdated ?? a.dateCreated);
export const sortFirstUpdated = (
a: CollectionEntry<"blog">,
b: CollectionEntry<"blog">,
): number => sortLastUpdated(b, a);
export async function getSigners(
{ data }: CollectionEntry<"blog">,
): Promise<{
id: string;
entity: CollectionEntry<"entity">;
role: z.infer<typeof Blog>["signers"][number]["role"] | undefined;
}[]> {
const post = Blog.parse(data);
return await getEntries(post.signers.map(get("entity"))).then((x) =>
x.map((x) => ({
id: x.id,
entity: x,
role: post.signers?.find((y) => y.entity.id === x.id)?.role,
})).filter(({ role }) => defined(role))
);
}
export async function getFirstAuthorEmail(
blog: CollectionEntry<"blog">,
): Promise<string | undefined> {
const signers = await getSigners(blog);
const emails = await Promise.all(
signers.filter(({ role }) => role === "author").map(async ({ entity }) => {
const { publickey } = Entity.parse(entity.data);
const key = await createKeyFromArmor(publickey.armor);
const users = getUserIDsFromKey(undefined, key);
return users.map(get("email")).filter(Boolean)?.[0];
}),
);
return emails.filter(defined)?.[0];
}
export async function getFirstUserID(
blog: CollectionEntry<"blog">,
): Promise<
(Partial<UserIDPacket> & { entity: string; website: string | undefined })
> {
const signers = await getSigners(blog);
const userIDs = await Promise.all(
signers.filter(({ role }) => role === "author").map(
async ({ id, entity }) => {
const { publickey, websites } = Entity.parse(entity.data);
const website = websites?.[0];
const key = await createKeyFromArmor(publickey.armor);
const users = getUserIDsFromKey(undefined, key);
return users.map((user) => {
return { ...user, entity: id, website };
})?.[0];
},
),
);
return userIDs.filter(defined)?.[0];
}
export async function fromPosts<T extends Entry, U>(
filter: (entry: CollectionEntry<"blog">) => entry is T,
predicate: (entries: T[]) => U = identity as (entries: T[]) => U,
): Promise<U> {
const entries = await getCollection<"blog", T>("blog", filter);
return predicate(entries);
}
export const isEntry = (
_entry: CollectionEntry<"blog">,
): _entry is Entry => true;
export const isOriginal = (
entry: CollectionEntry<"blog">,
): entry is OriginalEntry => entry.data.kind === "original";
export const isTranslation = (
entry: CollectionEntry<"blog">,
): entry is TranslationEntry => entry.data.kind === "translation";
export const isMicro = (
entry: CollectionEntry<"blog">,
): entry is MicroEntry => entry.data.kind === "micro";
export async function getTranslationOriginal(
translation: TranslationEntry,
): Promise<OriginalEntry | undefined> {
if (!isTranslation(translation)) {
throw new Error();
}
return await getEntry(translation.data.translationOf);
}
export const datePaths = (async (): Promise<
{
params: { date?: string };
props: {
posts: OriginalEntry[];
next?: string;
previous?: string;
years: number[];
months: number[];
days?: number[];
};
}[]
> => {
const posts = await fromPosts(isEntry, identity);
const archive = {
years: new Set<number>(),
monthsByYear: new Map<string, Set<number>>(),
daysByMonth: new Map<string, Set<number>>(),
postsByDate: new Map<string, typeof posts>(),
sortedDates: [] as string[],
};
const getYMD = (date: Date) => {
const y = date.getFullYear();
const m = date.getMonth() + 1;
const d = date.getDate();
return { y, m, d };
};
for (const post of posts) {
const { y, m, d } = getYMD(post.data.dateCreated);
archive.years.add(y);
const months = archive.monthsByYear.get(y.toString());
if (months === undefined) {
archive.monthsByYear.set(y.toString(), new Set([m]));
} else {
months.add(m);
}
const ym = `${y}/${String(m).padStart(2, "0")}`;
const days = archive.daysByMonth.get(ym);
if (days === undefined) {
archive.daysByMonth.set(ym, new Set([d]));
} else {
days.add(d);
}
const ymd = `${ym}/${String(d).padStart(2, "0")}`;
const posts = archive.postsByDate.get(ymd);
if (posts === undefined) {
archive.postsByDate.set(ymd, [post]);
} else {
posts.push(post);
}
}
archive.sortedDates = Array.from(archive.postsByDate.keys()).sort();
const paths: {
params: { date?: string };
props: {
posts: OriginalEntry[];
next?: string;
previous?: string;
years: number[];
months: number[];
days?: number[];
};
}[] = [] satisfies GetStaticPathsItem[];
const sortedYears = Array.from(archive.years).sort();
const lastYear = Math.max(...sortedYears.map(Number));
paths.push({
params: { date: undefined },
props: {
posts: posts.filter((p) => p.data.dateCreated.getFullYear() === lastYear),
next: undefined,
previous: sortedYears?.[sortedYears.length - 2]?.toString(),
years: sortedYears,
months: Array.from(archive.monthsByYear.get(lastYear.toString()) ?? []),
},
});
for (const y of sortedYears) {
const yearPosts = posts.filter((p) =>
p.data.dateCreated.getFullYear() === Number(y)
);
const idx = sortedYears.indexOf(y);
paths.push({
params: { date: y.toString() },
props: {
posts: yearPosts,
next: sortedYears?.[idx + 1]?.toString(),
previous: sortedYears?.[idx - 1]?.toString(),
years: sortedYears,
months: Array.from(archive.monthsByYear.get(y.toString()) ?? []),
},
});
}
const allMonths = Array.from(archive.monthsByYear.entries())
.flatMap(([year, mset]) =>
Array.from(mset).map((m) => `${year}/${String(m).padStart(2, "0")}`)
)
.sort();
for (const [y, months] of archive.monthsByYear) {
const sortedMonths = Array.from(months).sort();
for (const m of sortedMonths) {
const monthPosts = posts.filter((p) => {
const d = p.data.dateCreated;
return (
d.getFullYear() === Number(y) &&
d.getMonth() + 1 === m
);
});
const ym = `${y}/${String(m).padStart(2, "0")}`;
const idx = allMonths.indexOf(ym);
paths.push({
params: { date: ym },
props: {
posts: monthPosts,
next: allMonths?.[idx + 1],
previous: allMonths?.[idx - 1],
years: sortedYears,
months: Array.from(months).sort(),
days: Array.from(archive.daysByMonth.get(ym) ?? []).sort(),
},
});
}
}
for (let i = 0; i < archive.sortedDates.length; i++) {
const ymd = archive.sortedDates[i];
const [y, m] = ymd.split("/");
paths.push({
params: { date: ymd },
props: {
posts: archive.postsByDate.get(ymd) ?? [],
next: archive.sortedDates?.[i + 1],
previous: archive.sortedDates?.[i - 1],
years: sortedYears,
months: Array.from(archive.monthsByYear.get(y) ?? []).sort(),
days: Array.from(archive.daysByMonth.get(`${y}/${m}`) ?? []).sort(),
},
});
}
return paths;
}) satisfies GetStaticPaths;
|