From 5d85a3a093075a7849b2b614fab8859ccfbe8357 Mon Sep 17 00:00:00 2001 From: Apoorv Mishra Date: Sun, 25 Aug 2024 18:57:45 +0530 Subject: [PATCH] Specify time conversion unit (#7458) * fix: specificity in time units * fix: milliseconds -> ms --- app/hooks/useAutoRefresh.ts | 4 +-- app/hooks/useIdle.ts | 2 +- .../Document/components/CommentThreadItem.tsx | 2 +- plugins/github/server/index.ts | 2 +- plugins/iframely/server/index.ts | 2 +- .../slack/server/processors/SlackProcessor.ts | 2 +- server/collaboration/ViewsExtension.ts | 2 +- .../emails/templates/CommentCreatedEmail.tsx | 2 +- .../templates/CommentMentionedEmail.tsx | 2 +- .../DocumentPublishedOrUpdatedEmail.tsx | 2 +- server/queues/HealthMonitor.ts | 2 +- server/queues/processors/EmailsProcessor.ts | 10 +++--- server/queues/queue.ts | 2 +- server/routes/index.ts | 8 ++--- server/services/cron.ts | 6 ++-- server/services/web.ts | 2 +- server/utils/CacheHelper.ts | 2 +- shared/utils/time.ts | 34 ++++++++++++++----- 18 files changed, 53 insertions(+), 35 deletions(-) diff --git a/app/hooks/useAutoRefresh.ts b/app/hooks/useAutoRefresh.ts index 4aba63d926..d2d4e3c943 100644 --- a/app/hooks/useAutoRefresh.ts +++ b/app/hooks/useAutoRefresh.ts @@ -16,7 +16,7 @@ let isReloaded = false; export default function useAutoRefresh() { const [minutes, setMinutes] = React.useState(0); const isVisible = usePageVisibility(); - const isIdle = useIdle(15 * Minute); + const isIdle = useIdle(15 * Minute.ms); useInterval(() => { setMinutes((prev) => prev + 1); @@ -39,5 +39,5 @@ export default function useAutoRefresh() { window.location.reload(); isReloaded = true; } - }, Minute); + }, Minute.ms); } diff --git a/app/hooks/useIdle.ts b/app/hooks/useIdle.ts index e9dced717f..23e0e5c185 100644 --- a/app/hooks/useIdle.ts +++ b/app/hooks/useIdle.ts @@ -23,7 +23,7 @@ const activityEvents = [ * @returns boolean if the user is idle */ export default function useIdle( - timeToIdle: number = 3 * Minute, + timeToIdle: number = 3 * Minute.ms, events = activityEvents ) { const isMounted = useIsMounted(); diff --git a/app/scenes/Document/components/CommentThreadItem.tsx b/app/scenes/Document/components/CommentThreadItem.tsx index 73a9a7b931..ab332fefc9 100644 --- a/app/scenes/Document/components/CommentThreadItem.tsx +++ b/app/scenes/Document/components/CommentThreadItem.tsx @@ -53,7 +53,7 @@ function useShowTime( return ( !msSincePreviousComment || - (msSincePreviousComment > 15 * Minute && + (msSincePreviousComment > 15 * Minute.ms && previousTimeStamp !== currentTimeStamp) ); } diff --git a/plugins/github/server/index.ts b/plugins/github/server/index.ts index 6015ed2fcb..f42927aaa7 100644 --- a/plugins/github/server/index.ts +++ b/plugins/github/server/index.ts @@ -22,7 +22,7 @@ if (enabled) { }, { type: Hook.UnfurlProvider, - value: { unfurl: GitHub.unfurl, cacheExpiry: Minute / 1000 }, + value: { unfurl: GitHub.unfurl, cacheExpiry: Minute.seconds }, }, { type: Hook.Uninstall, diff --git a/plugins/iframely/server/index.ts b/plugins/iframely/server/index.ts index 1e1f6ffd9c..66d3ebe399 100644 --- a/plugins/iframely/server/index.ts +++ b/plugins/iframely/server/index.ts @@ -19,7 +19,7 @@ if (enabled) { PluginManager.add([ { type: Hook.UnfurlProvider, - value: { unfurl: Iframely.unfurl, cacheExpiry: Day / 1000 }, + value: { unfurl: Iframely.unfurl, cacheExpiry: Day.seconds }, // Make sure this is last in the stack to be evaluated after all other unfurl providers priority: PluginPriority.VeryLow, diff --git a/plugins/slack/server/processors/SlackProcessor.ts b/plugins/slack/server/processors/SlackProcessor.ts index 8c3776c29f..95606f097b 100644 --- a/plugins/slack/server/processors/SlackProcessor.ts +++ b/plugins/slack/server/processors/SlackProcessor.ts @@ -104,7 +104,7 @@ export default class SlackProcessor extends BaseProcessor { if ( event.name === "revisions.create" && differenceInMilliseconds(document.updatedAt, document.publishedAt) < - Minute + Minute.ms ) { return; } diff --git a/server/collaboration/ViewsExtension.ts b/server/collaboration/ViewsExtension.ts index e68cd04e2a..7014c1bc95 100644 --- a/server/collaboration/ViewsExtension.ts +++ b/server/collaboration/ViewsExtension.ts @@ -34,7 +34,7 @@ export class ViewsExtension implements Extension { const lastUpdate = this.lastViewBySocket.get(socketId); const [, documentId] = documentName.split("."); - if (!lastUpdate || Date.now() - lastUpdate.getTime() > Minute) { + if (!lastUpdate || Date.now() - lastUpdate.getTime() > Minute.ms) { this.lastViewBySocket.set(socketId, new Date()); Logger.debug( diff --git a/server/emails/templates/CommentCreatedEmail.tsx b/server/emails/templates/CommentCreatedEmail.tsx index a421b84d86..4771c9f8e0 100644 --- a/server/emails/templates/CommentCreatedEmail.tsx +++ b/server/emails/templates/CommentCreatedEmail.tsx @@ -77,7 +77,7 @@ export default class CommentCreatedEmail extends BaseEmail< content = await TextHelper.attachmentsToSignedUrls( content, document.teamId, - (4 * Day) / 1000 + 4 * Day.seconds ); if (content) { diff --git a/server/emails/templates/CommentMentionedEmail.tsx b/server/emails/templates/CommentMentionedEmail.tsx index ca5d379d1f..2ab4181bdc 100644 --- a/server/emails/templates/CommentMentionedEmail.tsx +++ b/server/emails/templates/CommentMentionedEmail.tsx @@ -69,7 +69,7 @@ export default class CommentMentionedEmail extends BaseEmail< content = await TextHelper.attachmentsToSignedUrls( content, document.teamId, - (4 * Day) / 1000 + 4 * Day.seconds ); if (content) { diff --git a/server/emails/templates/DocumentPublishedOrUpdatedEmail.tsx b/server/emails/templates/DocumentPublishedOrUpdatedEmail.tsx index 2369d747f2..a5cc239698 100644 --- a/server/emails/templates/DocumentPublishedOrUpdatedEmail.tsx +++ b/server/emails/templates/DocumentPublishedOrUpdatedEmail.tsx @@ -68,7 +68,7 @@ export default class DocumentPublishedOrUpdatedEmail extends BaseEmail< const content = await DocumentHelper.toEmailDiff(before, revision, { includeTitle: false, centered: false, - signedUrls: (4 * Day) / 1000, + signedUrls: 4 * Day.seconds, baseUrl: props.teamUrl, }); diff --git a/server/queues/HealthMonitor.ts b/server/queues/HealthMonitor.ts index f4c5aad3d0..d526e68912 100644 --- a/server/queues/HealthMonitor.ts +++ b/server/queues/HealthMonitor.ts @@ -35,6 +35,6 @@ export default class HealthMonitor { } ); } - }, 30 * Second); + }, 30 * Second.ms); } } diff --git a/server/queues/processors/EmailsProcessor.ts b/server/queues/processors/EmailsProcessor.ts index f5fbcaa2fc..a6eb671fa5 100644 --- a/server/queues/processors/EmailsProcessor.ts +++ b/server/queues/processors/EmailsProcessor.ts @@ -60,7 +60,7 @@ export default class EmailsProcessor extends BaseProcessor { }, { notificationId } ).schedule({ - delay: Minute, + delay: Minute.ms, }); return; } @@ -76,7 +76,7 @@ export default class EmailsProcessor extends BaseProcessor { }, { notificationId } ).schedule({ - delay: Minute, + delay: Minute.ms, }); return; } @@ -107,7 +107,7 @@ export default class EmailsProcessor extends BaseProcessor { }, { notificationId: notification.id } ).schedule({ - delay: Minute, + delay: Minute.ms, }); return; } @@ -122,7 +122,7 @@ export default class EmailsProcessor extends BaseProcessor { }, { notificationId: notification.id } ).schedule({ - delay: Minute, + delay: Minute.ms, }); return; } @@ -139,7 +139,7 @@ export default class EmailsProcessor extends BaseProcessor { }, { notificationId: notification.id } ).schedule({ - delay: Minute, + delay: Minute.ms, }); } } diff --git a/server/queues/queue.ts b/server/queues/queue.ts index b56aba6fd5..a32305482b 100644 --- a/server/queues/queue.ts +++ b/server/queues/queue.ts @@ -57,7 +57,7 @@ export function createQueue( setInterval(async () => { Metrics.gauge(`${prefix}.count`, await queue.count()); Metrics.gauge(`${prefix}.delayed_count`, await queue.getDelayedCount()); - }, 5 * Second); + }, 5 * Second.ms); } ShutdownHelper.add(name, ShutdownOrder.normal, async () => { diff --git a/server/routes/index.ts b/server/routes/index.ts index f65f478269..871e2b457b 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -35,7 +35,7 @@ router.use(["/images/*", "/email/*", "/fonts/*"], async (ctx, next) => { done = await send(ctx, ctx.path, { root: path.resolve(__dirname, "../../../public"), // 7 day expiry, these assets are mostly static but do not contain a hash - maxAge: Day * 7, + maxAge: Day.ms * 7, setHeaders: (res) => { res.setHeader("Access-Control-Allow-Origin", "*"); }, @@ -71,7 +71,7 @@ if (env.isProduction) { await send(ctx, pathname, { root: path.join(__dirname, "../../app/"), // Hashed static assets get 1 year expiry plus immutable flag - maxAge: Day * 365, + maxAge: Day.ms * 365, immutable: true, setHeaders: (res) => { res.setHeader("Service-Worker-Allowed", "/"); @@ -105,7 +105,7 @@ router.get("/locales/:lng.json", async (ctx) => { await send(ctx, path.join(lng, "translation.json"), { setHeaders: (res, _, stats) => { res.setHeader("Last-Modified", formatRFC7231(stats.mtime)); - res.setHeader("Cache-Control", `public, max-age=${(7 * Day) / 1000}`); + res.setHeader("Cache-Control", `public, max-age=${7 * Day.seconds}`); res.setHeader( "ETag", crypto.createHash("md5").update(stats.mtime.toISOString()).digest("hex") @@ -121,7 +121,7 @@ router.get("/robots.txt", (ctx) => { router.get("/opensearch.xml", (ctx) => { ctx.type = "text/xml"; - ctx.response.set("Cache-Control", `public, max-age=${(7 * Day) / 1000}`); + ctx.response.set("Cache-Control", `public, max-age=${7 * Day.seconds}`); ctx.body = opensearchResponse(ctx.request.URL.origin); }); diff --git a/server/services/cron.ts b/server/services/cron.ts index cd34e09611..32e460f801 100644 --- a/server/services/cron.ts +++ b/server/services/cron.ts @@ -12,13 +12,13 @@ export default function init() { } } - setInterval(() => void run(TaskSchedule.Daily), Day); - setInterval(() => void run(TaskSchedule.Hourly), Hour); + setInterval(() => void run(TaskSchedule.Daily), Day.ms); + setInterval(() => void run(TaskSchedule.Hourly), Hour.ms); // Just give everything time to startup before running the first time. Not // _technically_ required to function. setTimeout(() => { void run(TaskSchedule.Daily); void run(TaskSchedule.Hourly); - }, 30 * Second); + }, 30 * Second.ms); } diff --git a/server/services/web.ts b/server/services/web.ts index d596e0d459..1dd4dc399c 100644 --- a/server/services/web.ts +++ b/server/services/web.ts @@ -84,7 +84,7 @@ export default function init(app: Koa = new Koa(), server?: Server) { } Metrics.gaugePerInstance("connections.count", count); }); - }, 5 * Second); + }, 5 * Second.ms); } ShutdownHelper.add("connections", ShutdownOrder.normal, async () => { diff --git a/server/utils/CacheHelper.ts b/server/utils/CacheHelper.ts index b53728bdf1..c5bb5fe5e7 100644 --- a/server/utils/CacheHelper.ts +++ b/server/utils/CacheHelper.ts @@ -7,7 +7,7 @@ import Redis from "@server/storage/redis"; */ export class CacheHelper { // Default expiry time for cache data in seconds - private static defaultDataExpiry = Day / 1000; + private static defaultDataExpiry = Day.seconds; /** * Given a key, gets the data from cache store diff --git a/shared/utils/time.ts b/shared/utils/time.ts index de8b4e9f8c..ae8ac5a489 100644 --- a/shared/utils/time.ts +++ b/shared/utils/time.ts @@ -1,11 +1,29 @@ -/** A second in ms */ -export const Second = 1000; +export class Second { + /** Milliseconds in a second */ + public static ms = 1000; +} -/** A minute in ms */ -export const Minute = 60 * Second; +export class Minute { + /** Milliseconds in a minute */ + public static ms = 60 * Second.ms; + /** Seconds in a minute */ + public static seconds = 60; +} -/** An hour in ms */ -export const Hour = 60 * Minute; +export class Hour { + /** Milliseconds in an hour */ + public static ms = 60 * Minute.ms; + /** Seconds in an hour */ + public static seconds = 60 * Minute.seconds; + /** Minutes in an hour */ + public static minutes = 60; +} -/** A day in ms */ -export const Day = 24 * Hour; +export class Day { + /** Milliseconds in a day */ + public static ms = 24 * Hour.ms; + /** Seconds in a day */ + public static seconds = 24 * Hour.seconds; + /** Minutes in a day */ + public static minutes = 24 * Hour.minutes; +}