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()); },