mirror of
https://github.com/flarum/framework.git
synced 2025-02-18 08:52:46 +08:00
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:
parent
bf23f32a92
commit
d8d85a9c14
|
@ -1,5 +1,6 @@
|
||||||
import app from '../common/app';
|
import app from '../common/app';
|
||||||
import { FlarumRequestOptions } from './Application';
|
import { FlarumRequestOptions } from './Application';
|
||||||
|
import { fireDeprecationWarning } from './helpers/fireDebugWarning';
|
||||||
import Store, { ApiPayloadSingle, ApiResponseSingle, MetaInformation } from './Store';
|
import Store, { ApiPayloadSingle, ApiResponseSingle, MetaInformation } from './Store';
|
||||||
|
|
||||||
export interface ModelIdentifier {
|
export interface ModelIdentifier {
|
||||||
|
@ -111,6 +112,19 @@ export default abstract class Model {
|
||||||
|
|
||||||
if ('attributes' in data) {
|
if ('attributes' in data) {
|
||||||
this.data.attributes ||= {};
|
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);
|
Object.assign(this.data.attributes, data.attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -213,7 +213,7 @@ export default {
|
||||||
* @return {Promise<void>}
|
* @return {Promise<void>}
|
||||||
*/
|
*/
|
||||||
hideAction() {
|
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 });
|
return this.save({ isHidden: true });
|
||||||
},
|
},
|
||||||
|
@ -224,7 +224,7 @@ export default {
|
||||||
* @return {Promise<void>}
|
* @return {Promise<void>}
|
||||||
*/
|
*/
|
||||||
restoreAction() {
|
restoreAction() {
|
||||||
this.pushAttributes({ hiddenAt: null, hiddenUser: null });
|
this.pushData({ attributes: { hiddenAt: null }, relationships: { hiddenUser: null } });
|
||||||
|
|
||||||
return this.save({ isHidden: false });
|
return this.save({ isHidden: false });
|
||||||
},
|
},
|
||||||
|
|
|
@ -151,7 +151,7 @@ export default {
|
||||||
*/
|
*/
|
||||||
hideAction() {
|
hideAction() {
|
||||||
if (!confirm(extractText(app.translator.trans('core.forum.post_controls.hide_confirmation')))) return;
|
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());
|
return this.save({ isHidden: true }).then(() => m.redraw());
|
||||||
},
|
},
|
||||||
|
@ -162,7 +162,7 @@ export default {
|
||||||
* @return {Promise<void>}
|
* @return {Promise<void>}
|
||||||
*/
|
*/
|
||||||
restoreAction() {
|
restoreAction() {
|
||||||
this.pushAttributes({ hiddenAt: null, hiddenUser: null });
|
this.pushData({ attributes: { hiddenAt: null }, relationships: { hiddenUser: null } });
|
||||||
|
|
||||||
return this.save({ isHidden: false }).then(() => m.redraw());
|
return this.save({ isHidden: false }).then(() => m.redraw());
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue
Block a user