www-new/lib/team.ts

92 lines
2.4 KiB
TypeScript
Raw Normal View History

import { readFile, readdir, access } from "fs/promises";
import path from "path";
import matter from "gray-matter";
2022-02-02 02:32:53 -05:00
import { Client } from "ldapts";
import { serialize } from "next-mdx-remote/serialize";
2022-02-02 02:32:53 -05:00
import { getCurrentTerm } from "@/lib/events";
const EXECS_PATH = path.join("content", "team", "execs");
const fileType = ".md";
2022-02-02 02:32:53 -05:00
const { year, term } = getCurrentTerm();
export interface Metadata {
name: string;
role: string;
image: string;
}
2022-02-02 02:32:53 -05:00
// export async function getExecNames() {
// return (await readdir(EXECS_PATH))
// .filter((name) => name.endsWith(fileType))
// .map((name) => name.slice(0, -1 * fileType.length));
// }
export async function getExecNames() {
2022-02-02 02:32:53 -05:00
if (process.env.USE_LDAP?.toLowerCase() !== "true") {
return (await readdir(EXECS_PATH))
.filter((name) => name.endsWith(fileType))
.map((name) => name.slice(0, -1 * fileType.length));
}
const url = "ldap://ldap1.csclub.uwaterloo.ca";
const searchDN = "ou=People,dc=csclub,dc=uwaterloo,dc=ca";
const client = new Client({ url });
try {
await client.bind("", "");
const { searchEntries } = await client.search(searchDN, {
scope: "sub",
filter: `(&(objectClass=member)(term=${(term as string).slice(
0,
1
)}${year})(exec=True))`,
});
const execMembers = searchEntries.map((item) => item.cn as string);
} finally {
await client.unbind();
}
return execMembers;
}
export async function getExec(fileName: string, convert = true) {
const raw = await readFile(path.join(EXECS_PATH, `${fileName}${fileType}`));
const { content, data: metadata } = matter(raw);
const image =
(metadata.image as string | undefined) ??
(await getMemberImagePath(metadata.name));
return {
content: convert ? await serialize(content) : content,
metadata: { ...metadata, image } as Metadata,
};
}
async function getImage(imgPath: string) {
try {
await access(path.join("public", imgPath));
return imgPath;
} catch {
return undefined;
}
}
export async function getMemberImagePath(name: string) {
const imgPath = path.join("images", "team", name.replace(" ", ""));
const placeholder = path.join(
"images",
"team",
"team-member-placeholder.svg"
);
const img =
(await getImage(imgPath + ".jpg")) ??
(await getImage(imgPath + ".png")) ??
(await getImage(imgPath + ".gif")) ??
(await getImage(imgPath + ".jpeg")) ??
placeholder;
return img;
}