feat: support auto-pause live updates (#2947)

Co-authored-by: Vinh <vinh@vinh.tech>
This commit is contained in:
Wyatt Johnson
2020-05-07 16:21:37 +00:00
committed by GitHub
co-authored by Vinh
parent 1624787004
commit d0ab2ef8c5
8 changed files with 141 additions and 46 deletions
@@ -1,9 +1,14 @@
import { isUndefined } from "lodash";
import { DateTime } from "luxon";
import { GQLLiveConfigurationTypeResolver } from "coral-server/graph/schema/__generated__/types";
import * as settings from "coral-server/models/settings";
export type LiveConfigurationInput = settings.LiveConfiguration;
import { GQLLiveConfigurationTypeResolver } from "coral-server/graph/schema/__generated__/types";
export interface LiveConfigurationInput extends settings.LiveConfiguration {
lastCommentedAt?: Date;
createdAt?: Date;
}
export const LiveConfiguration: GQLLiveConfigurationTypeResolver<LiveConfigurationInput> = {
configurable: (source, args, ctx) =>
@@ -13,6 +18,32 @@ export const LiveConfiguration: GQLLiveConfigurationTypeResolver<LiveConfigurati
return false;
}
// This typecast is needed because the custom `ms` format does not return the
// desired `number` type even though that's the only type it can output.
const disableLiveUpdatesTimeout = (ctx.config.get(
"disable_live_updates_timeout"
) as unknown) as number;
if (disableLiveUpdatesTimeout > 0) {
// If one of these is available, use it to determine the time since the
// last comment.
const lastCommentedAt = source.lastCommentedAt || source.createdAt;
if (
// If a date is found...
lastCommentedAt &&
// And the date (when we add the timeout duration) is before the current
// date...
DateTime.fromJSDate(lastCommentedAt)
.plus({
milliseconds: disableLiveUpdatesTimeout,
})
.toJSDate() <= ctx.now
) {
// Then we know that the last comment (or lack there of) was left more
// than the timeout specified in configuration.
return false;
}
}
if (isUndefined(source.enabled)) {
return ctx.tenant.live.enabled;
}
+11 -1
View File
@@ -11,6 +11,7 @@ import {
import { CommentCountsInput } from "./CommentCounts";
import { storyModerationInputResolver } from "./ModerationQueues";
import { StorySettingsInput } from "./StorySettings";
export const Story: GQLStoryTypeResolver<story.Story> = {
comments: (s, input, ctx) => ctx.loaders.Comments.forStory(s.id, input),
@@ -26,7 +27,16 @@ export const Story: GQLStoryTypeResolver<story.Story> = {
commentCounts: (s): CommentCountsInput => s,
// Merge tenant settings into the story settings so we can easily inherit the
// options if they exist.
settings: (s, input, ctx) => defaultsDeep({}, s.settings, ctx.tenant),
settings: (s, input, ctx): StorySettingsInput =>
defaultsDeep(
{
// Pass these options as required by StorySettingsInput.
lastCommentedAt: s.lastCommentedAt,
createdAt: s.createdAt,
},
s.settings,
ctx.tenant
),
moderationQueues: storyModerationInputResolver,
site: (s, input, ctx) => ctx.loaders.Sites.site.load(s.siteID),
};
@@ -7,8 +7,21 @@ import {
GQLStorySettingsTypeResolver,
} from "../schema/__generated__/types";
export const StorySettings: GQLStorySettingsTypeResolver<story.StorySettings> = {
live: (s) => s.live || {},
import { LiveConfigurationInput } from "./LiveConfiguration";
export interface StorySettingsInput extends story.StorySettings {
lastCommentedAt?: Date;
createdAt?: Date;
}
export const StorySettings: GQLStorySettingsTypeResolver<StorySettingsInput> = {
live: (s): LiveConfigurationInput => ({
// Live may not be available sometimes, fix it here with the inline ||.
...(s.live || { enabled: false }),
// Pass these options as required by LiveConfigurationInput.
lastCommentedAt: s.lastCommentedAt,
createdAt: s.createdAt,
}),
moderation: (s, input, ctx) => s.moderation || ctx.tenant.moderation,
premodLinksEnable: (s, input, ctx) =>
s.premodLinksEnable || ctx.tenant.premodLinksEnable,