2022-02-02 04:06:13 -05:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
2021-08-23 11:11:42 -04:00
|
|
|
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";
|
2021-08-23 11:11:42 -04:00
|
|
|
import { serialize } from "next-mdx-remote/serialize";
|
|
|
|
|
2022-02-02 02:32:53 -05:00
|
|
|
import { getCurrentTerm } from "@/lib/events";
|
|
|
|
|
2021-09-03 21:55:23 -04:00
|
|
|
const EXECS_PATH = path.join("content", "team", "execs");
|
2021-08-23 11:11:42 -04:00
|
|
|
const fileType = ".md";
|
2022-02-02 02:32:53 -05:00
|
|
|
const { year, term } = getCurrentTerm();
|
2021-08-23 11:11:42 -04:00
|
|
|
|
2022-02-02 04:06:13 -05:00
|
|
|
export interface execMembers {
|
|
|
|
name: string;
|
|
|
|
order: string;
|
|
|
|
}
|
|
|
|
|
2021-08-23 11:11:42 -04:00
|
|
|
export interface Metadata {
|
|
|
|
name: string;
|
|
|
|
role: string;
|
|
|
|
image: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getExecNames() {
|
2022-02-02 08:48:21 -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));
|
|
|
|
// }
|
2022-02-02 02:32:53 -05:00
|
|
|
|
|
|
|
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))`,
|
|
|
|
});
|
|
|
|
|
2022-02-02 04:06:13 -05:00
|
|
|
let execMembers: execMembers[] = [];
|
|
|
|
|
|
|
|
execMembers = searchEntries
|
|
|
|
.map((item) => {
|
|
|
|
return {
|
|
|
|
name: item.cn as string,
|
|
|
|
order: item.order as string,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.sort((item1: execMembers, item2: execMembers) =>
|
|
|
|
item1.order.localeCompare(item2.order)
|
|
|
|
);
|
|
|
|
|
|
|
|
const formattedExec: string[] = execMembers.map(
|
|
|
|
(member) =>
|
|
|
|
`0${member.order}-
|
|
|
|
${member.name.split(" ")[1].toLowerCase()}-
|
|
|
|
${member.name.split(" ")[0].toLowerCase()}`
|
|
|
|
);
|
2022-02-02 02:32:53 -05:00
|
|
|
} finally {
|
|
|
|
await client.unbind();
|
|
|
|
}
|
|
|
|
|
2022-02-02 04:06:13 -05:00
|
|
|
return formattedExec;
|
2021-08-23 11:11:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getExec(fileName: string, convert = true) {
|
|
|
|
const raw = await readFile(path.join(EXECS_PATH, `${fileName}${fileType}`));
|
|
|
|
const { content, data: metadata } = matter(raw);
|
2021-08-29 22:42:00 -04:00
|
|
|
const image =
|
|
|
|
(metadata.image as string | undefined) ??
|
|
|
|
(await getMemberImagePath(metadata.name));
|
2021-08-23 11:11:42 -04:00
|
|
|
|
|
|
|
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")) ??
|
2021-10-31 16:58:14 -04:00
|
|
|
(await getImage(imgPath + ".jpeg")) ??
|
2021-08-23 11:11:42 -04:00
|
|
|
placeholder;
|
|
|
|
return img;
|
|
|
|
}
|