Merge pull request #1315 from coralproject/better-change-detection

Better handle comment edits
This commit is contained in:
Wyatt Johnson
2018-01-29 14:54:00 -07:00
committed by GitHub
4 changed files with 34 additions and 9 deletions
@@ -86,7 +86,6 @@ export default class Configure extends Component {
}
Configure.propTypes = {
notify: PropTypes.func.isRequired,
savePending: PropTypes.func.isRequired,
auth: PropTypes.object.isRequired,
data: PropTypes.object.isRequired,
@@ -112,6 +112,10 @@ function getOlderDate(a, b) {
function determineLatestChange(comment) {
let lc = null;
comment.body_history.forEach(item => {
lc = getOlderDate(lc, item.created_at);
});
comment.status_history.forEach(item => {
lc = getOlderDate(lc, item.created_at);
});
@@ -124,12 +128,16 @@ function determineLatestChange(comment) {
}
function reconstructPreviousCommentState(comment) {
const history = comment.status_history;
const statusHistory = comment.status_history;
const bodyHistory = comment.body_history;
const actions = comment.actions;
const lastChangeDate = determineLatestChange(comment);
const previousComment = {
...comment,
status_history: history.filter(
body_history: bodyHistory.filter(
item => new Date(item.created_at) < lastChangeDate
),
status_history: statusHistory.filter(
item => new Date(item.created_at) < lastChangeDate
),
actions: actions.filter(item => new Date(item.created_at) < lastChangeDate),
@@ -145,6 +153,9 @@ function reconstructPreviousCommentState(comment) {
previousComment.status_history.length - 1
].type;
previousComment.body =
previousComment.body_history[previousComment.body_history.length - 1].body;
return previousComment;
}
@@ -383,6 +394,11 @@ export function handleIndicatorChange(root, comment, queueConfig) {
export const subscriptionFields = `
status
body
body_history {
body
created_at
}
actions {
__typename
created_at
+8
View File
@@ -453,6 +453,11 @@ type EditInfo {
editableUntil: Date
}
type CommentBodyHistory {
body: String!
created_at: Date!
}
type CommentStatusHistory {
type: COMMENT_STATUS!
created_at: Date!
@@ -471,6 +476,9 @@ type Comment {
# The actual comment data.
body: String!
# The body history of the comment.
body_history: [CommentBodyHistory!]!
# the tags on the comment
tags: [TagLink!]
+8 -6
View File
@@ -19,6 +19,7 @@ module.exports = class CommentsService {
static async publicCreate(input) {
// Extract the parent_id from the comment, if there is one.
const { status = 'NONE', parent_id = null } = input;
const created_at = new Date();
// Check to see if we are replying to a comment, and if that comment is
// visible.
@@ -37,14 +38,14 @@ module.exports = class CommentsService {
? [
{
type: status,
created_at: new Date(),
created_at,
},
]
: [],
body_history: [
{
body: input.body,
created_at: new Date(),
created_at,
},
],
},
@@ -85,6 +86,7 @@ module.exports = class CommentsService {
*/
static async edit({ id, author_id, body, status }) {
const EDITABLE_STATUSES = ['NONE', 'PREMOD', 'ACCEPTED'];
const created_at = new Date();
const query = {
id,
@@ -112,11 +114,11 @@ module.exports = class CommentsService {
$push: {
body_history: {
body,
created_at: new Date(),
created_at,
},
status_history: {
type: status,
created_at: new Date(),
created_at,
},
},
});
@@ -160,11 +162,11 @@ module.exports = class CommentsService {
editedComment.body = body;
editedComment.body_history.push({
body,
created_at: new Date(),
created_at,
});
editedComment.status_history.push({
type: status,
created_at: new Date(),
created_at,
});
await events.emitAsync(COMMENTS_EDIT, originalComment, editedComment);