From d93ad38dd18af0af0e97860f0b5d92bb6d4c7d6e Mon Sep 17 00:00:00 2001 From: Benjamin Goering Date: Wed, 17 May 2017 15:39:19 -0700 Subject: [PATCH] Admins can change Edit Comment Window length in Moderation Settings --- .../src/containers/Configure/Configure.css | 21 ++++++++------- .../Configure/ModerationSettings.js | 27 +++++++++++++++++++ .../containers/Configure/StreamSettings.js | 2 +- client/coral-admin/src/translations.json | 6 +++++ .../coral-embed-stream/src/graphql/index.js | 6 ++++- graph/resolvers/comment.js | 6 +++-- models/comment.js | 7 ----- models/setting.js | 7 +++++ services/comments.js | 6 +++-- 9 files changed, 66 insertions(+), 22 deletions(-) diff --git a/client/coral-admin/src/containers/Configure/Configure.css b/client/coral-admin/src/containers/Configure/Configure.css index 04ef96dd2..3d5750693 100644 --- a/client/coral-admin/src/containers/Configure/Configure.css +++ b/client/coral-admin/src/containers/Configure/Configure.css @@ -96,24 +96,27 @@ } } +.inlineTextfield { + border-color: #ccc; + border-style: solid; + border-width: 0px 0px 1px 0px; + text-align: center; + font-size: inherit; +} + +.inlineTextfield:focus { + outline: none; +} + .charCountTexfield { width: 4em; padding: 0px; - border-color: #ccc; - border-style: solid; - border-width: 0px 0px 1px 0px; - font-size: 14px; - text-align: center; } .charCountTexfieldEnabled { border-color: #00796b; } -.charCountTexfield:focus { - outline: none; -} - .changedSave { background-color: #00796B; color: white; diff --git a/client/coral-admin/src/containers/Configure/ModerationSettings.js b/client/coral-admin/src/containers/Configure/ModerationSettings.js index 5b7286b98..56be58c02 100644 --- a/client/coral-admin/src/containers/Configure/ModerationSettings.js +++ b/client/coral-admin/src/containers/Configure/ModerationSettings.js @@ -27,6 +27,12 @@ const ModerationSettings = ({settings, updateSettings, onChangeWordlist}) => { const on = styles.enabledSetting; const off = styles.disabledSetting; + const onChangeEditCommentWindowLength = (e) => { + const value = e.target.value; + const valueAsNumber = parseFloat(value); + const milliseconds = (!isNaN(valueAsNumber)) && (valueAsNumber * 1000); + updateSettings({editCommentWindowLength: milliseconds || value}); + }; return (
@@ -72,6 +78,27 @@ const ModerationSettings = ({settings, updateSettings, onChangeWordlist}) => { bannedWords={settings.wordlist.banned} suspectWords={settings.wordlist.suspect} onChangeWordlist={onChangeWordlist} /> + + {/* Edit Comment Timeframe */} + +
{lang.t('configure.edit-comment-timeframe-heading')}
+

+ {lang.t('configure.edit-comment-timeframe-text-pre')} +   + +   + {lang.t('configure.edit-comment-timeframe-text-post')} +

+
); }; diff --git a/client/coral-admin/src/containers/Configure/StreamSettings.js b/client/coral-admin/src/containers/Configure/StreamSettings.js index 646eb9d9e..341c47ae9 100644 --- a/client/coral-admin/src/containers/Configure/StreamSettings.js +++ b/client/coral-admin/src/containers/Configure/StreamSettings.js @@ -81,7 +81,7 @@ const StreamSettings = ({updateSettings, settingsError, settings, errors}) => {

{lang.t('configure.comment-count-text-pre')} ({ updateQueries: { - EmbedQuery: (previousData, {mutationResult: {data: {editComment: {comment: {status}}}}}) => { + EmbedQuery: (previousData, {mutationResult: {data: {editComment: {comment, errors}}}}) => { + if (errors && errors.length) { + return previousData; + } + const {status} = comment; const updateCommentWithEdit = (comment, edit) => { const {body} = edit; const editedComment = update(comment, { diff --git a/graph/resolvers/comment.js b/graph/resolvers/comment.js index 8d0473900..6f3313356 100644 --- a/graph/resolvers/comment.js +++ b/graph/resolvers/comment.js @@ -48,10 +48,12 @@ const Comment = { asset({asset_id}, _, {loaders: {Assets}}) { return Assets.getByID.load(asset_id); }, - editing(comment) { + async editing(comment, _, {loaders: {Settings}}) { + const settings = await Settings.load(); + const editableUntil = new Date(Number(comment.created_at) + settings.editCommentWindowLength); return { edited: comment.edited, - editableUntil: comment.editableUntil + editableUntil: editableUntil }; } }; diff --git a/models/comment.js b/models/comment.js index ddd39c68e..27a2bfae3 100644 --- a/models/comment.js +++ b/models/comment.js @@ -2,8 +2,6 @@ const mongoose = require('../services/mongoose'); const Schema = mongoose.Schema; const uuid = require('uuid'); -const EDIT_WINDOW_MS = 30 * 1000; // 30 seconds - const STATUSES = [ 'ACCEPTED', 'REJECTED', @@ -113,12 +111,7 @@ CommentSchema.virtual('edited').get(function() { return this.body_history.length > 1; }); -CommentSchema.virtual('editableUntil').get(function() { - return new Date(Number(this.created_at) + EDIT_WINDOW_MS); -}); - // Comment model. const Comment = mongoose.model('Comment', CommentSchema); module.exports = Comment; -module.exports.EDIT_WINDOW_MS = EDIT_WINDOW_MS; diff --git a/models/setting.js b/models/setting.js index e63fa8f3e..ecd9dfd26 100644 --- a/models/setting.js +++ b/models/setting.js @@ -88,6 +88,13 @@ const SettingSchema = new Schema({ type: Array, default: ['localhost'] } + }, + + // Length of time (in milliseconds) after a comment is posted that it can still be edited by the author + editCommentWindowLength: { + type: Number, + min: [0, 'Edit Comment Window length must be greater than zero'], + default: 30 * 1000, } }, { timestamps: { diff --git a/services/comments.js b/services/comments.js index 272b19cd0..f5defb935 100644 --- a/services/comments.js +++ b/services/comments.js @@ -1,8 +1,8 @@ const CommentModel = require('../models/comment'); -const EDIT_WINDOW_MS = CommentModel.EDIT_WINDOW_MS; const ActionModel = require('../models/action'); const ActionsService = require('./actions'); +const SettingsService = require('./settings'); const errors = require('../errors'); @@ -53,8 +53,10 @@ module.exports = class CommentsService { // Establish the edit window (if it exists) and add the condition to the // original query. - const lastEditableCommentCreatedAt = new Date((new Date()).getTime() - EDIT_WINDOW_MS); + let lastEditableCommentCreatedAt; if (!ignoreEditWindow) { + const editWindowMs = (await SettingsService.retrieve()).editCommentWindowLength; + lastEditableCommentCreatedAt = new Date((new Date()).getTime() - editWindowMs); query.created_at = { $gt: lastEditableCommentCreatedAt, };