Use relative time for recent dates in case summaries

Comes with an option to toggle on/off and to set the "recent" threshold.
This commit is contained in:
Dragory 2020-08-10 03:53:04 +03:00
parent d30cd0b764
commit 936ea5311b
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
3 changed files with 16 additions and 3 deletions

View file

@ -17,6 +17,8 @@ const defaultOptions = {
config: {
log_automatic_actions: true,
case_log_channel: null,
show_relative_times: true,
relative_time_cutoff: "7d",
},
};

View file

@ -1,16 +1,20 @@
import { PluginData } from "knub";
import { CasesPluginType } from "../types";
import { disableLinkPreviews, messageLink } from "../../../utils";
import { convertDelayStringToMS, DAYS, disableLinkPreviews, messageLink } from "../../../utils";
import { DBDateFormat, getDateFormat } from "../../../utils/dateFormats";
import { CaseTypes } from "../../../data/CaseTypes";
import moment from "moment-timezone";
import { Case } from "../../../data/entities/Case";
import { inGuildTz } from "../../../utils/timezones";
import humanizeDuration from "humanize-duration";
import { humanizeDurationShort } from "../../../humanizeDurationShort";
const CASE_SUMMARY_REASON_MAX_LENGTH = 300;
const INCLUDE_MORE_NOTES_THRESHOLD = 20;
const UPDATED_STR = "__[Update]__";
const RELATIVE_TIME_THRESHOLD = 7 * DAYS;
export async function getCaseSummary(
pluginData: PluginData<CasesPluginType>,
caseOrCaseId: Case | number,
@ -41,7 +45,12 @@ export async function getCaseSummary(
reason = disableLinkPreviews(reason);
const timestamp = moment.utc(theCase.created_at, DBDateFormat);
const prettyTimestamp = inGuildTz(pluginData, timestamp).format(getDateFormat(pluginData, "date"));
const config = pluginData.config.get();
const relativeTimeCutoff = convertDelayStringToMS(config.relative_time_cutoff);
const useRelativeTime = config.show_relative_times && Date.now() - timestamp.valueOf() < relativeTimeCutoff;
const prettyTimestamp = useRelativeTime
? moment.utc().to(timestamp)
: inGuildTz(pluginData, timestamp).format(getDateFormat(pluginData, "date"));
let caseTitle = `\`Case #${theCase.case_number}\``;
if (withLinks && theCase.log_message_id) {

View file

@ -1,5 +1,5 @@
import * as t from "io-ts";
import { tNullable } from "../../utils";
import { tDelayString, tNullable } from "../../utils";
import { CaseTypes } from "../../data/CaseTypes";
import { BasePluginType } from "knub";
import { GuildLogs } from "../../data/GuildLogs";
@ -9,6 +9,8 @@ import { GuildArchives } from "../../data/GuildArchives";
export const ConfigSchema = t.type({
log_automatic_actions: t.boolean,
case_log_channel: tNullable(t.string),
show_relative_times: t.boolean,
relative_time_cutoff: tDelayString,
});
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;