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 <hi@davwheat.dev>

* Update js/src/common/Model.ts

Co-authored-by: David Wheatley <hi@davwheat.dev>

* chore: format

Co-authored-by: David Wheatley <hi@davwheat.dev>
This commit is contained in:
Alexander Skvortsov 2022-01-04 08:26:46 -05:00 committed by GitHub
parent bf23f32a92
commit d8d85a9c14
3 changed files with 18 additions and 4 deletions

View File

@ -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);
}

View File

@ -213,7 +213,7 @@ export default {
* @return {Promise<void>}
*/
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<void>}
*/
restoreAction() {
this.pushAttributes({ hiddenAt: null, hiddenUser: null });
this.pushData({ attributes: { hiddenAt: null }, relationships: { hiddenUser: null } });
return this.save({ isHidden: false });
},

View File

@ -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<void>}
*/
restoreAction() {
this.pushAttributes({ hiddenAt: null, hiddenUser: null });
this.pushData({ attributes: { hiddenAt: null }, relationships: { hiddenUser: null } });
return this.save({ isHidden: false }).then(() => m.redraw());
},