Skip to content

Commit 88d90bd

Browse files
committed
feat: use guild time/date settings in archives metadata
1 parent 6795bf7 commit 88d90bd

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

backend/src/api/archives.ts

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,58 @@
11
import express, { Request, Response } from "express";
22
import moment from "moment-timezone";
3+
import { Configs } from "../data/Configs.js";
34
import { GuildArchives } from "../data/GuildArchives.js";
5+
import { defaultDateFormats } from "../plugins/TimeAndDate/defaultDateFormats.js";
6+
import { zTimeAndDateConfig } from "../plugins/TimeAndDate/types.js";
7+
import { zZeppelinGuildConfig } from "../types.js";
8+
import { loadYamlSafely } from "../utils/loadYamlSafely.js";
49
import { notFound } from "./responses.js";
510

11+
const defaultTimeAndDateConfig = zTimeAndDateConfig.parse({});
12+
function getDefaultTimeAndDateSettings() {
13+
return {
14+
timezone: defaultTimeAndDateConfig.timezone,
15+
dateFormats: { ...defaultTimeAndDateConfig.date_formats },
16+
};
17+
}
18+
19+
async function getTimeAndDateSettingsForGuild(guildId: string, configs: Configs) {
20+
try {
21+
const configEntry = await configs.getActiveByKey(`guild-${guildId}`);
22+
if (!configEntry?.config) {
23+
return getDefaultTimeAndDateSettings();
24+
}
25+
26+
const parsedConfig = loadYamlSafely(configEntry.config);
27+
const guildConfig = zZeppelinGuildConfig.safeParse(parsedConfig);
28+
if (!guildConfig.success) {
29+
return getDefaultTimeAndDateSettings();
30+
}
31+
32+
const pluginOptions = guildConfig.data.plugins?.time_and_date;
33+
if (!pluginOptions || typeof pluginOptions !== "object") {
34+
return getDefaultTimeAndDateSettings();
35+
}
36+
37+
const basePluginConfig =
38+
typeof (pluginOptions as any).config === "object" ? (pluginOptions as any).config : pluginOptions;
39+
const pluginConfig = zTimeAndDateConfig.safeParse(basePluginConfig ?? {});
40+
if (!pluginConfig.success) {
41+
return getDefaultTimeAndDateSettings();
42+
}
43+
44+
return {
45+
timezone: pluginConfig.data.timezone,
46+
dateFormats: pluginConfig.data.date_formats,
47+
};
48+
} catch {
49+
return getDefaultTimeAndDateSettings();
50+
}
51+
}
52+
653
export function initArchives(router: express.Router) {
754
const archives = new GuildArchives(null);
55+
const configs = new Configs();
856

957
// Legacy redirect
1058
router.get("/spam-logs/:id", (req: Request, res: Response) => {
@@ -18,13 +66,16 @@ export function initArchives(router: express.Router) {
1866
let body = archive.body;
1967

2068
// Add some metadata at the end of the log file (but only if it doesn't already have it directly in the body)
21-
// TODO: Use server timezone / date formats
2269
if (archive.body.indexOf("Log file generated on") === -1) {
23-
const createdAt = moment.utc(archive.created_at).format("YYYY-MM-DD [at] HH:mm:ss [(+00:00)]");
70+
const timeAndDate = await getTimeAndDateSettingsForGuild(archive.guild_id, configs);
71+
const prettyDatetimeFormat =
72+
timeAndDate.dateFormats.pretty_datetime ?? defaultDateFormats.pretty_datetime;
73+
74+
const createdAt = moment.utc(archive.created_at).tz(timeAndDate.timezone).format(prettyDatetimeFormat);
2475
body += `\n\nLog file generated on ${createdAt}`;
2576

2677
if (archive.expires_at !== null) {
27-
const expiresAt = moment.utc(archive.expires_at).format("YYYY-MM-DD [at] HH:mm:ss [(+00:00)]");
78+
const expiresAt = moment.utc(archive.expires_at).tz(timeAndDate.timezone).format(prettyDatetimeFormat);
2879
body += `\nExpires at ${expiresAt}`;
2980
}
3081
}

0 commit comments

Comments
 (0)