From d8d85a9c140fbdb007b99a40c62b7e5e9e3099c1 Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov <38059171+askvortsov1@users.noreply.github.com> Date: Tue, 4 Jan 2022 08:26:46 -0500 Subject: [PATCH] fix: broken post/discussion soft delete (#3249) * FIx broken post/discussion soft delete Before the Model typescript rewrite, `pushAttributes` supported including relationship objects, which is hacky but incorrect behavior. With the rewrite, this functionality was broken. This PR deprecates the functionality, adds a deprecated BC layer with a debug warning, and removes instances of incorrect usage. * Update js/src/common/Model.ts Co-authored-by: David Wheatley * Update js/src/common/Model.ts Co-authored-by: David Wheatley * chore: format Co-authored-by: David Wheatley --- framework/core/js/src/common/Model.ts | 14 ++++++++++++++ .../core/js/src/forum/utils/DiscussionControls.js | 4 ++-- framework/core/js/src/forum/utils/PostControls.js | 4 ++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/framework/core/js/src/common/Model.ts b/framework/core/js/src/common/Model.ts index 9fce10624..1912ed685 100644 --- a/framework/core/js/src/common/Model.ts +++ b/framework/core/js/src/common/Model.ts @@ -1,5 +1,6 @@ import app from '../common/app'; import { FlarumRequestOptions } from './Application'; +import { fireDeprecationWarning } from './helpers/fireDebugWarning'; import Store, { ApiPayloadSingle, ApiResponseSingle, MetaInformation } from './Store'; export interface ModelIdentifier { @@ -111,6 +112,19 @@ export default abstract class Model { if ('attributes' in data) { this.data.attributes ||= {}; + + // @deprecated + // Filter out relationships that got in by accident. + for (const key in data.attributes) { + const val = data.attributes[key]; + if (val && val instanceof Model) { + fireDeprecationWarning('Providing models as attributes to `Model.pushData()` or `Model.pushAttributes()` is deprecated.', '3249'); + delete data.attributes[key]; + data.relationships ||= {}; + data.relationships[key] = { data: Model.getIdentifier(val) }; + } + } + Object.assign(this.data.attributes, data.attributes); } diff --git a/framework/core/js/src/forum/utils/DiscussionControls.js b/framework/core/js/src/forum/utils/DiscussionControls.js index cb81fffbe..c656eed09 100644 --- a/framework/core/js/src/forum/utils/DiscussionControls.js +++ b/framework/core/js/src/forum/utils/DiscussionControls.js @@ -213,7 +213,7 @@ export default { * @return {Promise} */ hideAction() { - this.pushAttributes({ hiddenAt: new Date(), hiddenUser: app.session.user }); + this.pushData({ attributes: { hiddenAt: new Date() }, relationships: { hiddenUser: app.session.user } }); return this.save({ isHidden: true }); }, @@ -224,7 +224,7 @@ export default { * @return {Promise} */ restoreAction() { - this.pushAttributes({ hiddenAt: null, hiddenUser: null }); + this.pushData({ attributes: { hiddenAt: null }, relationships: { hiddenUser: null } }); return this.save({ isHidden: false }); }, diff --git a/framework/core/js/src/forum/utils/PostControls.js b/framework/core/js/src/forum/utils/PostControls.js index bf00e1dce..3df36b333 100644 --- a/framework/core/js/src/forum/utils/PostControls.js +++ b/framework/core/js/src/forum/utils/PostControls.js @@ -151,7 +151,7 @@ export default { */ hideAction() { if (!confirm(extractText(app.translator.trans('core.forum.post_controls.hide_confirmation')))) return; - this.pushAttributes({ hiddenAt: new Date(), hiddenUser: app.session.user }); + this.pushData({ attributes: { hiddenAt: new Date() }, relationships: { hiddenUser: app.session.user } }); return this.save({ isHidden: true }).then(() => m.redraw()); }, @@ -162,7 +162,7 @@ export default { * @return {Promise} */ restoreAction() { - this.pushAttributes({ hiddenAt: null, hiddenUser: null }); + this.pushData({ attributes: { hiddenAt: null }, relationships: { hiddenUser: null } }); return this.save({ isHidden: false }).then(() => m.redraw()); },